@commercengine/storefront-sdk 0.2.1 → 0.3.1

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/lib/cart.js CHANGED
@@ -1,45 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CartClient = void 0;
4
- const client_1 = require("./client");
1
+ import { StorefrontAPIClient } from "./client";
5
2
  /**
6
3
  * Client for interacting with cart endpoints
7
4
  */
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
- }
5
+ export class CartClient extends StorefrontAPIClient {
43
6
  /**
44
7
  * Create a new cart
45
8
  *
@@ -47,118 +10,50 @@ class CartClient extends client_1.StorefrontAPIClient {
47
10
  * @returns Promise with the created cart
48
11
  */
49
12
  async createCart(payload) {
50
- return this.executeRequest(async () => {
51
- const { data, error } = await this.client.POST("/carts", {
52
- body: payload,
53
- });
54
- if (error) {
55
- this.handleError(error);
56
- }
57
- // Store the cart ID
58
- if (data?.content?.cart?.id) {
59
- this.setCartId(data.content.cart.id);
60
- }
61
- return data?.content;
62
- });
13
+ return this.executeRequest(() => this.client.POST("/carts", {
14
+ body: payload,
15
+ }));
63
16
  }
64
17
  /**
65
18
  * Get cart details - either by ID or using the stored cart ID
66
19
  *
67
- * @param cartId - Optional cart ID (if not provided, uses stored cart ID)
20
+ * @param cartId - The ID of the cart
68
21
  * @returns Promise with cart details
69
- * @throws Error if no cartId is provided and none is stored
70
22
  */
71
23
  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
- }
77
- return this.executeRequest(async () => {
78
- const { data, error } = await this.client.GET("/carts/{id}", {
79
- params: {
80
- path: { id },
81
- },
82
- });
83
- if (error) {
84
- this.handleError(error);
85
- }
86
- return data?.content;
87
- });
24
+ return this.executeRequest(() => this.client.GET("/carts/{id}", {
25
+ params: {
26
+ path: cartId,
27
+ },
28
+ }));
88
29
  }
89
30
  /**
90
31
  * Delete a cart - either by ID or using the stored cart ID
91
32
  *
92
- * @param cartId - Optional cart ID (if not provided, uses stored cart ID)
33
+ * @param cartId - The ID of the cart
93
34
  * @returns Promise that resolves when the cart is deleted
94
- * @throws Error if no cartId is provided and none is stored
95
35
  */
96
36
  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
- }
102
- return this.executeRequest(async () => {
103
- const { error } = await this.client.DELETE("/carts/{id}", {
104
- params: {
105
- path: { id },
106
- },
107
- body: undefined,
108
- });
109
- if (error) {
110
- this.handleError(error);
111
- }
112
- // Clear the stored cart ID if we deleted the current cart
113
- if (cartId === undefined || cartId === this.getCartId()) {
114
- this.clearCartId();
115
- }
116
- });
117
- }
118
- /**
119
- * Add item to cart - either by specified ID or stored cart ID
120
- * Will create a new cart if no cart ID is available
121
- *
122
- * @param item - The item to add to the cart
123
- * @param cartId - Optional cart ID (if not provided, uses stored cart ID or creates a new cart)
124
- * @returns Promise with updated or created cart
125
- */
126
- async addItem(item, cartId) {
127
- // Check if we have a cart ID (either provided or stored)
128
- const id = cartId || this.getCartId();
129
- // If we don't have a cart ID, create a new cart
130
- if (!id) {
131
- return this.createCart({ items: [item] });
132
- }
133
- // Otherwise update existing cart
134
- return this.updateCartItem(id, item);
37
+ return this.executeRequest(() => this.client.DELETE("/carts/{id}", {
38
+ params: {
39
+ path: cartId,
40
+ },
41
+ }));
135
42
  }
136
43
  /**
137
44
  * Update cart items (add, update quantity, remove)
138
45
  *
139
- * @param cartId - The ID of the cart or undefined to use stored cart ID
140
- * @param item - The item data
46
+ * @param cartId - The cart id
47
+ * @param body - The body of the request
141
48
  * @returns Promise with updated cart
142
- * @throws Error if no cartId is provided and none is stored
143
49
  */
144
- async updateCartItem(cartId, item) {
145
- // Use provided cartId or stored cartId
146
- const id = cartId || this.getCartId();
147
- if (!id) {
148
- throw new Error("Cart ID is required but not provided");
149
- }
150
- return this.executeRequest(async () => {
151
- const { data, error } = await this.client.POST("/carts/{id}/items", {
152
- params: {
153
- path: { id },
154
- },
155
- body: item,
156
- });
157
- if (error) {
158
- this.handleError(error);
159
- }
160
- return data?.content;
161
- });
50
+ async addDeleteCartItem(cartId, body) {
51
+ return this.executeRequest(() => this.client.POST("/carts/{id}/items", {
52
+ params: {
53
+ path: cartId,
54
+ },
55
+ body: body,
56
+ }));
162
57
  }
163
58
  /**
164
59
  * Get cart details by user ID
@@ -167,17 +62,11 @@ class CartClient extends client_1.StorefrontAPIClient {
167
62
  * @returns Promise with cart details
168
63
  */
169
64
  async getUserCart(userId) {
170
- return this.executeRequest(async () => {
171
- const { data, error } = await this.client.GET("/carts/users/{user_id}", {
172
- params: {
173
- path: { user_id: userId },
174
- },
175
- });
176
- if (error) {
177
- this.handleError(error);
178
- }
179
- return data?.content;
180
- });
65
+ return this.executeRequest(() => this.client.GET("/carts/users/{user_id}", {
66
+ params: {
67
+ path: userId,
68
+ },
69
+ }));
181
70
  }
182
71
  /**
183
72
  * Delete a cart by user ID
@@ -186,17 +75,12 @@ class CartClient extends client_1.StorefrontAPIClient {
186
75
  * @returns Promise that resolves when the cart is deleted
187
76
  */
188
77
  async deleteUserCart(userId) {
189
- return this.executeRequest(async () => {
190
- const { error } = await this.client.DELETE("/carts/users/{user_id}", {
191
- params: {
192
- path: { user_id: userId },
193
- },
194
- body: undefined,
195
- });
196
- if (error) {
197
- this.handleError(error);
198
- }
199
- });
78
+ return this.executeRequest(() => this.client.DELETE("/carts/users/{user_id}", {
79
+ params: {
80
+ path: userId,
81
+ },
82
+ body: undefined,
83
+ }));
200
84
  }
201
85
  /**
202
86
  * Update cart addresses
@@ -206,18 +90,12 @@ class CartClient extends client_1.StorefrontAPIClient {
206
90
  * @returns Promise with updated cart
207
91
  */
208
92
  async updateCartAddress(cartId, addressData) {
209
- return this.executeRequest(async () => {
210
- const { data, error } = await this.client.POST("/carts/{id}/address", {
211
- params: {
212
- path: { id: cartId },
213
- },
214
- body: addressData,
215
- });
216
- if (error) {
217
- this.handleError(error);
218
- }
219
- return data?.content;
220
- });
93
+ return this.executeRequest(() => this.client.POST("/carts/{id}/address", {
94
+ params: {
95
+ path: cartId,
96
+ },
97
+ body: addressData,
98
+ }));
221
99
  }
222
100
  /**
223
101
  * Apply a coupon to the cart
@@ -227,18 +105,12 @@ class CartClient extends client_1.StorefrontAPIClient {
227
105
  * @returns Promise with updated cart
228
106
  */
229
107
  async applyCoupon(cartId, couponCode) {
230
- return this.executeRequest(async () => {
231
- const { data, error } = await this.client.POST("/carts/{id}/coupon", {
232
- params: {
233
- path: { id: cartId },
234
- },
235
- body: { coupon_code: couponCode },
236
- });
237
- if (error) {
238
- this.handleError(error);
239
- }
240
- return data?.content;
241
- });
108
+ return this.executeRequest(() => this.client.POST("/carts/{id}/coupon", {
109
+ params: {
110
+ path: cartId,
111
+ },
112
+ body: couponCode,
113
+ }));
242
114
  }
243
115
  /**
244
116
  * Remove a coupon from the cart
@@ -247,18 +119,12 @@ class CartClient extends client_1.StorefrontAPIClient {
247
119
  * @returns Promise with updated cart
248
120
  */
249
121
  async removeCoupon(cartId) {
250
- return this.executeRequest(async () => {
251
- const { data, error } = await this.client.DELETE("/carts/{id}/coupon", {
252
- params: {
253
- path: { id: cartId },
254
- },
255
- body: undefined,
256
- });
257
- if (error) {
258
- this.handleError(error);
259
- }
260
- return data?.content;
261
- });
122
+ return this.executeRequest(() => this.client.DELETE("/carts/{id}/coupon", {
123
+ params: {
124
+ path: cartId,
125
+ },
126
+ body: undefined,
127
+ }));
262
128
  }
263
129
  /**
264
130
  * Redeem loyalty points
@@ -268,18 +134,12 @@ class CartClient extends client_1.StorefrontAPIClient {
268
134
  * @returns Promise with updated cart
269
135
  */
270
136
  async redeemLoyaltyPoints(cartId, points) {
271
- return this.executeRequest(async () => {
272
- const { data, error } = await this.client.POST("/carts/{id}/loyalty-points", {
273
- params: {
274
- path: { id: cartId },
275
- },
276
- body: { loyalty_point_redeemed: points },
277
- });
278
- if (error) {
279
- this.handleError(error);
280
- }
281
- return data?.content;
282
- });
137
+ return this.executeRequest(() => this.client.POST("/carts/{id}/loyalty-points", {
138
+ params: {
139
+ path: cartId,
140
+ },
141
+ body: points,
142
+ }));
283
143
  }
284
144
  /**
285
145
  * Remove loyalty points
@@ -288,18 +148,127 @@ class CartClient extends client_1.StorefrontAPIClient {
288
148
  * @returns Promise with updated cart
289
149
  */
290
150
  async removeLoyaltyPoints(cartId) {
291
- return this.executeRequest(async () => {
292
- const { data, error } = await this.client.DELETE("/carts/{id}/loyalty-points", {
293
- params: {
294
- path: { id: cartId },
295
- },
296
- body: undefined,
297
- });
298
- if (error) {
299
- this.handleError(error);
300
- }
301
- return data?.content;
302
- });
151
+ return this.executeRequest(() => this.client.DELETE("/carts/{id}/loyalty-points", {
152
+ params: {
153
+ path: cartId,
154
+ },
155
+ body: undefined,
156
+ }));
157
+ }
158
+ /**
159
+ * Update shipping method
160
+ *
161
+ * @param cartId - The ID of the cart
162
+ * @param body - The body of the request
163
+ * @returns Promise with updated cart
164
+ */
165
+ async updateShippingMethod(cartId, body) {
166
+ return this.executeRequest(() => this.client.POST("/carts/{id}/shipping-method", {
167
+ params: {
168
+ path: cartId,
169
+ },
170
+ body: body,
171
+ }));
172
+ }
173
+ /**
174
+ * Use credit balance
175
+ *
176
+ * @param cartId - The ID of the cart
177
+ * @param body - The body of the request
178
+ * @returns Promise with updated cart
179
+ */
180
+ async redeemCreditBalance(cartId, body) {
181
+ return this.executeRequest(() => this.client.POST("/carts/{id}/credit-balance", {
182
+ params: {
183
+ path: cartId,
184
+ },
185
+ body: body,
186
+ }));
187
+ }
188
+ /**
189
+ * Remove credit balance
190
+ *
191
+ * @param cartId - The ID of the cart
192
+ * @returns Promise with updated cart
193
+ */
194
+ async removeCreditBalance(cartId) {
195
+ return this.executeRequest(() => this.client.DELETE("/carts/{id}/credit-balance", {
196
+ params: {
197
+ path: cartId,
198
+ },
199
+ body: undefined,
200
+ }));
201
+ }
202
+ /**
203
+ * Redeem gift card
204
+ *
205
+ * @param cartId - The ID of the cart
206
+ * @param body - The body of the request
207
+ * @returns Promise with updated cart
208
+ */
209
+ async redeemGiftCard(cartId, body) {
210
+ return this.executeRequest(() => this.client.POST("/carts/{id}/gift-card", {
211
+ params: {
212
+ path: cartId,
213
+ },
214
+ body: body,
215
+ }));
216
+ }
217
+ /**
218
+ * Remove gift card
219
+ *
220
+ * @param cartId - The ID of the cart
221
+ * @returns Promise with updated cart
222
+ */
223
+ async removeGiftCard(cartId) {
224
+ return this.executeRequest(() => this.client.DELETE("/carts/{id}/gift-card", {
225
+ params: {
226
+ path: cartId,
227
+ },
228
+ body: undefined,
229
+ }));
230
+ }
231
+ /**
232
+ * Get wishlist items
233
+ *
234
+ * @param userId - The ID of the user
235
+ * @returns Promise with wishlist items
236
+ */
237
+ async getWishlist(userId) {
238
+ return this.executeRequest(() => this.client.GET("/wishlist/{user_id}", {
239
+ params: {
240
+ path: userId,
241
+ },
242
+ }));
243
+ }
244
+ /**
245
+ * Add item to wishlist
246
+ *
247
+ * @param userId - The ID of the user
248
+ * @param itemId - The ID of the item
249
+ * @returns Promise with updated wishlist
250
+ */
251
+ async addToWishlist(userId, itemId) {
252
+ return this.executeRequest(() => this.client.POST("/wishlist/{user_id}", {
253
+ params: {
254
+ path: userId,
255
+ },
256
+ body: itemId,
257
+ }));
258
+ }
259
+ /**
260
+ * Remove item from wishlist
261
+ *
262
+ * @param userId - The ID of the user
263
+ * @param itemId - The ID of the item
264
+ * @returns Promise with updated wishlist
265
+ */
266
+ async removeFromWishlist(userId, body) {
267
+ return this.executeRequest(() => this.client.DELETE("/wishlist/{user_id}", {
268
+ params: {
269
+ path: userId,
270
+ },
271
+ body: body,
272
+ }));
303
273
  }
304
274
  }
