@sesamecare-oss/fetch-with-token
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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');

Package Sidebar

Install

npm i @sesamecare-oss/fetch-with-token

Weekly Downloads

12

Version

1.0.0

License

UNLICENSED

Unpacked Size

53.7 kB

Total Files

25

Last publish

Collaborators

  • djmax