# axios-token-interceptor

> An interceptor which makes it easier to work with tokens in [axios](https://github.com/mzabriskie/axios).

Latest version **0.2.0** (published 2019-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install axios-token-interceptor
pnpm add axios-token-interceptor
yarn add axios-token-interceptor
bun add axios-token-interceptor
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2019-10-26 |
| First published | 2017-08-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/axios-token-interceptor) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 18.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 36 |
| Author | Sandrino Di Mattia |
| Maintainers | sandrinodimattia |

## Links

- npm: https://www.npmjs.com/package/axios-token-interceptor
- Repository: https://github.com/sandrinodimattia/axios-token-interceptor
- Homepage: https://github.com/sandrinodimattia/axios-token-interceptor#readme
- Issues: https://github.com/sandrinodimattia/axios-token-interceptor/issues
- npm.io page: https://npm.io/package/axios-token-interceptor

## Dependencies (1)

- [lock](https://npm.io/package/lock.md) ^1.1.0

## Recent versions

- 0.2.0 (latest) — 2019-10-26
- 0.1.0 — 2017-08-23

## README

# Axios Token Interceptor

An interceptor which makes it easier to work with tokens in [axios](https://github.com/mzabriskie/axios).

## Usage

```js
const tokenProvider = require('axios-token-interceptor');

const instance = axios.create({
  baseURL: 'https://api.example.com'
});

// Configure the provider with the necessary options.
const options = { ... };
instance.interceptors.request.use(tokenProvider(options));

// When a call to an endpoint is made, a token will be provided as a header.
instance.get('/foo')
```

### Providing a token

There are different ways to provide a token. You can provide the token as a static value:

```js
instance.interceptors.request.use(tokenProvider({
  token: 'abc'
}));

// This will send the "Authorization: Bearer abc" header when making the call to the API endpoint.
instance.get('/foo')
```

Instead of providing a static value you can also use a method to get the token:

```js
instance.interceptors.request.use(tokenProvider({
  getToken: () => localStorage.get('access_token')
}));

// This will send the "Authorization: Bearer ..." header when making the call to the API endpoint.
instance.get('/foo')
```

And this method can also return a promise:

```js
instance.interceptors.request.use(tokenProvider({
  getToken: () => someMethod()
    .then(response => response.access_token);
}));

// This will send the "Authorization: Bearer ..." header when making the call to the API endpoint.
instance.get('/foo')
```

### Customizing the Header

The following options allow you to set the header and the header value:

```js
instance.interceptors.request.use(tokenProvider({
  token: 'abc',
  header: 'X-Api-Key',
  headerFormatter: (token) => 'token/' + token,
}));

// This will send the "X-Api-Key: token/abc" header when making the call to the API endpoint.
instance.get('/foo')
```

### Caching

In cases where getting a token is an expensive operation (eg: exchanging a refresh token for an access token) you'll want to cache this work for as long as the token is valid.

The following example shows how we can cache tokens for 8 hours:

```js
const cache = tokenProvider.tokenCache(
  getTokenFromAuthorizationServer().then(res => res.body.access_token),
  { maxAge: ms('8h') }
);

instance.interceptors.request.use(tokenProvider({
  getToken: cache
}));
```

Now it could also be that the token itself contains the expiration time (this is typically `expires_in` you'll get from your Authorization Server). In that case you can also use this to configure the maximum age of the cache:

```js
const cache = tokenProvider.tokenCache(
  () => getTokenFromAuthorizationServer().then(res => res.body),
  { getMaxAge: (body) => body.expires_in * 1000 }
);

instance.interceptors.request.use(tokenProvider({
  getToken: cache,
  headerFormatter: (body) => 'Bearer ' + body.access_token,
}));
```

And the cache can also be reset:

```js
const cache = tokenProvider.tokenCache(
  getTokenFromAuthorizationServer().then(res => res.body),
  { getMaxAge: (res) => res.expires_in * 1000 }
);

cache.reset();
```

> Note that `expires_in` coming from your authorization server is expressed in seconds, so you'll need to convert it to milliseconds when returning it to the `getMaxAge`function.

---
_Source: https://npm.io/package/axios-token-interceptor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
