@commercengine/storefront-sdk 0.1.2 → 0.2.0

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
@@ -53,12 +53,32 @@ export declare class StorefrontSDK {
53
53
  * Client for authentication-related endpoints
54
54
  */
55
55
  readonly auth: AuthClient;
56
+ /**
57
+ * The client storage instance used by this SDK
58
+ */
59
+ private readonly clientStorage;
56
60
  /**
57
61
  * Create a new StorefrontSDK instance
58
62
  *
59
63
  * @param options - Configuration options for the SDK
60
64
  */
61
65
  constructor(options: StorefrontSDKOptions);
66
+ /**
67
+ * Get the current cart ID if one is stored
68
+ *
69
+ * @returns The current cart ID or null if none exists
70
+ */
71
+ getCartId(): string | null;
72
+ /**
73
+ * Set a specific cart ID to use
74
+ *
75
+ * @param cartId - The cart ID to use
76
+ */
77
+ setCartId(cartId: string): void;
78
+ /**
79
+ * Clear the current cart ID
80
+ */
81
+ clearCartId(): void;
62
82
  /**
63
83
  * Set the authentication token for all clients
64
84
  *
package/dist/index.js CHANGED
@@ -20,6 +20,8 @@ class StorefrontSDK {
20
20
  * @param options - Configuration options for the SDK
21
21
  */
22
22
  constructor(options) {
23
+ // Set up client storage
24
+ this.clientStorage = options.tokenStorage || new auth_1.MemoryTokenStorage();
23
25
  // Convert options to internal config format
24
26
  const config = {
25
27
  storeId: options.storeId,
@@ -31,7 +33,31 @@ class StorefrontSDK {
31
33
  };
32
34
  this.catalog = new catalog_1.CatalogClient(config);
33
35
  this.cart = new cart_1.CartClient(config);
34
- this.auth = new auth_1.AuthClient(config, options.tokenStorage);
36
+ this.auth = new auth_1.AuthClient(config, this.clientStorage);
37
+ // Set client storage for cart management
38
+ this.cart.setClientStorage(this.clientStorage);
39
+ }
40
+ /**
41
+ * Get the current cart ID if one is stored
42
+ *
43
+ * @returns The current cart ID or null if none exists
44
+ */
45
+ getCartId() {
46
+ return this.cart.getCartId();
47
+ }
48
+ /**
49
+ * Set a specific cart ID to use
50
+ *
51
+ * @param cartId - The cart ID to use
52
+ */
53
+ setCartId(cartId) {
54
+ this.cart.setCartId(cartId);
55
+ }
56
+ /**
57
+ * Clear the current cart ID
58
+ */
59
+ clearCartId() {
60
+ this.cart.clearCartId();
35
61
  }
36
62
  /**
37
63
  * Set the authentication token for all clients
@@ -148,51 +148,65 @@ export declare class AuthClient extends StorefrontAPIClient {
148
148
  executeWithTokenRefresh<T>(requestFn: () => Promise<T>): Promise<T>;
149
149
  }
150
150
  /**
151
- * Interface for token storage implementations
151
+ * Interface for client storage implementations (tokens and cart ID)
152
152
  */
153
- export interface TokenStorage {
153
+ export interface ClientStorage {
154
154
  getAccessToken(): string | null;
155
155
  setAccessToken(token: string): void;
156
156
  getRefreshToken(): string | null;
157
157
  setRefreshToken(token: string): void;
158
158
  clearTokens(): void;
159
+ getCartId(): string | null;
160
+ setCartId(cartId: string): void;
161
+ clearCartId(): void;
159
162
  }
163
+ export type TokenStorage = ClientStorage;
160
164
  /**
161
- * Default in-memory implementation of token storage
165
+ * Default in-memory implementation of client storage
162
166
  */
163
- export declare class MemoryTokenStorage implements TokenStorage {
167
+ export declare class MemoryTokenStorage implements ClientStorage {
164
168
  private accessToken;
165
169
  private refreshToken;
170
+ private cartId;
166
171
  getAccessToken(): string | null;
167
172
  setAccessToken(token: string): void;
168
173
  getRefreshToken(): string | null;
169
174
  setRefreshToken(token: string): void;
170
175
  clearTokens(): void;
176
+ getCartId(): string | null;
177
+ setCartId(cartId: string): void;
178
+ clearCartId(): void;
171
179
  }
172
180
  /**
173
181
  * Browser storage implementation using localStorage
174
182
  */
175
- export declare class BrowserTokenStorage implements TokenStorage {
183
+ export declare class BrowserTokenStorage implements ClientStorage {
176
184
  private accessTokenKey;
177
185
  private refreshTokenKey;
186
+ private cartIdKey;
178
187
  constructor(prefix?: string);
179
188
  getAccessToken(): string | null;
180
189
  setAccessToken(token: string): void;
181
190
  getRefreshToken(): string | null;
182
191
  setRefreshToken(token: string): void;
183
192
  clearTokens(): void;
193
+ getCartId(): string | null;
194
+ setCartId(cartId: string): void;
195
+ clearCartId(): void;
184
196
  }
185
197
  /**
186
198
  * Cookie-based token storage for browser or server environments
187
199
  */
188
- export declare class CookieTokenStorage implements TokenStorage {
200
+ export declare class CookieTokenStorage implements ClientStorage {
189
201
  private accessTokenKey;
190
202
  private refreshTokenKey;
203
+ private cartIdKey;
191
204
  private cookieOptions;
192
205
  constructor(prefix?: string, cookieOptions?: {
193
206
  path: string;
194
207
  secure: boolean;
195
208
  sameSite: string;
209
+ httpOnly: boolean;
196
210
  maxAge: number;
197
211
  });
198
212
  /**
@@ -219,14 +233,29 @@ export declare class CookieTokenStorage implements TokenStorage {
219
233
  * Clear all tokens from cookies
220
234
  */
221
235
  clearTokens(): void;
236
+ /**
237
+ * Get cart ID from cookies
238
+ * Works in both browser and Next.js server components
239
+ */
240
+ getCartId(): string | null;
241
+ /**
242
+ * Set cart ID in cookies
243
+ * Works in browser environment
244
+ */
245
+ setCartId(cartId: string): void;
246
+ /**
247
+ * Clear cart ID from cookies
248
+ */
249
+ clearCartId(): void;
222
250
  }
223
251
  /**
224
252
  * Next.js specific cookie storage implementation
225
253
  * Works with the Next.js cookies API
226
254
  */
227
- export declare class NextCookieTokenStorage implements TokenStorage {
255
+ export declare class NextCookieTokenStorage implements ClientStorage {
228
256
  private accessTokenKey;
229
257
  private refreshTokenKey;
258
+ private cartIdKey;
230
259
  private cookieStore;
231
260
  constructor(cookieStore: any, prefix?: string);
232
261
  /**
@@ -249,6 +278,18 @@ export declare class NextCookieTokenStorage implements TokenStorage {
249
278
  * Clear all tokens from Next.js cookies
250
279
  */
251
280
  clearTokens(): void;
281
+ /**
282
+ * Get cart ID from Next.js cookies
283
+ */
284
+ getCartId(): string | null;
285
+ /**
286
+ * Set cart ID in Next.js cookies
287
+ */
288
+ setCartId(cartId: string): void;
289
+ /**
290
+ * Clear cart ID from Next.js cookies
291
+ */
292
+ clearCartId(): void;
252
293
  }
253
294
  /**
254
295
  * Helper to create a token storage instance based on environment
package/dist/lib/auth.js CHANGED
@@ -385,12 +385,13 @@ class AuthClient extends client_1.StorefrontAPIClient {
385
385
  }
386
386
  exports.AuthClient = AuthClient;
387
387
  /**
388
- * Default in-memory implementation of token storage
388
+ * Default in-memory implementation of client storage
389
389
  */
390
390
  class MemoryTokenStorage {
391
391
  constructor() {
392
392
  this.accessToken = null;
393
393
  this.refreshToken = null;
394
+ this.cartId = null;
394
395
  }
395
396
  getAccessToken() {
396
397
  return this.accessToken;
@@ -408,6 +409,15 @@ class MemoryTokenStorage {
408
409
  this.accessToken = null;
409
410
  this.refreshToken = null;
410
411
  }
412
+ getCartId() {
413
+ return this.cartId;
414
+ }
415
+ setCartId(cartId) {
416
+ this.cartId = cartId;
417
+ }
418
+ clearCartId() {
419
+ this.cartId = null;
420
+ }
411
421
  }
412
422
  exports.MemoryTokenStorage = MemoryTokenStorage;
413
423
  /**
@@ -417,6 +427,7 @@ class BrowserTokenStorage {
417
427
  constructor(prefix = "storefront_") {
418
428
  this.accessTokenKey = `${prefix}access_token`;
419
429
  this.refreshTokenKey = `${prefix}refresh_token`;
430
+ this.cartIdKey = `${prefix}cart_id`;
420
431
  }
421
432
  getAccessToken() {
422
433
  if (typeof localStorage === "undefined")
@@ -444,6 +455,21 @@ class BrowserTokenStorage {
444
455
  localStorage.removeItem(this.accessTokenKey);
445
456
  localStorage.removeItem(this.refreshTokenKey);
446
457
  }
458
+ getCartId() {
459
+ if (typeof localStorage === "undefined")
460
+ return null;
461
+ return localStorage.getItem(this.cartIdKey);
462
+ }
463
+ setCartId(cartId) {
464
+ if (typeof localStorage === "undefined")
465
+ return;
466
+ localStorage.setItem(this.cartIdKey, cartId);
467
+ }
468
+ clearCartId() {
469
+ if (typeof localStorage === "undefined")
470
+ return;
471
+ localStorage.removeItem(this.cartIdKey);
472
+ }
447
473
  }
448
474
  exports.BrowserTokenStorage = BrowserTokenStorage;
449
475
  /**
@@ -453,11 +479,13 @@ class CookieTokenStorage {
453
479
  constructor(prefix = "storefront_", cookieOptions = {
454
480
  path: "/",
455
481
  secure: true,
456
- sameSite: "strict",
482
+ sameSite: "lax",
483
+ httpOnly: true,
457
484
  maxAge: 60 * 60 * 24 * 30, // 30 days
458
485
  }) {
459
486
  this.accessTokenKey = `${prefix}access_token`;
460
487
  this.refreshTokenKey = `${prefix}refresh_token`;
488
+ this.cartIdKey = `${prefix}cart_id`;
461
489
  this.cookieOptions = cookieOptions;
462
490
  }
463
491
  /**
@@ -519,6 +547,39 @@ class CookieTokenStorage {
519
547
  }
520
548
  // Next.js - would need to be implemented by user
521
549
  }
550
+ /**
551
+ * Get cart ID from cookies
552
+ * Works in both browser and Next.js server components
553
+ */
554
+ getCartId() {
555
+ // Browser environment
556
+ if (typeof document !== "undefined") {
557
+ return getCookieValue(this.cartIdKey);
558
+ }
559
+ // Next.js server component - would need to be implemented by user
560
+ return null;
561
+ }
562
+ /**
563
+ * Set cart ID in cookies
564
+ * Works in browser environment
565
+ */
566
+ setCartId(cartId) {
567
+ // Browser environment
568
+ if (typeof document !== "undefined") {
569
+ setCookie(this.cartIdKey, cartId, this.cookieOptions);
570
+ }
571
+ // Next.js - would need to be implemented by user
572
+ }
573
+ /**
574
+ * Clear cart ID from cookies
575
+ */
576
+ clearCartId() {
577
+ // Browser environment
578
+ if (typeof document !== "undefined") {
579
+ deleteCookie(this.cartIdKey);
580
+ }
581
+ // Next.js - would need to be implemented by user
582
+ }
522
583
  }
523
584
  exports.CookieTokenStorage = CookieTokenStorage;
524
585
  /**
@@ -574,6 +635,7 @@ class NextCookieTokenStorage {
574
635
  constructor(cookieStore, prefix = "storefront_") {
575
636
  this.accessTokenKey = `${prefix}access_token`;
576
637
  this.refreshTokenKey = `${prefix}refresh_token`;
638
+ this.cartIdKey = `${prefix}cart_id`;
577
639
  this.cookieStore = cookieStore;
578
640
  }
579
641
  /**
@@ -651,6 +713,49 @@ class NextCookieTokenStorage {
651
713
  // Silently fail
652
714
  }
653
715
  }
716
+ /**
717
+ * Get cart ID from Next.js cookies
718
+ */
719
+ getCartId() {
720
+ try {
721
+ // Assuming cookieStore.get method exists
722
+ const cookie = this.cookieStore.get(this.cartIdKey);
723
+ return cookie ? cookie.value : null;
724
+ }
725
+ catch (error) {
726
+ return null;
727
+ }
728
+ }
729
+ /**
730
+ * Set cart ID in Next.js cookies
731
+ */
732
+ setCartId(cartId) {
733
+ try {
734
+ // Assuming cookieStore.set method exists
735
+ this.cookieStore.set(this.cartIdKey, cartId, {
736
+ path: "/",
737
+ secure: process.env.NODE_ENV === "production",
738
+ httpOnly: true,
739
+ sameSite: "strict",
740
+ maxAge: 60 * 60 * 24 * 30, // 30 days
741
+ });
742
+ }
743
+ catch (error) {
744
+ // Silently fail
745
+ }
746
+ }
747
+ /**
748
+ * Clear cart ID from Next.js cookies
749
+ */
750
+ clearCartId() {
751
+ try {
752
+ // Assuming cookieStore.delete method exists
753
+ this.cookieStore.delete(this.cartIdKey);
754
+ }
755
+ catch (error) {
756
+ // Silently fail
757
+ }
758
+ }
654
759
  }
655
760
  exports.NextCookieTokenStorage = NextCookieTokenStorage;
656
761
  /**
@@ -1,9 +1,33 @@
1
1
  import { StorefrontAPIClient } from "./client";
2
2
  import type { components } from "../types/storefront";
3
+ import { ClientStorage } from "./auth";
3
4
  /**
4
5
  * Client for interacting with cart endpoints
5
6
  */
6
7
  export declare class CartClient extends StorefrontAPIClient {
8
+ private clientStorage;
9
+ /**
10
+ * Set the client storage implementation to use for cart ID management
11
+ *
12
+ * @param storage - The client storage instance
13
+ */
14
+ setClientStorage(storage: ClientStorage): void;
15
+ /**
16
+ * Get the stored cart ID
17
+ *
18
+ * @returns The cart ID or null if not found
19
+ */
20
+ getCartId(): string | null;
21
+ /**
22
+ * Set the cart ID in storage
23
+ *
24
+ * @param cartId - The cart ID to store
25
+ */
26
+ setCartId(cartId: string): void;
27
+ /**
28
+ * Clear the stored cart ID
29
+ */
30
+ clearCartId(): void;
7
31
  /**
8
32
  * Create a new cart
9
33
  *
@@ -16,21 +40,45 @@ export declare class CartClient extends StorefrontAPIClient {
16
40
  cart: components["schemas"]["Cart"];
17
41
  }>;
18
42
  /**
19
- * Get cart details by ID
43
+ * Get cart details - either by ID or using the stored cart ID
20
44
  *
21
- * @param cartId - The ID of the cart
45
+ * @param cartId - Optional cart ID (if not provided, uses stored cart ID)
22
46
  * @returns Promise with cart details
47
+ * @throws Error if no cartId is provided and none is stored
23
48
  */
24
- getCart(cartId: string): Promise<{
49
+ getCart(cartId?: string): Promise<{
25
50
  cart: components["schemas"]["Cart"];
26
51
  }>;
27
52
  /**
28
- * Delete a cart by ID
53
+ * Delete a cart - either by ID or using the stored cart ID
29
54
  *
30
- * @param cartId - The ID of the cart
55
+ * @param cartId - Optional cart ID (if not provided, uses stored cart ID)
31
56
  * @returns Promise that resolves when the cart is deleted
57
+ * @throws Error if no cartId is provided and none is stored
32
58
  */
33
- deleteCart(cartId: string): Promise<void>;
59
+ deleteCart(cartId?: string): Promise<void>;
60
+ /**
61
+ * Add item to cart - either by specified ID or stored cart ID
62
+ * Will create a new cart if no cart ID is available
63
+ *
64
+ * @param item - The item to add to the cart
65
+ * @param cartId - Optional cart ID (if not provided, uses stored cart ID or creates a new cart)
66
+ * @returns Promise with updated or created cart
67
+ */
68
+ addItem(item: components["schemas"]["UpdateCartItem"], cartId?: string): Promise<{
69
+ cart: components["schemas"]["Cart"];
70
+ }>;
71
+ /**
72
+ * Update cart items (add, update quantity, remove)
73
+ *
74
+ * @param cartId - The ID of the cart or undefined to use stored cart ID
75
+ * @param item - The item data
76
+ * @returns Promise with updated cart
77
+ * @throws Error if no cartId is provided and none is stored
78
+ */
79
+ updateCartItem(cartId: string | undefined, item: components["schemas"]["UpdateCartItem"]): Promise<{
80
+ cart: components["schemas"]["Cart"];
81
+ }>;
34
82
  /**
35
83
  * Get cart details by user ID
36
84
  *
@@ -47,16 +95,6 @@ export declare class CartClient extends StorefrontAPIClient {
47
95
  * @returns Promise that resolves when the cart is deleted
48
96
  */
49
97
  deleteUserCart(userId: string): Promise<void>;
50
- /**
51
- * Update cart items (add, update quantity, remove)
52
- *
53
- * @param cartId - The ID of the cart
54
- * @param item - The item data
55
- * @returns Promise with updated cart
56
- */
57
- updateCartItem(cartId: string, item: components["schemas"]["UpdateCartItem"]): Promise<{
58
- cart: components["schemas"]["Cart"];
59
- }>;
60
98
  /**
61
99
  * Update cart addresses
62
100
  *
package/dist/lib/cart.js CHANGED
@@ -6,6 +6,40 @@ const client_1 = require("./client");
6
6
  * Client for interacting with cart endpoints
7
7
  */
8
8
  class CartClient extends client_1.StorefrontAPIClient {
9
+ constructor() {
10
+ super(...arguments);
11
+ this.clientStorage = null;
12
+ }
13
+ /**
14
+ * Set the client storage implementation to use for cart ID management
15
+ *
16
+ * @param storage - The client storage instance
17
+ */
18
+ setClientStorage(storage) {
19
+ this.clientStorage = storage;
20
+ }
21
+ /**
22
+ * Get the stored cart ID
23
+ *
24
+ * @returns The cart ID or null if not found
25
+ */
26
+ getCartId() {
27
+ return this.clientStorage?.getCartId() || null;
28
+ }
29
+ /**
30
+ * Set the cart ID in storage
31
+ *
32
+ * @param cartId - The cart ID to store
33
+ */
34
+ setCartId(cartId) {
35
+ this.clientStorage?.setCartId(cartId);
36
+ }
37
+ /**
38
+ * Clear the stored cart ID
39
+ */
40
+ clearCartId() {
41
+ this.clientStorage?.clearCartId();
42
+ }
9
43
  /**
10
44
  * Create a new cart
11
45
  *
@@ -20,20 +54,30 @@ class CartClient extends client_1.StorefrontAPIClient {
20
54
  if (error) {
21
55
  this.handleError(error);
22
56
  }
57
+ // Store the cart ID
58
+ if (data?.content?.cart?.id) {
59
+ this.setCartId(data.content.cart.id);
60
+ }
23
61
  return data?.content;
24
62
  });
25
63
  }
26
64
  /**
27
- * Get cart details by ID
65
+ * Get cart details - either by ID or using the stored cart ID
28
66
  *
29
- * @param cartId - The ID of the cart
67
+ * @param cartId - Optional cart ID (if not provided, uses stored cart ID)
30
68
  * @returns Promise with cart details
69
+ * @throws Error if no cartId is provided and none is stored
31
70
  */
32
71
  async getCart(cartId) {
72
+ // Use provided cartId or stored cartId
73
+ const id = cartId || this.getCartId();
74
+ if (!id) {
75
+ throw new Error("Cart ID is required but not provided");
76
+ }
33
77
  return this.executeRequest(async () => {
34
78
  const { data, error } = await this.client.GET("/carts/{id}", {
35
79
  params: {
36
- path: { id: cartId },
80
+ path: { id },
37
81
  },
38
82
  });
39
83
  if (error) {
@@ -43,21 +87,74 @@ class CartClient extends client_1.StorefrontAPIClient {
43
87
  });
44
88
  }
45
89
  /**
46
- * Delete a cart by ID
90
+ * Delete a cart - either by ID or using the stored cart ID
47
91
  *
48
- * @param cartId - The ID of the cart
92
+ * @param cartId - Optional cart ID (if not provided, uses stored cart ID)
49
93
  * @returns Promise that resolves when the cart is deleted
94
+ * @throws Error if no cartId is provided and none is stored
50
95
  */
51
96
  async deleteCart(cartId) {
97
+ // Use provided cartId or stored cartId
98
+ const id = cartId || this.getCartId();
99
+ if (!id) {
100
+ throw new Error("Cart ID is required but not provided");
101
+ }
52
102
  const { error } = await this.client.DELETE("/carts/{id}", {
53
103
  params: {
54
- path: { id: cartId },
104
+ path: { id },
55
105
  },
56
106
  body: undefined,
57
107
  });
58
108
  if (error) {
59
109
  this.handleError(error);
60
110
  }
111
+ // Clear the stored cart ID if we deleted the current cart
112
+ if (cartId === undefined || cartId === this.getCartId()) {
113
+ this.clearCartId();
114
+ }
115
+ }
116
+ /**
117
+ * Add item to cart - either by specified ID or stored cart ID
118
+ * Will create a new cart if no cart ID is available
119
+ *
120
+ * @param item - The item to add to the cart
121
+ * @param cartId - Optional cart ID (if not provided, uses stored cart ID or creates a new cart)
122
+ * @returns Promise with updated or created cart
123
+ */
124
+ async addItem(item, cartId) {
125
+ // Check if we have a cart ID (either provided or stored)
126
+ const id = cartId || this.getCartId();
127
+ // If we don't have a cart ID, create a new cart
128
+ if (!id) {
129
+ return this.createCart({ items: [item] });
130
+ }
131
+ // Otherwise update existing cart
132
+ return this.updateCartItem(id, item);
133
+ }
134
+ /**
135
+ * Update cart items (add, update quantity, remove)
136
+ *
137
+ * @param cartId - The ID of the cart or undefined to use stored cart ID
138
+ * @param item - The item data
139
+ * @returns Promise with updated cart
140
+ * @throws Error if no cartId is provided and none is stored
141
+ */
142
+ async updateCartItem(cartId, item) {
143
+ // Use provided cartId or stored cartId
144
+ const id = cartId || this.getCartId();
145
+ if (!id) {
146
+ throw new Error("Cart ID is required but not provided");
147
+ }
148
+ const { data, error } = await this.client.POST("/carts/{id}/items", {
149
+ params: {
150
+ path: { id },
151
+ },
152
+ body: item,
153
+ });
154
+ if (error) {
155
+ this.handleError(error);
156
+ }
157
+ return data?.content;
61
158
  }
62
159
  /**
63
160
  * Get cart details by user ID
@@ -93,25 +190,6 @@ class CartClient extends client_1.StorefrontAPIClient {
93
190
  this.handleError(error);
94
191
  }
95
192
  }
96
- /**
97
- * Update cart items (add, update quantity, remove)
98
- *
99
- * @param cartId - The ID of the cart
100
- * @param item - The item data
101
- * @returns Promise with updated cart
102
- */
103
- async updateCartItem(cartId, item) {
104
- const { data, error } = await this.client.POST("/carts/{id}/items", {
105
- params: {
106
- path: { id: cartId },
107
- },
108
- body: item,
109
- });
110
- if (error) {
111
- this.handleError(error);
112
- }
113
- return data?.content;
114
- }
115
193
  /**
116
194
  * Update cart addresses
117
195
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercengine/storefront-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "TypeScript SDK for the Storefront API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",