Skip to main content

Function: bearerAuth()

bearerAuth(token): AuthProvider

Defined in: src/core/auth.ts:138

Authorization: Bearer <token>; token may be a (possibly async) getter. An empty / undefined token throws an SdvError naming the family — the request is never sent with Bearer undefined.

Parameters​

token​

string | (() => string | Promise<string | undefined> | undefined) | undefined

The bearer token, or a sync / async getter for it (read on every request, so a rotated token is picked up). undefined / "" means "no credential".

Returns​

AuthProvider

An AuthProvider whose apply sets Authorization: Bearer <token> unless the request already carries an Authorization header (any casing). It has no refresh.

Throws​

SdvError (from apply) when the token is empty / undefined: "<family>: no credential — the bearerAuth token is empty. …"; no request is made.

Example​

import { configure, bearerAuth } from 'sportsdataverse';

configure({ auth: { pff_api: bearerAuth(process.env.PFF_API_KEY) } });
// or a getter, evaluated per request:
configure({ auth: { pff_api: bearerAuth(async () => readKeyFromVault()) } });

Remarks​

A getter that throws is not retried by request(); a non-SdvError becomes AssetFetchError("<family>: auth failed (apply)").