diff options
author | Nullptrderef <nullptrderef@proton.me> | 2024-04-21 09:54:28 +0200 |
---|---|---|
committer | Nullptrderef <nullptrderef@proton.me> | 2024-04-21 09:54:28 +0200 |
commit | 6b61e565b5372d241e52d6fa6b6e2ff35cc707a4 (patch) | |
tree | de6b29e3fe50a2c0be5f82a914d54ff1d7457765 /packages | |
parent | 8841c30a571f751fc4f9a5a7e7cb9b220618b0c5 (diff) |
check for non-2xx, cache any 2xx responses that pass minimal validation
Diffstat (limited to 'packages')
-rw-r--r-- | packages/anastasis-webui/src/pages/home/AddingProviderScreen/index.ts | 41 |
1 files 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 index 5f49f39e8..365d2e8e7 100644 --- 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; } |