1.0.1 • Published 1 year ago

authorized-fetch-refresh v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

authorized-fetch-refresh

This npm package provides a method createAuthorizedFetch for creating an authorized fetch function that automatically handles token authentication and refresh.

Install

npm i authorized-fetch-refresh

Method

createAuthorizedFetch(
  getTokens: () => Promise<Tokens>,
  setTokens: (tokens: Tokens) => void,
  refreshUrl?: string,
  options?: CreateAuthorizedFetchOptions,
): (url: string, options: RequestInit) => Promise<Response>;

Parameters

getTokens: A function that returns a Promise resolving to the current tokens. setTokens: A function to update tokens. refreshUrl: An optional URL for token refresh. options: Additional options in json format

Additional options

OptionTypeDescriptionDefault Value
fetchtypeof fetchThe fetch function used for making HTTP requests.The global fetch function.
isSuccessfulStatusCode(statusCode: number) => booleanFunction to check if the HTTP status code indicates success.Returns true if the status code is in the range 200-299, false otherwise.
isUnauthorizedStatusCode(statusCode: number) => booleanFunction to check if the HTTP status code indicates unauthorized access.Returns true if the status code is 401, false otherwise.
onRefreshFailure() => voidCallback for unsuccessful token refresh attemptundefined

Default Options

Usage

import { createAuthorizedFetch, Tokens } from 'authorized-fetch-refresh';
import { customFetch } from './custom-fetch';

// Define getTokens function
const getTokens = async () => {
  return JSON.parse(localStorage.get('tokens'))
};

// Define setTokens function
const setTokens = (tokens: Tokens) => {
  localStorage.setItem('tokens', JSON.stringify(tokens))
};

// Create authorized fetch function
const authorizedFetch = createAuthorizedFetch(getTokens, setTokens, 'https://to_your_api/refresh_url_endpoint', {
  fetch: customFetch
});

// Usage example
authorizedFetch('https://api.example.com/data', {
  method: 'GET',
}).then(response => {
  // Handle response
}).catch(error => {
  // Handle error
});

This method handles token authentication by automatically attaching the token to the Authorization header of the request. If the token expires (returns 401), it attempts to refresh the token using the provided refreshUrl. If successful, it retries the original request with the new token.

1.0.1

1 year ago

1.0.0

1 year ago