Skip to content

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

Open
yusefnapora wants to merge 10 commits intomainfrom
chore/refactor-frontend-api-methods
Open

chore: refactor frontend API client methods & add doc comments#1864
yusefnapora wants to merge 10 commits intomainfrom
chore/refactor-frontend-api-methods

Conversation

@yusefnapora
Copy link
Copy Markdown
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
Copy Markdown

codecov-commenter commented Apr 28, 2022

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (c13c4aa) to head (48b0a1d).
⚠️ Report is 305 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1864   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1242      1242           
=========================================
  Hits          1242      1242           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

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
Copy Markdown
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

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