New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

authorized-fetch-refresh

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

authorized-fetch-refresh

Fetch wrapper that adds auth token in headers and does refresh if token expired

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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.

Keywords

FAQs

Package last updated on 22 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc