UNPKG

@parcl-finance/product-sdk

Version:

TypeScript SDK for interacting with Parcl's product APIs

27 lines 1.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setAuthorizationHeader = void 0; const constants_1 = require("../constants"); function setAuthorizationHeader(headers, accessToken) { if (accessToken !== undefined) { headers["Authorization"] = "Bearer " + accessToken; } else if (typeof window !== "undefined") { const infoStr = localStorage.getItem(constants_1.LOCAL_STORAGE_KEY_USER_ACCESS_TOKEN_INFO); if (infoStr === null) { throw new Error(`${constants_1.AUTH_ERROR_PREFIX} failed to get user access token info from local storage`); } const info = JSON.parse(infoStr); const now = Math.floor(Date.now() / 1000); if (info.expiration < now) { throw new Error(`${constants_1.AUTH_ERROR_PREFIX} user access token is expired`); } headers["Authorization"] = "Bearer " + info.token; } else { throw new Error(`${constants_1.AUTH_ERROR_PREFIX} no access token provided AND environment is not compatible with browser APIs`); } return headers; } exports.setAuthorizationHeader = setAuthorizationHeader; //# sourceMappingURL=auth.js.map