@reactionary/provider-commercetools 0.6.1 → 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,274 @@
1
+ import {
2
+ CartIdentifierSchema,
3
+ CartSchema,
4
+ CategoryPaginatedResultSchema,
5
+ CategorySchema,
6
+ CheckoutSchema,
7
+ IdentitySchema,
8
+ InventorySchema,
9
+ OrderSearchResultSchema,
10
+ OrderSchema,
11
+ PaymentMethodSchema,
12
+ ProductAssociationSchema,
13
+ ProductListItemPaginatedResultsSchema,
14
+ ProductListItemSchema,
15
+ ProductListPaginatedResultsSchema,
16
+ ProductListSchema,
17
+ ProductRatingSummarySchema,
18
+ ProductReviewPaginatedResultSchema,
19
+ ProductReviewSchema,
20
+ ProductSchema,
21
+ ProductSearchResultSchema,
22
+ ProfileSchema,
23
+ PriceSchema,
24
+ ShippingMethodSchema,
25
+ StoreSchema
26
+ } from "@reactionary/core";
27
+ import {
28
+ } from "../schema/capabilities.schema.js";
29
+ import { CommercetoolsCartFactory } from "../factories/cart/cart.factory.js";
30
+ import { CommercetoolsCategoryFactory } from "../factories/category/category.factory.js";
31
+ import { CommercetoolsCheckoutFactory } from "../factories/checkout/checkout.factory.js";
32
+ import { CommercetoolsIdentityFactory } from "../factories/identity/identity.factory.js";
33
+ import { CommercetoolsInventoryFactory } from "../factories/inventory/inventory.factory.js";
34
+ import { CommercetoolsOrderFactory } from "../factories/order/order.factory.js";
35
+ import { CommercetoolsOrderSearchFactory } from "../factories/order-search/order-search.factory.js";
36
+ import { CommercetoolsPriceFactory } from "../factories/price/price.factory.js";
37
+ import { CommercetoolsProductAssociationsFactory } from "../factories/product-associations/product-associations.factory.js";
38
+ import { CommercetoolsProductListFactory } from "../factories/product-list/product-list.factory.js";
39
+ import { CommercetoolsProductFactory } from "../factories/product/product.factory.js";
40
+ import { CommercetoolsProductReviewsFactory } from "../factories/product-reviews/product-reviews.factory.js";
41
+ import { CommercetoolsProductSearchFactory } from "../factories/product-search/product-search.factory.js";
42
+ import { CommercetoolsProfileFactory } from "../factories/profile/profile.factory.js";
43
+ import { CommercetoolsStoreFactory } from "../factories/store/store.factory.js";
44
+ import { CommercetoolsCartProvider } from "../providers/cart.provider.js";
45
+ import { CommercetoolsCategoryProvider } from "../providers/category.provider.js";
46
+ import { CommercetoolsCheckoutProvider } from "../providers/checkout.provider.js";
47
+ import { CommercetoolsIdentityProvider } from "../providers/identity.provider.js";
48
+ import { CommercetoolsInventoryProvider } from "../providers/inventory.provider.js";
49
+ import { CommercetoolsOrderProvider } from "../providers/order.provider.js";
50
+ import { CommercetoolsOrderSearchProvider } from "../providers/order-search.provider.js";
51
+ import { CommercetoolsPriceProvider } from "../providers/price.provider.js";
52
+ import { CommercetoolsProductAssociationsProvider } from "../providers/product-associations.provider.js";
53
+ import { CommercetoolsProductListProvider } from "../providers/product-list.provider.js";
54
+ import { CommercetoolsProductProvider } from "../providers/product.provider.js";
55
+ import { CommercetoolsProductReviewsProvider } from "../providers/product-reviews.provider.js";
56
+ import { CommercetoolsSearchProvider } from "../providers/product-search.provider.js";
57
+ import { CommercetoolsProfileProvider } from "../providers/profile.provider.js";
58
+ import { CommercetoolsStoreProvider } from "../providers/store.provider.js";
59
+ const capabilityKeys = [
60
+ "product",
61
+ "profile",
62
+ "productSearch",
63
+ "productAssociations",
64
+ "productList",
65
+ "productReviews",
66
+ "identity",
67
+ "cart",
68
+ "inventory",
69
+ "price",
70
+ "category",
71
+ "checkout",
72
+ "store",
73
+ "order",
74
+ "orderSearch"
75
+ ];
76
+ const capabilityDescriptors = {
77
+ product: {
78
+ isEnabled: (caps) => caps.product?.enabled,
79
+ getOverride: (caps) => caps.product,
80
+ createDefaultFactory: () => new CommercetoolsProductFactory(ProductSchema),
81
+ createDefaultProvider: (args) => new CommercetoolsProductProvider(
82
+ args.cache,
83
+ args.context,
84
+ args.config,
85
+ args.commercetoolsApi,
86
+ args.factory
87
+ )
88
+ },
89
+ profile: {
90
+ isEnabled: (caps) => caps.profile?.enabled,
91
+ getOverride: (caps) => caps.profile,
92
+ createDefaultFactory: () => new CommercetoolsProfileFactory(ProfileSchema),
93
+ createDefaultProvider: (args) => new CommercetoolsProfileProvider(
94
+ args.config,
95
+ args.cache,
96
+ args.context,
97
+ args.commercetoolsApi,
98
+ args.factory
99
+ )
100
+ },
101
+ productSearch: {
102
+ isEnabled: (caps) => caps.productSearch?.enabled,
103
+ getOverride: (caps) => caps.productSearch,
104
+ createDefaultFactory: () => new CommercetoolsProductSearchFactory(ProductSearchResultSchema),
105
+ createDefaultProvider: (args) => new CommercetoolsSearchProvider(
106
+ args.config,
107
+ args.cache,
108
+ args.context,
109
+ args.commercetoolsApi,
110
+ args.factory
111
+ )
112
+ },
113
+ productAssociations: {
114
+ isEnabled: (caps) => caps.productAssociations?.enabled,
115
+ getOverride: (caps) => caps.productAssociations,
116
+ createDefaultFactory: () => new CommercetoolsProductAssociationsFactory(ProductAssociationSchema),
117
+ createDefaultProvider: (args) => new CommercetoolsProductAssociationsProvider(
118
+ args.config,
119
+ args.cache,
120
+ args.context,
121
+ args.commercetoolsApi,
122
+ args.factory
123
+ )
124
+ },
125
+ productList: {
126
+ isEnabled: (caps) => caps.productList?.enabled,
127
+ getOverride: (caps) => caps.productList,
128
+ createDefaultFactory: () => new CommercetoolsProductListFactory(
129
+ ProductListSchema,
130
+ ProductListItemSchema,
131
+ ProductListPaginatedResultsSchema,
132
+ ProductListItemPaginatedResultsSchema
133
+ ),
134
+ createDefaultProvider: (args) => new CommercetoolsProductListProvider(
135
+ args.config,
136
+ args.cache,
137
+ args.context,
138
+ args.commercetoolsApi,
139
+ args.factory
140
+ )
141
+ },
142
+ productReviews: {
143
+ isEnabled: (caps) => caps.productReviews?.enabled,
144
+ getOverride: (caps) => caps.productReviews,
145
+ createDefaultFactory: () => new CommercetoolsProductReviewsFactory(
146
+ ProductRatingSummarySchema,
147
+ ProductReviewSchema,
148
+ ProductReviewPaginatedResultSchema
149
+ ),
150
+ createDefaultProvider: (args) => new CommercetoolsProductReviewsProvider(
151
+ args.config,
152
+ args.cache,
153
+ args.context,
154
+ args.commercetoolsApi,
155
+ args.factory
156
+ )
157
+ },
158
+ identity: {
159
+ isEnabled: (caps) => caps.identity?.enabled,
160
+ getOverride: (caps) => caps.identity,
161
+ createDefaultFactory: () => new CommercetoolsIdentityFactory(IdentitySchema),
162
+ createDefaultProvider: (args) => new CommercetoolsIdentityProvider(
163
+ args.config,
164
+ args.cache,
165
+ args.context,
166
+ args.commercetoolsApi,
167
+ args.factory
168
+ )
169
+ },
170
+ cart: {
171
+ isEnabled: (caps) => caps.cart?.enabled,
172
+ getOverride: (caps) => caps.cart,
173
+ createDefaultFactory: () => new CommercetoolsCartFactory(CartSchema, CartIdentifierSchema),
174
+ createDefaultProvider: (args) => new CommercetoolsCartProvider(
175
+ args.config,
176
+ args.cache,
177
+ args.context,
178
+ args.commercetoolsApi,
179
+ args.factory
180
+ )
181
+ },
182
+ inventory: {
183
+ isEnabled: (caps) => caps.inventory?.enabled,
184
+ getOverride: (caps) => caps.inventory,
185
+ createDefaultFactory: () => new CommercetoolsInventoryFactory(InventorySchema),
186
+ createDefaultProvider: (args) => new CommercetoolsInventoryProvider(
187
+ args.config,
188
+ args.cache,
189
+ args.context,
190
+ args.commercetoolsApi,
191
+ args.factory
192
+ )
193
+ },
194
+ price: {
195
+ isEnabled: (caps) => caps.price?.enabled,
196
+ getOverride: (caps) => caps.price,
197
+ createDefaultFactory: () => new CommercetoolsPriceFactory(PriceSchema),
198
+ createDefaultProvider: (args) => new CommercetoolsPriceProvider(
199
+ args.config,
200
+ args.cache,
201
+ args.context,
202
+ args.commercetoolsApi,
203
+ args.factory
204
+ )
205
+ },
206
+ category: {
207
+ isEnabled: (caps) => caps.category?.enabled,
208
+ getOverride: (caps) => caps.category,
209
+ createDefaultFactory: () => new CommercetoolsCategoryFactory(CategorySchema, CategoryPaginatedResultSchema),
210
+ createDefaultProvider: (args) => new CommercetoolsCategoryProvider(
211
+ args.config,
212
+ args.cache,
213
+ args.context,
214
+ args.commercetoolsApi,
215
+ args.factory
216
+ )
217
+ },
218
+ checkout: {
219
+ isEnabled: (caps) => caps.checkout?.enabled,
220
+ getOverride: (caps) => caps.checkout,
221
+ createDefaultFactory: () => new CommercetoolsCheckoutFactory(
222
+ CheckoutSchema,
223
+ ShippingMethodSchema,
224
+ PaymentMethodSchema
225
+ ),
226
+ createDefaultProvider: (args) => new CommercetoolsCheckoutProvider(
227
+ args.config,
228
+ args.cache,
229
+ args.context,
230
+ args.commercetoolsApi,
231
+ args.factory
232
+ )
233
+ },
234
+ store: {
235
+ isEnabled: (caps) => caps.store?.enabled,
236
+ getOverride: (caps) => caps.store,
237
+ createDefaultFactory: () => new CommercetoolsStoreFactory(StoreSchema),
238
+ createDefaultProvider: (args) => new CommercetoolsStoreProvider(
239
+ args.config,
240
+ args.cache,
241
+ args.context,
242
+ args.commercetoolsApi,
243
+ args.factory
244
+ )
245
+ },
246
+ order: {
247
+ isEnabled: (caps) => caps.order?.enabled,
248
+ getOverride: (caps) => caps.order,
249
+ createDefaultFactory: () => new CommercetoolsOrderFactory(OrderSchema),
250
+ createDefaultProvider: (args) => new CommercetoolsOrderProvider(
251
+ args.config,
252
+ args.cache,
253
+ args.context,
254
+ args.commercetoolsApi,
255
+ args.factory
256
+ )
257
+ },
258
+ orderSearch: {
259
+ isEnabled: (caps) => caps.orderSearch?.enabled,
260
+ getOverride: (caps) => caps.orderSearch,
261
+ createDefaultFactory: () => new CommercetoolsOrderSearchFactory(OrderSearchResultSchema),
262
+ createDefaultProvider: (args) => new CommercetoolsOrderSearchProvider(
263
+ args.config,
264
+ args.cache,
265
+ args.context,
266
+ args.commercetoolsApi,
267
+ args.factory
268
+ )
269
+ }
270
+ };
271
+ export {
272
+ capabilityDescriptors,
273
+ capabilityKeys
274
+ };
@@ -1,151 +1,42 @@
1
1
  import {
2
2
  CommercetoolsCapabilitiesSchema
3
3
  } from "../schema/capabilities.schema.js";
4
- import { CommercetoolsSearchProvider } from "../providers/product-search.provider.js";
5
- import { CommercetoolsProductProvider } from "../providers/product.provider.js";
6
4
  import {
7
5
  CommercetoolsConfigurationSchema
8
6
  } from "../schema/configuration.schema.js";
9
- import { CommercetoolsIdentityProvider } from "../providers/identity.provider.js";
10
- import { CommercetoolsCartProvider } from "../providers/cart.provider.js";
11
- import { CommercetoolsInventoryProvider } from "../providers/inventory.provider.js";
12
- import { CommercetoolsPriceProvider } from "../providers/price.provider.js";
13
- import { CommercetoolsCategoryProvider } from "../providers/category.provider.js";
14
- import {
15
- CommercetoolsCheckoutProvider,
16
- CommercetoolsOrderProvider,
17
- CommercetoolsOrderSearchProvider,
18
- CommercetoolsProfileProvider,
19
- CommercetoolsStoreProvider,
20
- CommercetoolsProductReviewsProvider,
21
- CommercetoolsProductAssociationsProvider,
22
- CommercetoolsProductListProvider
23
- } from "../providers/index.js";
24
7
  import { CommercetoolsAPI } from "./client.js";
8
+ import {
9
+ capabilityDescriptors,
10
+ capabilityKeys
11
+ } from "./capability-descriptors.js";
12
+ import {
13
+ resolveCapabilityProvider
14
+ } from "./initialize.types.js";
25
15
  function withCommercetoolsCapabilities(configuration, capabilities) {
26
16
  return (cache, context) => {
27
17
  const client = {};
28
18
  const config = CommercetoolsConfigurationSchema.parse(configuration);
29
19
  const caps = CommercetoolsCapabilitiesSchema.parse(capabilities);
30
20
  const commercetoolsApi = new CommercetoolsAPI(config, context);
31
- if (caps.product) {
32
- client.product = new CommercetoolsProductProvider(
33
- config,
34
- cache,
35
- context,
36
- commercetoolsApi
37
- );
38
- }
39
- if (caps.profile) {
40
- client.profile = new CommercetoolsProfileProvider(
41
- config,
42
- cache,
43
- context,
44
- commercetoolsApi
45
- );
46
- }
47
- if (caps.productSearch) {
48
- client.productSearch = new CommercetoolsSearchProvider(
49
- config,
50
- cache,
51
- context,
52
- commercetoolsApi
53
- );
54
- }
55
- if (caps.productAssociations) {
56
- client.productAssociations = new CommercetoolsProductAssociationsProvider(
57
- config,
58
- cache,
59
- context,
60
- commercetoolsApi
61
- );
62
- }
63
- if (caps.productList) {
64
- client.productList = new CommercetoolsProductListProvider(
65
- config,
66
- cache,
67
- context,
68
- commercetoolsApi
69
- );
70
- }
71
- if (caps.productReviews) {
72
- client.productReviews = new CommercetoolsProductReviewsProvider(
73
- config,
74
- cache,
75
- context,
76
- commercetoolsApi
77
- );
78
- }
79
- if (caps.identity) {
80
- client.identity = new CommercetoolsIdentityProvider(
81
- config,
82
- cache,
83
- context,
84
- commercetoolsApi
85
- );
86
- }
87
- if (caps.cart) {
88
- client.cart = new CommercetoolsCartProvider(
89
- config,
90
- cache,
91
- context,
92
- commercetoolsApi
93
- );
94
- }
95
- if (caps.inventory) {
96
- client.inventory = new CommercetoolsInventoryProvider(
97
- config,
98
- cache,
99
- context,
100
- commercetoolsApi
101
- );
102
- }
103
- if (caps.price) {
104
- client.price = new CommercetoolsPriceProvider(
105
- config,
106
- cache,
107
- context,
108
- commercetoolsApi
109
- );
110
- }
111
- if (caps.category) {
112
- client.category = new CommercetoolsCategoryProvider(
113
- config,
114
- cache,
115
- context,
116
- commercetoolsApi
117
- );
118
- }
119
- if (caps.checkout) {
120
- client.checkout = new CommercetoolsCheckoutProvider(
121
- config,
122
- cache,
123
- context,
124
- commercetoolsApi
125
- );
126
- }
127
- if (caps.store) {
128
- client.store = new CommercetoolsStoreProvider(
129
- config,
130
- cache,
131
- context,
132
- commercetoolsApi
133
- );
134
- }
135
- if (caps.order) {
136
- client.store = new CommercetoolsOrderProvider(
137
- config,
138
- cache,
139
- context,
140
- commercetoolsApi
141
- );
142
- }
143
- if (caps.orderSearch) {
144
- client.orderSearch = new CommercetoolsOrderSearchProvider(
145
- config,
146
- cache,
147
- context,
148
- commercetoolsApi
21
+ const buildProviderArgs = (factory) => ({
22
+ cache,
23
+ context,
24
+ config,
25
+ commercetoolsApi,
26
+ factory
27
+ });
28
+ for (const key of capabilityKeys) {
29
+ const descriptor = capabilityDescriptors[key];
30
+ if (!descriptor.isEnabled(caps)) {
31
+ continue;
32
+ }
33
+ client[key] = resolveCapabilityProvider(
34
+ descriptor.getOverride(capabilities),
35
+ {
36
+ factory: descriptor.createDefaultFactory(),
37
+ provider: descriptor.createDefaultProvider
38
+ },
39
+ buildProviderArgs
149
40
  );
150
41
  }
151
42
  return client;
@@ -0,0 +1,8 @@
1
+ function resolveCapabilityProvider(capability, defaults, buildProviderArgs) {
2
+ const factory = capability?.factory ?? defaults.factory;
3
+ const provider = capability?.provider ?? defaults.provider;
4
+ return provider(buildProviderArgs(factory));
5
+ }
6
+ export {
7
+ resolveCapabilityProvider
8
+ };
@@ -0,0 +1,108 @@
1
+ import {
2
+ CartItemSchema
3
+ } from "@reactionary/core";
4
+ class CommercetoolsCartFactory {
5
+ constructor(cartSchema, cartIdentifierSchema) {
6
+ this.cartSchema = cartSchema;
7
+ this.cartIdentifierSchema = cartIdentifierSchema;
8
+ }
9
+ parseCartIdentifier(_context, data) {
10
+ return this.cartIdentifierSchema.parse({
11
+ key: data.key || ""
12
+ });
13
+ }
14
+ parseCart(context, data) {
15
+ const identifier = this.parseCartIdentifier(context, {
16
+ key: data.id
17
+ });
18
+ const grandTotal = data.totalPrice.centAmount || 0;
19
+ const shippingTotal = data.shippingInfo?.price.centAmount || 0;
20
+ const productTotal = grandTotal - shippingTotal;
21
+ const taxTotal = data.taxedPrice?.totalTax?.centAmount || 0;
22
+ const discountTotal = data.discountOnTotalPrice?.discountedAmount.centAmount || 0;
23
+ const surchargeTotal = 0;
24
+ const currency = data.totalPrice.currencyCode;
25
+ const price = {
26
+ totalTax: {
27
+ value: taxTotal / 100,
28
+ currency
29
+ },
30
+ totalDiscount: {
31
+ value: discountTotal / 100,
32
+ currency
33
+ },
34
+ totalSurcharge: {
35
+ value: surchargeTotal / 100,
36
+ currency
37
+ },
38
+ totalShipping: {
39
+ value: shippingTotal / 100,
40
+ currency
41
+ },
42
+ totalProductPrice: {
43
+ value: productTotal / 100,
44
+ currency
45
+ },
46
+ grandTotal: {
47
+ value: grandTotal / 100,
48
+ currency
49
+ }
50
+ };
51
+ const items = [];
52
+ for (const lineItem of data.lineItems) {
53
+ items.push(this.parseCartItem(lineItem));
54
+ }
55
+ const result = {
56
+ identifier,
57
+ userId: {
58
+ userId: "???"
59
+ },
60
+ name: data.custom?.fields["name"] || "",
61
+ description: data.custom?.fields["description"] || "",
62
+ price,
63
+ items,
64
+ appliedPromotions: []
65
+ };
66
+ return this.cartSchema.parse(result);
67
+ }
68
+ parseCartItem(lineItem) {
69
+ const unitPrice = lineItem.price.value.centAmount;
70
+ const totalPrice = lineItem.totalPrice.centAmount || 0;
71
+ const totalDiscount = lineItem.price.discounted?.value.centAmount || 0;
72
+ const unitDiscount = totalDiscount / lineItem.quantity;
73
+ const currency = lineItem.price.value.currencyCode.toUpperCase();
74
+ return CartItemSchema.parse({
75
+ identifier: {
76
+ key: lineItem.id
77
+ },
78
+ product: {
79
+ key: lineItem.productId
80
+ },
81
+ variant: {
82
+ sku: lineItem.variant.sku || ""
83
+ },
84
+ quantity: lineItem.quantity,
85
+ price: {
86
+ unitPrice: {
87
+ value: unitPrice / 100,
88
+ currency
89
+ },
90
+ unitDiscount: {
91
+ value: unitDiscount / 100,
92
+ currency
93
+ },
94
+ totalPrice: {
95
+ value: totalPrice / 100,
96
+ currency
97
+ },
98
+ totalDiscount: {
99
+ value: totalDiscount / 100,
100
+ currency
101
+ }
102
+ }
103
+ });
104
+ }
105
+ }
106
+ export {
107
+ CommercetoolsCartFactory
108
+ };
@@ -0,0 +1,38 @@
1
+ import {
2
+ } from "@reactionary/core";
3
+ class CommercetoolsCategoryFactory {
4
+ constructor(categorySchema, categoryPaginatedResultSchema) {
5
+ this.categorySchema = categorySchema;
6
+ this.categoryPaginatedResultSchema = categoryPaginatedResultSchema;
7
+ }
8
+ parseCategory(context, data) {
9
+ const identifier = { key: data.key || "" };
10
+ const result = {
11
+ identifier,
12
+ name: data.name[context.languageContext.locale] || "No Name",
13
+ slug: data.slug ? data.slug[context.languageContext.locale] || "" : "",
14
+ text: data.description ? data.description[context.languageContext.locale] || "" : "",
15
+ parentCategory: data.parent && data.parent.obj && data.parent.obj?.key ? { key: data.parent.obj.key } : void 0,
16
+ images: (data.assets || []).filter((asset) => asset.sources.length > 0).filter((asset) => asset.sources[0].contentType?.startsWith("image/")).map((asset) => ({
17
+ sourceUrl: asset.sources[0].uri,
18
+ altText: asset.description?.[context.languageContext.locale] || asset.name[context.languageContext.locale] || "",
19
+ height: asset.sources[0].dimensions?.h || 0,
20
+ width: asset.sources[0].dimensions?.w || 0
21
+ }))
22
+ };
23
+ return this.categorySchema.parse(result);
24
+ }
25
+ parseCategoryPaginatedResult(context, data) {
26
+ const result = {
27
+ pageNumber: Math.floor(data.offset / data.count) + 1,
28
+ pageSize: data.count,
29
+ totalCount: data.total || 0,
30
+ totalPages: Math.ceil((data.total ?? 0) / data.count),
31
+ items: data.results.map((category) => this.parseCategory(context, category))
32
+ };
33
+ return this.categoryPaginatedResultSchema.parse(result);
34
+ }
35
+ }
36
+ export {
37
+ CommercetoolsCategoryFactory
38
+ };
@@ -0,0 +1,67 @@
1
+ import {
2
+ CheckoutSchema,
3
+ PaymentMethodSchema,
4
+ ShippingMethodSchema,
5
+ unwrapValue
6
+ } from "@reactionary/core";
7
+ import { withCommercetoolsCapabilities } from "../../core/initialize.js";
8
+ import { CommercetoolsCheckoutProvider } from "../../providers/checkout.provider.js";
9
+ import { assertNotAny, assertType } from "../product/utils.example.js";
10
+ import { CommercetoolsCheckoutFactory } from "./checkout.factory.js";
11
+ import * as z from "zod";
12
+ const cache = {};
13
+ const context = {};
14
+ const config = {};
15
+ const ExtendedCheckoutSchema = CheckoutSchema.safeExtend({
16
+ extendedValue: z.string().default("")
17
+ });
18
+ class ExtendedCommercetoolsCheckoutFactory extends CommercetoolsCheckoutFactory {
19
+ constructor() {
20
+ super(ExtendedCheckoutSchema, ShippingMethodSchema, PaymentMethodSchema);
21
+ }
22
+ parseCheckout(context2, data) {
23
+ const base = super.parseCheckout(context2, data);
24
+ return {
25
+ ...base,
26
+ extendedValue: "from-factory"
27
+ };
28
+ }
29
+ }
30
+ class ExtendedCommercetoolsCheckoutProvider extends CommercetoolsCheckoutProvider {
31
+ async getByIdOrThrow(payload) {
32
+ const result = await this.getById(payload);
33
+ return unwrapValue(result);
34
+ }
35
+ }
36
+ const extendedFactory = new ExtendedCommercetoolsCheckoutFactory();
37
+ const capabilityFactory = withCommercetoolsCapabilities(config, {
38
+ checkout: {
39
+ enabled: true,
40
+ factory: extendedFactory,
41
+ provider: ({ cache: cache2, context: context2, config: config2, commercetoolsApi }) => new ExtendedCommercetoolsCheckoutProvider(
42
+ config2,
43
+ cache2,
44
+ context2,
45
+ commercetoolsApi,
46
+ extendedFactory
47
+ )
48
+ }
49
+ });
50
+ const client = capabilityFactory(cache, context);
51
+ client.checkout.getById({
52
+ identifier: { key: "checkout-1" }
53
+ }).then((result) => {
54
+ assertNotAny(result);
55
+ if (result.success) {
56
+ assertNotAny(result.value);
57
+ assertNotAny(result.value.extendedValue);
58
+ assertType(result.value.extendedValue);
59
+ }
60
+ });
61
+ client.checkout.getByIdOrThrow({
62
+ identifier: { key: "checkout-2" }
63
+ }).then((checkout) => {
64
+ assertNotAny(checkout);
65
+ assertNotAny(checkout.extendedValue);
66
+ assertType(checkout.extendedValue);
67
+ });