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