Registry / security / fetch-plus-bearerauth

fetch-plus-bearerauth

JSON →
library3.10.4jsnpmunverified

Bearer authentication middleware for fetch-plus. Version 3.10.4 provides a simple way to attach Bearer tokens to HTTP requests via fetch-plus's middleware system. It integrates with fetch-plus's composable pipeline, allowing token injection from a static string, a function returning a token, or an async function. The library is lightweight and focused, relying on fetch-plus ^3.0.1. Compared to other auth helpers, it leverages fetch-plus's middleware architecture for cleaner separation of concerns, but is tightly coupled to fetch-plus and not usable standalone.

npm install fetch-plus-bearerauth
INSTALL
IMPORT
SIG · FETCH-PLUS-BEARERA
F
fetch-plus-bearerauth
securityjavascriptv3.10.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import bearerauth from 'fetch-plus-bearerauth'
const bearerauth = require('fetch-plus-bearerauth').default
Package is ESM-only. Use default import for the middleware factory.
bearerauth import
import { bearerauth } from 'fetch-plus-bearerauth'
Named export 'bearerauth' is also available (same as default). Prefer default for clarity.
require
const bearerauth = require('fetch-plus-bearerauth')
const { bearerauth } = require('fetch-plus-bearerauth')
CJS require returns the default export directly, not a named export object.

Shows how to create a fetch-plus client with bearer token authentication middleware.

import fetch from 'fetch-plus'; import bearerauth from 'fetch-plus-bearerauth'; // Create a fetch client with bearer auth middleware const client = fetch.create({ base: 'https://api.example.com', middleware: [ bearerauth({ token: process.env.API_TOKEN ?? '', header: 'Authorization', type: 'Bearer' }) ] }); // Use the client client.get('/user') .then(response => response.json()) .then(console.log);
Debug
Known issues
gotchaThe middleware must be in the 'middleware' array when creating a client; passing it directly to fetch() calls does not work.
fix
Use fetch.create({ middleware: [bearerauth({...})] }) or client.use(bearerauth({...})).
affects: >=3.0
deprecatedThe 'token' option as a plain string will not refresh tokens automatically. For dynamic tokens, pass a function.
fix
Use bearerauth({ token: () => getToken() }) to support token rotation.
affects: >=3.0
Errors
Common errors & fixes
TypeError: bearerauth is not a function
Importing the package incorrectly (e.g., using named import when default expected or vice versa).
fix
Use default import: import bearerauth from 'fetch-plus-bearerauth'
Uncaught (in promise) TypeError: Cannot destructure property 'headers' of undefined
Called bearerauth() outside of fetch-plus's middleware context, e.g., directly on a fetch call.
fix
Only use bearerauth() within a middleware array passed to fetch-plus create() or use().
Upgrade
Version history
3.10.4latest on npm
Audit
Dependencies
fetch-plusrequiredpeer dependency: required to provide the middleware system
Agent activity
20 hits · last 30 days
node
18
Amazon
1
Resources
fetch-plus-bearerauth — npm install fetch-plus-bearerauth · libregistry