Skip to content

A simple wrapper around fetch that manages a token such as an oauth2 token and atomic refresh of expired tokens

Notifications You must be signed in to change notification settings

sesamecare/fetch-with-token

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fetch-with-token

A simple wrapper around fetch that manages getting a token (typically oauth2 token or other refresh-able token) and updating it atomically when it expires or stops working.

You just need to write the code to get a new token and the code to analyze whether a retry is required.

import { createFetchFunction } from '@sesamecare-oss/fetch-with-token';

const partnerFetch = createFetchFunction({
  async getToken() {
    const body = await fetch('https://auth.partner.com?clientId=foo&secret=bar').then((r) => r.json());
    return {
      value: body.access_token,
      expiration: new Date(Date.now() + body.expires_in),
    };
  },
});

// This call will go out with an authorization header
const response = await partnerFetch('https://partner.com');

About

A simple wrapper around fetch that manages a token such as an oauth2 token and atomic refresh of expired tokens

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •