@crimson-education/sdk 0.3.8 → 0.3.9

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.
@@ -24,8 +24,8 @@ export declare class CrimsonClient {
24
24
  private tenantDomainPromise?;
25
25
  constructor(config: CrimsonClientConfig);
26
26
  /**
27
- * Initialize the client (required for OAuth mode and tenant resolution)
28
- * Call this after creating the client to load stored tokens and resolve tenant
27
+ * Initialize the client (required for OAuth mode)
28
+ * Call this after creating the client to load stored tokens
29
29
  */
30
30
  initialize(): Promise<void>;
31
31
  /**
@@ -82,15 +82,14 @@ class CrimsonClient {
82
82
  }
83
83
  }
84
84
  /**
85
- * Initialize the client (required for OAuth mode and tenant resolution)
86
- * Call this after creating the client to load stored tokens and resolve tenant
85
+ * Initialize the client (required for OAuth mode)
86
+ * Call this after creating the client to load stored tokens
87
87
  */
88
88
  initialize() {
89
89
  return __awaiter(this, void 0, void 0, function* () {
90
90
  if (this.oauthAdapter) {
91
91
  yield this.oauthAdapter.initialize();
92
92
  }
93
- yield this.resolveTenantDomain();
94
93
  });
95
94
  }
96
95
  /**
@@ -105,6 +104,13 @@ class CrimsonClient {
105
104
  this.tenantDomainPromise = (() => __awaiter(this, void 0, void 0, function* () {
106
105
  var _a, _b;
107
106
  try {
107
+ const token = yield this.getToken();
108
+ if (!token) {
109
+ // Silently return without error if called early;
110
+ // it will be lazily retried in fetch() or when token is received.
111
+ this.tenantDomainPromise = undefined;
112
+ return;
113
+ }
108
114
  // A newer version of profile will return domain
109
115
  const profile = yield this.account.getMyProfile();
110
116
  if ((_a = profile.tenant) === null || _a === void 0 ? void 0 : _a.domain) {
@@ -125,6 +131,7 @@ class CrimsonClient {
125
131
  }
126
132
  catch (err) {
127
133
  console.warn('[CrimsonSDK] Failed to resolve tenant domain:', err);
134
+ this.tenantDomainPromise = undefined;
128
135
  }
129
136
  finally {
130
137
  this.isResolvingDomain = false;
@@ -236,8 +243,8 @@ class CrimsonClient {
236
243
  fetch(path_1) {
237
244
  return __awaiter(this, arguments, void 0, function* (path, options = {}) {
238
245
  // If it's a roadmap endpoint, wait for tenant domain to be resolved first
239
- if (path.includes("/roadmap") && this.tenantDomainPromise) {
240
- yield this.tenantDomainPromise;
246
+ if (path.includes("/roadmap")) {
247
+ yield this.resolveTenantDomain();
241
248
  }
242
249
  const token = yield this.getToken();
243
250
  if (!token) {
@@ -34,14 +34,20 @@ function CrimsonProvider({ children, apiUrl, allowedParentOrigins, queryClient:
34
34
  appName,
35
35
  appTenant,
36
36
  }), [apiUrl, clientId, appName, appTenant]);
37
- // trigger initialization, we need to determine tenant domain at first place.
38
- (0, react_1.useEffect)(() => {
39
- client.initialize();
40
- }, [client]);
41
37
  (0, react_1.useEffect)(() => {
42
38
  const origins = allowedParentOrigins || (0, iframe_1.getDefaultAllowedOrigins)();
43
- const cleanup = (0, iframe_1.setupIframeListener)(origins);
44
- return cleanup;
45
- }, [allowedParentOrigins]);
39
+ const cleanupIframe = (0, iframe_1.setupIframeListener)(origins);
40
+ // Eagerly resolve tenant domain as soon as token becomes available from iframe
41
+ const cleanupAuth = (0, iframe_1.subscribeToAuthState)(() => {
42
+ const state = (0, iframe_1.getAuthState)();
43
+ if (state.token) {
44
+ client.resolveTenantDomain().catch(() => { });
45
+ }
46
+ });
47
+ return () => {
48
+ cleanupIframe();
49
+ cleanupAuth();
50
+ };
51
+ }, [allowedParentOrigins, client]);
46
52
  return ((0, jsx_runtime_1.jsx)(CrimsonContext.Provider, { value: { client }, children: (0, jsx_runtime_1.jsx)(react_query_1.QueryClientProvider, { client: queryClient, children: children }) }));
47
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crimson-education/sdk",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
4
4
  "description": "Crimson SDK for accessing Crimson App APIs",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",