UNPKG

@tnwx/accesstoken

Version:

TNWX 微信系开发脚手架之微信 accesstoken

88 lines 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cache_1 = require("@tnwx/cache"); class ApiConfigKit { set devMode(devMode) { this.devMode = devMode; } static isDevMode() { return this.devMode; } static putApiConfig(apiConfig) { if (this.CFG_MAP.size == 0) { this.CFG_MAP.set(this.DEFAULT_CFG_KEY, apiConfig); } return this.CFG_MAP.set(apiConfig.getAppId, apiConfig); } static removeApiConfigByConfig(apiConfig) { return this.removeApiConfig(apiConfig.getAppId); } static removeApiConfig(appId) { return this.CFG_MAP.delete(appId); } static setCurrentAppId(appId) { if (appId) { this.currentAppId = appId; } else { let apiConfig = this.CFG_MAP.get(this.DEFAULT_CFG_KEY); if (apiConfig) { appId = apiConfig.getAppId; this.currentAppId = appId; } } } static removeCurrentAppId() { this.currentAppId = ''; } static get getAppId() { let appId = this.currentAppId; if (!appId) { let apiConfig = this.CFG_MAP.get(this.DEFAULT_CFG_KEY); if (apiConfig) { appId = apiConfig.getAppId; } } return appId; } static get getToken() { let token; let appId = this.DEFAULT_CFG_KEY; if (this.currentAppId) { appId = this.currentAppId; } let apiConfig = this.CFG_MAP.get(appId); if (apiConfig) { token = apiConfig.getToken; } if (!token) { throw new Error('需事先调用 ApiConfigKit.putApiConfig(apiConfig) 将 appId 对应的 ApiConfig 对象存入后, 才可以使用 ApiConfigKit.getToken 系列方法'); } return token; } static get getApiConfig() { return this.getApiConfigByAppId(this.getAppId); } static getApiConfigByAppId(appId) { if (ApiConfigKit.isDevMode()) { console.debug(`getApiConfigByAppId appId: ${appId}`); } let cfg = this.CFG_MAP.get(appId); if (!cfg) { throw new Error('需事先调用 ApiConfigKit.putApiConfig(apiConfig) 将 appId 对应的 ApiConfig 对象存入后,才可以使用 ApiConfigKit.getApiConfig 系列方法'); } return cfg; } static get getCache() { return this._cache; } static set setCache(cache) { this._cache = cache; } } exports.ApiConfigKit = ApiConfigKit; ApiConfigKit.CFG_MAP = new Map(); ApiConfigKit.DEFAULT_CFG_KEY = '_default_cfg_key_'; ApiConfigKit.devMode = false; ApiConfigKit._cache = new cache_1.DefaultCache(); //# sourceMappingURL=ApiConfigKit.js.map