@commercengine/storefront-sdk 0.2.1 → 0.3.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.
@@ -15,19 +15,19 @@ export declare class CartClient extends StorefrontAPIClient {
15
15
  /**
16
16
  * Get the stored cart ID
17
17
  *
18
- * @returns The cart ID or null if not found
18
+ * @returns Promise that resolves to the cart ID or null if not found
19
19
  */
20
- getCartId(): string | null;
20
+ getCartId(): Promise<string | null>;
21
21
  /**
22
22
  * Set the cart ID in storage
23
23
  *
24
24
  * @param cartId - The cart ID to store
25
25
  */
26
- setCartId(cartId: string): void;
26
+ setCartId(cartId: string): Promise<void>;
27
27
  /**
28
28
  * Clear the stored cart ID
29
29
  */
30
- clearCartId(): void;
30
+ clearCartId(): Promise<void>;
31
31
  /**
32
32
  * Create a new cart
33
33
  *
package/dist/lib/cart.js CHANGED
@@ -21,24 +21,24 @@ class CartClient extends client_1.StorefrontAPIClient {
21
21
  /**
22
22
  * Get the stored cart ID
23
23
  *
24
- * @returns The cart ID or null if not found
24
+ * @returns Promise that resolves to the cart ID or null if not found
25
25
  */
26
- getCartId() {
27
- return this.clientStorage?.getCartId() || null;
26
+ async getCartId() {
27
+ return this.clientStorage?.getCartId() ?? null;
28
28
  }
29
29
  /**
30
30
  * Set the cart ID in storage
31
31
  *
32
32
  * @param cartId - The cart ID to store
33
33
  */
34
- setCartId(cartId) {
35
- this.clientStorage?.setCartId(cartId);
34
+ async setCartId(cartId) {
35
+ await this.clientStorage?.setCartId(cartId);
36
36
  }
37
37
  /**
38
38
  * Clear the stored cart ID
39
39
  */
40
- clearCartId() {
41
- this.clientStorage?.clearCartId();
40
+ async clearCartId() {
41
+ await this.clientStorage?.clearCartId();
42
42
  }
43
43
  /**
44
44
  * Create a new cart
@@ -56,7 +56,7 @@ class CartClient extends client_1.StorefrontAPIClient {
56
56
  }
57
57
  // Store the cart ID
58
58
  if (data?.content?.cart?.id) {
59
- this.setCartId(data.content.cart.id);
59
+ await this.setCartId(data.content.cart.id);
60
60
  }
61
61
  return data?.content;
62
62
  });
@@ -70,7 +70,7 @@ class CartClient extends client_1.StorefrontAPIClient {
70
70
  */
71
71
  async getCart(cartId) {
72
72
  // Use provided cartId or stored cartId
73
- const id = cartId || this.getCartId();
73
+ const id = cartId || (await this.getCartId());
74
74
  if (!id) {
75
75
  throw new Error("Cart ID is required but not provided");
76
76
  }
@@ -95,7 +95,7 @@ class CartClient extends client_1.StorefrontAPIClient {
95
95
  */
96
96
  async deleteCart(cartId) {
97
97
  // Use provided cartId or stored cartId
98
- const id = cartId || this.getCartId();
98
+ const id = cartId || (await this.getCartId());
99
99
  if (!id) {
100
100
  throw new Error("Cart ID is required but not provided");
101
101
  }
@@ -110,8 +110,9 @@ class CartClient extends client_1.StorefrontAPIClient {
110
110
  this.handleError(error);
111
111
  }
112
112
  // Clear the stored cart ID if we deleted the current cart
113
- if (cartId === undefined || cartId === this.getCartId()) {
114
- this.clearCartId();
113
+ const storedCartId = await this.getCartId();
114
+ if (cartId === undefined || cartId === storedCartId) {
115
+ await this.clearCartId();
115
116
  }
116
117
  });
117
118
  }
@@ -125,7 +126,7 @@ class CartClient extends client_1.StorefrontAPIClient {
125
126
  */
126
127
  async addItem(item, cartId) {
127
128
  // Check if we have a cart ID (either provided or stored)
128
- const id = cartId || this.getCartId();
129
+ const id = cartId || (await this.getCartId());
129
130
  // If we don't have a cart ID, create a new cart
130
131
  if (!id) {
131
132
  return this.createCart({ items: [item] });
@@ -143,7 +144,7 @@ class CartClient extends client_1.StorefrontAPIClient {
143
144
  */
144
145
  async updateCartItem(cartId, item) {
145
146
  // Use provided cartId or stored cartId
146
- const id = cartId || this.getCartId();
147
+ const id = cartId || (await this.getCartId());
147
148
  if (!id) {
148
149
  throw new Error("Cart ID is required but not provided");
149
150
  }
@@ -176,6 +177,10 @@ class CartClient extends client_1.StorefrontAPIClient {
176
177
  if (error) {
177
178
  this.handleError(error);
178
179
  }
180
+ // Store the cart ID if present
181
+ if (data?.content?.cart?.id) {
182
+ await this.setCartId(data.content.cart.id);
183
+ }
179
184
  return data?.content;
180
185
  });
181
186
  }
@@ -196,6 +201,8 @@ class CartClient extends client_1.StorefrontAPIClient {
196
201
  if (error) {
197
202
  this.handleError(error);
198
203
  }
204
+ // Clear the stored cart ID since we deleted the user's cart
205
+ await this.clearCartId();
199
206
  });
200
207
  }
201
208
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercengine/storefront-sdk",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "TypeScript SDK for the Storefront API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",