tradly
Version:
Tradly JS SDK
161 lines (140 loc) • 4.07 kB
JavaScript
import axios from "axios";
import { APPCONSTANT } from "../Constants/AppConstant.js";
import {
isAuthInitialized,
getAuthConfig,
getCachedPKKeyFromAuth,
} from "../Helper/AuthHelper.js";
// Simple in-memory cache: key = `${env}:${domain}`
const tenantConfigCache = new Map();
class InitV2 {
async config(
init = { domain, env, custom_header, is_skip_pk_by_domain }
) {
const { domain, env, custom_header, is_skip_pk_by_domain } =
init || {};
if (!domain || !env) {
throw new Error(
"[Tradly InitV2] `domain` and `env` are required for initialization."
);
}
const cacheKey = `${env}:${domain}`;
// 1. If we have cached config, just reuse it and update APPCONSTANT
if (tenantConfigCache.has(cacheKey)) {
const cached = tenantConfigCache.get(cacheKey);
APPCONSTANT.TOKEN = cached.key;
APPCONSTANT.DOMAIN_ID = cached.domain_details?.id ?? 0;
APPCONSTANT.ENVIRONMENT = env;
APPCONSTANT.DOMAIN = domain;
APPCONSTANT.CUSTOM_HEADER = custom_header
? custom_header
: {};
APPCONSTANT.IS_SKIP_PK_BY_DOMAIN =
is_skip_pk_by_domain ?? false;
return cached;
}
// 1.5. Check if auth package is initialized and use its PK key if available
if (isAuthInitialized()) {
try {
const authConfig = getAuthConfig();
// Use auth's configuration if domain and env match
if (
authConfig &&
authConfig.domain === domain &&
authConfig.env === env
) {
const pkData = getCachedPKKeyFromAuth(domain, env);
if (pkData?.key) {
// Use auth's PK key
APPCONSTANT.TOKEN = pkData.key;
APPCONSTANT.DOMAIN_ID = pkData.domain?.id ?? 0;
APPCONSTANT.ENVIRONMENT = env;
APPCONSTANT.DOMAIN = domain;
APPCONSTANT.CUSTOM_HEADER = custom_header || {};
APPCONSTANT.IS_SKIP_PK_BY_DOMAIN =
is_skip_pk_by_domain ?? false;
// Cache in beluga's cache too
const result = {
status: true,
key: pkData.key,
domain_details: pkData.domain || { id: 0 },
tenant_name: pkData.domain?.tenant_name || "",
};
tenantConfigCache.set(cacheKey, result);
return result;
}
}
} catch (e) {
// Auth package available but not initialized or error, continue normally
console.warn(
"[Tradly InitV2] Could not sync with auth package:",
e
);
}
}
if (is_skip_pk_by_domain) {
APPCONSTANT.TOKEN = "";
APPCONSTANT.DOMAIN_ID = 0;
APPCONSTANT.ENVIRONMENT = env;
APPCONSTANT.DOMAIN = domain;
APPCONSTANT.CUSTOM_HEADER = custom_header
? custom_header
: {};
APPCONSTANT.IS_SKIP_PK_BY_DOMAIN =
is_skip_pk_by_domain ?? false;
return {
status: true,
key: "",
domain_details: {
id: 0,
},
};
}
// 2. Otherwise fetch pk_by_domain once and cache it
try {
const base =
env && env.toString().startsWith("dev")
? "https://api.dev.tradly.app"
: "https://api.tradly.app";
const response = await axios({
url: `${base}/skua/tenants/pk_by_domain?domain=${domain}&env=${env}`,
method: "GET",
responseType: "json",
headers: {
"Content-Type": "application/json",
},
});
if (!response?.data?.status) {
throw new Error(
"[Tradly InitV2] Unable to fetch tenant config for given domain/env."
);
}
const key = response.data.data.key;
const domain_details = {
...response.data.data.domain,
tenant_name: response.data.data.tenant_name ?? "",
};
// Set global app constants so existing modules work
APPCONSTANT.TOKEN = key;
APPCONSTANT.DOMAIN_ID = domain_details.id ?? 0;
APPCONSTANT.ENVIRONMENT = env;
APPCONSTANT.DOMAIN = domain;
APPCONSTANT.CUSTOM_HEADER = custom_header
? custom_header
: {};
const result = {
status: true,
key,
domain_details,
tenant_name: domain_details.tenant_name,
};
tenantConfigCache.set(cacheKey, result);
return result;
} catch (error) {
// Do not cache failures; just surface the error
throw error;
}
}
}
const init_v2 = new InitV2();
export default init_v2;