@reactionary/commercetools 0.6.3

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.
Files changed (104) hide show
  1. package/README.md +11 -0
  2. package/capabilities/cart.capability.js +324 -0
  3. package/capabilities/category.capability.js +198 -0
  4. package/capabilities/checkout.capability.js +374 -0
  5. package/capabilities/identity.capability.js +95 -0
  6. package/capabilities/index.js +15 -0
  7. package/capabilities/inventory.capability.js +63 -0
  8. package/capabilities/order-search.capability.js +106 -0
  9. package/capabilities/order.capability.js +48 -0
  10. package/capabilities/price.capability.js +114 -0
  11. package/capabilities/product-associations.capability.js +120 -0
  12. package/capabilities/product-list.capability.js +393 -0
  13. package/capabilities/product-reviews.capability.js +193 -0
  14. package/capabilities/product-search.capability.js +297 -0
  15. package/capabilities/product.capability.js +103 -0
  16. package/capabilities/profile.capability.js +337 -0
  17. package/capabilities/store.capability.js +51 -0
  18. package/core/capability-descriptors.js +274 -0
  19. package/core/client.js +318 -0
  20. package/core/initialize.js +47 -0
  21. package/core/initialize.types.js +8 -0
  22. package/core/token-cache.js +36 -0
  23. package/factories/cart/cart.factory.js +110 -0
  24. package/factories/category/category.factory.js +40 -0
  25. package/factories/checkout/checkout-initializer-overrides.example.js +67 -0
  26. package/factories/checkout/checkout.factory.js +235 -0
  27. package/factories/identity/identity.factory.js +14 -0
  28. package/factories/inventory/inventory.factory.js +30 -0
  29. package/factories/order/order.factory.js +114 -0
  30. package/factories/order-search/order-search.factory.js +68 -0
  31. package/factories/price/price.factory.js +52 -0
  32. package/factories/product/product-capability-custom-method-only.example.js +38 -0
  33. package/factories/product/product-capability-schema-signature-extension.example.js +46 -0
  34. package/factories/product/product-factory-baseline.example.js +10 -0
  35. package/factories/product/product-factory-schema-and-parse-extension.example.js +25 -0
  36. package/factories/product/product-factory-schema-extension.example.js +18 -0
  37. package/factories/product/product-initializer-factory-extension.example.js +33 -0
  38. package/factories/product/product.factory.js +151 -0
  39. package/factories/product/utils.example.js +8 -0
  40. package/factories/product-associations/product-associations.factory.js +63 -0
  41. package/factories/product-list/product-list.factory.js +65 -0
  42. package/factories/product-reviews/product-reviews.factory.js +42 -0
  43. package/factories/product-search/product-search.factory.js +114 -0
  44. package/factories/profile/profile.factory.js +62 -0
  45. package/factories/store/store.factory.js +29 -0
  46. package/index.js +28 -0
  47. package/package.json +17 -0
  48. package/schema/capabilities.schema.js +46 -0
  49. package/schema/commercetools.schema.js +30 -0
  50. package/schema/configuration.schema.js +19 -0
  51. package/schema/session.schema.js +9 -0
  52. package/src/capabilities/cart.capability.d.ts +34 -0
  53. package/src/capabilities/category.capability.d.ts +47 -0
  54. package/src/capabilities/checkout.capability.d.ts +39 -0
  55. package/src/capabilities/identity.capability.d.ts +14 -0
  56. package/src/capabilities/index.d.ts +15 -0
  57. package/src/capabilities/inventory.capability.d.ts +13 -0
  58. package/src/capabilities/order-search.capability.d.ts +13 -0
  59. package/src/capabilities/order.capability.d.ts +13 -0
  60. package/src/capabilities/price.capability.d.ts +18 -0
  61. package/src/capabilities/product-associations.capability.d.ts +17 -0
  62. package/src/capabilities/product-list.capability.d.ts +29 -0
  63. package/src/capabilities/product-reviews.capability.d.ts +18 -0
  64. package/src/capabilities/product-search.capability.d.ts +82 -0
  65. package/src/capabilities/product.capability.d.ts +17 -0
  66. package/src/capabilities/profile.capability.d.ts +30 -0
  67. package/src/capabilities/store.capability.d.ts +13 -0
  68. package/src/core/capability-descriptors.d.ts +16 -0
  69. package/src/core/client.d.ts +55 -0
  70. package/src/core/initialize.d.ts +5 -0
  71. package/src/core/initialize.types.d.ts +118 -0
  72. package/src/core/token-cache.d.ts +9 -0
  73. package/src/factories/cart/cart.factory.d.ts +14 -0
  74. package/src/factories/category/category.factory.d.ts +11 -0
  75. package/src/factories/checkout/checkout-initializer-overrides.example.d.ts +1 -0
  76. package/src/factories/checkout/checkout.factory.d.ts +18 -0
  77. package/src/factories/identity/identity.factory.d.ts +8 -0
  78. package/src/factories/inventory/inventory.factory.d.ts +9 -0
  79. package/src/factories/order/order.factory.d.ts +9 -0
  80. package/src/factories/order-search/order-search.factory.d.ts +11 -0
  81. package/src/factories/price/price.factory.d.ts +11 -0
  82. package/src/factories/product/product-capability-custom-method-only.example.d.ts +1 -0
  83. package/src/factories/product/product-capability-schema-signature-extension.example.d.ts +1 -0
  84. package/src/factories/product/product-factory-baseline.example.d.ts +1 -0
  85. package/src/factories/product/product-factory-schema-and-parse-extension.example.d.ts +1 -0
  86. package/src/factories/product/product-factory-schema-extension.example.d.ts +1 -0
  87. package/src/factories/product/product-initializer-factory-extension.example.d.ts +1 -0
  88. package/src/factories/product/product.factory.d.ts +18 -0
  89. package/src/factories/product/utils.example.d.ts +4 -0
  90. package/src/factories/product-associations/product-associations.factory.d.ts +15 -0
  91. package/src/factories/product-list/product-list.factory.d.ts +17 -0
  92. package/src/factories/product-reviews/product-reviews.factory.d.ts +14 -0
  93. package/src/factories/product-search/product-search.factory.d.ts +13 -0
  94. package/src/factories/profile/profile.factory.d.ts +11 -0
  95. package/src/factories/store/store.factory.d.ts +9 -0
  96. package/src/index.d.ts +28 -0
  97. package/src/schema/capabilities.schema.d.ts +104 -0
  98. package/src/schema/commercetools.schema.d.ts +30 -0
  99. package/src/schema/configuration.schema.d.ts +30 -0
  100. package/src/schema/session.schema.d.ts +7 -0
  101. package/src/test/client-builder-merge-extensions.example.d.ts +1 -0
  102. package/src/test/test-utils.d.ts +27 -0
  103. package/test/client-builder-merge-extensions.example.js +94 -0
  104. package/test/test-utils.js +27 -0
@@ -0,0 +1,235 @@
1
+ import {
2
+ CheckoutItemSchema
3
+ } from "@reactionary/core";
4
+ class CommercetoolsCheckoutFactory {
5
+ checkoutSchema;
6
+ shippingMethodSchema;
7
+ paymentMethodSchema;
8
+ constructor(checkoutSchema, shippingMethodSchema, paymentMethodSchema) {
9
+ this.checkoutSchema = checkoutSchema;
10
+ this.shippingMethodSchema = shippingMethodSchema;
11
+ this.paymentMethodSchema = paymentMethodSchema;
12
+ }
13
+ parseCheckout(context, data) {
14
+ const identifier = {
15
+ key: data.id,
16
+ version: data.version || 0
17
+ };
18
+ const originalCartReference = {
19
+ key: data.custom?.fields["commerceToolsCartId"] || "",
20
+ version: 0
21
+ };
22
+ let shippingAddress;
23
+ if (data.shippingAddress) {
24
+ shippingAddress = this.parseAddress(data.shippingAddress);
25
+ }
26
+ let billingAddress;
27
+ if (data.billingAddress) {
28
+ billingAddress = this.parseAddress(data.billingAddress);
29
+ }
30
+ const paymentInstructions = [];
31
+ for (const payment of data.paymentInfo?.payments || []) {
32
+ if (payment.obj) {
33
+ paymentInstructions.push(this.parsePaymentInstruction(context, payment.obj));
34
+ }
35
+ }
36
+ const grandTotal = data.totalPrice.centAmount || 0;
37
+ const shippingTotal = data.shippingInfo?.price.centAmount || 0;
38
+ const productTotal = grandTotal - shippingTotal;
39
+ const taxTotal = data.taxedPrice?.totalTax?.centAmount || 0;
40
+ const discountTotal = data.discountOnTotalPrice?.discountedAmount.centAmount || 0;
41
+ const surchargeTotal = 0;
42
+ const currency = data.totalPrice.currencyCode;
43
+ const price = {
44
+ totalTax: {
45
+ value: taxTotal / 100,
46
+ currency
47
+ },
48
+ totalDiscount: {
49
+ value: discountTotal / 100,
50
+ currency
51
+ },
52
+ totalSurcharge: {
53
+ value: surchargeTotal / 100,
54
+ currency
55
+ },
56
+ totalShipping: {
57
+ value: shippingTotal / 100,
58
+ currency
59
+ },
60
+ totalProductPrice: {
61
+ value: productTotal / 100,
62
+ currency
63
+ },
64
+ grandTotal: {
65
+ value: grandTotal / 100,
66
+ currency
67
+ }
68
+ };
69
+ const items = [];
70
+ for (const lineItem of data.lineItems) {
71
+ items.push(this.parseCheckoutItem(lineItem));
72
+ }
73
+ const shippingInstruction = this.parseShippingInstruction(data);
74
+ const readyForFinalization = this.isReadyForFinalization(
75
+ price,
76
+ paymentInstructions,
77
+ billingAddress,
78
+ shippingAddress,
79
+ shippingInstruction
80
+ );
81
+ const result = {
82
+ identifier,
83
+ originalCartReference,
84
+ name: data.custom?.fields["name"] || "",
85
+ description: data.custom?.fields["description"] || "",
86
+ readyForFinalization,
87
+ billingAddress,
88
+ shippingAddress,
89
+ shippingInstruction,
90
+ paymentInstructions,
91
+ items,
92
+ price
93
+ };
94
+ return this.checkoutSchema.parse(result);
95
+ }
96
+ parseShippingMethod(context, data) {
97
+ const identifier = {
98
+ key: data.key || ""
99
+ };
100
+ const result = {
101
+ deliveryTime: "",
102
+ description: data.localizedDescription?.[context.languageContext.locale] || "",
103
+ identifier,
104
+ name: data.name,
105
+ price: data.zoneRates[0].shippingRates[0].price ? {
106
+ value: (data.zoneRates[0].shippingRates[0].price.centAmount || 0) / 100,
107
+ currency: data.zoneRates[0].shippingRates[0].price.currencyCode || context.languageContext.currencyCode
108
+ } : { value: 0, currency: context.languageContext.currencyCode }
109
+ };
110
+ return this.shippingMethodSchema.parse(result);
111
+ }
112
+ parsePaymentMethod(_context, data) {
113
+ return this.paymentMethodSchema.parse(data);
114
+ }
115
+ parseCheckoutItem(remoteItem) {
116
+ const unitPrice = remoteItem.price.value.centAmount;
117
+ const totalPrice = remoteItem.totalPrice.centAmount || 0;
118
+ const totalDiscount = remoteItem.price.discounted?.value.centAmount || 0;
119
+ const unitDiscount = totalDiscount / remoteItem.quantity;
120
+ const currency = remoteItem.price.value.currencyCode.toUpperCase();
121
+ const item = {
122
+ identifier: {
123
+ key: remoteItem.id
124
+ },
125
+ variant: {
126
+ sku: remoteItem.variant.sku || ""
127
+ },
128
+ quantity: remoteItem.quantity,
129
+ price: {
130
+ unitPrice: {
131
+ value: unitPrice / 100,
132
+ currency
133
+ },
134
+ unitDiscount: {
135
+ value: unitDiscount / 100,
136
+ currency
137
+ },
138
+ totalPrice: {
139
+ value: totalPrice / 100,
140
+ currency
141
+ },
142
+ totalDiscount: {
143
+ value: totalDiscount / 100,
144
+ currency
145
+ }
146
+ }
147
+ };
148
+ return CheckoutItemSchema.parse(item);
149
+ }
150
+ isReadyForFinalization(price, paymentInstructions, billingAddress, shippingAddress, shippingInstruction) {
151
+ if (!billingAddress)
152
+ return false;
153
+ if (!shippingInstruction)
154
+ return false;
155
+ if (!shippingAddress && !shippingInstruction.pickupPoint)
156
+ return false;
157
+ if (paymentInstructions.length === 0)
158
+ return false;
159
+ const authorizedPayments = paymentInstructions.filter((paymentInstruction) => paymentInstruction.status === "authorized").map((paymentInstruction) => paymentInstruction.amount.value).reduce((sum, value) => sum + value, 0);
160
+ if (price.grandTotal.value !== authorizedPayments)
161
+ return false;
162
+ return true;
163
+ }
164
+ parsePaymentInstruction(context, data) {
165
+ const identifier = {
166
+ key: data.id
167
+ };
168
+ const amount = {
169
+ value: data.amountPlanned.centAmount / 100,
170
+ currency: data.amountPlanned.currencyCode
171
+ };
172
+ const method = data.paymentMethodInfo?.method || "unknown";
173
+ const paymentProcessor = data.paymentMethodInfo?.paymentInterface || method;
174
+ const paymentName = data.paymentMethodInfo.name?.[context.languageContext.locale];
175
+ const paymentMethod = {
176
+ method,
177
+ paymentProcessor,
178
+ name: paymentName || method || "Unknown"
179
+ };
180
+ const customData = data.custom?.fields || {};
181
+ const protocolData = Object.keys(customData).map((key) => ({
182
+ key,
183
+ value: customData[key]
184
+ }));
185
+ let status = "pending";
186
+ if (data.transactions && data.transactions.length > 0) {
187
+ const lastTransaction = data.transactions[data.transactions.length - 1];
188
+ if (lastTransaction.type === "Authorization" && lastTransaction.state === "Success") {
189
+ status = "authorized";
190
+ }
191
+ }
192
+ const result = {
193
+ amount,
194
+ identifier,
195
+ paymentMethod,
196
+ protocolData,
197
+ status
198
+ };
199
+ return result;
200
+ }
201
+ parseAddress(data) {
202
+ return {
203
+ countryCode: data.country || "",
204
+ firstName: data.firstName || "",
205
+ lastName: data.lastName || "",
206
+ streetAddress: data.streetName || "",
207
+ streetNumber: data.streetNumber || "",
208
+ postalCode: data.postalCode || "",
209
+ city: data.city || "",
210
+ identifier: {
211
+ nickName: ""
212
+ },
213
+ region: ""
214
+ };
215
+ }
216
+ parseShippingInstruction(data) {
217
+ if (!data.shippingInfo)
218
+ return void 0;
219
+ const instructions = data.custom?.fields["shippingInstruction"] || "";
220
+ const consentForUnattendedDelivery = data.custom?.fields["consentForUnattendedDelivery"] === "true";
221
+ const pickupPoint = data.custom?.fields["pickupPointId"] || "";
222
+ const result = {
223
+ shippingMethod: {
224
+ key: data.shippingInfo.shippingMethod?.obj?.key || ""
225
+ },
226
+ pickupPoint,
227
+ instructions,
228
+ consentForUnattendedDelivery
229
+ };
230
+ return result;
231
+ }
232
+ }
233
+ export {
234
+ CommercetoolsCheckoutFactory
235
+ };
@@ -0,0 +1,14 @@
1
+ import {
2
+ } from "@reactionary/core";
3
+ class CommercetoolsIdentityFactory {
4
+ identitySchema;
5
+ constructor(identitySchema) {
6
+ this.identitySchema = identitySchema;
7
+ }
8
+ parseIdentity(_context, data) {
9
+ return this.identitySchema.parse(data);
10
+ }
11
+ }
12
+ export {
13
+ CommercetoolsIdentityFactory
14
+ };
@@ -0,0 +1,30 @@
1
+ import {
2
+ } from "@reactionary/core";
3
+ class CommercetoolsInventoryFactory {
4
+ inventorySchema;
5
+ constructor(inventorySchema) {
6
+ this.inventorySchema = inventorySchema;
7
+ }
8
+ parseInventory(_context, data) {
9
+ const identifier = {
10
+ variant: { sku: data.sku || "" },
11
+ fulfillmentCenter: {
12
+ key: data.supplyChannel?.obj?.key || ""
13
+ }
14
+ };
15
+ const quantity = data.availableQuantity || 0;
16
+ let status = "outOfStock";
17
+ if (quantity > 0) {
18
+ status = "inStock";
19
+ }
20
+ const result = {
21
+ identifier,
22
+ quantity,
23
+ status
24
+ };
25
+ return this.inventorySchema.parse(result);
26
+ }
27
+ }
28
+ export {
29
+ CommercetoolsInventoryFactory
30
+ };
@@ -0,0 +1,114 @@
1
+ import {
2
+ } from "@reactionary/core";
3
+ class CommercetoolsOrderFactory {
4
+ orderSchema;
5
+ constructor(orderSchema) {
6
+ this.orderSchema = orderSchema;
7
+ }
8
+ parseOrder(_context, data) {
9
+ const identifier = {
10
+ key: data.id,
11
+ version: data.version || 0
12
+ };
13
+ const name = data.custom?.fields["name"] || "";
14
+ const description = data.custom?.fields["description"] || "";
15
+ const grandTotal = data.totalPrice.centAmount || 0;
16
+ const shippingTotal = data.shippingInfo?.price.centAmount || 0;
17
+ const productTotal = grandTotal - shippingTotal;
18
+ const taxTotal = data.taxedPrice?.totalTax?.centAmount || 0;
19
+ const discountTotal = data.discountOnTotalPrice?.discountedAmount.centAmount || 0;
20
+ const surchargeTotal = 0;
21
+ const currency = data.totalPrice.currencyCode;
22
+ const price = {
23
+ totalTax: {
24
+ value: taxTotal / 100,
25
+ currency
26
+ },
27
+ totalDiscount: {
28
+ value: discountTotal / 100,
29
+ currency
30
+ },
31
+ totalSurcharge: {
32
+ value: surchargeTotal / 100,
33
+ currency
34
+ },
35
+ totalShipping: {
36
+ value: shippingTotal / 100,
37
+ currency: data.shippingInfo?.price.currencyCode
38
+ },
39
+ totalProductPrice: {
40
+ value: productTotal / 100,
41
+ currency
42
+ },
43
+ grandTotal: {
44
+ value: grandTotal / 100,
45
+ currency
46
+ }
47
+ };
48
+ let orderStatus = "AwaitingPayment";
49
+ if (data.paymentState === "Paid" && data.orderState === "Confirmed") {
50
+ orderStatus = "ReleasedToFulfillment";
51
+ }
52
+ if (data.shipmentState === "Ready" && data.orderState === "Confirmed") {
53
+ orderStatus = "ReleasedToFulfillment";
54
+ }
55
+ if ((data.shipmentState === "Shipped" || data.shipmentState === "Delivered") && data.orderState === "Completed") {
56
+ orderStatus = "Shipped";
57
+ }
58
+ const items = [];
59
+ for (const remoteItem of data.lineItems) {
60
+ const lineIdentifier = {
61
+ key: remoteItem.id
62
+ };
63
+ const variant = {
64
+ sku: remoteItem.variant.sku || ""
65
+ };
66
+ const unitPrice = remoteItem.price.value.centAmount;
67
+ const totalPrice = remoteItem.totalPrice.centAmount || 0;
68
+ const totalDiscount = remoteItem.price.discounted?.value.centAmount || 0;
69
+ const unitDiscount = totalDiscount / remoteItem.quantity;
70
+ const linePrice = {
71
+ unitPrice: {
72
+ value: unitPrice / 100,
73
+ currency
74
+ },
75
+ unitDiscount: {
76
+ value: unitDiscount / 100,
77
+ currency
78
+ },
79
+ totalPrice: {
80
+ value: totalPrice / 100,
81
+ currency
82
+ },
83
+ totalDiscount: {
84
+ value: totalDiscount / 100,
85
+ currency
86
+ }
87
+ };
88
+ items.push({
89
+ identifier: lineIdentifier,
90
+ inventoryStatus: "NotAllocated",
91
+ price: linePrice,
92
+ quantity: remoteItem.quantity,
93
+ variant
94
+ });
95
+ }
96
+ const result = {
97
+ identifier,
98
+ name,
99
+ description,
100
+ price,
101
+ items,
102
+ inventoryStatus: "NotAllocated",
103
+ paymentInstructions: [],
104
+ userId: {
105
+ userId: ""
106
+ },
107
+ orderStatus
108
+ };
109
+ return this.orderSchema.parse(result);
110
+ }
111
+ }
112
+ export {
113
+ CommercetoolsOrderFactory
114
+ };
@@ -0,0 +1,68 @@
1
+ import {
2
+ } from "@reactionary/core";
3
+ class CommercetoolsOrderSearchFactory {
4
+ orderSearchResultSchema;
5
+ constructor(orderSearchResultSchema) {
6
+ this.orderSearchResultSchema = orderSearchResultSchema;
7
+ }
8
+ parseOrderSearchResult(context, data, query) {
9
+ const result = {
10
+ identifier: {
11
+ ...query.search
12
+ },
13
+ pageNumber: (Math.ceil(data.offset / data.limit) || 0) + 1,
14
+ pageSize: data.limit,
15
+ totalCount: data.total || 0,
16
+ totalPages: Math.ceil((data.total || 0) / data.limit || 0) + 1,
17
+ items: data.results.map((order) => this.parseOrderSearchItem(context, order))
18
+ };
19
+ return this.orderSearchResultSchema.parse(result);
20
+ }
21
+ parseAddress(data) {
22
+ return {
23
+ countryCode: data.country || "",
24
+ firstName: data.firstName || "",
25
+ lastName: data.lastName || "",
26
+ streetAddress: data.streetName || "",
27
+ streetNumber: data.streetNumber || "",
28
+ postalCode: data.postalCode || "",
29
+ city: data.city || "",
30
+ identifier: {
31
+ nickName: ""
32
+ },
33
+ region: ""
34
+ };
35
+ }
36
+ parseOrderSearchItem(context, data) {
37
+ const userId = {
38
+ userId: data.customerId || data.anonymousId || ""
39
+ };
40
+ let orderStatus = "AwaitingPayment";
41
+ if (data.paymentState === "Paid" && data.orderState === "Confirmed") {
42
+ orderStatus = "ReleasedToFulfillment";
43
+ }
44
+ if (data.shipmentState === "Ready" && data.orderState === "Confirmed") {
45
+ orderStatus = "ReleasedToFulfillment";
46
+ }
47
+ if ((data.shipmentState === "Shipped" || data.shipmentState === "Delivered") && data.orderState === "Completed") {
48
+ orderStatus = "Shipped";
49
+ }
50
+ const totalAmount = {
51
+ currency: data.totalPrice ? data.totalPrice.currencyCode : context.languageContext.currencyCode,
52
+ value: data.totalPrice ? data.totalPrice.centAmount / 100 : 0
53
+ };
54
+ return {
55
+ identifier: { key: data.id },
56
+ userId,
57
+ customerName: `${data.billingAddress?.firstName} ${data.billingAddress?.lastName}`,
58
+ shippingAddress: this.parseAddress(data.shippingAddress),
59
+ orderDate: data.createdAt,
60
+ orderStatus,
61
+ inventoryStatus: orderStatus === "Shipped" ? "Allocated" : "NotAllocated",
62
+ totalAmount
63
+ };
64
+ }
65
+ }
66
+ export {
67
+ CommercetoolsOrderSearchFactory
68
+ };
@@ -0,0 +1,52 @@
1
+ import {
2
+ } from "@reactionary/core";
3
+ class CommercetoolsPriceFactory {
4
+ priceSchema;
5
+ constructor(priceSchema) {
6
+ this.priceSchema = priceSchema;
7
+ }
8
+ parsePrice(context, data, options = { includeDiscounts: false }) {
9
+ const price = data.price;
10
+ if (!price) {
11
+ return this.priceSchema.parse({
12
+ identifier: {
13
+ variant: {
14
+ sku: data.sku || ""
15
+ }
16
+ },
17
+ onSale: false,
18
+ tieredPrices: [],
19
+ unitPrice: {
20
+ value: -1,
21
+ currency: context.languageContext.currencyCode
22
+ }
23
+ });
24
+ }
25
+ let unitPrice = {
26
+ value: price.value.centAmount / 100,
27
+ currency: price.value.currencyCode
28
+ };
29
+ if (options.includeDiscounts) {
30
+ const discountedPrice = price.discounted?.value || price.value;
31
+ unitPrice = {
32
+ value: discountedPrice.centAmount / 100,
33
+ currency: price.value.currencyCode
34
+ };
35
+ }
36
+ const identifier = {
37
+ variant: {
38
+ sku: data.sku || ""
39
+ }
40
+ };
41
+ const result = {
42
+ identifier,
43
+ onSale: options.includeDiscounts && !!price.discounted,
44
+ tieredPrices: [],
45
+ unitPrice
46
+ };
47
+ return this.priceSchema.parse(result);
48
+ }
49
+ }
50
+ export {
51
+ CommercetoolsPriceFactory
52
+ };
@@ -0,0 +1,38 @@
1
+ import { ProductSchema } from "@reactionary/core";
2
+ import { CommercetoolsProductFactory } from "./product.factory.js";
3
+ import { CommercetoolsProductCapability } from "../../capabilities/product.capability.js";
4
+ import { withCommercetoolsCapabilities } from "../../core/initialize.js";
5
+ import { assertNotAny, assertType } from "./utils.example.js";
6
+ const cache = {};
7
+ const context = {};
8
+ const config = {};
9
+ const api = {};
10
+ class ExtendedCommercetoolsProductCapability extends CommercetoolsProductCapability {
11
+ async getByCustomIdentifier(identifier) {
12
+ return this.getById({
13
+ identifier: { key: identifier }
14
+ });
15
+ }
16
+ }
17
+ const withExtendedCapabilityFactory = withCommercetoolsCapabilities(config, {
18
+ product: {
19
+ enabled: true,
20
+ factory: new CommercetoolsProductFactory(ProductSchema),
21
+ capability: ({ cache: cache2, context: context2, config: config2, commercetoolsApi }) => new ExtendedCommercetoolsProductCapability(
22
+ cache2,
23
+ context2,
24
+ config2,
25
+ commercetoolsApi,
26
+ new CommercetoolsProductFactory(ProductSchema)
27
+ )
28
+ }
29
+ });
30
+ const client = withExtendedCapabilityFactory(cache, context);
31
+ client.product.getByCustomIdentifier("p-2").then((x) => {
32
+ assertNotAny(x);
33
+ if (x.success) {
34
+ assertNotAny(x.value);
35
+ assertNotAny(x.value.name);
36
+ assertType(x.value.name);
37
+ }
38
+ });
@@ -0,0 +1,46 @@
1
+ import { ProductQueryByIdSchema, ProductSchema } from "@reactionary/core";
2
+ import { CommercetoolsProductFactory } from "./product.factory.js";
3
+ import { CommercetoolsProductCapability } from "../../capabilities/product.capability.js";
4
+ import * as z from "zod";
5
+ import { assertNotAny, assertType } from "./utils.example.js";
6
+ const cache = {};
7
+ const context = {};
8
+ const config = {};
9
+ const api = {};
10
+ const ExtendedProductSchema = ProductSchema.safeExtend({
11
+ extendedValue: z.string()
12
+ });
13
+ class ExtendedCommercetoolsProductFactory extends CommercetoolsProductFactory {
14
+ constructor() {
15
+ super(ExtendedProductSchema);
16
+ }
17
+ }
18
+ const ExtendedProductQueryByIdSchema = ProductQueryByIdSchema.extend({
19
+ auditTrail: z.boolean()
20
+ });
21
+ class ExtendedCommercetoolsProductCapability extends CommercetoolsProductCapability {
22
+ async getById(payload) {
23
+ const basePayload = {
24
+ identifier: payload.identifier
25
+ };
26
+ return super.getById(basePayload);
27
+ }
28
+ }
29
+ const extendedCapability = new ExtendedCommercetoolsProductCapability(
30
+ cache,
31
+ context,
32
+ config,
33
+ api,
34
+ new ExtendedCommercetoolsProductFactory()
35
+ );
36
+ extendedCapability.getById({
37
+ identifier: { key: "p-1" },
38
+ auditTrail: true
39
+ }).then((x) => {
40
+ assertNotAny(x);
41
+ if (x.success) {
42
+ assertNotAny(x.value);
43
+ assertNotAny(x.value.extendedValue);
44
+ assertType(x.value.extendedValue);
45
+ }
46
+ });
@@ -0,0 +1,10 @@
1
+ import { ProductSchema } from "@reactionary/core";
2
+ import { CommercetoolsProductFactory } from "./product.factory.js";
3
+ import { assertNotAny, assertType } from "./utils.example.js";
4
+ const context = {};
5
+ const baseFactory = new CommercetoolsProductFactory(ProductSchema);
6
+ const baseProduct = baseFactory.parseProduct(context, {});
7
+ assertNotAny(baseProduct);
8
+ assertNotAny(baseProduct.name);
9
+ assertType(baseProduct.name);
10
+ assertType(baseProduct.extendedValue);
@@ -0,0 +1,25 @@
1
+ import { ProductSchema } from "@reactionary/core";
2
+ import { CommercetoolsProductFactory } from "./product.factory.js";
3
+ import * as z from "zod";
4
+ import { assertNotAny, assertType } from "./utils.example.js";
5
+ const context = {};
6
+ const ExtendedProductSchema = ProductSchema.safeExtend({
7
+ extendedValue: z.string()
8
+ });
9
+ class ExtendedCommercetoolsProductFactory extends CommercetoolsProductFactory {
10
+ constructor() {
11
+ super(ExtendedProductSchema);
12
+ }
13
+ parseProduct(context2, data) {
14
+ const base = super.parseProduct(context2, data);
15
+ return {
16
+ ...base,
17
+ extendedValue: "from-parse"
18
+ };
19
+ }
20
+ }
21
+ const extendedFactory = new ExtendedCommercetoolsProductFactory();
22
+ const extendedProduct = extendedFactory.parseProduct(context, {});
23
+ assertNotAny(extendedProduct);
24
+ assertNotAny(extendedProduct.extendedValue);
25
+ assertType(extendedProduct.extendedValue);
@@ -0,0 +1,18 @@
1
+ import { ProductSchema } from "@reactionary/core";
2
+ import { CommercetoolsProductFactory } from "./product.factory.js";
3
+ import * as z from "zod";
4
+ import { assertNotAny, assertType } from "./utils.example.js";
5
+ const context = {};
6
+ const ExtendedProductSchema = ProductSchema.safeExtend({
7
+ extendedValue: z.string()
8
+ });
9
+ class ExtendedCommercetoolsProductFactory extends CommercetoolsProductFactory {
10
+ constructor() {
11
+ super(ExtendedProductSchema);
12
+ }
13
+ }
14
+ const extendedFactory = new ExtendedCommercetoolsProductFactory();
15
+ const extendedProduct = extendedFactory.parseProduct(context, {});
16
+ assertNotAny(extendedProduct);
17
+ assertNotAny(extendedProduct.extendedValue);
18
+ assertType(extendedProduct.extendedValue);
@@ -0,0 +1,33 @@
1
+ import { ProductSchema } from "@reactionary/core";
2
+ import { CommercetoolsProductFactory } from "./product.factory.js";
3
+ import { withCommercetoolsCapabilities } from "../../core/initialize.js";
4
+ import * as z from "zod";
5
+ import { assertNotAny, assertType } from "./utils.example.js";
6
+ const cache = {};
7
+ const context = {};
8
+ const config = {};
9
+ const ExtendedProductSchema = ProductSchema.safeExtend({
10
+ extendedValue: z.string()
11
+ });
12
+ class ExtendedCommercetoolsProductFactory extends CommercetoolsProductFactory {
13
+ constructor() {
14
+ super(ExtendedProductSchema);
15
+ }
16
+ }
17
+ const capabilityFactory = withCommercetoolsCapabilities(config, {
18
+ product: {
19
+ enabled: true,
20
+ factory: new ExtendedCommercetoolsProductFactory()
21
+ }
22
+ });
23
+ const client = capabilityFactory(cache, context);
24
+ client.product.getById({
25
+ identifier: { key: "p-1" }
26
+ }).then((x) => {
27
+ assertNotAny(x);
28
+ if (x.success) {
29
+ assertNotAny(x.value);
30
+ assertNotAny(x.value.extendedValue);
31
+ assertType(x.value.extendedValue);
32
+ }
33
+ });