Skip to content

This documentation site is deprecated. Please visit our new documentation site at lancedb.com/docs for the latest information.

@lancedb/lancedb β€’ Docs


@lancedb/lancedb / OAuthHeaderProvider

Class: OAuthHeaderProvider

Example implementation: OAuth token provider with automatic refresh.

This is an example implementation showing how to manage OAuth tokens with automatic refresh when they expire.

Example

async function fetchToken(): Promise<TokenResponse> {
  const response = await fetch("https://oauth.example.com/token", {
    method: "POST",
    body: JSON.stringify({
      grant_type: "client_credentials",
      client_id: "your-client-id",
      client_secret: "your-client-secret"
    }),
    headers: { "Content-Type": "application/json" }
  });
  const data = await response.json();
  return {
    accessToken: data.access_token,
    expiresIn: data.expires_in
  };
}

const provider = new OAuthHeaderProvider(fetchToken);
const headers = provider.getHeaders();
// Returns: {"authorization": "Bearer <your-token>"}

Extends

Constructors

new OAuthHeaderProvider()

new OAuthHeaderProvider(tokenFetcher, refreshBufferSeconds): OAuthHeaderProvider

Initialize the OAuth provider.

Parameters

  • tokenFetcher Function to fetch new tokens. Should return object with 'accessToken' and optionally 'expiresIn'.

  • refreshBufferSeconds: number = 300 Seconds before expiry to refresh token. Default 300 (5 minutes).

Returns

OAuthHeaderProvider

Overrides

HeaderProvider.constructor

Methods

getHeaders()

getHeaders(): Record<string, string>

Get OAuth headers, refreshing token if needed. Note: This is synchronous for now as the Rust implementation expects sync. In a real implementation, this would need to handle async properly.

Returns

Record<string, string>

Headers with Bearer token authorization.

Throws

If unable to fetch or refresh token.

Overrides

HeaderProvider.getHeaders


refreshToken()

refreshToken(): Promise<void>

Manually refresh the token. Call this before using getHeaders() to ensure token is available.

Returns

Promise<void>