npm.io
1.0.1 • Published 2 years ago

@sesamecare-oss/fetch-with-token

Licence
UNLICENSED
Version
1.0.1
Deps
0
Size
43 kB
Vulns
0
Weekly
0

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

Keywords