# @sesamecare-oss/fetch-with-token

> A simple wrapper around fetch that manages a token such as an oauth2 token and atomic refresh of expired tokens.

Latest version **1.0.1** (published 2024-07-17) · UNLICENSED license · 0 weekly downloads

## Install

```sh
npm install @sesamecare-oss/fetch-with-token
pnpm add @sesamecare-oss/fetch-with-token
yarn add @sesamecare-oss/fetch-with-token
bun add @sesamecare-oss/fetch-with-token
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2024-07-17 |
| First published | 2023-10-11 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 43.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Developers |
| Maintainers | djmax |
| Keywords | typescript, sesame, oauth, fetch |

## Links

- npm: https://www.npmjs.com/package/@sesamecare-oss/fetch-with-token
- Repository: https://github.com/sesamecare/fetch-with-token
- Homepage: https://github.com/sesamecare/fetch-with-token#readme
- Issues: https://github.com/sesamecare/fetch-with-token/issues
- npm.io page: https://npm.io/package/@sesamecare-oss/fetch-with-token

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2024-07-17
- 1.0.0 — 2023-10-11

## README

# 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.

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

---
_Source: https://npm.io/package/@sesamecare-oss/fetch-with-token · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
