Skip to content

Commit

Permalink
fix: clean up faucet utils and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Feb 9, 2024
1 parent 4a362a1 commit e832a7b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
7 changes: 0 additions & 7 deletions apps/faucet/src/App/Faucet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,9 @@ export const FaucetForm: React.FC<Props> = ({
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");
Expand Down
57 changes: 21 additions & 36 deletions apps/faucet/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ type SettingsResponse = {
tokens_alias_to_address: Record<string, string>;
};

// TODO: We would need the ReadableStream type from node with a polyfill to properly handle this response type
/* eslint-disable */
export async function parseResponse<T = unknown>(response: any): Promise<T> {
if (response.ok) {
return response.json();
}
const reader = response?.body?.getReader();
return reader
?.read()
.then(({ value }: { value: any }) =>
Promise.reject(JSON.parse(new TextDecoder().decode(value)))
);
}
/* eslint-enable */

/**
* Request faucet settings
*/
Expand All @@ -22,22 +37,13 @@ export const requestSettings = async (
return await fetch(new URL(`${url}/setting`), {
method: "GET",
})
.then((response) => {
if (response.ok) {
return response.json();
}
const reader = response?.body?.getReader();
return reader
?.read()
.then(({ value }) =>
Promise.reject(JSON.parse(new TextDecoder().decode(value)))
);
})
.then(parseResponse<SettingsResponse>)
.catch((e) => {
console.error(e);
return Promise.reject(e);
});
};

/**
* Request challenge from endpoint url
*
Expand All @@ -47,21 +53,11 @@ export const requestSettings = async (
export const requestChallenge = async (
url: string,
publicKey: string
): Promise<ChallengeResponse | undefined> => {
): Promise<ChallengeResponse> => {
const response = await fetch(new URL(`${url}/challenge/${publicKey}`), {
method: "GET",
})
.then((response) => {
if (response.ok) {
return response.json() as Promise<ChallengeResponse>;
}
const reader = response?.body?.getReader();
return reader
?.read()
.then(({ value }) =>
Promise.reject(JSON.parse(new TextDecoder().decode(value)))
);
})
.then(parseResponse<ChallengeResponse>)
.catch((e) => {
console.error(e);
return Promise.reject(e);
Expand Down Expand Up @@ -110,18 +106,7 @@ export const requestTransfer = async (
"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)))
);
})
.then(parseResponse<TransferResponse>)
.catch((e) => {
return Promise.reject(e);
});
Expand Down Expand Up @@ -171,7 +156,7 @@ export const getSolutionBytes = (int: number): Uint8Array => {
export const computePowSolution = (
challenge: string,
difficulty: number
): string | undefined => {
): string => {
let i = 0;
let solution: string = "";

Expand Down

0 comments on commit e832a7b

Please sign in to comment.