diff options
Diffstat (limited to 'packages/web-util/src/context/bank-api.ts')
-rw-r--r-- | packages/web-util/src/context/bank-api.ts | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/packages/web-util/src/context/bank-api.ts b/packages/web-util/src/context/bank-api.ts index a5e5654ac..d2938f150 100644 --- a/packages/web-util/src/context/bank-api.ts +++ b/packages/web-util/src/context/bank-api.ts @@ -25,7 +25,7 @@ import { TalerCoreBankCacheEviction, TalerCoreBankHttpClient, TalerCorebankApi, - TalerError + TalerError, } from "@gnu-taler/taler-util"; import { ComponentChildren, @@ -56,7 +56,8 @@ export type BankContextType = { // @ts-expect-error default value to undefined, should it be another thing? const BankContext = createContext<BankContextType>(undefined); -export const useBankCoreApiContext = (): BankContextType => useContext(BankContext); +export const useBankCoreApiContext = (): BankContextType => + useContext(BankContext); enum VersionHint { NONE, @@ -65,7 +66,7 @@ enum VersionHint { type Evictors = { conversion?: CacheEvictor<TalerBankConversionCacheEviction>; bank?: CacheEvictor<TalerCoreBankCacheEviction>; -} +}; type ConfigResult<T> = | undefined @@ -81,13 +82,15 @@ export const BankApiProvider = ({ }: { baseUrl: URL; children: ComponentChildren; - evictors?: Evictors, + evictors?: Evictors; frameOnError: FunctionComponent<{ children: ComponentChildren }>; }): VNode => { - const [checked, setChecked] = useState<ConfigResult<TalerCorebankApi.Config>>(); + const [checked, setChecked] = + useState<ConfigResult<TalerCorebankApi.Config>>(); const { i18n } = useTranslationContext(); - const { getRemoteConfig, VERSION, lib, cancelRequest, onActivity } = buildBankApiClient(baseUrl, evictors); + const { getRemoteConfig, VERSION, lib, cancelRequest, onActivity } = + buildBankApiClient(baseUrl, evictors); useEffect(() => { getRemoteConfig() @@ -110,7 +113,9 @@ export const BankApiProvider = ({ }, []); if (checked === undefined) { - return h(frameOnError, { children: h("div", {}, "checking compatibility with server...") }); + return h(frameOnError, { + children: h("div", {}, "checking compatibility with server..."), + }); } if (checked.type === "error") { return h(frameOnError, { @@ -141,7 +146,9 @@ export const BankApiProvider = ({ }); }; -function buildBankApiClient(url: URL, evictors: Evictors, +function buildBankApiClient( + url: URL, + evictors: Evictors, ): APIClient<BankLib, TalerCorebankApi.Config> { const httpFetch = new BrowserFetchHttpLib({ enableThrottling: true, @@ -154,11 +161,7 @@ function buildBankApiClient(url: URL, evictors: Evictors, }, }); - const bank = new TalerCoreBankHttpClient( - url.href, - httpLib, - evictors.bank, - ); + const bank = new TalerCoreBankHttpClient(url.href, httpLib, evictors.bank); const conversion = new TalerBankConversionHttpClient( bank.getConversionInfoAPI().href, httpLib, @@ -170,32 +173,36 @@ function buildBankApiClient(url: URL, evictors: Evictors, httpLib, ); - async function getRemoteConfig() { - const resp = await bank.getConfig() - return resp.body + async function getRemoteConfig(): Promise<TalerCorebankApi.Config> { + const resp = await bank.getConfig(); + if (resp.type === "fail") { + throw TalerError.fromUncheckedDetail(resp.detail); + } + return resp.body; } return { getRemoteConfig, VERSION: bank.PROTOCOL_VERSION, lib: { - bank, conversion, auth + bank, + conversion, + auth, }, onActivity: tracker.subscribe, cancelRequest: httpLib.cancelRequest, }; } - export const BankApiProviderTesting = ({ children, value, }: { - value: BankContextType + value: BankContextType; children: ComponentChildren; }): VNode => { return h(BankContext.Provider, { value, children, }); -} +}; |