@smash-sdk/core
Version:
Smash Sdk Core
246 lines (245 loc) • 6.58 kB
JavaScript
import { InvalidRegionError } from "../errors/sdkError";
import { isNode } from "../helper/node";
import { SmashEnvRegion, SmashEnvToken } from "./types/index";
import { version } from '../version';
import { RefreshTokenManager } from "../helper/refreshTokenManager";
const globalKey = Symbol('smashSdkConfig_' + version);
;
export class Config {
get hosts() {
if (isNode()) {
if (!global.smashSdkHosts) {
global.smashSdkHosts = {};
}
return global.smashSdkHosts;
}
else {
if (!window.smashSdkHosts) {
window.smashSdkHosts = {};
}
return window.smashSdkHosts;
}
}
set hosts(hosts) {
if (isNode()) {
global.smashSdkHosts = hosts;
}
else {
window.smashSdkHosts = hosts;
}
}
get token() {
if (isNode()) {
return global.smashSdkToken;
}
else {
return window.smashSdkToken;
}
}
set token(token) {
if (isNode()) {
global.smashSdkToken = token;
}
else {
window.smashSdkToken = token;
}
}
get region() {
if (isNode()) {
return global.smashSdkRegion;
}
else {
return window.smashSdkRegion;
}
}
set region(region) {
if (isNode()) {
global.smashSdkRegion = region;
}
else {
window.smashSdkRegion = region;
}
}
get timeout() {
if (isNode()) {
return global.smashSdkTimeout;
}
else {
return window.smashSdkTimeout;
}
}
set timeout(timeout) {
if (isNode()) {
global.smashSdkTimeout = timeout;
}
else {
window.smashSdkTimeout = timeout;
}
}
get httpAgent() {
if (isNode()) {
return global.smashSdkHttpAgent;
}
else {
return window.smashSdkHttpAgent;
}
}
set httpAgent(httpAgent) {
if (isNode()) {
global.smashSdkHttpAgent = httpAgent;
}
else {
window.smashSdkHttpAgent = httpAgent;
}
}
get httpsAgent() {
if (isNode()) {
return global.smashSdkHttpsAgent;
}
else {
return window.smashSdkHttpsAgent;
}
}
set httpsAgent(httpsAgent) {
if (isNode()) {
global.smashSdkHttpsAgent = httpsAgent;
}
else {
window.smashSdkHttpsAgent = httpsAgent;
}
}
get refreshTokenMethod() {
if (isNode()) {
return global.smashSdkRefreshTokenMethod;
}
else {
return window.smashSdkRefreshTokenMethod;
}
}
set refreshTokenMethod(refreshTokenMethod) {
if (isNode()) {
global.smashSdkRefreshTokenMethod = refreshTokenMethod;
}
else {
window.smashSdkRefreshTokenMethod = refreshTokenMethod;
}
if (refreshTokenMethod) {
this.refreshtokenManager = new RefreshTokenManager(refreshTokenMethod);
}
else {
this.refreshtokenManager = undefined;
}
}
get refreshtokenManager() {
if (isNode()) {
return global.smashSdkRefreshTokenManager;
}
else {
return window.smashSdkRefreshTokenManager;
}
}
set refreshtokenManager(refreshTokenManager) {
if (isNode()) {
global.smashSdkRefreshTokenManager = refreshTokenManager;
}
else {
window.smashSdkRefreshTokenManager = refreshTokenManager;
}
}
static get Instance() {
if (isNode()) {
if (!global[globalKey]) {
Config.instance = new Config();
global[globalKey] = Config.instance;
}
Config.instance = global[globalKey];
}
else {
if (!window[globalKey]) {
Config.instance = new Config();
window[globalKey] = Config.instance;
}
Config.instance = window[globalKey];
}
return Config.instance;
}
constructor() {
if (isNode()) {
if (process?.env) {
if (process?.env[SmashEnvRegion]) {
this.setRegion(process.env[SmashEnvRegion]);
}
if (process?.env[SmashEnvToken]) {
this.setToken(process.env[SmashEnvToken]);
}
}
}
}
getHost(service, region = "global") {
if (region === "global" && this.hosts[service][region]) {
return this.hosts[service][region];
}
else if (this.hosts[service][region]) {
return this.hosts[service][region];
}
else {
throw new InvalidRegionError("Invalid region asked: " + region + " for service " + service + ", available regions are " + Object.keys(this.hosts[service]).join(", ") + ".");
}
}
setHosts(service, hosts, overwrite = true) {
if (overwrite === true) {
this.hosts[service] = hosts;
}
else {
const keys = Object.keys(hosts);
keys.forEach(key => {
if (!this.hosts[service]) {
this.hosts[service] = {};
}
if (!this.hosts[service][key]) {
this.hosts[service][key] = hosts[key];
}
});
}
}
setRegion(region) {
this.region = region;
}
getRegion() {
return this.region;
}
setToken(token) {
this.token = token;
}
getToken() {
return this.token;
}
setRefreshTokenMethod(refreshtokenMethod) {
this.refreshTokenMethod = refreshtokenMethod;
}
getRefreshTokenMethod() {
return this.refreshTokenMethod;
}
getRefreshTokenManager() {
return this.refreshtokenManager;
}
setTimeout(timeout) {
this.timeout = timeout;
}
getTimeout() {
return this.timeout;
}
setHttpAgent(httpAgent) {
this.httpAgent = httpAgent;
}
getHttpAgent() {
return this.httpAgent;
}
setHttpsAgent(httpsAgent) {
this.httpsAgent = httpsAgent;
}
getHttpsAgent() {
return this.httpsAgent;
}
}
export const config = Config.Instance;