wallet-core/packages/demobank-ui/src/hooks/backend.ts

325 lines
8.4 KiB
TypeScript
Raw Normal View History

2022-12-09 13:09:20 +01:00
/*
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2023-02-08 21:41:19 +01:00
import { canonicalizeBaseUrl } from "@gnu-taler/taler-util";
2023-02-10 13:51:37 +01:00
import {
RequestError,
useLocalStorage,
} from "@gnu-taler/web-util/lib/index.browser";
2023-02-08 21:41:19 +01:00
import {
HttpResponse,
HttpResponseOk,
RequestOptions,
} from "@gnu-taler/web-util/lib/index.browser";
import { useApiContext } from "@gnu-taler/web-util/lib/index.browser";
import { useCallback, useEffect, useState } from "preact/hooks";
import { useSWRConfig } from "swr";
import { useBackendContext } from "../context/backend.js";
2023-02-26 00:06:17 +01:00
import { bankUiSettings } from "../settings.js";
2022-12-07 13:29:36 +01:00
/**
* Has the information to reach and
* authenticate at the bank's backend.
*/
2022-12-07 22:45:49 +01:00
export type BackendState = LoggedIn | LoggedOut;
2023-02-08 21:41:19 +01:00
export interface BackendCredentials {
username: string;
password: string;
}
2023-02-08 21:41:19 +01:00
interface LoggedIn extends BackendCredentials {
url: string;
2022-12-07 22:45:49 +01:00
status: "loggedIn";
2023-02-08 21:41:19 +01:00
isUserAdministrator: boolean;
}
interface LoggedOut {
2023-02-08 21:41:19 +01:00
url: string;
2022-12-07 22:45:49 +01:00
status: "loggedOut";
2022-12-07 13:29:36 +01:00
}
2023-02-26 00:06:17 +01:00
const maybeRootPath = bankUiSettings.backendBaseURL;
2023-02-08 21:41:19 +01:00
export function getInitialBackendBaseURL(): string {
const overrideUrl = localStorage.getItem("bank-base-url");
return canonicalizeBaseUrl(overrideUrl ? overrideUrl : maybeRootPath);
}
export const defaultState: BackendState = {
status: "loggedOut",
2023-02-10 13:51:37 +01:00
url: getInitialBackendBaseURL(),
2023-02-08 21:41:19 +01:00
};
export interface BackendStateHandler {
2022-12-07 22:45:49 +01:00
state: BackendState;
2023-02-08 21:41:19 +01:00
logOut(): void;
logIn(info: BackendCredentials): void;
}
2022-12-07 13:29:36 +01:00
/**
* Return getters and setters for
* login credentials and backend's
* base URL.
*/
export function useBackendState(): BackendStateHandler {
2022-12-09 15:58:39 +01:00
const [value, update] = useLocalStorage(
2022-12-07 22:45:49 +01:00
"backend-state",
JSON.stringify(defaultState),
);
2023-02-08 21:41:19 +01:00
2022-12-07 22:45:49 +01:00
let parsed;
try {
2022-12-07 22:45:49 +01:00
parsed = JSON.parse(value!);
} catch {
2022-12-07 22:45:49 +01:00
parsed = undefined;
}
2022-12-07 22:45:49 +01:00
const state: BackendState = !parsed?.status ? defaultState : parsed;
return {
state,
2023-02-08 21:41:19 +01:00
logOut() {
update(JSON.stringify({ ...defaultState, url: state.url }));
},
2023-02-08 21:41:19 +01:00
logIn(info) {
//admin is defined by the username
2023-02-10 13:51:37 +01:00
const nextState: BackendState = {
status: "loggedIn",
url: state.url,
...info,
isUserAdministrator: info.username === "admin",
};
2022-12-07 22:45:49 +01:00
update(JSON.stringify(nextState));
},
2022-12-07 22:45:49 +01:00
};
2022-12-07 13:29:36 +01:00
}
2023-02-08 21:41:19 +01:00
interface useBackendType {
request: <T>(
path: string,
options?: RequestOptions,
) => Promise<HttpResponseOk<T>>;
fetcher: <T>(endpoint: string) => Promise<HttpResponseOk<T>>;
2023-02-10 13:51:37 +01:00
multiFetcher: <T>(endpoint: string[][]) => Promise<HttpResponseOk<T>[]>;
paginatedFetcher: <T>(
args: [string, number, number],
) => Promise<HttpResponseOk<T>>;
sandboxAccountsFetcher: <T>(
args: [string, number, number, string],
) => Promise<HttpResponseOk<T>>;
2023-02-17 20:23:37 +01:00
sandboxCashoutFetcher: <T>(endpoint: string[]) => Promise<HttpResponseOk<T>>;
2023-02-08 21:41:19 +01:00
}
export function usePublicBackend(): useBackendType {
const { state } = useBackendContext();
const { request: requestHandler } = useApiContext();
2023-02-10 13:51:37 +01:00
const baseUrl = state.url;
2023-02-08 21:41:19 +01:00
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(
2023-02-10 13:51:37 +01:00
function fetcherImpl<T>([endpoint, page, size]: [
string,
number,
number,
]): Promise<HttpResponseOk<T>> {
return requestHandler<T>(baseUrl, endpoint, {
params: { page: page || 1, size },
});
2023-02-08 21:41:19 +01:00
},
[baseUrl],
);
const multiFetcher = useCallback(
2023-02-10 13:51:37 +01:00
function multiFetcherImpl<T>([endpoints]: string[][]): Promise<
HttpResponseOk<T>[]
> {
2023-02-08 21:41:19 +01:00
return Promise.all(
endpoints.map((endpoint) => requestHandler<T>(baseUrl, endpoint)),
);
},
[baseUrl],
);
const sandboxAccountsFetcher = useCallback(
2023-02-10 13:51:37 +01:00
function fetcherImpl<T>([endpoint, page, size, account]: [
string,
number,
number,
string,
]): Promise<HttpResponseOk<T>> {
return requestHandler<T>(baseUrl, endpoint, {
params: { page: page || 1, size },
});
2023-02-08 21:41:19 +01:00
},
[baseUrl],
);
2023-02-17 20:23:37 +01:00
const sandboxCashoutFetcher = useCallback(
function fetcherImpl<T>([endpoint, account]: string[]): Promise<
HttpResponseOk<T>
> {
return requestHandler<T>(baseUrl, endpoint);
},
[baseUrl],
);
2023-02-10 13:51:37 +01:00
return {
request,
fetcher,
paginatedFetcher,
multiFetcher,
sandboxAccountsFetcher,
2023-02-17 20:23:37 +01:00
sandboxCashoutFetcher,
2023-02-10 13:51:37 +01:00
};
2023-02-08 21:41:19 +01:00
}
export function useAuthenticatedBackend(): useBackendType {
const { state } = useBackendContext();
const { request: requestHandler } = useApiContext();
2023-02-10 13:51:37 +01:00
const creds = state.status === "loggedIn" ? state : undefined;
const baseUrl = state.url;
2023-02-08 21:41:19 +01:00
const request = useCallback(
function requestImpl<T>(
path: string,
options: RequestOptions = {},
): Promise<HttpResponseOk<T>> {
return requestHandler<T>(baseUrl, path, { basicAuth: creds, ...options });
},
[baseUrl, creds],
);
const fetcher = useCallback(
function fetcherImpl<T>(endpoint: string): Promise<HttpResponseOk<T>> {
return requestHandler<T>(baseUrl, endpoint, { basicAuth: creds });
},
[baseUrl, creds],
);
const paginatedFetcher = useCallback(
2023-02-10 13:51:37 +01:00
function fetcherImpl<T>([endpoint, page = 0, size]: [
string,
number,
number,
]): Promise<HttpResponseOk<T>> {
return requestHandler<T>(baseUrl, endpoint, {
basicAuth: creds,
params: { page, size },
});
2023-02-08 21:41:19 +01:00
},
[baseUrl, creds],
);
const multiFetcher = useCallback(
2023-02-10 13:51:37 +01:00
function multiFetcherImpl<T>([endpoints]: string[][]): Promise<
HttpResponseOk<T>[]
> {
2023-02-08 21:41:19 +01:00
return Promise.all(
2023-02-10 13:51:37 +01:00
endpoints.map((endpoint) =>
requestHandler<T>(baseUrl, endpoint, { basicAuth: creds }),
),
2023-02-08 21:41:19 +01:00
);
},
[baseUrl, creds],
);
const sandboxAccountsFetcher = useCallback(
2023-02-10 13:51:37 +01:00
function fetcherImpl<T>([endpoint, page, size, account]: [
string,
number,
number,
string,
]): Promise<HttpResponseOk<T>> {
return requestHandler<T>(baseUrl, endpoint, {
basicAuth: creds,
params: { page: page || 1, size },
});
2023-02-08 21:41:19 +01:00
},
[baseUrl],
);
2023-02-10 13:51:37 +01:00
2023-02-17 20:23:37 +01:00
const sandboxCashoutFetcher = useCallback(
function fetcherImpl<T>([endpoint, account]: string[]): Promise<
HttpResponseOk<T>
> {
return requestHandler<T>(baseUrl, endpoint, {
basicAuth: creds,
params: { account },
});
},
[baseUrl, creds],
);
2023-02-10 13:51:37 +01:00
return {
request,
fetcher,
paginatedFetcher,
multiFetcher,
sandboxAccountsFetcher,
2023-02-17 20:23:37 +01:00
sandboxCashoutFetcher,
2023-02-10 13:51:37 +01:00
};
2023-02-08 21:41:19 +01:00
}
2023-02-10 13:51:37 +01:00
export function useBackendConfig(): HttpResponse<
SandboxBackend.Config,
SandboxBackend.SandboxError
> {
2023-02-08 21:41:19 +01:00
const { request } = usePublicBackend();
type Type = SandboxBackend.Config;
2023-02-10 13:51:37 +01:00
const [result, setResult] = useState<
HttpResponse<Type, SandboxBackend.SandboxError>
>({ loading: true });
2023-02-08 21:41:19 +01:00
useEffect(() => {
request<Type>(`/config`)
.then((data) => setResult(data))
.catch((error) => setResult(error));
}, [request]);
return result;
}
export function useMatchMutate(): (
re: RegExp,
value?: unknown,
) => Promise<any> {
const { cache, mutate } = useSWRConfig();
if (!(cache instanceof Map)) {
throw new Error(
"matchMutate requires the cache provider to be a Map instance",
);
}
return function matchRegexMutate(re: RegExp, value?: unknown) {
const allKeys = Array.from(cache.keys());
const keys = allKeys.filter((key) => re.test(key));
const mutations = keys.map((key) => {
2023-02-10 13:51:37 +01:00
return mutate(key, value, true);
2023-02-08 21:41:19 +01:00
});
return Promise.all(mutations);
};
}