aboutsummaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/hooks/backend.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/demobank-ui/src/hooks/backend.ts')
-rw-r--r--packages/demobank-ui/src/hooks/backend.ts265
1 files changed, 3 insertions, 262 deletions
diff --git a/packages/demobank-ui/src/hooks/backend.ts b/packages/demobank-ui/src/hooks/backend.ts
index 889618646..589d7fab0 100644
--- a/packages/demobank-ui/src/hooks/backend.ts
+++ b/packages/demobank-ui/src/hooks/backend.ts
@@ -15,6 +15,7 @@
*/
import {
+ AccessToken,
Codec,
buildCodecForObject,
buildCodecForUnion,
@@ -24,23 +25,11 @@ import {
codecForString,
} from "@gnu-taler/taler-util";
import {
- ErrorType,
- HttpError,
- RequestError,
buildStorageKey,
- useLocalStorage,
+ useLocalStorage
} from "@gnu-taler/web-util/browser";
-import {
- HttpResponse,
- HttpResponseOk,
- RequestOptions,
-} from "@gnu-taler/web-util/browser";
-import { useApiContext } from "@gnu-taler/web-util/browser";
-import { useCallback, useEffect, useState } from "preact/hooks";
import { useSWRConfig } from "swr";
-import { useBackendContext } from "../context/backend.js";
import { bankUiSettings } from "../settings.js";
-import { AccessToken } from "./useCredentialsChecker.js";
/**
* Has the information to reach and
@@ -91,34 +80,6 @@ export const codecForBackendState = (): Codec<BackendState> =>
.alternative("expired", codecForBackendStateExpired())
.build("BackendState");
-export function getInitialBackendBaseURL(): string {
- const overrideUrl =
- typeof localStorage !== "undefined"
- ? localStorage.getItem("bank-base-url")
- : undefined;
- let result: string;
- if (!overrideUrl) {
- //normal path
- if (!bankUiSettings.backendBaseURL) {
- console.error(
- "ERROR: backendBaseURL was overridden by a setting file and missing. Setting value to 'window.origin'",
- );
- result = window.origin
- } else {
- result = bankUiSettings.backendBaseURL;
- }
- } else {
- // testing/development path
- result = overrideUrl
- }
- try {
- return canonicalizeBaseUrl(result)
- } catch (e) {
- //fall back
- return canonicalizeBaseUrl(window.origin)
- }
-}
-
export const defaultState: BackendState = {
status: "loggedOut",
};
@@ -127,7 +88,7 @@ export interface BackendStateHandler {
state: BackendState;
logOut(): void;
expired(): void;
- logIn(info: {username: string, token: AccessToken}): void;
+ logIn(info: { username: string, token: AccessToken }): void;
}
const BACKEND_STATE_KEY = buildStorageKey(
@@ -174,226 +135,6 @@ export function useBackendState(): BackendStateHandler {
};
}
-interface useBackendType {
- request: <T>(
- path: string,
- options?: RequestOptions,
- ) => Promise<HttpResponseOk<T>>;
- fetcher: <T>(endpoint: string) => Promise<HttpResponseOk<T>>;
- multiFetcher: <T>(endpoint: string[][]) => Promise<HttpResponseOk<T>[]>;
- paginatedFetcher: <T>(
- args: [string, string | undefined, number],
- ) => Promise<HttpResponseOk<T>>;
- sandboxAccountsFetcher: <T>(
- args: [string, number, number, string],
- ) => Promise<HttpResponseOk<T>>;
- sandboxCashoutFetcher: <T>(endpoint: string[]) => Promise<HttpResponseOk<T>>;
-}
-export function usePublicBackend(): useBackendType {
- const { request: requestHandler } = useApiContext();
-
- const baseUrl = getInitialBackendBaseURL();
-
- const request = useCallback(
- function requestImpl<T>(
- path: string,
- options: RequestOptions = {},
- ): Promise<HttpResponseOk<T>> {
- return requestHandler<T>(baseUrl, path, options);
- },
- [baseUrl],
- );
-
- const fetcher = useCallback(
- function fetcherImpl<T>(endpoint: string): Promise<HttpResponseOk<T>> {
- return requestHandler<T>(baseUrl, endpoint);
- },
- [baseUrl],
- );
- const paginatedFetcher = useCallback(
- function fetcherImpl<T>([endpoint, start, size]: [
- string,
- string | undefined,
- number,
- ]): Promise<HttpResponseOk<T>> {
- const delta = -1 * size //descending order
- const params = start ? { delta, start } : { delta }
- return requestHandler<T>(baseUrl, endpoint, {
- params,
- });
- },
- [baseUrl],
- );
- const multiFetcher = useCallback(
- function multiFetcherImpl<T>([endpoints]: string[][]): Promise<
- HttpResponseOk<T>[]
- > {
- return Promise.all(
- endpoints.map((endpoint) => requestHandler<T>(baseUrl, endpoint)),
- );
- },
- [baseUrl],
- );
- const sandboxAccountsFetcher = useCallback(
- function fetcherImpl<T>([endpoint, page, size, account]: [
- string,
- number,
- number,
- string,
- ]): Promise<HttpResponseOk<T>> {
- return requestHandler<T>(baseUrl, endpoint, {
- params: { page: page || 1, size },
- });
- },
- [baseUrl],
- );
- const sandboxCashoutFetcher = useCallback(
- function fetcherImpl<T>([endpoint, account]: string[]): Promise<
- HttpResponseOk<T>
- > {
- return requestHandler<T>(baseUrl, endpoint);
- },
- [baseUrl],
- );
- return {
- request,
- fetcher,
- paginatedFetcher,
- multiFetcher,
- sandboxAccountsFetcher,
- sandboxCashoutFetcher,
- };
-}
-
-type CheckResult = ValidResult | RequestInvalidResult | InvalidationResult;
-
-interface ValidResult {
- valid: true;
-}
-interface RequestInvalidResult {
- valid: false;
- requestError: true;
- cause: RequestError<any>["cause"];
-}
-interface InvalidationResult {
- valid: false;
- requestError: false;
- error: unknown;
-}
-
-export function useAuthenticatedBackend(): useBackendType {
- const { state } = useBackendContext();
- const { request: requestHandler } = useApiContext();
-
- // FIXME: libeufin returns 400 insteand of 401 if there is no auth token
- const creds = state.status === "loggedIn" ? state.token : "secret-token:a";
- const baseUrl = getInitialBackendBaseURL();
-
- const request = useCallback(
- function requestImpl<T>(
- path: string,
- options: RequestOptions = {},
- ): Promise<HttpResponseOk<T>> {
- return requestHandler<T>(baseUrl, path, { token: creds, ...options });
- },
- [baseUrl, creds],
- );
-
- const fetcher = useCallback(
- function fetcherImpl<T>(endpoint: string): Promise<HttpResponseOk<T>> {
- return requestHandler<T>(baseUrl, endpoint, { token: creds });
- },
- [baseUrl, creds],
- );
- const paginatedFetcher = useCallback(
- function fetcherImpl<T>([endpoint, start, size]: [
- string,
- string | undefined,
- number,
- ]): Promise<HttpResponseOk<T>> {
- const delta = -1 * size //descending order
- const params = start ? { delta, start } : { delta }
- return requestHandler<T>(baseUrl, endpoint, {
- token: creds,
- params,
- });
- },
- [baseUrl, creds],
- );
- const multiFetcher = useCallback(
- function multiFetcherImpl<T>([endpoints]: string[][]): Promise<
- HttpResponseOk<T>[]
- > {
- return Promise.all(
- endpoints.map((endpoint) =>
- requestHandler<T>(baseUrl, endpoint, { token: creds }),
- ),
- );
- },
- [baseUrl, creds],
- );
- const sandboxAccountsFetcher = useCallback(
- function fetcherImpl<T>([endpoint, page, size, account]: [
- string,
- number,
- number,
- string,
- ]): Promise<HttpResponseOk<T>> {
- return requestHandler<T>(baseUrl, endpoint, {
- token: creds,
- params: { page: page || 1, size },
- });
- },
- [baseUrl],
- );
-
- const sandboxCashoutFetcher = useCallback(
- function fetcherImpl<T>([endpoint, account]: string[]): Promise<
- HttpResponseOk<T>
- > {
- return requestHandler<T>(baseUrl, endpoint, {
- token: creds,
- params: { account },
- });
- },
- [baseUrl, creds],
- );
- return {
- request,
- fetcher,
- paginatedFetcher,
- multiFetcher,
- sandboxAccountsFetcher,
- sandboxCashoutFetcher,
- };
-}
-/**
- *
- * @deprecated
- */
-export function useBackendConfig(): HttpResponse<
- SandboxBackend.Config,
- SandboxBackend.SandboxError
-> {
- const { request } = usePublicBackend();
-
- type Type = SandboxBackend.Config;
-
- const [result, setResult] = useState<
- HttpResponse<Type, SandboxBackend.SandboxError>
- >({ loading: true });
-
- useEffect(() => {
- request<Type>(`/config`)
- .then((data) => setResult(data))
- .catch((error: RequestError<SandboxBackend.SandboxError>) =>
- setResult(error.cause),
- );
- }, [request]);
-
- return result;
-}
-
export function useMatchMutate(): (
re: RegExp,
value?: unknown,