## Summary This PR introduces a `HeaderProvider` which is called for all remote HTTP calls to get the latest headers to inject. This is useful for features like adding the latest auth tokens where the header provider can auto-refresh tokens internally and each request always set the refreshed token. --------- Co-authored-by: Claude <noreply@anthropic.com>
2.3 KiB
@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=300Seconds before expiry to refresh token. Default 300 (5 minutes).
Returns
Overrides
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
refreshToken()
refreshToken(): Promise<void>
Manually refresh the token. Call this before using getHeaders() to ensure token is available.
Returns
Promise<void>