UNPKG

@aitianyu.cn/tianyu-store

Version:
50 lines 2.21 kB
"use strict"; /**@format */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Infra = exports.InfraEnvironment = void 0; const types_1 = require("@aitianyu.cn/types"); const TIANYU_STORE_DOM_FEATURE_TOGGLE_PREFIX = "FEATURE_TIANYU_STORE"; const TIANYU_STORE_DOM_COOKIE_LANGUAGE = "LANGUAGE"; var InfraEnvironment; (function (InfraEnvironment) { InfraEnvironment[InfraEnvironment["DOM"] = 0] = "DOM"; InfraEnvironment[InfraEnvironment["Node"] = 1] = "Node"; })(InfraEnvironment = exports.InfraEnvironment || (exports.InfraEnvironment = {})); class Infra { static environment() { return typeof process !== "undefined" ? InfraEnvironment.Node : InfraEnvironment.DOM; } static readCookie(key) { const result = document.cookie.match(new RegExp(`(^| )${key}=([^;]*)(;|$)`)); return result && decodeURI(result[2]); } static getFeatureStatus(key) { if (InfraEnvironment.DOM === Infra.environment()) { // Browser env, to check the tianyu-shell status const featureToggle = window?.tianyuShell?.core?.featureToggle; if (featureToggle && typeof featureToggle.isActive === "function") { return featureToggle.isActive(`${TIANYU_STORE_DOM_FEATURE_TOGGLE_PREFIX}_${key}`); } else { // for tianyu shell is not valid // to generate info return (0, types_1.getBoolean)(Infra.readCookie(key)); } } else { // Node env, to get value from env configuration return (0, types_1.getBoolean)(process.env[key]); } } static getLanguage() { if (InfraEnvironment.DOM === Infra.environment()) { const language = Infra.readCookie(TIANYU_STORE_DOM_COOKIE_LANGUAGE); return language ? (0, types_1.parseAreaString)(language, false) : types_1.AreaCode.unknown; } else { return process.env["LANGUAGE"] ? (0, types_1.parseAreaString)(process.env["LANGUAGE"], false) : types_1.AreaCode.unknown; } } } exports.Infra = Infra; //# sourceMappingURL=Infra.js.map