taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 6b61e565b5372d241e52d6fa6b6e2ff35cc707a4
parent 8841c30a571f751fc4f9a5a7e7cb9b220618b0c5
Author: Nullptrderef <nullptrderef@proton.me>
Date:   Sun, 21 Apr 2024 09:54:28 +0200

check for non-2xx, cache any 2xx responses that pass minimal validation

Diffstat:
Mpackages/anastasis-webui/src/pages/home/AddingProviderScreen/index.ts | 41++++++++++++++++++++++++++---------------
1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/packages/anastasis-webui/src/pages/home/AddingProviderScreen/index.ts b/packages/anastasis-webui/src/pages/home/AddingProviderScreen/index.ts @@ -65,27 +65,37 @@ const map: StateViewMap<State> = { export default compose("AddingProviderScreen", useComponentState, map); +const providerResponseCache = new Map<string, any>(); // `any` is the return type of res.json() export async function testProvider( url: string, expectedMethodType?: string, ): Promise<void> { try { // TODO: look into using core.getProviderInfo :) - const json = await fetch(new URL("config", url).href) - .catch((error) => { - console.error("Provider HTTP Error:", error); - throw new Error( - "Encountered a fatal error whilst testing the provider: " + url, - ); - }) - .then((response) => - response.json().catch((error) => { - console.error("Provider Parsing Error:", error); - throw new Error( - "Encountered a fatal error whilst testing the provider: " + url, - ); - }), - ); + const providerHasUrl = providerResponseCache.has(url); + const json = providerHasUrl + ? providerResponseCache.get(url) + : await fetch(new URL("config", url).href) + .catch((error) => { + console.error("Provider HTTP Error:", error); + throw new Error( + "Encountered a fatal error whilst testing the provider: " + url, + ); + }) + .then(async (response) => { + if (!response.ok) + throw new Error( + `The server ${response.url} responded with a non-2xx response.`, + ); + try { + return await response.json(); + } catch (error) { + console.error("Provider Parsing Error:", error); + throw new Error( + "Encountered a fatal error whilst testing the provider: " + url, + ); + } + }); if (typeof json !== "object") throw new Error( "Encountered a fatal error whilst testing the provider: " + @@ -102,6 +112,7 @@ export async function testProvider( "This provider doesn't have authentication method. Please check the provider's URL and ensure it is properly configured.", ); } + if (!providerHasUrl) providerResponseCache.set(url, json); if (!expectedMethodType) { return; }