@commercengine/storefront-sdk 0.3.8 → 0.3.10

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.
package/dist/index.d.ts CHANGED
@@ -8,6 +8,18 @@ import { HelpersClient } from "./lib/helper";
8
8
  import { CustomerClient } from "./lib/customer";
9
9
  import { TokenStorage, MemoryTokenStorage, BrowserTokenStorage, CookieTokenStorage } from "./lib/middleware";
10
10
  import { type UserInfo } from "./lib/jwt-utils";
11
+ import { ResponseUtils, type DebugLoggerFn } from "./lib/logger-utils";
12
+ /**
13
+ * Supported default headers that can be set at the SDK level
14
+ * Only includes headers that are actually supported by API endpoints
15
+ */
16
+ export interface SupportedDefaultHeaders {
17
+ /**
18
+ * Customer group ID used for pricing, promotions, and subscription rates
19
+ * If not provided, the API will use default pricing
20
+ */
21
+ customer_group_id?: string;
22
+ }
11
23
  /**
12
24
  * SDK initialization options
13
25
  */
@@ -58,6 +70,23 @@ export interface StorefrontSDKOptions {
58
70
  * Callback when tokens are cleared (logout/error)
59
71
  */
60
72
  onTokensCleared?: () => void;
73
+ /**
74
+ * Default headers to include with API requests
75
+ * These can be overridden at the method level
76
+ * Only supports headers that are actually available in the API
77
+ */
78
+ defaultHeaders?: SupportedDefaultHeaders;
79
+ /**
80
+ * Enable debug mode for detailed request/response logging
81
+ * When enabled, detailed debug information will be logged via the logger
82
+ * Note: Response objects are always included in ApiResult regardless of debug mode
83
+ */
84
+ debug?: boolean;
85
+ /**
86
+ * Custom logger function for debug information
87
+ * If not provided and debug is enabled, will use console.log
88
+ */
89
+ logger?: DebugLoggerFn;
61
90
  }
62
91
  /**
63
92
  * Main SDK class for the Storefront API
@@ -166,12 +195,25 @@ export declare class StorefrontSDK {
166
195
  * @returns Customer group ID or null if no token, invalid token, or user has no customer group
167
196
  */
168
197
  getCustomerGroupId(): Promise<string | null>;
198
+ /**
199
+ * Set default headers for all clients
200
+ *
201
+ * @param headers - Default headers to set (only supported headers allowed)
202
+ */
203
+ setDefaultHeaders(headers: SupportedDefaultHeaders): void;
204
+ /**
205
+ * Get current default headers
206
+ *
207
+ * @returns Current default headers
208
+ */
209
+ getDefaultHeaders(): SupportedDefaultHeaders | undefined;
169
210
  }
170
211
  export default StorefrontSDK;
171
- export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient, };
212
+ export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient, ResponseUtils, };
172
213
  export { Environment };
173
214
  export { TokenStorage, MemoryTokenStorage, BrowserTokenStorage, CookieTokenStorage, };
174
215
  export type { CookieTokenStorageOptions } from "./lib/middleware";
175
216
  export type { UserInfo } from "./lib/jwt-utils";
217
+ export type { DebugLoggerFn } from "./lib/logger-utils";
176
218
  export type { components, operations, paths } from "./types/storefront";
177
219
  export type * from "./types/storefront-api-types";
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ import { HelpersClient } from "./lib/helper";
8
8
  import { CustomerClient } from "./lib/customer";
9
9
  import { MemoryTokenStorage, BrowserTokenStorage, CookieTokenStorage, } from "./lib/middleware";
10
10
  import { extractUserInfoFromToken, getUserIdFromToken, isUserLoggedIn, isUserAnonymous, } from "./lib/jwt-utils";
11
+ import { ResponseUtils, } from "./lib/logger-utils";
11
12
  /**
12
13
  * Main SDK class for the Storefront API
13
14
  */
@@ -58,6 +59,9 @@ export class StorefrontSDK {
58
59
  tokenStorage: options.tokenStorage,
59
60
  onTokensUpdated: options.onTokensUpdated,
60
61
  onTokensCleared: options.onTokensCleared,
62
+ defaultHeaders: options.defaultHeaders,
63
+ debug: options.debug,
64
+ logger: options.logger,
61
65
  };
62
66
  this.catalog = new CatalogClient(config);
63
67
  this.cart = new CartClient(config);
@@ -198,11 +202,35 @@ export class StorefrontSDK {
198
202
  const userInfo = await this.getUserInfo();
199
203
  return userInfo?.customerGroupId || null;
200
204
  }
205
+ /**
206
+ * Set default headers for all clients
207
+ *
208
+ * @param headers - Default headers to set (only supported headers allowed)
209
+ */
210
+ setDefaultHeaders(headers) {
211
+ // Update config for all clients
212
+ const newConfig = { ...this.catalog["config"], defaultHeaders: headers };
213
+ this.catalog["config"] = newConfig;
214
+ this.cart["config"] = newConfig;
215
+ this.auth["config"] = newConfig;
216
+ this.customer["config"] = newConfig;
217
+ this.helpers["config"] = newConfig;
218
+ this.shipping["config"] = newConfig;
219
+ this.order["config"] = newConfig;
220
+ }
221
+ /**
222
+ * Get current default headers
223
+ *
224
+ * @returns Current default headers
225
+ */
226
+ getDefaultHeaders() {
227
+ return this.catalog["config"].defaultHeaders;
228
+ }
201
229
  }
202
230
  // Export the main SDK class
203
231
  export default StorefrontSDK;
204
232
  // Export individual clients for advanced usage
205
- export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient, };
233
+ export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient, ResponseUtils, };
206
234
  // Export environment enum
207
235
  export { Environment };
208
236
  // Export token storage types
@@ -149,15 +149,15 @@ export declare class CartClient extends StorefrontAPIClient {
149
149
  /**
150
150
  * Get all available coupons
151
151
  *
152
- * @param customerGroupId - The ID of the customer group
152
+ * @param headers - Optional header parameters (customer_group_id, etc.)
153
153
  * @returns Promise with all available coupons
154
154
  */
155
- getAvailableCoupons(headers: ListCouponsHeaderParams): Promise<ApiResult<ListCouponsContent>>;
155
+ getAvailableCoupons(headers?: ListCouponsHeaderParams): Promise<ApiResult<ListCouponsContent>>;
156
156
  /**
157
157
  * Get all available promotions
158
158
  *
159
- * @param customerGroupId - The ID of the customer group
159
+ * @param headers - Optional header parameters (customer_group_id, etc.)
160
160
  * @returns Promise with all available promotions
161
161
  */
162
- getAvailablePromotions(headers: ListPromotionsHeaderParams): Promise<ApiResult<ListPromotionsContent>>;
162
+ getAvailablePromotions(headers?: ListPromotionsHeaderParams): Promise<ApiResult<ListPromotionsContent>>;
163
163
  }
package/dist/lib/cart.js CHANGED
@@ -274,23 +274,29 @@ export class CartClient extends StorefrontAPIClient {
274
274
  /**
275
275
  * Get all available coupons
276
276
  *
277
- * @param customerGroupId - The ID of the customer group
277
+ * @param headers - Optional header parameters (customer_group_id, etc.)
278
278
  * @returns Promise with all available coupons
279
279
  */
280
280
  async getAvailableCoupons(headers) {
281
+ const mergedHeaders = this.mergeHeaders(headers);
281
282
  return this.executeRequest(() => this.client.GET("/carts/available-coupons", {
282
- headers: headers,
283
+ params: {
284
+ header: mergedHeaders,
285
+ },
283
286
  }));
284
287
  }
285
288
  /**
286
289
  * Get all available promotions
287
290
  *
288
- * @param customerGroupId - The ID of the customer group
291
+ * @param headers - Optional header parameters (customer_group_id, etc.)
289
292
  * @returns Promise with all available promotions
290
293
  */
291
294
  async getAvailablePromotions(headers) {
295
+ const mergedHeaders = this.mergeHeaders(headers);
292
296
  return this.executeRequest(() => this.client.GET("/carts/available-promotions", {
293
- headers: headers,
297
+ params: {
298
+ header: mergedHeaders,
299
+ },
294
300
  }));
295
301
  }
296
302
  }
@@ -1,5 +1,5 @@
1
1
  import { StorefrontAPIClient } from "./client";
2
- import type { ApiResult, GetProductDetailContent, GetProductDetailPathParams, GetProductDetailQuery, GetVariantDetailContent, GetVariantDetailQuery, GetVariantDetailPathParams, ListProductsContent, ListProductsQuery, ListProductVariantsContent, ListProductVariantsPathParams, ListProductVariantsQuery, ListCategoriesQuery, ListCategoriesContent, ListProductReviewsQuery, ListProductReviewsPathParams, ListProductReviewsContent, CreateProductReviewPathParams, CreateProductReviewFormData, CreateProductReviewResponse, SearchProductsBody, SearchProductsContent, ListSkusQuery, ListSkusContent, ListCrosssellProductsContent, ListCrosssellProductsQuery, ListUpsellProductsQuery, ListUpsellProductsContent, ListSimilarProductsQuery, ListSimilarProductsContent } from "../types/storefront-api-types";
2
+ import type { ApiResult, GetProductDetailContent, GetProductDetailPathParams, GetProductDetailHeaderParams, GetVariantDetailContent, GetVariantDetailPathParams, GetVariantDetailHeaderParams, ListProductsContent, ListProductsQuery, ListProductsHeaderParams, ListProductVariantsContent, ListProductVariantsPathParams, ListProductVariantsHeaderParams, ListCategoriesQuery, ListCategoriesContent, ListProductReviewsQuery, ListProductReviewsPathParams, ListProductReviewsContent, CreateProductReviewPathParams, CreateProductReviewFormData, CreateProductReviewResponse, SearchProductsBody, SearchProductsContent, ListSkusQuery, ListSkusContent, ListSkusHeaderParams, ListCrosssellProductsContent, ListCrosssellProductsQuery, ListCrosssellProductsHeaderParams, ListUpsellProductsQuery, ListUpsellProductsContent, ListUpsellProductsHeaderParams, ListSimilarProductsQuery, ListSimilarProductsContent, ListSimilarProductsHeaderParams } from "../types/storefront-api-types";
3
3
  /**
4
4
  * Client for interacting with catalog endpoints
5
5
  */
@@ -8,41 +8,42 @@ export declare class CatalogClient extends StorefrontAPIClient {
8
8
  * List all products
9
9
  *
10
10
  * @param options - Optional query parameters
11
+ * @param headers - Optional header parameters (customer_group_id, etc.)
11
12
  * @returns Promise with products and pagination info
12
13
  */
13
- listProducts(options?: ListProductsQuery): Promise<ApiResult<ListProductsContent>>;
14
+ listProducts(options?: ListProductsQuery, headers?: ListProductsHeaderParams): Promise<ApiResult<ListProductsContent>>;
14
15
  /**
15
16
  * List all skus
16
17
  *
17
18
  * @param options - Optional query parameters
19
+ * @param headers - Optional header parameters (customer_group_id, etc.)
18
20
  * @returns Promise with skus and pagination info
19
21
  */
20
- listSkus(options?: ListSkusQuery): Promise<ApiResult<ListSkusContent>>;
22
+ listSkus(options?: ListSkusQuery, headers?: ListSkusHeaderParams): Promise<ApiResult<ListSkusContent>>;
21
23
  /**
22
24
  * Get details for a specific product
23
25
  *
24
- * @param productId - The ID of the product
25
- * @param options - Optional query parameters
26
+ * @param pathParams - The path parameters (product ID or slug)
27
+ * @param headers - Optional header parameters (customer_group_id, etc.)
26
28
  * @returns Promise with product details
27
29
  */
28
- getProductDetail(pathParams: GetProductDetailPathParams, queryParams?: GetProductDetailQuery): Promise<ApiResult<GetProductDetailContent>>;
30
+ getProductDetail(pathParams: GetProductDetailPathParams, headers?: GetProductDetailHeaderParams): Promise<ApiResult<GetProductDetailContent>>;
29
31
  /**
30
32
  * List variants for a specific product
31
33
  *
32
- * @param productId - The ID of the product
33
- * @param options - Optional query parameters
34
+ * @param pathParams - The path parameters (product ID)
35
+ * @param headers - Optional header parameters (customer_group_id, etc.)
34
36
  * @returns Promise with variants
35
37
  */
36
- listProductVariants(pathParams: ListProductVariantsPathParams, queryParams?: ListProductVariantsQuery): Promise<ApiResult<ListProductVariantsContent>>;
38
+ listProductVariants(pathParams: ListProductVariantsPathParams, headers?: ListProductVariantsHeaderParams): Promise<ApiResult<ListProductVariantsContent>>;
37
39
  /**
38
40
  * Get details for a specific variant
39
41
  *
40
- * @param productId - The ID of the product
41
- * @param variantId - The ID of the variant
42
- * @param options - Optional query parameters
42
+ * @param pathParams - The path parameters (product ID and variant ID)
43
+ * @param headers - Optional header parameters (customer_group_id, etc.)
43
44
  * @returns Promise with variant details
44
45
  */
45
- getVariantDetail(pathParams: GetVariantDetailPathParams, queryParams?: GetVariantDetailQuery): Promise<ApiResult<GetVariantDetailContent>>;
46
+ getVariantDetail(pathParams: GetVariantDetailPathParams, headers?: GetVariantDetailHeaderParams): Promise<ApiResult<GetVariantDetailContent>>;
46
47
  /**
47
48
  * List all categories
48
49
  *
@@ -53,16 +54,16 @@ export declare class CatalogClient extends StorefrontAPIClient {
53
54
  /**
54
55
  * List reviews for a specific product
55
56
  *
56
- * @param productId - The ID of the product
57
- * @param options - Optional query parameters
57
+ * @param pathParams - The path parameters (product ID)
58
+ * @param queryParams - Optional query parameters
58
59
  * @returns Promise with reviews and pagination info
59
60
  */
60
61
  listProductReviews(pathParams: ListProductReviewsPathParams, queryParams?: ListProductReviewsQuery): Promise<ApiResult<ListProductReviewsContent>>;
61
62
  /**
62
63
  * Create a review for a specific product
63
64
  *
64
- * @param productId - The ID of the product
65
- * @param reviewData - The review data
65
+ * @param pathParams - The path parameters (product ID)
66
+ * @param formData - The review data
66
67
  * @returns Promise that resolves when the review is created
67
68
  */
68
69
  createProductReview(pathParams: CreateProductReviewPathParams, formData: CreateProductReviewFormData): Promise<ApiResult<CreateProductReviewResponse>>;
@@ -74,24 +75,27 @@ export declare class CatalogClient extends StorefrontAPIClient {
74
75
  */
75
76
  searchProducts(searchData: SearchProductsBody): Promise<ApiResult<SearchProductsContent>>;
76
77
  /**
77
- * Retrieve cross-sell recommendations for a specific product
78
+ * List cross-sell products
78
79
  *
79
80
  * @param options - Optional query parameters
80
- * @returns Promise with cross-sell recommendations with pagination
81
+ * @param headers - Optional header parameters (customer_group_id, etc.)
82
+ * @returns Promise with cross-sell products
81
83
  */
82
- listCrossSellProducts(options?: ListCrosssellProductsQuery): Promise<ApiResult<ListCrosssellProductsContent>>;
84
+ listCrossSellProducts(options?: ListCrosssellProductsQuery, headers?: ListCrosssellProductsHeaderParams): Promise<ApiResult<ListCrosssellProductsContent>>;
83
85
  /**
84
- * Retrieve up-sell recommendations for a specific product
86
+ * List up-sell products
85
87
  *
86
88
  * @param options - Optional query parameters
87
- * @returns Promise with up-sell recommendations with pagination
89
+ * @param headers - Optional header parameters (customer_group_id, etc.)
90
+ * @returns Promise with up-sell products
88
91
  */
89
- listUpSellProducts(options?: ListUpsellProductsQuery): Promise<ApiResult<ListUpsellProductsContent>>;
92
+ listUpSellProducts(options?: ListUpsellProductsQuery, headers?: ListUpsellProductsHeaderParams): Promise<ApiResult<ListUpsellProductsContent>>;
90
93
  /**
91
- * Retrieve related products for a specific product
94
+ * List similar products
92
95
  *
93
96
  * @param options - Optional query parameters
94
- * @returns Promise with related products with pagination
97
+ * @param headers - Optional header parameters (customer_group_id, etc.)
98
+ * @returns Promise with similar products
95
99
  */
96
- listSimilarProducts(options?: ListSimilarProductsQuery): Promise<ApiResult<ListSimilarProductsContent>>;
100
+ listSimilarProducts(options?: ListSimilarProductsQuery, headers?: ListSimilarProductsHeaderParams): Promise<ApiResult<ListSimilarProductsContent>>;
97
101
  }
@@ -7,67 +7,79 @@ export class CatalogClient extends StorefrontAPIClient {
7
7
  * List all products
8
8
  *
9
9
  * @param options - Optional query parameters
10
+ * @param headers - Optional header parameters (customer_group_id, etc.)
10
11
  * @returns Promise with products and pagination info
11
12
  */
12
- async listProducts(options) {
13
- return this.executeRequest(() => this.client.GET('/catalog/products', {
14
- params: { query: options },
13
+ async listProducts(options, headers) {
14
+ const mergedHeaders = this.mergeHeaders(headers);
15
+ return this.executeRequest(() => this.client.GET("/catalog/products", {
16
+ params: {
17
+ query: options,
18
+ header: mergedHeaders,
19
+ },
15
20
  }));
16
21
  }
17
22
  /**
18
23
  * List all skus
19
24
  *
20
25
  * @param options - Optional query parameters
26
+ * @param headers - Optional header parameters (customer_group_id, etc.)
21
27
  * @returns Promise with skus and pagination info
22
28
  */
23
- async listSkus(options) {
24
- return this.executeRequest(() => this.client.GET('/catalog/skus', {
25
- params: { query: options },
29
+ async listSkus(options, headers) {
30
+ const mergedHeaders = this.mergeHeaders(headers);
31
+ return this.executeRequest(() => this.client.GET("/catalog/skus", {
32
+ params: {
33
+ query: options,
34
+ header: mergedHeaders,
35
+ },
26
36
  }));
27
37
  }
28
38
  /**
29
39
  * Get details for a specific product
30
40
  *
31
- * @param productId - The ID of the product
32
- * @param options - Optional query parameters
41
+ * @param pathParams - The path parameters (product ID or slug)
42
+ * @param headers - Optional header parameters (customer_group_id, etc.)
33
43
  * @returns Promise with product details
34
44
  */
35
- async getProductDetail(pathParams, queryParams) {
36
- return this.executeRequest(() => this.client.GET('/catalog/products/{product_id}', {
45
+ async getProductDetail(pathParams, headers) {
46
+ const mergedHeaders = this.mergeHeaders(headers);
47
+ return this.executeRequest(() => this.client.GET("/catalog/products/{product_id_or_slug}", {
37
48
  params: {
38
49
  path: pathParams,
39
- query: queryParams,
50
+ header: mergedHeaders,
40
51
  },
41
52
  }));
42
53
  }
43
54
  /**
44
55
  * List variants for a specific product
45
56
  *
46
- * @param productId - The ID of the product
47
- * @param options - Optional query parameters
57
+ * @param pathParams - The path parameters (product ID)
58
+ * @param headers - Optional header parameters (customer_group_id, etc.)
48
59
  * @returns Promise with variants
49
60
  */
50
- async listProductVariants(pathParams, queryParams) {
61
+ async listProductVariants(pathParams, headers) {
62
+ const mergedHeaders = this.mergeHeaders(headers);
51
63
  return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/variants", {
52
64
  params: {
53
65
  path: pathParams,
54
- query: queryParams,
66
+ header: mergedHeaders,
55
67
  },
56
68
  }));
57
69
  }
58
70
  /**
59
71
  * Get details for a specific variant
60
72
  *
61
- * @param productId - The ID of the product
62
- * @param variantId - The ID of the variant
63
- * @param options - Optional query parameters
73
+ * @param pathParams - The path parameters (product ID and variant ID)
74
+ * @param headers - Optional header parameters (customer_group_id, etc.)
64
75
  * @returns Promise with variant details
65
76
  */
66
- async getVariantDetail(pathParams, queryParams) {
77
+ async getVariantDetail(pathParams, headers) {
78
+ const mergedHeaders = this.mergeHeaders(headers);
67
79
  return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/variants/{variant_id}", {
68
80
  params: {
69
81
  path: pathParams,
70
- query: queryParams,
82
+ header: mergedHeaders,
71
83
  },
72
84
  }));
73
85
  }
@@ -85,8 +97,8 @@ export class CatalogClient extends StorefrontAPIClient {
85
97
  /**
86
98
  * List reviews for a specific product
87
99
  *
88
- * @param productId - The ID of the product
89
- * @param options - Optional query parameters
100
+ * @param pathParams - The path parameters (product ID)
101
+ * @param queryParams - Optional query parameters
90
102
  * @returns Promise with reviews and pagination info
91
103
  */
92
104
  async listProductReviews(pathParams, queryParams) {
@@ -100,8 +112,8 @@ export class CatalogClient extends StorefrontAPIClient {
100
112
  /**
101
113
  * Create a review for a specific product
102
114
  *
103
- * @param productId - The ID of the product
104
- * @param reviewData - The review data
115
+ * @param pathParams - The path parameters (product ID)
116
+ * @param formData - The review data
105
117
  * @returns Promise that resolves when the review is created
106
118
  */
107
119
  async createProductReview(pathParams, formData) {
@@ -125,7 +137,7 @@ export class CatalogClient extends StorefrontAPIClient {
125
137
  }
126
138
  }
127
139
  return fd;
128
- }
140
+ },
129
141
  }));
130
142
  }
131
143
  /**
@@ -140,41 +152,50 @@ export class CatalogClient extends StorefrontAPIClient {
140
152
  }));
141
153
  }
142
154
  /**
143
- * Retrieve cross-sell recommendations for a specific product
155
+ * List cross-sell products
144
156
  *
145
157
  * @param options - Optional query parameters
146
- * @returns Promise with cross-sell recommendations with pagination
158
+ * @param headers - Optional header parameters (customer_group_id, etc.)
159
+ * @returns Promise with cross-sell products
147
160
  */
148
- async listCrossSellProducts(options) {
161
+ async listCrossSellProducts(options, headers) {
162
+ const mergedHeaders = this.mergeHeaders(headers);
149
163
  return this.executeRequest(() => this.client.GET("/catalog/products/cross-sell", {
150
164
  params: {
151
165
  query: options,
166
+ header: mergedHeaders,
152
167
  },
153
168
  }));
154
169
  }
155
170
  /**
156
- * Retrieve up-sell recommendations for a specific product
171
+ * List up-sell products
157
172
  *
158
173
  * @param options - Optional query parameters
159
- * @returns Promise with up-sell recommendations with pagination
174
+ * @param headers - Optional header parameters (customer_group_id, etc.)
175
+ * @returns Promise with up-sell products
160
176
  */
161
- async listUpSellProducts(options) {
177
+ async listUpSellProducts(options, headers) {
178
+ const mergedHeaders = this.mergeHeaders(headers);
162
179
  return this.executeRequest(() => this.client.GET("/catalog/products/up-sell", {
163
180
  params: {
164
181
  query: options,
182
+ header: mergedHeaders,
165
183
  },
166
184
  }));
167
185
  }
168
186
  /**
169
- * Retrieve related products for a specific product
187
+ * List similar products
170
188
  *
171
189
  * @param options - Optional query parameters
172
- * @returns Promise with related products with pagination
190
+ * @param headers - Optional header parameters (customer_group_id, etc.)
191
+ * @returns Promise with similar products
173
192
  */
174
- async listSimilarProducts(options) {
193
+ async listSimilarProducts(options, headers) {
194
+ const mergedHeaders = this.mergeHeaders(headers);
175
195
  return this.executeRequest(() => this.client.GET("/catalog/products/similar", {
176
196
  params: {
177
197
  query: options,
198
+ header: mergedHeaders,
178
199
  },
179
200
  }));
180
201
  }
@@ -102,4 +102,12 @@ export declare class StorefrontAPIClient {
102
102
  * Initialize tokens in storage (private helper method)
103
103
  */
104
104
  private initializeTokens;
105
+ /**
106
+ * Merge default headers with method-level headers
107
+ * Method-level headers take precedence over default headers
108
+ *
109
+ * @param methodHeaders - Headers passed to the specific method call
110
+ * @returns Merged headers object
111
+ */
112
+ protected mergeHeaders<T extends Record<string, any> = Record<string, any>>(methodHeaders?: T): T;
105
113
  }
@@ -1,6 +1,7 @@
1
1
  import createClient from "openapi-fetch";
2
2
  import { createDefaultAuthMiddleware } from "./middleware";
3
3
  import { getPathnameFromUrl, isAnonymousAuthEndpoint } from "./auth-utils";
4
+ import { createDebugMiddleware } from "./logger-utils";
4
5
  /**
5
6
  * Available API environments
6
7
  */
@@ -81,6 +82,11 @@ export class StorefrontAPIClient {
81
82
  if (this.config.timeout) {
82
83
  this.setupTimeoutMiddleware();
83
84
  }
85
+ // Set up debug middleware if enabled
86
+ if (this.config.debug) {
87
+ const debugMiddleware = createDebugMiddleware(this.config.logger);
88
+ this.client.use(debugMiddleware);
89
+ }
84
90
  }
85
91
  /**
86
92
  * Set up timeout middleware
@@ -215,21 +221,39 @@ export class StorefrontAPIClient {
215
221
  */
216
222
  async executeRequest(apiCall) {
217
223
  try {
218
- const { data, error } = await apiCall();
224
+ const { data, error, response } = await apiCall();
225
+ // Debug logging is now handled by middleware
219
226
  // openapi-fetch returns error for 4xx/5xx, data for 2xx
220
227
  if (error) {
221
- return { data: null, error };
228
+ return {
229
+ data: null,
230
+ error,
231
+ response,
232
+ };
222
233
  }
223
234
  // If response has content, return it directly
224
235
  if (data && data.content !== undefined) {
225
- return { data: data.content, error: null };
236
+ return {
237
+ data: data.content,
238
+ error: null,
239
+ response,
240
+ };
226
241
  }
227
242
  // If no content, return the response object (with message, success, etc.)
228
- return { data: data, error: null };
243
+ return {
244
+ data: data,
245
+ error: null,
246
+ response,
247
+ };
229
248
  }
230
249
  catch (err) {
231
250
  // This handles network errors or other unexpected errors
232
- return {
251
+ // Network errors don't have Response objects, so we create a mock response
252
+ const mockResponse = new Response(null, {
253
+ status: 0,
254
+ statusText: "Network Error",
255
+ });
256
+ const errorResult = {
233
257
  data: null,
234
258
  error: {
235
259
  success: false,
@@ -237,7 +261,10 @@ export class StorefrontAPIClient {
237
261
  message: "Network error occurred",
238
262
  error: err,
239
263
  },
264
+ response: mockResponse,
240
265
  };
266
+ // Network errors are logged by middleware if debug is enabled
267
+ return errorResult;
241
268
  }
242
269
  }
243
270
  /**
@@ -256,4 +283,37 @@ export class StorefrontAPIClient {
256
283
  console.warn("Failed to initialize tokens in storage:", error);
257
284
  }
258
285
  }
286
+ /**
287
+ * Merge default headers with method-level headers
288
+ * Method-level headers take precedence over default headers
289
+ *
290
+ * @param methodHeaders - Headers passed to the specific method call
291
+ * @returns Merged headers object
292
+ */
293
+ mergeHeaders(methodHeaders) {
294
+ if (!this.config.defaultHeaders && !methodHeaders) {
295
+ return {};
296
+ }
297
+ // Start with default headers, but only include supported ones
298
+ const merged = {};
299
+ // Add default headers if they exist
300
+ if (this.config.defaultHeaders) {
301
+ if (this.config.defaultHeaders.customer_group_id !== undefined) {
302
+ merged.customer_group_id =
303
+ this.config.defaultHeaders.customer_group_id;
304
+ }
305
+ // Future: Add other supported headers here as they become available
306
+ }
307
+ if (methodHeaders) {
308
+ // Method headers override default headers
309
+ Object.assign(merged, methodHeaders);
310
+ }
311
+ // Remove undefined values
312
+ Object.keys(merged).forEach((key) => {
313
+ if (merged[key] === undefined) {
314
+ delete merged[key];
315
+ }
316
+ });
317
+ return merged;
318
+ }
259
319
  }