@commercengine/storefront-sdk 0.3.7 → 0.3.8

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.
@@ -22,6 +22,7 @@ export declare class StorefrontAPIClient {
22
22
  protected client: ReturnType<typeof createClient<paths>>;
23
23
  protected config: StorefrontSDKOptions;
24
24
  private readonly baseUrl;
25
+ private initializationPromise;
25
26
  /**
26
27
  * Create a new StorefrontAPIClient
27
28
  *
@@ -97,4 +98,8 @@ export declare class StorefrontAPIClient {
97
98
  error?: ApiErrorResponse;
98
99
  response: Response;
99
100
  }>): Promise<ApiResult<T>>;
101
+ /**
102
+ * Initialize tokens in storage (private helper method)
103
+ */
104
+ private initializeTokens;
100
105
  }
@@ -22,6 +22,7 @@ export class StorefrontAPIClient {
22
22
  client;
23
23
  config;
24
24
  baseUrl;
25
+ initializationPromise = null;
25
26
  /**
26
27
  * Create a new StorefrontAPIClient
27
28
  *
@@ -46,17 +47,9 @@ export class StorefrontAPIClient {
46
47
  this.client.use(authMiddleware);
47
48
  // If initial tokens were provided, store them in tokenStorage
48
49
  if (this.config.accessToken) {
49
- this.config.tokenStorage.setAccessToken(this.config.accessToken).catch(error => {
50
- console.warn('Failed to set initial access token in storage:', error);
51
- });
52
- // Clear the manual token since we're using storage
50
+ this.initializationPromise = this.initializeTokens(this.config.accessToken, this.config.refreshToken);
51
+ // Clear the manual tokens since we're using storage
53
52
  this.config.accessToken = undefined;
54
- }
55
- if (this.config.refreshToken) {
56
- this.config.tokenStorage.setRefreshToken(this.config.refreshToken).catch(error => {
57
- console.warn('Failed to set initial refresh token in storage:', error);
58
- });
59
- // Clear the manual refresh token since we're using storage
60
53
  this.config.refreshToken = undefined;
61
54
  }
62
55
  }
@@ -100,14 +93,14 @@ export class StorefrontAPIClient {
100
93
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
101
94
  // Merge with existing signal if present
102
95
  if (request.signal) {
103
- request.signal.addEventListener('abort', () => controller.abort());
96
+ request.signal.addEventListener("abort", () => controller.abort());
104
97
  }
105
98
  // Create new request with timeout signal
106
99
  const newRequest = new Request(request, {
107
100
  signal: controller.signal,
108
101
  });
109
102
  // Clean up timeout when request completes
110
- controller.signal.addEventListener('abort', () => clearTimeout(timeoutId));
103
+ controller.signal.addEventListener("abort", () => clearTimeout(timeoutId));
111
104
  return newRequest;
112
105
  },
113
106
  });
@@ -149,6 +142,10 @@ export class StorefrontAPIClient {
149
142
  * @returns The Authorization header value or empty string if no token is set
150
143
  */
151
144
  async getAuthorizationHeader() {
145
+ // Wait for initialization to complete if using token storage
146
+ if (this.config.tokenStorage && this.initializationPromise) {
147
+ await this.initializationPromise;
148
+ }
152
149
  if (this.config.tokenStorage) {
153
150
  const token = await this.config.tokenStorage.getAccessToken();
154
151
  return token ? `Bearer ${token}` : "";
@@ -236,11 +233,27 @@ export class StorefrontAPIClient {
236
233
  data: null,
237
234
  error: {
238
235
  success: false,
239
- code: 'NETWORK_ERROR',
240
- message: 'Network error occurred',
236
+ code: "NETWORK_ERROR",
237
+ message: "Network error occurred",
241
238
  error: err,
242
239
  },
243
240
  };
244
241
  }
245
242
  }
243
+ /**
244
+ * Initialize tokens in storage (private helper method)
245
+ */
246
+ async initializeTokens(accessToken, refreshToken) {
247
+ try {
248
+ if (this.config.tokenStorage) {
249
+ await this.config.tokenStorage.setAccessToken(accessToken);
250
+ if (refreshToken) {
251
+ await this.config.tokenStorage.setRefreshToken(refreshToken);
252
+ }
253
+ }
254
+ }
255
+ catch (error) {
256
+ console.warn("Failed to initialize tokens in storage:", error);
257
+ }
258
+ }
246
259
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercengine/storefront-sdk",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "description": "TypeScript SDK for the Storefront API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",