UNPKG

@aliretail/react-utils

Version:
87 lines (64 loc) 1.67 kB
import env from './env'; import console from './console'; var ApiMap = /*#__PURE__*/function () { function ApiMap(apimaps) { var _this = this; if (apimaps === void 0) { apimaps = {}; } this.apicfg = {}; this.init = function (cfg) { if (cfg === void 0) { cfg = {}; } Object.assign(_this.apicfg, cfg); if ('base' in _this.apicfg) { for (var _key in _this.apicfg) { if (_key !== 'base') { _this.extendConfig(_key); } } } }; this.getApiCfg = function () { var cfg = _this.apicfg[_this.getEnv()]; if (!cfg && _this.apicfg.base) { return _this.apicfg.base; } return cfg; }; this.setEnv = function (newEnv) { env.set(newEnv); }; this.init(apimaps); } var _proto = ApiMap.prototype; _proto.extendConfig = function extendConfig(key) { this.apicfg[key] = Object.assign({}, this.apicfg.base, this.apicfg[key]); }; _proto.getUrl = function getUrl(api) { var cfg = this.getApiCfg(); if (cfg && cfg[api]) { var apiurl = cfg[api]; if (this.isURL(apiurl)) { return apiurl; } else if (cfg._HOST) { return cfg._HOST + apiurl; } return apiurl; } console.warn('apimap:%s not found in ENV:%s', api, this.getEnv()); return null; }; _proto.isURL = function isURL(url) { return /^\s*(http(s)?:\/\/|\/\/)/i.test(url); }; _proto.isLocalEnv = function isLocalEnv() { return env.isLocal(); }; _proto.getEnv = function getEnv() { return env.get(); }; return ApiMap; }(); export default ApiMap;