UNPKG

myinvois-sdk

Version:

TypeScript SDK for interacting with the Malaysia e-invoicing system (MyInvois) API

88 lines (87 loc) 2.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenManager = void 0; const moment_timezone_1 = __importDefault(require("moment-timezone")); /** * Manages authentication tokens for multiple TINs */ class TokenManager { constructor() { this.tokens = new Map(); this.defaultToken = null; } /** * Sets a token for a specific TIN * @param tin The TIN to set the token for * @param token The token to set * @param expiresIn The token expiry in seconds */ setTokenForTIN(tin, token, expiresIn) { const expiresAt = (0, moment_timezone_1.default)().add(expiresIn, 'seconds').toISOString(); this.tokens.set(tin, { token, expiresAt }); } /** * Sets the default token (for system authentication) * @param token The token to set * @param expiresIn The token expiry in seconds */ setDefaultToken(token, expiresIn) { const expiresAt = (0, moment_timezone_1.default)().add(expiresIn, 'seconds').toISOString(); this.defaultToken = { token, expiresAt }; } /** * Gets a token for a specific TIN if it exists and is valid * @param tin The TIN to get the token for * @returns The token if it exists and is valid, null otherwise */ getTokenForTIN(tin) { const tokenInfo = this.tokens.get(tin); if (tokenInfo && (0, moment_timezone_1.default)(tokenInfo.expiresAt).tz('Asia/Kuala_Lumpur').isAfter((0, moment_timezone_1.default)().utc())) { return tokenInfo.token; } return null; } /** * Gets the default token if it exists and is valid * @returns The default token if it exists and is valid, null otherwise */ getDefaultToken() { if (this.defaultToken && (0, moment_timezone_1.default)(this.defaultToken.expiresAt).tz('Asia/Kuala_Lumpur').isAfter((0, moment_timezone_1.default)().utc())) { return this.defaultToken.token; } return null; } /** * Checks if a token for a specific TIN is valid * @param tin The TIN to check * @returns Whether the token is valid */ isTokenValid(tin) { return this.getTokenForTIN(tin) !== null; } /** * Checks if the default token is valid * @returns Whether the default token is valid */ isDefaultTokenValid() { return this.getDefaultToken() !== null; } /** * Gets all TINs with stored tokens * @returns An array of TINs */ getAllTINs() { return Array.from(this.tokens.keys()); } /** * Clears all tokens */ clearAllTokens() { this.tokens.clear(); this.defaultToken = null; } } exports.TokenManager = TokenManager;