UNPKG

@paykit-sdk/core

Version:

The Payment Toolkit for Typescript

80 lines (77 loc) 2.32 kB
import { HTTPClient } from './http-client.js'; import './tools/utils.js'; import 'zod'; import './resources/metadata.js'; interface OAuth2TokenResponse { accessToken: string; expiresIn: number; } interface OAuth2TokenManagerConfig { /** * HTTPClient instance for making token requests */ client: HTTPClient; /** * Provider name for error messages */ provider: string; /** * Token endpoint path (e.g., '/oauth2/token'). Include any required * query string directly (e.g. '/oauth/accesstoken?grant_type=client_credentials'). */ tokenEndpoint: string; /** * HTTP method the token endpoint expects. Defaults to 'POST' (the * common case: client_id/secret or grant_type in a form/JSON body). * Use 'GET' for providers that pass `grant_type` as a query param * instead (e.g. MoneyGram) - `requestBody` is ignored in that case. */ method?: 'GET' | 'POST'; /** * Credentials for Basic auth (username:password format) */ credentials: { username: string; password: string; }; /** * Adapter function to extract token and expiry from provider response */ responseAdapter: (response: Record<string, any>) => OAuth2TokenResponse; /** * Optional request body */ requestBody?: string | URLSearchParams; /** * Optional request headers */ requestHeaders?: Record<string, string>; /** * Optional expiry buffer in seconds (defaults to 300 = 5 minutes) */ expiryBuffer?: number; /** * Optional additional headers to include in getAuthHeaders() */ authHeaders?: Record<string, string>; } /** * Manages OAuth2 access tokens with automatic refresh and caching */ declare class OAuth2TokenManager { private _accessToken; private _expiresAt; private _refreshPromise; private readonly config; constructor(config: OAuth2TokenManagerConfig); /** * Get a valid access token, refreshing if necessary */ getToken: () => Promise<string>; private _refreshToken; /** * Get authorization headers for API requests */ getAuthHeaders: () => Promise<Record<string, string>>; } export { OAuth2TokenManager, type OAuth2TokenManagerConfig, type OAuth2TokenResponse };