From c3c694349c096145eb21464049cdf75da09bb2d7 Mon Sep 17 00:00:00 2001 From: "Justin R. Evans" Date: Fri, 9 Feb 2024 10:55:10 -0500 Subject: [PATCH] fix: clean up faucet utils and return types --- apps/faucet/src/App/Faucet.tsx | 7 ---- apps/faucet/src/utils/index.ts | 73 ++++++++++++---------------------- 2 files changed, 25 insertions(+), 55 deletions(-) diff --git a/apps/faucet/src/App/Faucet.tsx b/apps/faucet/src/App/Faucet.tsx index 3d548cc62d..d75e86aca6 100644 --- a/apps/faucet/src/App/Faucet.tsx +++ b/apps/faucet/src/App/Faucet.tsx @@ -132,16 +132,9 @@ export const FaucetForm: React.FC = ({ throw new Error(`${code} - ${message}`); } )) || {}; - if (!tag || !challenge) { - throw new Error("Request challenge did not return a valid response"); - } const solution = computePowSolution(challenge, difficulty || 0); - if (!solution) { - throw new Error("A solution was not computed!"); - } - const signer = integration.signer(); if (!signer) { throw new Error("signer not defined"); diff --git a/apps/faucet/src/utils/index.ts b/apps/faucet/src/utils/index.ts index d7e9fd55bc..f5ae86a250 100644 --- a/apps/faucet/src/utils/index.ts +++ b/apps/faucet/src/utils/index.ts @@ -14,13 +14,14 @@ type SettingsResponse = { }; /** - * Request faucet settings + * Wrapper for fetch requests to handle ReadableStream response when errors are received from API */ -export const requestSettings = async ( - url: string -): Promise => { - return await fetch(new URL(`${url}/setting`), { - method: "GET", +export async function request( + url: string, + options: RequestInit = { method: "GET" } +): Promise { + return (await fetch(new URL(url), { + ...options, }) .then((response) => { if (response.ok) { @@ -29,15 +30,25 @@ export const requestSettings = async ( const reader = response?.body?.getReader(); return reader ?.read() - .then(({ value }) => - Promise.reject(JSON.parse(new TextDecoder().decode(value))) + .then((data) => + Promise.reject(JSON.parse(new TextDecoder().decode(data.value))) ); }) .catch((e) => { console.error(e); return Promise.reject(e); - }); + })) as T; +} + +/** + * Request faucet settings + */ +export const requestSettings = async ( + url: string +): Promise => { + return request(`${url}/setting`); }; + /** * Request challenge from endpoint url * @@ -47,27 +58,8 @@ export const requestSettings = async ( export const requestChallenge = async ( url: string, publicKey: string -): Promise => { - const response = await fetch(new URL(`${url}/challenge/${publicKey}`), { - method: "GET", - }) - .then((response) => { - if (response.ok) { - return response.json() as Promise; - } - const reader = response?.body?.getReader(); - return reader - ?.read() - .then(({ value }) => - Promise.reject(JSON.parse(new TextDecoder().decode(value))) - ); - }) - .catch((e) => { - console.error(e); - return Promise.reject(e); - }); - - return response; +): Promise => { + return request(`${url}/challenge/${publicKey}`); }; export type TransferDetails = { @@ -103,28 +95,13 @@ export const requestTransfer = async ( url: string, data: Data ): Promise => { - return await fetch(new URL(url), { + return request(url, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json", }, - }) - .then((response) => { - if (response.ok) { - return response.json(); - } - // Handle ReadableStream - const reader = response?.body?.getReader(); - return reader - ?.read() - .then(({ value }) => - Promise.reject(JSON.parse(new TextDecoder().decode(value))) - ); - }) - .catch((e) => { - return Promise.reject(e); - }); + }); }; /** @@ -171,7 +148,7 @@ export const getSolutionBytes = (int: number): Uint8Array => { export const computePowSolution = ( challenge: string, difficulty: number -): string | undefined => { +): string => { let i = 0; let solution: string = "";