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

chore: refactor frontend API client methods & add doc comments #1864

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

yusefnapora
Copy link
Contributor

This refactors a few things related to the frontend login flow that I found awkward while reading through the code. I'm definitely open to suggestions / change requests if anyone thinks I've made things worse :)

The major changes:

  • getToken from api.js has been renamed to getMagicUserToken, since there's a billion things called "token" in this codebase. Since this method only ever returns magic.link user id tokens, I gave it a more specific name, and also moved it to magic.js, since it's magic.link specific. Also renamed some of the global state vars related to saving the token to be more specific (e.g. LIFESPAN => MAGIC_USER_TOKEN_LIFESPAN_SEC).
  • isLoggedIn got renamed to getMagicUserMetadata, since that's what it actually does, and a function called isSomething that doesn't return a boolean is surprising.
  • added a logoutMagicSession function, so the logout component doesn't need direct access to the magic SDK instance
  • made the getMagic accessor internal to magic.js
  • added a getStorageClient accessor to api.js that returns an authenticated NFTStorage instance (using the key from getMagicUserToken).
    • this lets us stop exporting the API constant and the getToken functions, which were only used to new up client instances in UI code
  • added type definitions for API response objects
  • added doc comments

I also refactored almost all of the functions in api.js to remove copy/paste boilerplate

  • there's now a fetchRoute helper that appends the route path to the API base url, sets the Content-Type to json, and unpacks the JSON response body
  • fetchAuthenticated also sets the auth header to the value of getMagicUserToken

@redaphid would you mind checking this out and let me know if there's anything surprising?

@codecov-commenter
Copy link

codecov-commenter commented Apr 28, 2022

Codecov Report

Merging #1864 (48b0a1d) into main (c13c4aa) will not change coverage.
The diff coverage is n/a.

@@            Coverage Diff            @@
##              main     #1864   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1242      1242           
=========================================
  Hits          1242      1242           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4f10824...48b0a1d. Read the comment docs.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Apr 28, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 48b0a1d
Status: ✅  Deploy successful!
Preview URL: https://9c129a28.nft-storage-1at.pages.dev

View logs

Copy link
Contributor

@hugomrdias hugomrdias left a comment

Choose a reason for hiding this comment

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

LGTM just one console.log leftover

packages/website/lib/magic.js Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants