@reactionary/provider-commercetools 0.1.13 → 0.2.2

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/core/client.js CHANGED
@@ -113,13 +113,6 @@ class CommercetoolsClient {
113
113
  async logout() {
114
114
  await this.cache.set({ token: "", refreshToken: "", expirationTime: 0 });
115
115
  const identity = {
116
- meta: {
117
- cache: {
118
- hit: false,
119
- key: ""
120
- },
121
- placeholder: false
122
- },
123
116
  type: "Anonymous"
124
117
  };
125
118
  return identity;
@@ -129,13 +122,6 @@ class CommercetoolsClient {
129
122
  const session = await this.cache.get();
130
123
  if (!session || !session.token) {
131
124
  const identity = {
132
- meta: {
133
- cache: {
134
- hit: false,
135
- key: ""
136
- },
137
- placeholder: false
138
- },
139
125
  type: "Anonymous"
140
126
  };
141
127
  return identity;
@@ -167,14 +153,7 @@ class CommercetoolsClient {
167
153
  id: {
168
154
  userId: id
169
155
  },
170
- type: "Guest",
171
- meta: {
172
- cache: {
173
- hit: false,
174
- key: id
175
- },
176
- placeholder: false
177
- }
156
+ type: "Guest"
178
157
  };
179
158
  return identity;
180
159
  }
@@ -186,26 +165,12 @@ class CommercetoolsClient {
186
165
  id: {
187
166
  userId: id
188
167
  },
189
- type: "Registered",
190
- meta: {
191
- cache: {
192
- hit: false,
193
- key: id
194
- },
195
- placeholder: false
196
- }
168
+ type: "Registered"
197
169
  };
198
170
  return identity;
199
171
  }
200
172
  return {
201
- type: "Anonymous",
202
- meta: {
203
- cache: {
204
- hit: false,
205
- key: ""
206
- },
207
- placeholder: false
208
- }
173
+ type: "Anonymous"
209
174
  };
210
175
  }
211
176
  async becomeGuest() {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@reactionary/provider-commercetools",
3
- "version": "0.1.13",
3
+ "version": "0.2.2",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
7
- "@reactionary/core": "0.1.13",
7
+ "@reactionary/core": "0.2.2",
8
8
  "debug": "^4.4.3",
9
9
  "zod": "4.1.9",
10
10
  "@commercetools/ts-client": "^4.2.1",
@@ -22,7 +22,11 @@ import {
22
22
  CartProvider,
23
23
  CartQueryByIdSchema,
24
24
  CartSchema,
25
- Reactionary
25
+ Reactionary,
26
+ success,
27
+ error,
28
+ unwrapValue,
29
+ assertSuccess
26
30
  } from "@reactionary/core";
27
31
  import { CommercetoolsCartIdentifierSchema } from "../schema/commercetools.schema.js";
28
32
  class CommercetoolsCartProvider extends CartProvider {
@@ -32,21 +36,24 @@ class CommercetoolsCartProvider extends CartProvider {
32
36
  this.client = client;
33
37
  }
34
38
  async getById(payload) {
39
+ const client = await this.getClient();
40
+ const ctId = payload.cart;
35
41
  try {
36
- const client = await this.getClient();
37
- const ctId = payload.cart;
38
42
  const remote = await client.carts.withId({ ID: ctId.key }).get().execute();
39
- return this.parseSingle(remote.body);
40
- } catch (e) {
41
- return this.createEmptyCart();
43
+ return success(this.parseSingle(remote.body));
44
+ } catch (err) {
45
+ return error({
46
+ type: "NotFound",
47
+ identifier: ctId
48
+ });
42
49
  }
43
50
  }
44
51
  async add(payload) {
45
52
  let cartIdentifier = payload.cart;
46
- if (!cartIdentifier.key) {
53
+ if (!cartIdentifier) {
47
54
  cartIdentifier = await this.createCart();
48
55
  }
49
- return this.applyActions(cartIdentifier, [
56
+ const result = await this.applyActions(cartIdentifier, [
50
57
  {
51
58
  action: "addLineItem",
52
59
  quantity: payload.quantity,
@@ -61,9 +68,10 @@ class CommercetoolsCartProvider extends CartProvider {
61
68
  action: "recalculate"
62
69
  }
63
70
  ]);
71
+ return success(result);
64
72
  }
65
73
  async remove(payload) {
66
- return this.applyActions(payload.cart, [
74
+ const result = await this.applyActions(payload.cart, [
67
75
  {
68
76
  action: "removeLineItem",
69
77
  lineItemId: payload.item.key
@@ -72,12 +80,15 @@ class CommercetoolsCartProvider extends CartProvider {
72
80
  action: "recalculate"
73
81
  }
74
82
  ]);
83
+ return success(result);
75
84
  }
76
85
  async changeQuantity(payload) {
77
86
  if (payload.quantity === 0) {
78
- return this.getById({ cart: payload.cart });
87
+ const existing = await this.getById({ cart: payload.cart });
88
+ assertSuccess(existing);
89
+ return existing;
79
90
  }
80
- return this.applyActions(payload.cart, [
91
+ const result = await this.applyActions(payload.cart, [
81
92
  {
82
93
  action: "changeLineItemQuantity",
83
94
  lineItemId: payload.item.key,
@@ -87,19 +98,21 @@ class CommercetoolsCartProvider extends CartProvider {
87
98
  action: "recalculate"
88
99
  }
89
100
  ]);
101
+ return success(result);
90
102
  }
91
103
  async getActiveCartId() {
92
104
  const client = await this.getClient();
93
105
  try {
94
106
  const carts = await client.activeCart.get().execute();
95
- return CommercetoolsCartIdentifierSchema.parse({
107
+ const result = await CommercetoolsCartIdentifierSchema.parse({
96
108
  key: carts.body.id,
97
109
  version: carts.body.version || 0
98
110
  });
111
+ return success(result);
99
112
  } catch (e) {
100
- return CommercetoolsCartIdentifierSchema.parse({
101
- key: "",
102
- version: 0
113
+ return error({
114
+ type: "NotFound",
115
+ identifier: {}
103
116
  });
104
117
  }
105
118
  }
@@ -114,11 +127,10 @@ class CommercetoolsCartProvider extends CartProvider {
114
127
  }
115
128
  }).execute();
116
129
  }
117
- const activeCartId = await this.getActiveCartId();
118
- return this.getById({ cart: activeCartId });
130
+ return success(void 0);
119
131
  }
120
- applyCouponCode(payload) {
121
- return this.applyActions(payload.cart, [
132
+ async applyCouponCode(payload) {
133
+ const result = await this.applyActions(payload.cart, [
122
134
  {
123
135
  action: "addDiscountCode",
124
136
  code: payload.couponCode
@@ -127,9 +139,10 @@ class CommercetoolsCartProvider extends CartProvider {
127
139
  action: "recalculate"
128
140
  }
129
141
  ]);
142
+ return success(result);
130
143
  }
131
- removeCouponCode(payload) {
132
- return this.applyActions(payload.cart, [
144
+ async removeCouponCode(payload) {
145
+ const result = await this.applyActions(payload.cart, [
133
146
  {
134
147
  action: "removeDiscountCode",
135
148
  discountCode: {
@@ -141,6 +154,7 @@ class CommercetoolsCartProvider extends CartProvider {
141
154
  action: "recalculate"
142
155
  }
143
156
  ]);
157
+ return success(result);
144
158
  }
145
159
  async changeCurrency(payload) {
146
160
  const client = await this.getClient();
@@ -174,7 +188,7 @@ class CommercetoolsCartProvider extends CartProvider {
174
188
  dataErasure: false
175
189
  }
176
190
  }).execute();
177
- return response;
191
+ return success(response);
178
192
  }
179
193
  async createCart() {
180
194
  const client = await this.getClient();
@@ -314,13 +328,6 @@ class CommercetoolsCartProvider extends CartProvider {
314
328
  name: remote.custom?.fields["name"] || "",
315
329
  description: remote.custom?.fields["description"] || "",
316
330
  price,
317
- meta: {
318
- cache: {
319
- hit: false,
320
- key: this.generateCacheKeySingle(identifier)
321
- },
322
- placeholder: false
323
- },
324
331
  items
325
332
  };
326
333
  return cart;
@@ -18,7 +18,9 @@ import {
18
18
  CategoryQueryForChildCategoriesSchema,
19
19
  CategoryQueryForTopCategoriesSchema,
20
20
  CategorySchema,
21
- Reactionary
21
+ Reactionary,
22
+ success,
23
+ error
22
24
  } from "@reactionary/core";
23
25
  import z from "zod";
24
26
  class CommercetoolsCategoryProvider extends CategoryProvider {
@@ -35,25 +37,12 @@ class CommercetoolsCategoryProvider extends CategoryProvider {
35
37
  const client = await this.getClient();
36
38
  try {
37
39
  const response = await client.withKey({ key: payload.id.key }).get().execute();
38
- return this.parseSingle(response.body);
39
- } catch (error) {
40
- const dummyCategory = {
41
- identifier: {
42
- key: payload.id.key
43
- },
44
- images: [],
45
- name: "",
46
- slug: "",
47
- text: "",
48
- meta: {
49
- cache: {
50
- hit: false,
51
- key: ""
52
- },
53
- placeholder: true
54
- }
55
- };
56
- return dummyCategory;
40
+ return success(this.parseSingle(response.body));
41
+ } catch (err) {
42
+ return error({
43
+ type: "NotFound",
44
+ identifier: payload.id
45
+ });
57
46
  }
58
47
  }
59
48
  async getBySlug(payload) {
@@ -69,12 +58,18 @@ class CommercetoolsCategoryProvider extends CategoryProvider {
69
58
  }
70
59
  }).execute();
71
60
  if (response.body.results.length === 0) {
72
- return null;
61
+ return error({
62
+ type: "NotFound",
63
+ identifier: payload.slug
64
+ });
73
65
  }
74
- return this.parseSingle(response.body.results[0]);
75
- } catch (error) {
66
+ return success(this.parseSingle(response.body.results[0]));
67
+ } catch (err) {
76
68
  console.error(`Error fetching category by slug:`, error);
77
- return null;
69
+ return error({
70
+ type: "NotFound",
71
+ identifier: payload.slug
72
+ });
78
73
  }
79
74
  }
80
75
  async getBreadcrumbPathToCategory(payload) {
@@ -94,13 +89,13 @@ class CommercetoolsCategoryProvider extends CategoryProvider {
94
89
  }
95
90
  }
96
91
  path.push(category);
97
- } catch (error) {
92
+ } catch (error2) {
98
93
  console.error(
99
94
  `Error fetching category path for ${payload.id.key}:`,
100
- error
95
+ error2
101
96
  );
102
97
  }
103
- return path;
98
+ return success(path);
104
99
  }
105
100
  async findChildCategories(payload) {
106
101
  const client = await this.getClient();
@@ -117,38 +112,21 @@ class CommercetoolsCategoryProvider extends CategoryProvider {
117
112
  }
118
113
  }).execute();
119
114
  const result = this.parsePaginatedResult(response.body);
120
- result.meta = {
121
- cache: {
122
- hit: false,
123
- key: this.generateCacheKeyPaginatedResult(
124
- "children-of-" + payload.parentId.key,
125
- result
126
- )
127
- },
128
- placeholder: false
129
- };
130
- return result;
131
- } catch (error) {
115
+ return success(result);
116
+ } catch (error2) {
132
117
  console.error(
133
118
  `Error fetching category path for ${payload.parentId.key}:`,
134
- error
119
+ error2
135
120
  );
136
121
  }
137
122
  const empty = {
138
123
  items: [],
139
- meta: {
140
- cache: {
141
- hit: false,
142
- key: ""
143
- },
144
- placeholder: true
145
- },
146
124
  pageNumber: payload.paginationOptions.pageNumber,
147
125
  pageSize: payload.paginationOptions.pageSize,
148
126
  totalCount: 0,
149
127
  totalPages: 0
150
128
  };
151
- return empty;
129
+ return success(empty);
152
130
  }
153
131
  async findTopCategories(payload) {
154
132
  const client = await this.getClient();
@@ -163,32 +141,18 @@ class CommercetoolsCategoryProvider extends CategoryProvider {
163
141
  }
164
142
  }).execute();
165
143
  const result = this.parsePaginatedResult(response.body);
166
- result.meta = {
167
- cache: {
168
- hit: false,
169
- key: this.generateCacheKeyPaginatedResult("top", result)
170
- },
171
- placeholder: false
172
- };
173
- return result;
174
- } catch (error) {
175
- console.error(`Error fetching category top categories:`, error);
144
+ return success(result);
145
+ } catch (error2) {
146
+ console.error(`Error fetching category top categories:`, error2);
176
147
  }
177
148
  const empty = {
178
149
  items: [],
179
- meta: {
180
- cache: {
181
- hit: false,
182
- key: ""
183
- },
184
- placeholder: true
185
- },
186
150
  pageNumber: payload.paginationOptions.pageNumber,
187
151
  pageSize: payload.paginationOptions.pageSize,
188
152
  totalCount: 0,
189
153
  totalPages: 0
190
154
  };
191
- return empty;
155
+ return success(empty);
192
156
  }
193
157
  /**
194
158
  * Handler for parsing a response from a remote provider and converting it
@@ -211,11 +175,7 @@ class CommercetoolsCategoryProvider extends CategoryProvider {
211
175
  height: asset.sources[0].dimensions?.h || 0,
212
176
  width: asset.sources[0].dimensions?.w || 0
213
177
  };
214
- }),
215
- meta: {
216
- cache: { hit: false, key: this.generateCacheKeySingle(identifier) },
217
- placeholder: false
218
- }
178
+ })
219
179
  };
220
180
  return model;
221
181
  }
@@ -223,10 +183,6 @@ class CommercetoolsCategoryProvider extends CategoryProvider {
223
183
  const body = _body;
224
184
  const items = body.results.map((x) => this.parseSingle(x));
225
185
  const result = {
226
- meta: {
227
- cache: { hit: false, key: "unknown" },
228
- placeholder: false
229
- },
230
186
  pageNumber: Math.floor(body.offset / body.count) + 1,
231
187
  pageSize: body.count,
232
188
  totalCount: body.total || 0,
@@ -245,7 +201,7 @@ __decorateClass([
245
201
  __decorateClass([
246
202
  Reactionary({
247
203
  inputSchema: CategoryQueryBySlugSchema,
248
- outputSchema: CategorySchema.nullable()
204
+ outputSchema: CategorySchema
249
205
  })
250
206
  ], CommercetoolsCategoryProvider.prototype, "getBySlug", 1);
251
207
  __decorateClass([
@@ -24,7 +24,10 @@ import {
24
24
  CheckoutSchema,
25
25
  PaymentMethodSchema,
26
26
  Reactionary,
27
- ShippingMethodSchema
27
+ ShippingMethodSchema,
28
+ success,
29
+ error,
30
+ unwrapValue
28
31
  } from "@reactionary/core";
29
32
  import z from "zod";
30
33
  import {
@@ -110,7 +113,7 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
110
113
  actions: [...actions]
111
114
  }
112
115
  }).execute();
113
- return this.parseSingle(checkoutResponse.body);
116
+ return success(this.parseSingle(checkoutResponse.body));
114
117
  }
115
118
  async getById(payload) {
116
119
  const client = await this.getClient();
@@ -130,7 +133,7 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
130
133
  key: order.body.results[0].id
131
134
  };
132
135
  }
133
- return checkout;
136
+ return success(checkout);
134
137
  }
135
138
  async setShippingAddress(payload) {
136
139
  const client = await this.getClient();
@@ -154,7 +157,7 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
154
157
  ]
155
158
  }
156
159
  }).execute();
157
- return this.parseSingle(checkoutResponse.body);
160
+ return success(this.parseSingle(checkoutResponse.body));
158
161
  }
159
162
  async getAvailableShippingMethods(payload) {
160
163
  const client = await this.getClient();
@@ -183,12 +186,12 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
183
186
  };
184
187
  result.push(shippingMethod);
185
188
  }
186
- return result;
189
+ return success(result);
187
190
  }
188
191
  async getAvailablePaymentMethods(payload) {
189
192
  const staticMethods = this.getStaticPaymentMethods(payload.checkout);
190
193
  const dynamicMethods = [];
191
- return [...staticMethods, ...dynamicMethods];
194
+ return success([...staticMethods, ...dynamicMethods]);
192
195
  }
193
196
  async addPaymentInstruction(payload) {
194
197
  const client = await this.getClient();
@@ -228,15 +231,16 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
228
231
  }
229
232
  }
230
233
  ];
231
- return this.applyActions(
234
+ const result = await this.applyActions(
232
235
  payload.checkout,
233
236
  actions
234
237
  );
238
+ return success(result);
235
239
  }
236
240
  async removePaymentInstruction(payload) {
237
241
  const client = await this.getClient();
238
- const checkout = await this.getById({ identifier: payload.checkout });
239
- return checkout;
242
+ const checkout = unwrapValue(await this.getById({ identifier: payload.checkout }));
243
+ return success(checkout);
240
244
  }
241
245
  async setShippingInstruction(payload) {
242
246
  const actions = [];
@@ -262,14 +266,15 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
262
266
  name: "pickupPointId",
263
267
  value: payload.shippingInstruction.pickupPoint
264
268
  });
265
- return this.applyActions(
269
+ const result = await this.applyActions(
266
270
  payload.checkout,
267
271
  actions
268
272
  );
273
+ return success(result);
269
274
  }
270
275
  async finalizeCheckout(payload) {
271
276
  const checkout = await this.getById({ identifier: payload.checkout });
272
- if (!checkout || !checkout.readyForFinalization) {
277
+ if (!checkout.success || !checkout.value.readyForFinalization) {
273
278
  throw new CheckoutNotReadyForFinalizationError(payload.checkout);
274
279
  }
275
280
  const client = await this.getClient();
@@ -426,13 +431,6 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
426
431
  name: remote.custom?.fields["name"] || "",
427
432
  description: remote.custom?.fields["description"] || "",
428
433
  readyForFinalization,
429
- meta: {
430
- cache: {
431
- hit: false,
432
- key: this.generateCacheKeySingle(identifier)
433
- },
434
- placeholder: false
435
- },
436
434
  billingAddress,
437
435
  shippingAddress,
438
436
  shippingInstruction,
@@ -485,17 +483,9 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
485
483
  } else {
486
484
  status = "pending";
487
485
  }
488
- const meta = {
489
- cache: {
490
- hit: false,
491
- key: ""
492
- },
493
- placeholder: false
494
- };
495
486
  const result = {
496
487
  amount,
497
488
  identifier,
498
- meta,
499
489
  paymentMethod,
500
490
  protocolData,
501
491
  status
@@ -514,13 +504,6 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
514
504
  identifier: {
515
505
  nickName: ""
516
506
  },
517
- meta: {
518
- cache: {
519
- hit: false,
520
- key: ""
521
- },
522
- placeholder: false
523
- },
524
507
  region: ""
525
508
  };
526
509
  }
@@ -536,14 +519,7 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
536
519
  },
537
520
  pickupPoint: pickupPoint || "",
538
521
  instructions: instructions || "",
539
- consentForUnattendedDelivery: consentForUnattendedDelivery || false,
540
- meta: {
541
- cache: {
542
- hit: false,
543
- key: ""
544
- },
545
- placeholder: false
546
- }
522
+ consentForUnattendedDelivery: consentForUnattendedDelivery || false
547
523
  };
548
524
  return shippingInstruction;
549
525
  }
@@ -15,7 +15,8 @@ import {
15
15
  IdentityQuerySelfSchema,
16
16
  IdentitySchema,
17
17
  IdentityMutationRegisterSchema,
18
- IdentityMutationLoginSchema
18
+ IdentityMutationLoginSchema,
19
+ success
19
20
  } from "@reactionary/core";
20
21
  class CommercetoolsIdentityProvider extends IdentityProvider {
21
22
  constructor(config, cache, context, client) {
@@ -25,25 +26,25 @@ class CommercetoolsIdentityProvider extends IdentityProvider {
25
26
  }
26
27
  async getSelf(payload) {
27
28
  const identity = await this.client.introspect();
28
- return identity;
29
+ return success(identity);
29
30
  }
30
31
  async login(payload) {
31
32
  const identity = await this.client.login(
32
33
  payload.username,
33
34
  payload.password
34
35
  );
35
- return identity;
36
+ return success(identity);
36
37
  }
37
38
  async logout(payload) {
38
39
  const identity = await this.client.logout();
39
- return identity;
40
+ return success(identity);
40
41
  }
41
42
  async register(payload) {
42
43
  const identity = await this.client.register(
43
44
  payload.username,
44
45
  payload.password
45
46
  );
46
- return identity;
47
+ return success(identity);
47
48
  }
48
49
  }
49
50
  __decorateClass([
@@ -9,7 +9,7 @@ var __decorateClass = (decorators, target, key, kind) => {
9
9
  __defProp(target, key, result);
10
10
  return result;
11
11
  };
12
- import { InventoryProvider, InventoryQueryBySKUSchema, InventorySchema, Reactionary } from "@reactionary/core";
12
+ import { InventoryProvider, InventoryQueryBySKUSchema, InventorySchema, Reactionary, success, error } from "@reactionary/core";
13
13
  class CommercetoolsInventoryProvider extends InventoryProvider {
14
14
  constructor(config, cache, context, client) {
15
15
  super(cache, context);
@@ -35,15 +35,13 @@ class CommercetoolsInventoryProvider extends InventoryProvider {
35
35
  }).execute();
36
36
  const result = remote.body.results[0];
37
37
  const model = this.parseSingle(result);
38
- return model;
39
- } catch (error) {
38
+ return success(model);
39
+ } catch (err) {
40
40
  console.error("Error fetching inventory by SKU and Fulfillment Center:", error, payload);
41
- return this.createEmptyInventory(
42
- {
43
- variant: { sku: payload.variant.sku },
44
- fulfillmentCenter: { key: payload.fulfilmentCenter.key }
45
- }
46
- );
41
+ return error({
42
+ type: "NotFound",
43
+ identifier: payload
44
+ });
47
45
  }
48
46
  }
49
47
  parseSingle(body) {
@@ -58,18 +56,10 @@ class CommercetoolsInventoryProvider extends InventoryProvider {
58
56
  if (quantity > 0) {
59
57
  status = "inStock";
60
58
  }
61
- const meta = {
62
- cache: {
63
- hit: false,
64
- key: this.generateCacheKeySingle(identifier)
65
- },
66
- placeholder: false
67
- };
68
59
  const result = {
69
60
  identifier,
70
61
  quantity,
71
- status,
72
- meta
62
+ status
73
63
  };
74
64
  return result;
75
65
  }