84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
import {
|
|
HttpResponseOk,
|
|
RequestOptions,
|
|
useApiContext,
|
|
} from "@gnu-taler/web-util/browser";
|
|
import { useCallback } from "preact/hooks";
|
|
import { uiSettings } from "../settings.js";
|
|
import { canonicalizeBaseUrl } from "@gnu-taler/taler-util";
|
|
import { useOfficer } from "./useOfficer.js";
|
|
import { buildQuerySignature } from "../account.js";
|
|
|
|
interface useBackendType {
|
|
request: <T>(
|
|
path: string,
|
|
options?: RequestOptions,
|
|
) => Promise<HttpResponseOk<T>>;
|
|
fetcher: <T>(args: [string, string]) => Promise<HttpResponseOk<T>>;
|
|
paginatedFetcher: <T>(
|
|
args: [string, number, number, 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, talerAmlOfficerSignature]: [string,string]): Promise<HttpResponseOk<T>> {
|
|
return requestHandler<T>(baseUrl, endpoint, {
|
|
talerAmlOfficerSignature
|
|
});
|
|
},
|
|
[baseUrl],
|
|
);
|
|
const paginatedFetcher = useCallback(
|
|
function fetcherImpl<T>([endpoint, page, size, talerAmlOfficerSignature]: [
|
|
string,
|
|
number,
|
|
number,
|
|
string,
|
|
]): Promise<HttpResponseOk<T>> {
|
|
return requestHandler<T>(baseUrl, endpoint, {
|
|
params: { page: page || 1, size },
|
|
talerAmlOfficerSignature,
|
|
});
|
|
},
|
|
[baseUrl],
|
|
);
|
|
return {
|
|
request,
|
|
fetcher,
|
|
paginatedFetcher,
|
|
};
|
|
}
|
|
|
|
export function getInitialBackendBaseURL(): string {
|
|
const overrideUrl =
|
|
typeof localStorage !== "undefined"
|
|
? localStorage.getItem("exchange-aml-base-url")
|
|
: undefined;
|
|
if (!overrideUrl) {
|
|
//normal path
|
|
if (!uiSettings.backendBaseURL) {
|
|
console.error(
|
|
"ERROR: backendBaseURL was overridden by a setting file and missing. Setting value to 'window.origin'",
|
|
);
|
|
return canonicalizeBaseUrl(window.origin);
|
|
}
|
|
return canonicalizeBaseUrl(uiSettings.backendBaseURL);
|
|
}
|
|
// testing/development path
|
|
return canonicalizeBaseUrl(overrideUrl);
|
|
}
|