tradly 1.1.93 → 1.1.94

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
1
+ import axios from "axios";
2
+ import { APPCONSTANT } from "../Constants/AppConstant.js";
3
+
4
+ // Simple in-memory cache: key = `${env}:${domain}`
5
+ const tenantConfigCache = new Map();
6
+
7
+ class InitV2 {
8
+ async config(init = { domain, env, custom_header }) {
9
+ const { domain, env, custom_header } = init || {};
10
+
11
+ if (!domain || !env) {
12
+ throw new Error(
13
+ "[Tradly InitV2] `domain` and `env` are required for initialization."
14
+ );
15
+ }
16
+
17
+ const cacheKey = `${env}:${domain}`;
18
+
19
+ // 1. If we have cached config, just reuse it and update APPCONSTANT
20
+ if (tenantConfigCache.has(cacheKey)) {
21
+ const cached = tenantConfigCache.get(cacheKey);
22
+ APPCONSTANT.TOKEN = cached.key;
23
+ APPCONSTANT.DOMAIN_ID = cached.domain_details?.id ?? 0;
24
+ APPCONSTANT.ENVIRONMENT = env;
25
+ APPCONSTANT.DOMAIN = domain;
26
+ APPCONSTANT.CUSTOM_HEADER = custom_header ? custom_header : {};
27
+ return cached;
28
+ }
29
+
30
+ // 2. Otherwise fetch pk_by_domain once and cache it
31
+ try {
32
+ const base =
33
+ env && env.toString().startsWith("dev")
34
+ ? "https://api.dev.tradly.app"
35
+ : "https://api.tradly.app";
36
+
37
+ const response = await axios({
38
+ url: `${base}/skua/tenants/pk_by_domain?domain=${domain}&env=${env}`,
39
+ method: "GET",
40
+ responseType: "json",
41
+ headers: {
42
+ "Content-Type": "application/json",
43
+ },
44
+ });
45
+
46
+ if (!response?.data?.status) {
47
+ throw new Error(
48
+ "[Tradly InitV2] Unable to fetch tenant config for given domain/env."
49
+ );
50
+ }
51
+
52
+ const key = response.data.data.key;
53
+ const domain_details = {
54
+ ...response.data.data.domain,
55
+ tenant_name: response.data.data.tenant_name ?? "",
56
+ };
57
+
58
+ // Set global app constants so existing modules work
59
+ APPCONSTANT.TOKEN = key;
60
+ APPCONSTANT.DOMAIN_ID = domain_details.id ?? 0;
61
+ APPCONSTANT.ENVIRONMENT = env;
62
+ APPCONSTANT.DOMAIN = domain;
63
+ APPCONSTANT.CUSTOM_HEADER = custom_header ? custom_header : {};
64
+
65
+ const result = {
66
+ status: true,
67
+ key,
68
+ domain_details,
69
+ tenant_name: domain_details.tenant_name,
70
+ };
71
+
72
+ tenantConfigCache.set(cacheKey, result);
73
+
74
+ return result;
75
+ } catch (error) {
76
+ // Do not cache failures; just surface the error
77
+ throw error;
78
+ }
79
+ }
80
+ }
81
+
82
+ const init_v2 = new InitV2();
83
+ export default init_v2;
84
+
85
+
package/Roots/Roots.js CHANGED
@@ -2,4 +2,6 @@ import user from "./User.js";
2
2
  import init from "./Initialize.js";
3
3
  import app from "./App.js";
4
4
  import set_sdk from "./SetSDK.js";
5
- export default { init, user, app, set_sdk };
5
+ import init_v2 from "./InitV2.js";
6
+
7
+ export default { init, user, app, set_sdk, init_v2 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tradly",
3
- "version": "1.1.93",
3
+ "version": "1.1.94",
4
4
  "description": "Tradly JS SDK",
5
5
  "main": "index.js",
6
6
  "type": "module",