Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor - Faucet: clean up faucet API & pow utils #627

Merged
merged 2 commits into from
Feb 13, 2024
Merged

Conversation

jurevans
Copy link
Collaborator

@jurevans jurevans commented Feb 9, 2024

This PR is to clean up the utils & return types used in Faucet app. We can add any other improvements here as well.

@jurevans jurevans self-assigned this Feb 9, 2024
@jurevans jurevans changed the title WIP: Faucet - clean up faucet utils and return types WIP: refactor - Faucet: clean up faucet utils and return types Feb 9, 2024
@jurevans jurevans changed the title WIP: refactor - Faucet: clean up faucet utils and return types refactor - Faucet: clean up faucet utils and return types Feb 12, 2024
@jurevans jurevans marked this pull request as ready for review February 12, 2024 13:35
@jurevans jurevans changed the title refactor - Faucet: clean up faucet utils and return types refactor - Faucet: clean up faucet API & pow utils Feb 12, 2024
Copy link
Collaborator

@mateuszjasiuk mateuszjasiuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Collaborator

@emccorson emccorson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me and the code looks much cleaner! I just left a couple of minor comments.

}
const reader = response?.body?.getReader();
return reader
?.read()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it matters, but I think this can still return a resolved Promise<undefined> even though the methods that use this return Promise<ChallengeResponse> etc.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I was trying to figure out why TypeScript doesn't catch that. I'm not 100% sure, but if you add a return type to the then callback, it will catch it:

return (await fetch(new URL(`${this.url}${endpoint}`), {
  ...options,                                           
})                                                      
  .then((response): Promise<T> => {
TS2322: Type 'Promise<T> | undefined' is not assignable to type 'Promise<T>'.
Type 'undefined' is not assignable to type 'Promise<T>'.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll take a look. I think in the case it can return undefined, we should throw an exception so it can be caught in the app. I wanted to remove any case of undefined as it's very tedious

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a type for that error response, with { message, code }, so a correctly parsed error should always be this type, and in the case of undefined (if it can't get the reader and parse the error response) it will instead throw an error

apps/faucet/src/utils/api.ts Outdated Show resolved Hide resolved
apps/faucet/src/App/Faucet.tsx Outdated Show resolved Hide resolved
@jurevans jurevans merged commit 265900a into main Feb 13, 2024
@jurevans jurevans deleted the fix/faucet-cleanup branch February 13, 2024 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants