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 c3c6943
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 55 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
73 changes: 25 additions & 48 deletions apps/faucet/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SettingsResponse> => {
return await fetch(new URL(`${url}/setting`), {
method: "GET",
export async function request<T = unknown>(
url: string,
options: RequestInit = { method: "GET" }
): Promise<T> {
return (await fetch(new URL(url), {
...options,
})
.then((response) => {
if (response.ok) {
Expand All @@ -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<SettingsResponse> => {
return request(`${url}/setting`);
};

/**
* Request challenge from endpoint url
*
Expand All @@ -47,27 +58,8 @@ export const requestSettings = async (
export const requestChallenge = async (
url: string,
publicKey: string
): Promise<ChallengeResponse | undefined> => {
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)))
);
})
.catch((e) => {
console.error(e);
return Promise.reject(e);
});

return response;
): Promise<ChallengeResponse> => {
return request(`${url}/challenge/${publicKey}`);
};

export type TransferDetails = {
Expand Down Expand Up @@ -103,28 +95,13 @@ export const requestTransfer = async (
url: string,
data: Data
): Promise<TransferResponse> => {
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);
});
});
};

/**
Expand Down Expand Up @@ -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 = "";

Expand Down

0 comments on commit c3c6943

Please sign in to comment.