@commercengine/storefront-sdk 0.2.0 → 0.2.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 +100 -82
- package/dist/lib/catalog.js +73 -59
- package/dist/lib/client.d.ts +1 -0
- package/dist/lib/client.js +36 -8
- package/package.json +1 -1
package/dist/lib/cart.js
CHANGED
|
@@ -99,19 +99,21 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
99
99
|
if (!id) {
|
|
100
100
|
throw new Error("Cart ID is required but not provided");
|
|
101
101
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
+
}
|
|
107
116
|
});
|
|
108
|
-
if (error) {
|
|
109
|
-
this.handleError(error);
|
|
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
117
|
}
|
|
116
118
|
/**
|
|
117
119
|
* Add item to cart - either by specified ID or stored cart ID
|
|
@@ -145,16 +147,18 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
145
147
|
if (!id) {
|
|
146
148
|
throw new Error("Cart ID is required but not provided");
|
|
147
149
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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;
|
|
153
161
|
});
|
|
154
|
-
if (error) {
|
|
155
|
-
this.handleError(error);
|
|
156
|
-
}
|
|
157
|
-
return data?.content;
|
|
158
162
|
}
|
|
159
163
|
/**
|
|
160
164
|
* Get cart details by user ID
|
|
@@ -163,15 +167,17 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
163
167
|
* @returns Promise with cart details
|
|
164
168
|
*/
|
|
165
169
|
async getUserCart(userId) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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;
|
|
170
180
|
});
|
|
171
|
-
if (error) {
|
|
172
|
-
this.handleError(error);
|
|
173
|
-
}
|
|
174
|
-
return data?.content;
|
|
175
181
|
}
|
|
176
182
|
/**
|
|
177
183
|
* Delete a cart by user ID
|
|
@@ -180,15 +186,17 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
180
186
|
* @returns Promise that resolves when the cart is deleted
|
|
181
187
|
*/
|
|
182
188
|
async deleteUserCart(userId) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
+
}
|
|
188
199
|
});
|
|
189
|
-
if (error) {
|
|
190
|
-
this.handleError(error);
|
|
191
|
-
}
|
|
192
200
|
}
|
|
193
201
|
/**
|
|
194
202
|
* Update cart addresses
|
|
@@ -198,16 +206,18 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
198
206
|
* @returns Promise with updated cart
|
|
199
207
|
*/
|
|
200
208
|
async updateCartAddress(cartId, addressData) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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;
|
|
206
220
|
});
|
|
207
|
-
if (error) {
|
|
208
|
-
this.handleError(error);
|
|
209
|
-
}
|
|
210
|
-
return data?.content;
|
|
211
221
|
}
|
|
212
222
|
/**
|
|
213
223
|
* Apply a coupon to the cart
|
|
@@ -217,16 +227,18 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
217
227
|
* @returns Promise with updated cart
|
|
218
228
|
*/
|
|
219
229
|
async applyCoupon(cartId, couponCode) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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;
|
|
225
241
|
});
|
|
226
|
-
if (error) {
|
|
227
|
-
this.handleError(error);
|
|
228
|
-
}
|
|
229
|
-
return data?.content;
|
|
230
242
|
}
|
|
231
243
|
/**
|
|
232
244
|
* Remove a coupon from the cart
|
|
@@ -235,16 +247,18 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
235
247
|
* @returns Promise with updated cart
|
|
236
248
|
*/
|
|
237
249
|
async removeCoupon(cartId) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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;
|
|
243
261
|
});
|
|
244
|
-
if (error) {
|
|
245
|
-
this.handleError(error);
|
|
246
|
-
}
|
|
247
|
-
return data?.content;
|
|
248
262
|
}
|
|
249
263
|
/**
|
|
250
264
|
* Redeem loyalty points
|
|
@@ -254,16 +268,18 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
254
268
|
* @returns Promise with updated cart
|
|
255
269
|
*/
|
|
256
270
|
async redeemLoyaltyPoints(cartId, points) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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;
|
|
262
282
|
});
|
|
263
|
-
if (error) {
|
|
264
|
-
this.handleError(error);
|
|
265
|
-
}
|
|
266
|
-
return data?.content;
|
|
267
283
|
}
|
|
268
284
|
/**
|
|
269
285
|
* Remove loyalty points
|
|
@@ -272,16 +288,18 @@ class CartClient extends client_1.StorefrontAPIClient {
|
|
|
272
288
|
* @returns Promise with updated cart
|
|
273
289
|
*/
|
|
274
290
|
async removeLoyaltyPoints(cartId) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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;
|
|
280
302
|
});
|
|
281
|
-
if (error) {
|
|
282
|
-
this.handleError(error);
|
|
283
|
-
}
|
|
284
|
-
return data?.content;
|
|
285
303
|
}
|
|
286
304
|
}
|
|
287
305
|
exports.CartClient = CartClient;
|
package/dist/lib/catalog.js
CHANGED
|
@@ -31,16 +31,18 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
31
31
|
* @returns Promise with product details
|
|
32
32
|
*/
|
|
33
33
|
async getProductDetail(productId, options) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
return this.executeRequest(async () => {
|
|
35
|
+
const { data, error } = await this.client.GET("/catalog/products/{product_id}", {
|
|
36
|
+
params: {
|
|
37
|
+
path: { product_id: productId },
|
|
38
|
+
query: options,
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
if (error) {
|
|
42
|
+
this.handleError(error);
|
|
43
|
+
}
|
|
44
|
+
return data?.content;
|
|
39
45
|
});
|
|
40
|
-
if (error) {
|
|
41
|
-
this.handleError(error);
|
|
42
|
-
}
|
|
43
|
-
return data?.content;
|
|
44
46
|
}
|
|
45
47
|
/**
|
|
46
48
|
* List variants for a specific product
|
|
@@ -50,16 +52,18 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
50
52
|
* @returns Promise with variants
|
|
51
53
|
*/
|
|
52
54
|
async listProductVariants(productId, options) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
return this.executeRequest(async () => {
|
|
56
|
+
const { data, error } = await this.client.GET("/catalog/products/{product_id}/variants", {
|
|
57
|
+
params: {
|
|
58
|
+
path: { product_id: productId },
|
|
59
|
+
query: options,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
if (error) {
|
|
63
|
+
this.handleError(error);
|
|
64
|
+
}
|
|
65
|
+
return data?.content;
|
|
58
66
|
});
|
|
59
|
-
if (error) {
|
|
60
|
-
this.handleError(error);
|
|
61
|
-
}
|
|
62
|
-
return data?.content;
|
|
63
67
|
}
|
|
64
68
|
/**
|
|
65
69
|
* Get details for a specific variant
|
|
@@ -70,19 +74,21 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
70
74
|
* @returns Promise with variant details
|
|
71
75
|
*/
|
|
72
76
|
async getVariantDetail(productId, variantId, options) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
return this.executeRequest(async () => {
|
|
78
|
+
const { data, error } = await this.client.GET("/catalog/products/{product_id}/variants/{variant_id}", {
|
|
79
|
+
params: {
|
|
80
|
+
path: {
|
|
81
|
+
product_id: productId,
|
|
82
|
+
variant_id: variantId,
|
|
83
|
+
},
|
|
84
|
+
query: options,
|
|
78
85
|
},
|
|
79
|
-
|
|
80
|
-
|
|
86
|
+
});
|
|
87
|
+
if (error) {
|
|
88
|
+
this.handleError(error);
|
|
89
|
+
}
|
|
90
|
+
return data?.content;
|
|
81
91
|
});
|
|
82
|
-
if (error) {
|
|
83
|
-
this.handleError(error);
|
|
84
|
-
}
|
|
85
|
-
return data?.content;
|
|
86
92
|
}
|
|
87
93
|
/**
|
|
88
94
|
* List all categories
|
|
@@ -91,13 +97,15 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
91
97
|
* @returns Promise with categories and pagination info
|
|
92
98
|
*/
|
|
93
99
|
async listCategories(options) {
|
|
94
|
-
|
|
95
|
-
|
|
100
|
+
return this.executeRequest(async () => {
|
|
101
|
+
const { data, error } = await this.client.GET("/catalog/categories", {
|
|
102
|
+
params: { query: options },
|
|
103
|
+
});
|
|
104
|
+
if (error) {
|
|
105
|
+
this.handleError(error);
|
|
106
|
+
}
|
|
107
|
+
return data?.content;
|
|
96
108
|
});
|
|
97
|
-
if (error) {
|
|
98
|
-
this.handleError(error);
|
|
99
|
-
}
|
|
100
|
-
return data?.content;
|
|
101
109
|
}
|
|
102
110
|
/**
|
|
103
111
|
* List reviews for a specific product
|
|
@@ -107,16 +115,18 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
107
115
|
* @returns Promise with reviews and pagination info
|
|
108
116
|
*/
|
|
109
117
|
async listProductReviews(productId, options) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
118
|
+
return this.executeRequest(async () => {
|
|
119
|
+
const { data, error } = await this.client.GET("/catalog/products/{product_id}/reviews", {
|
|
120
|
+
params: {
|
|
121
|
+
path: { product_id: productId },
|
|
122
|
+
query: options,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
if (error) {
|
|
126
|
+
this.handleError(error);
|
|
127
|
+
}
|
|
128
|
+
return data?.content;
|
|
115
129
|
});
|
|
116
|
-
if (error) {
|
|
117
|
-
this.handleError(error);
|
|
118
|
-
}
|
|
119
|
-
return data?.content;
|
|
120
130
|
}
|
|
121
131
|
/**
|
|
122
132
|
* Create a review for a specific product
|
|
@@ -128,16 +138,18 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
128
138
|
async createProductReview(productId, reviewData) {
|
|
129
139
|
// Note: In a real implementation, you would need to handle multipart/form-data
|
|
130
140
|
// This would require FormData and may need additional handling
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
141
|
+
return this.executeRequest(async () => {
|
|
142
|
+
const { error } = await this.client.POST("/catalog/products/{product_id}/reviews", {
|
|
143
|
+
params: {
|
|
144
|
+
path: { product_id: productId },
|
|
145
|
+
},
|
|
146
|
+
body: reviewData,
|
|
147
|
+
// This is simplified as we're not properly handling multipart/form-data
|
|
148
|
+
});
|
|
149
|
+
if (error) {
|
|
150
|
+
this.handleError(error);
|
|
151
|
+
}
|
|
137
152
|
});
|
|
138
|
-
if (error) {
|
|
139
|
-
this.handleError(error);
|
|
140
|
-
}
|
|
141
153
|
}
|
|
142
154
|
/**
|
|
143
155
|
* Search for products
|
|
@@ -146,13 +158,15 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
146
158
|
* @returns Promise with search results
|
|
147
159
|
*/
|
|
148
160
|
async searchProducts(searchData) {
|
|
149
|
-
|
|
150
|
-
|
|
161
|
+
return this.executeRequest(async () => {
|
|
162
|
+
const { data, error } = await this.client.POST("/catalog/products/search", {
|
|
163
|
+
body: searchData,
|
|
164
|
+
});
|
|
165
|
+
if (error) {
|
|
166
|
+
this.handleError(error);
|
|
167
|
+
}
|
|
168
|
+
return data?.content;
|
|
151
169
|
});
|
|
152
|
-
if (error) {
|
|
153
|
-
this.handleError(error);
|
|
154
|
-
}
|
|
155
|
-
return data?.content;
|
|
156
170
|
}
|
|
157
171
|
/**
|
|
158
172
|
* Get product details by ID (alternative implementation)
|
package/dist/lib/client.d.ts
CHANGED
package/dist/lib/client.js
CHANGED
|
@@ -44,25 +44,51 @@ class StorefrontAPIClient {
|
|
|
44
44
|
*/
|
|
45
45
|
constructor(config) {
|
|
46
46
|
this.isRefreshing = false;
|
|
47
|
-
|
|
47
|
+
// Use shared config reference for the same storeId to ensure all clients use the same config
|
|
48
|
+
const storeKey = config.storeId + (config.baseUrl || config.environment || "");
|
|
49
|
+
if (!StorefrontAPIClient.sharedConfigs.has(storeKey)) {
|
|
50
|
+
StorefrontAPIClient.sharedConfigs.set(storeKey, { ...config });
|
|
51
|
+
}
|
|
52
|
+
// Use the shared config reference
|
|
53
|
+
this.config = StorefrontAPIClient.sharedConfigs.get(storeKey);
|
|
54
|
+
// Copy non-shared values from the provided config
|
|
55
|
+
if (config.token && !this.config.token) {
|
|
56
|
+
this.config.token = config.token;
|
|
57
|
+
}
|
|
58
|
+
if (config.apiKey && !this.config.apiKey) {
|
|
59
|
+
this.config.apiKey = config.apiKey;
|
|
60
|
+
}
|
|
48
61
|
this.headers = {
|
|
49
62
|
"Content-Type": "application/json",
|
|
50
63
|
};
|
|
51
|
-
if (config.token) {
|
|
52
|
-
this.headers["Authorization"] = `Bearer ${config.token}`;
|
|
64
|
+
if (this.config.token) {
|
|
65
|
+
this.headers["Authorization"] = `Bearer ${this.config.token}`;
|
|
53
66
|
}
|
|
54
|
-
if (config.apiKey) {
|
|
55
|
-
this.headers["X-Api-Key"] = config.apiKey;
|
|
67
|
+
if (this.config.apiKey) {
|
|
68
|
+
this.headers["X-Api-Key"] = this.config.apiKey;
|
|
56
69
|
}
|
|
57
70
|
// Determine base URL from environment or use custom URL if provided
|
|
58
|
-
this.baseUrl = this.getBaseUrlFromConfig(config);
|
|
71
|
+
this.baseUrl = this.getBaseUrlFromConfig(this.config);
|
|
59
72
|
this.client = (0, openapi_fetch_1.default)({
|
|
60
73
|
baseUrl: this.baseUrl,
|
|
61
74
|
fetch: (input, init) => {
|
|
62
75
|
// Add timeout if configured
|
|
63
|
-
const timeoutSignal = config.timeout
|
|
64
|
-
? AbortSignal.timeout(config.timeout)
|
|
76
|
+
const timeoutSignal = this.config.timeout
|
|
77
|
+
? AbortSignal.timeout(this.config.timeout)
|
|
65
78
|
: undefined;
|
|
79
|
+
// Always check for the most current token and API key before each request
|
|
80
|
+
if (this.config.token) {
|
|
81
|
+
this.headers["Authorization"] = `Bearer ${this.config.token}`;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
delete this.headers["Authorization"];
|
|
85
|
+
}
|
|
86
|
+
if (this.config.apiKey) {
|
|
87
|
+
this.headers["X-Api-Key"] = this.config.apiKey;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
delete this.headers["X-Api-Key"];
|
|
91
|
+
}
|
|
66
92
|
// Merge headers
|
|
67
93
|
const headers = {
|
|
68
94
|
...this.headers,
|
|
@@ -237,3 +263,5 @@ class StorefrontAPIClient {
|
|
|
237
263
|
}
|
|
238
264
|
}
|
|
239
265
|
exports.StorefrontAPIClient = StorefrontAPIClient;
|
|
266
|
+
// Shared static reference for configs by storeId to ensure all clients use the same config
|
|
267
|
+
StorefrontAPIClient.sharedConfigs = new Map();
|