305
- exports.CartClient = CartClient;
@@ -1,5 +1,5 @@
1
1
  import { StorefrontAPIClient } from "./client";
2
- import type { components } from "../types/storefront";
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";
3
3
  /**
4
4
  * Client for interacting with catalog endpoints
5
5
  */
@@ -10,16 +10,14 @@ export declare class CatalogClient extends StorefrontAPIClient {
10
10
  * @param options - Optional query parameters
11
11
  * @returns Promise with products and pagination info
12
12
  */
13
- listProducts(options?: {
14
- page?: number;
15
- limit?: number;
16
- sort_by?: string;
17
- category_id?: string[];
18
- customer_group_id?: string;
19
- }): Promise<{
20
- products: components["schemas"]["Product"][];
21
- pagination: components["schemas"]["Pagination"];
22
- }>;
13
+ listProducts(options?: ListProductsQuery): Promise<ApiResult<ListProductsContent>>;
14
+ /**
15
+ * List all skus
16
+ *
17
+ * @param options - Optional query parameters
18
+ * @returns Promise with skus and pagination info
19
+ */
20
+ listSkus(options?: ListSkusQuery): Promise<ApiResult<ListSkusContent>>;
23
21
  /**
24
22
  * Get details for a specific product
25
23
  *
@@ -27,11 +25,7 @@ export declare class CatalogClient extends StorefrontAPIClient {
27
25
  * @param options - Optional query parameters
28
26
  * @returns Promise with product details
29
27
  */
30
- getProductDetail(productId: string, options?: {
31
- customer_group_id?: string;
32
- }): Promise<{
33
- product: components["schemas"]["ProductDetail"];
34
- }>;
28
+ getProductDetail(pathParams: GetProductDetailPathParams, queryParams?: GetProductDetailQuery): Promise<ApiResult<GetProductDetailContent>>;
35
29
  /**
36
30
  * List variants for a specific product
37
31
  *
@@ -39,11 +33,7 @@ export declare class CatalogClient extends StorefrontAPIClient {
39
33
  * @param options - Optional query parameters
40
34
  * @returns Promise with variants
41
35
  */
42
- listProductVariants(productId: string, options?: {
43
- customer_group_id?: string;
44
- }): Promise<{
45
- variants: components["schemas"]["Variant"][];
46
- }>;
36
+ listProductVariants(pathParams: ListProductVariantsPathParams, queryParams?: ListProductVariantsQuery): Promise<ApiResult<ListProductVariantsContent>>;
47
37
  /**
48
38
  * Get details for a specific variant
49
39
  *
@@ -52,26 +42,14 @@ export declare class CatalogClient extends StorefrontAPIClient {
52
42
  * @param options - Optional query parameters
53
43
  * @returns Promise with variant details
54
44
  */
55
- getVariantDetail(productId: string, variantId: string, options?: {
56
- customer_group_id?: string;
57
- }): Promise<{
58
- variant: components["schemas"]["VariantDetail"];
59
- }>;
45
+ getVariantDetail(pathParams: GetVariantDetailPathParams, queryParams?: GetVariantDetailQuery): Promise<ApiResult<GetVariantDetailContent>>;
60
46
  /**
61
47
  * List all categories
62
48
  *
63
49
  * @param options - Optional query parameters
64
50
  * @returns Promise with categories and pagination info
65
51
  */
66
- listCategories(options?: {
67
- parent_category_id?: string;
68
- nested_level?: number;
69
- search?: string;
70
- sort_by?: string;
71
- }): Promise<{
72
- categories: components["schemas"]["ProductCategory"][];
73
- pagination: components["schemas"]["Pagination"];
74
- }>;
52
+ listCategories(options?: ListCategoriesQuery): Promise<ApiResult<ListCategoriesContent>>;
75
53
  /**
76
54
  * List reviews for a specific product
77
55
  *
@@ -79,17 +57,7 @@ export declare class CatalogClient extends StorefrontAPIClient {
79
57
  * @param options - Optional query parameters
80
58
  * @returns Promise with reviews and pagination info
81
59
  */
82
- listProductReviews(productId: string, options?: {
83
- page?: number;
84
- limit?: number;
85
- sort_by?: string;
86
- search?: string;
87
- review_tag?: string;
88
- }): Promise<{
89
- reviews: components["schemas"]["ProductReview"][];
90
- review_tags?: string[] | null;
91
- pagination?: components["schemas"]["Pagination"];
92
- }>;
60
+ listProductReviews(pathParams: ListProductReviewsPathParams, queryParams?: ListProductReviewsQuery): Promise<ApiResult<ListProductReviewsContent>>;
93
61
  /**
94
62
  * Create a review for a specific product
95
63
  *
@@ -97,34 +65,33 @@ export declare class CatalogClient extends StorefrontAPIClient {
97
65
  * @param reviewData - The review data
98
66
  * @returns Promise that resolves when the review is created
99
67
  */
100
- createProductReview(productId: string, reviewData: components["schemas"]["CreateReview"]): Promise<void>;
68
+ createProductReview(pathParams: CreateProductReviewPathParams, formData: CreateProductReviewFormData): Promise<ApiResult<CreateProductReviewResponse>>;
101
69
  /**
102
70
  * Search for products
103
71
  *
104
72
  * @param searchData - The search parameters
105
- * @returns Promise with search results
73
+ * @returns Promise with search results, facet distribution, facet stats, and pagination
74
+ */
75
+ searchProducts(searchData: SearchProductsBody): Promise<ApiResult<SearchProductsContent>>;
76
+ /**
77
+ * Retrieve cross-sell recommendations for a specific product
78
+ *
79
+ * @param options - Optional query parameters
80
+ * @returns Promise with cross-sell recommendations with pagination
106
81
  */
107
- searchProducts(searchData: components["schemas"]["SearchProduct"]): Promise<{
108
- skus: components["schemas"]["SKU"][];
109
- facet_distribution: {
110
- [key: string]: {
111
- [key: string]: number;
112
- };
113
- };
114
- facet_stats: {
115
- [key: string]: {
116
- min: number;
117
- max: number;
118
- };
119
- };
120
- pagination: components["schemas"]["Pagination"];
121
- }>;
82
+ listCrossSellProducts(options?: ListCrosssellProductsQuery): Promise<ApiResult<ListCrosssellProductsContent>>;
122
83
  /**
123
- * Get product details by ID (alternative implementation)
124
- * @param productId - The product ID to retrieve
125
- * @returns Promise with the product details
84
+ * Retrieve up-sell recommendations for a specific product
85
+ *
86
+ * @param options - Optional query parameters
87
+ * @returns Promise with up-sell recommendations with pagination
88
+ */
89
+ listUpSellProducts(options?: ListUpsellProductsQuery): Promise<ApiResult<ListUpsellProductsContent>>;
90
+ /**
91
+ * Retrieve related products for a specific product
92
+ *
93
+ * @param options - Optional query parameters
94
+ * @returns Promise with related products with pagination
126
95
  */
127
- getProduct(productId: string): Promise<{
128
- product: components["schemas"]["ProductDetail"];
129
- }>;
96
+ listSimilarProducts(options?: ListSimilarProductsQuery): Promise<ApiResult<ListSimilarProductsContent>>;
130
97
  }