UNPKG

admitad-api-client

Version:

A TypeScript/JavaScript client for the Admitad API with built-in authentication and token management

38 lines 1.43 kB
/** * Encodes client credentials to base64 for HTTP Basic authentication * @param clientId - The Admitad client ID * @param clientSecret - The Admitad client secret * @returns Base64 encoded string in format "clientId:clientSecret" */ export function encodeCredentials(clientId, clientSecret) { const credentials = `${clientId}:${clientSecret}`; return btoa(credentials); } /** * Creates the Authorization header value for HTTP Basic authentication * @param base64Credentials - Base64 encoded credentials * @returns Authorization header value */ export function createAuthHeader(base64Credentials) { return `Basic ${base64Credentials}`; } /** * Validates if a token is expired based on its expiration timestamp * @param expiresAt - Token expiration timestamp in milliseconds * @param bufferSeconds - Buffer time in seconds before considering token expired (default: 60) * @returns True if token is expired or will expire within buffer time */ export function isTokenExpired(expiresAt, bufferSeconds = 60) { const now = Date.now(); const bufferMs = bufferSeconds * 1000; return now >= (expiresAt - bufferMs); } /** * Calculates token expiration timestamp * @param expiresIn - Token lifetime in seconds * @returns Expiration timestamp in milliseconds */ export function calculateExpirationTime(expiresIn) { return Date.now() + (expiresIn * 1000); } //# sourceMappingURL=auth.js.map