@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
@@ -10,19 +10,11 @@ var __decorateClass = (decorators, target, key, kind) => {
10
10
  return result;
11
11
  };
12
12
  import {
13
- FacetIdentifierSchema,
14
13
  FacetValueIdentifierSchema,
15
- ImageSchema,
16
- ProductOptionIdentifierSchema,
17
14
  ProductSearchProvider,
18
15
  ProductSearchQueryByTermSchema,
19
16
  ProductSearchQueryCreateNavigationFilterSchema,
20
- ProductSearchResultFacetSchema,
21
- ProductSearchResultFacetValueSchema,
22
- ProductSearchResultItemVariantSchema,
23
17
  ProductSearchResultSchema,
24
- ProductVariantIdentifierSchema,
25
- ProductVariantOptionSchema,
26
18
  Reactionary,
27
19
  success
28
20
  } from "@reactionary/core";
@@ -30,10 +22,11 @@ import createDebug from "debug";
30
22
  import { CommercetoolsCategoryLookupSchema, CommercetoolsResolveCategoryQueryByKeySchema } from "../schema/commercetools.schema.js";
31
23
  const debug = createDebug("reactionary:commercetools:search");
32
24
  class CommercetoolsSearchProvider extends ProductSearchProvider {
33
- constructor(config, cache, context, commercetools) {
25
+ constructor(config, cache, context, commercetools, factory) {
34
26
  super(cache, context);
35
27
  this.config = config;
36
28
  this.commercetools = commercetools;
29
+ this.factory = factory;
37
30
  }
38
31
  async getClient() {
39
32
  const client = await this.commercetools.getClient();
@@ -228,7 +221,8 @@ class CommercetoolsSearchProvider extends ProductSearchProvider {
228
221
  }
229
222
  }).execute();
230
223
  const responseBody = response.body;
231
- const result = this.parsePaginatedResult(
224
+ const result = this.factory.parseSearchResult(
225
+ this.context,
232
226
  responseBody,
233
227
  payload
234
228
  );
@@ -272,108 +266,6 @@ class CommercetoolsSearchProvider extends ProductSearchProvider {
272
266
  }
273
267
  }
274
268
  }
275
- parseSingle(body) {
276
- const identifier = { key: body.id };
277
- const name = body.name[this.context.languageContext.locale] || body.id;
278
- const slug = body.slug?.[this.context.languageContext.locale] || body.id;
279
- const variants = [body.masterVariant, ...body.variants].map(
280
- (variant) => this.parseVariant(variant, body)
281
- );
282
- const product = {
283
- identifier,
284
- name,
285
- slug,
286
- variants
287
- };
288
- return product;
289
- }
290
- parsePaginatedResult(body, query) {
291
- const identifier = {
292
- ...query.search
293
- };
294
- const products = body.results.map(
295
- (p) => this.parseSingle(p.productProjection)
296
- );
297
- const facets = [];
298
- for (const facet of body.facets) {
299
- const facetIdentifier = FacetIdentifierSchema.parse({
300
- key: facet.name
301
- });
302
- const candidateFacet = this.parseFacet(facetIdentifier, facet);
303
- if (candidateFacet.values.length > 0) {
304
- facets.push(candidateFacet);
305
- }
306
- }
307
- const result = {
308
- identifier,
309
- pageNumber: (Math.ceil(body.offset / body.limit) || 0) + 1,
310
- pageSize: body.limit,
311
- totalCount: body.total || 0,
312
- totalPages: Math.ceil((body.total || 0) / body.limit || 0) + 1,
313
- items: products,
314
- facets
315
- };
316
- return result;
317
- }
318
- /**
319
- * See version 0.0.81 for ProductProjection based facet parsing
320
- * @param facetIdentifier
321
- * @param facetValue
322
- * @returns
323
- */
324
- parseFacet(facetIdentifier, facet) {
325
- const result = ProductSearchResultFacetSchema.parse({
326
- identifier: facetIdentifier,
327
- name: facet.name,
328
- values: []
329
- });
330
- const distinctFacet = facet;
331
- if (distinctFacet) {
332
- distinctFacet.buckets.forEach((bucket) => {
333
- const facetValueIdentifier = FacetValueIdentifierSchema.parse({
334
- facet: facetIdentifier,
335
- key: bucket.key
336
- });
337
- result.values.push(
338
- this.parseFacetValue(facetValueIdentifier, bucket.key, bucket.count)
339
- );
340
- });
341
- }
342
- return result;
343
- }
344
- parseFacetValue(facetValueIdentifier, label, count) {
345
- return ProductSearchResultFacetValueSchema.parse({
346
- identifier: facetValueIdentifier,
347
- name: label,
348
- count,
349
- active: false
350
- });
351
- }
352
- parseVariant(variant, product) {
353
- const sourceImage = variant.images?.[0];
354
- const img = ImageSchema.parse({
355
- sourceUrl: sourceImage?.url || "",
356
- height: sourceImage?.dimensions.h || void 0,
357
- width: sourceImage?.dimensions.w || void 0,
358
- altText: sourceImage?.label || product.name[this.context.languageContext.locale] || void 0
359
- });
360
- const mappedOptions = variant.attributes?.filter((x) => x.name === "Color").map(
361
- (opt) => ProductVariantOptionSchema.parse({
362
- identifier: ProductOptionIdentifierSchema.parse({
363
- key: opt.name
364
- }),
365
- name: opt.value || ""
366
- })
367
- ) || [];
368
- const mappedOption = mappedOptions?.[0];
369
- return ProductSearchResultItemVariantSchema.parse({
370
- variant: ProductVariantIdentifierSchema.parse({
371
- sku: variant.sku || ""
372
- }),
373
- image: img,
374
- options: mappedOption
375
- });
376
- }
377
269
  }
378
270
  __decorateClass([
379
271
  Reactionary({
@@ -10,11 +10,6 @@ var __decorateClass = (decorators, target, key, kind) => {
10
10
  return result;
11
11
  };
12
12
  import {
13
- ImageSchema,
14
- ProductAttributeIdentifierSchema,
15
- ProductAttributeSchema,
16
- ProductAttributeValueIdentifierSchema,
17
- ProductAttributeValueSchema,
18
13
  ProductProvider,
19
14
  ProductQueryByIdSchema,
20
15
  ProductQueryBySKUSchema,
@@ -25,10 +20,11 @@ import {
25
20
  error
26
21
  } from "@reactionary/core";
27
22
  class CommercetoolsProductProvider extends ProductProvider {
28
- constructor(config, cache, context, commercetools) {
23
+ constructor(cache, context, config, commercetools, factory) {
29
24
  super(cache, context);
30
25
  this.config = config;
31
26
  this.commercetools = commercetools;
27
+ this.factory = factory;
32
28
  }
33
29
  async getClient() {
34
30
  const client = await this.commercetools.getClient();
@@ -37,7 +33,7 @@ class CommercetoolsProductProvider extends ProductProvider {
37
33
  async getById(payload) {
38
34
  const client = await this.getClient();
39
35
  const remote = await client.withKey({ key: payload.identifier.key }).get().execute();
40
- const value = this.parseSingle(remote.body);
36
+ const value = this.factory.parseProduct(this.context, remote.body);
41
37
  return success(value);
42
38
  }
43
39
  async getBySlug(payload) {
@@ -55,7 +51,7 @@ class CommercetoolsProductProvider extends ProductProvider {
55
51
  identifier: payload.slug
56
52
  });
57
53
  }
58
- const result = this.parseSingle(remote.body.results[0]);
54
+ const result = this.factory.parseProduct(this.context, remote.body.results[0]);
59
55
  return success(result);
60
56
  }
61
57
  async getBySKU(payload) {
@@ -68,139 +64,9 @@ class CommercetoolsProductProvider extends ProductProvider {
68
64
  "var.skus": [payload].map((p) => p.variant.sku)
69
65
  }
70
66
  }).execute();
71
- const result = this.parseSingle(remote.body.results[0]);
67
+ const result = this.factory.parseProduct(this.context, remote.body.results[0]);
72
68
  return success(result);
73
69
  }
74
- parseSingle(data) {
75
- const identifier = { key: data.key || data.id };
76
- const name = data.name[this.context.languageContext.locale];
77
- const slug = data.slug[this.context.languageContext.locale];
78
- let description = "";
79
- if (data.description) {
80
- description = data.description[this.context.languageContext.locale];
81
- }
82
- const variantLevelAttributes = data.masterVariant.attributes?.map((x) => this.parseAttribute(x)) || [];
83
- const specialAttributes = [
84
- "reactionaryaccessories",
85
- "reactionaryspareparts",
86
- "reactionaryreplacements"
87
- ];
88
- const productLevelAttributes = data.attributes.filter((x) => !specialAttributes.includes(x.name)).map((x) => this.parseAttribute(x)) || [];
89
- const sharedAttributes = [
90
- ...productLevelAttributes,
91
- ...variantLevelAttributes
92
- ];
93
- const mainVariant = this.parseVariant(data.masterVariant, data);
94
- const otherVariants = [];
95
- for (const variant of data.variants || []) {
96
- if (variant.id !== data.masterVariant.id) {
97
- otherVariants.push(this.parseVariant(variant, data));
98
- }
99
- }
100
- const result = {
101
- identifier,
102
- name,
103
- slug,
104
- description,
105
- sharedAttributes,
106
- mainVariant,
107
- brand: "",
108
- longDescription: "",
109
- manufacturer: "",
110
- options: [],
111
- parentCategories: [],
112
- published: true,
113
- variants: otherVariants
114
- };
115
- return result;
116
- }
117
- /**
118
- * Return true, if the attribute is a defining attribute (ie an option)
119
- * @param attr a variant attribute
120
- * @returns true if the attribute is an option
121
- */
122
- isVariantAttributeAnOption(attr) {
123
- return true;
124
- }
125
- parseVariant(variant, product) {
126
- const identifier = {
127
- sku: variant.sku
128
- };
129
- const images = [
130
- ...(variant.images || []).map(
131
- (img) => ImageSchema.parse({
132
- sourceUrl: img.url,
133
- altText: img.label || "",
134
- width: img.dimensions?.w,
135
- height: img.dimensions?.h
136
- })
137
- )
138
- ];
139
- const options = (variant.attributes ?? []).filter((attr) => this.isVariantAttributeAnOption(attr)).map((attr) => {
140
- const attrVal = this.parseAttributeValue(attr);
141
- const optionIdentifier = {
142
- key: attr.name
143
- };
144
- const option = {
145
- identifier: optionIdentifier,
146
- name: attr.name,
147
- value: {
148
- identifier: {
149
- key: attrVal.value,
150
- option: optionIdentifier
151
- },
152
- label: attrVal.label
153
- }
154
- };
155
- return option;
156
- }) || [];
157
- const result = {
158
- identifier,
159
- images,
160
- barcode: "",
161
- ean: "",
162
- gtin: "",
163
- name: product.name[this.context.languageContext.locale],
164
- options,
165
- upc: ""
166
- };
167
- return result;
168
- }
169
- parseAttribute(attr) {
170
- const result = ProductAttributeSchema.parse({
171
- identifier: ProductAttributeIdentifierSchema.parse({
172
- key: attr.name
173
- }),
174
- group: "",
175
- name: attr.name,
176
- values: [this.parseAttributeValue(attr)]
177
- });
178
- return result;
179
- }
180
- parseAttributeValue(attr) {
181
- let attrValue = "";
182
- if (attr.value && Array.isArray(attr.value)) {
183
- attrValue = attr.value[0];
184
- }
185
- if (attr.value && typeof attr.value === "object") {
186
- if (this.context.languageContext.locale in attr.value) {
187
- attrValue = attr.value[this.context.languageContext.locale];
188
- } else {
189
- attrValue = "-";
190
- }
191
- }
192
- if (typeof attr.value === "string") {
193
- attrValue = attr.value;
194
- }
195
- const attrVal = ProductAttributeValueSchema.parse({
196
- identifier: ProductAttributeValueIdentifierSchema.parse({
197
- key: attrValue
198
- }),
199
- value: String(attrValue),
200
- label: String(attrValue)
201
- });
202
- return attrVal;
203
- }
204
70
  }
205
71
  __decorateClass([
206
72
  Reactionary({
@@ -21,10 +21,11 @@ import {
21
21
  ProfileMutationAddShippingAddressSchema
22
22
  } from "@reactionary/core";
23
23
  class CommercetoolsProfileProvider extends ProfileProvider {
24
- constructor(config, cache, context, commercetools) {
24
+ constructor(config, cache, context, commercetools, factory) {
25
25
  super(cache, context);
26
26
  this.config = config;
27
27
  this.commercetools = commercetools;
28
+ this.factory = factory;
28
29
  }
29
30
  async getClient() {
30
31
  const client = await this.commercetools.getClient();
@@ -39,7 +40,7 @@ class CommercetoolsProfileProvider extends ProfileProvider {
39
40
  identifier: payload.identifier
40
41
  });
41
42
  }
42
- const model = this.parseSingle(remote.body);
43
+ const model = this.factory.parseProfile(this.context, remote.body);
43
44
  return success(model);
44
45
  }
45
46
  async addShippingAddress(payload) {
@@ -64,7 +65,7 @@ class CommercetoolsProfileProvider extends ProfileProvider {
64
65
  }
65
66
  }).execute();
66
67
  customer = updateResponse.body;
67
- const model = this.parseSingle(customer);
68
+ const model = this.factory.parseProfile(this.context, customer);
68
69
  return success(model);
69
70
  }
70
71
  async updateShippingAddress(payload) {
@@ -97,7 +98,7 @@ class CommercetoolsProfileProvider extends ProfileProvider {
97
98
  }
98
99
  }).execute();
99
100
  customer = updateResponse.body;
100
- const model = this.parseSingle(customer);
101
+ const model = this.factory.parseProfile(this.context, customer);
101
102
  return success(model);
102
103
  }
103
104
  async removeShippingAddress(payload) {
@@ -140,7 +141,7 @@ class CommercetoolsProfileProvider extends ProfileProvider {
140
141
  }
141
142
  }).execute();
142
143
  customer = updateResponse.body;
143
- const model = this.parseSingle(customer);
144
+ const model = this.factory.parseProfile(this.context, customer);
144
145
  return success(model);
145
146
  }
146
147
  async makeShippingAddressDefault(payload) {
@@ -178,7 +179,7 @@ class CommercetoolsProfileProvider extends ProfileProvider {
178
179
  }
179
180
  }).execute();
180
181
  customer = updateResponse.body;
181
- const model = this.parseSingle(customer);
182
+ const model = this.factory.parseProfile(this.context, customer);
182
183
  return success(model);
183
184
  }
184
185
  async setBillingAddress(payload) {
@@ -219,7 +220,7 @@ class CommercetoolsProfileProvider extends ProfileProvider {
219
220
  }).execute();
220
221
  customer = updateResponse.body;
221
222
  }
222
- const model = this.parseSingle(customer);
223
+ const model = this.factory.parseProfile(this.context, customer);
223
224
  return success(model);
224
225
  }
225
226
  async update(payload) {
@@ -274,53 +275,9 @@ class CommercetoolsProfileProvider extends ProfileProvider {
274
275
  }).execute();
275
276
  customer = updateResponse.body;
276
277
  }
277
- const model = this.parseSingle(customer);
278
+ const model = this.factory.parseProfile(this.context, customer);
278
279
  return success(model);
279
280
  }
280
- parseAddress(address) {
281
- const result = {
282
- identifier: {
283
- nickName: address.key || ""
284
- },
285
- firstName: address.firstName || "",
286
- lastName: address.lastName || "",
287
- streetAddress: address.streetName || "",
288
- streetNumber: address.streetNumber || "",
289
- city: address.city || "",
290
- region: address.region || "",
291
- postalCode: address.postalCode || "",
292
- countryCode: address.country
293
- };
294
- return result;
295
- }
296
- parseSingle(body) {
297
- const email = body.email;
298
- const emailVerified = body.isEmailVerified;
299
- let defaultCTBillingAddress = body.addresses.find((addr) => addr.id === body.defaultBillingAddressId);
300
- const phone = defaultCTBillingAddress?.phone ?? "";
301
- if (this.isIncompleteAddress(defaultCTBillingAddress)) {
302
- defaultCTBillingAddress = void 0;
303
- }
304
- const defaultCTShippingAddress = body.addresses.find((addr) => addr.id === body.defaultShippingAddressId);
305
- const alternateShippingAddresses = body.addresses.filter((x) => x.id !== body.defaultBillingAddressId && x.id !== body.defaultShippingAddressId).map((addr) => this.parseAddress(addr));
306
- const billingAddress = defaultCTBillingAddress ? this.parseAddress(defaultCTBillingAddress) : void 0;
307
- const shippingAddress = defaultCTShippingAddress ? this.parseAddress(defaultCTShippingAddress) : void 0;
308
- const result = {
309
- identifier: {
310
- userId: body.id
311
- },
312
- email,
313
- emailVerified,
314
- alternateShippingAddresses,
315
- billingAddress,
316
- shippingAddress,
317
- createdAt: body.createdAt,
318
- phone,
319
- phoneVerified: false,
320
- updatedAt: body.lastModifiedAt
321
- };
322
- return result;
323
- }
324
281
  createCTAddressDraft(address) {
325
282
  return {
326
283
  key: address.identifier.nickName,
@@ -12,10 +12,11 @@ var __decorateClass = (decorators, target, key, kind) => {
12
12
  import { Reactionary, StoreProvider, StoreQueryByProximitySchema, StoreSchema, success, error } from "@reactionary/core";
13
13
  import * as z from "zod";
14
14
  class CommercetoolsStoreProvider extends StoreProvider {
15
- constructor(config, cache, context, commercetools) {
15
+ constructor(config, cache, context, commercetools, factory) {
16
16
  super(cache, context);
17
17
  this.config = config;
18
18
  this.commercetools = commercetools;
19
+ this.factory = factory;
19
20
  }
20
21
  async getClient() {
21
22
  const client = await this.commercetools.getClient();
@@ -31,28 +32,10 @@ class CommercetoolsStoreProvider extends StoreProvider {
31
32
  }).execute();
32
33
  const results = [];
33
34
  for (const r of remote.body.results) {
34
- results.push(this.parseSingle(r));
35
+ results.push(this.factory.parseStore(this.context, r));
35
36
  }
36
37
  return success(results);
37
38
  }
38
- parseSingle(body) {
39
- let name = "";
40
- if (body.name && body.name["la"]) {
41
- name = body.name["la"];
42
- }
43
- const identifier = {
44
- key: body.key
45
- };
46
- const fulfillmentCenter = {
47
- key: body.key
48
- };
49
- const result = {
50
- identifier,
51
- fulfillmentCenter,
52
- name
53
- };
54
- return result;
55
- }
56
39
  }
57
40
  __decorateClass([
58
41
  Reactionary({
@@ -1,4 +1,13 @@
1
1
  import { CapabilitiesSchema } from "@reactionary/core";
2
+ import * as z from "zod";
3
+ const EnabledCapabilitySchema = z.looseObject({
4
+ enabled: z.boolean()
5
+ });
6
+ const OverridableCapabilitySchema = z.looseObject({
7
+ enabled: z.boolean(),
8
+ factory: z.unknown().optional(),
9
+ provider: z.unknown().optional()
10
+ });
2
11
  const CommercetoolsCapabilitiesSchema = CapabilitiesSchema.pick({
3
12
  product: true,
4
13
  productSearch: true,
@@ -15,6 +24,22 @@ const CommercetoolsCapabilitiesSchema = CapabilitiesSchema.pick({
15
24
  category: true,
16
25
  store: true,
17
26
  profile: true
27
+ }).extend({
28
+ productSearch: EnabledCapabilitySchema.optional(),
29
+ productAssociations: EnabledCapabilitySchema.optional(),
30
+ productReviews: EnabledCapabilitySchema.optional(),
31
+ productList: EnabledCapabilitySchema.optional(),
32
+ identity: EnabledCapabilitySchema.optional(),
33
+ cart: EnabledCapabilitySchema.optional(),
34
+ order: EnabledCapabilitySchema.optional(),
35
+ orderSearch: EnabledCapabilitySchema.optional(),
36
+ inventory: EnabledCapabilitySchema.optional(),
37
+ price: EnabledCapabilitySchema.optional(),
38
+ category: EnabledCapabilitySchema.optional(),
39
+ store: EnabledCapabilitySchema.optional(),
40
+ profile: EnabledCapabilitySchema.optional(),
41
+ product: OverridableCapabilitySchema.optional(),
42
+ checkout: OverridableCapabilitySchema.optional()
18
43
  }).partial();
19
44
  export {
20
45
  CommercetoolsCapabilitiesSchema
@@ -0,0 +1,16 @@
1
+ import type * as z from 'zod';
2
+ import { type CommercetoolsCapabilities, type CommercetoolsCapabilitiesSchema } from '../schema/capabilities.schema.js';
3
+ export declare const capabilityKeys: readonly ["product", "profile", "productSearch", "productAssociations", "productList", "productReviews", "identity", "cart", "inventory", "price", "category", "checkout", "store", "order", "orderSearch"];
4
+ export type OverridableCapabilityKey = (typeof capabilityKeys)[number];
5
+ type ParsedCapabilities = z.infer<typeof CommercetoolsCapabilitiesSchema>;
6
+ export type CapabilityDescriptor = {
7
+ isEnabled: (caps: ParsedCapabilities) => boolean | undefined;
8
+ getOverride: (caps: CommercetoolsCapabilities) => {
9
+ factory?: any;
10
+ provider?: (args: any) => any;
11
+ } | undefined;
12
+ createDefaultFactory: () => any;
13
+ createDefaultProvider: (args: any) => any;
14
+ };
15
+ export declare const capabilityDescriptors: Record<OverridableCapabilityKey, CapabilityDescriptor>;
16
+ export {};
@@ -1,4 +1,5 @@
1
- import type { Cache, RequestContext, ClientFromCapabilities } from '@reactionary/core';
1
+ import type { Cache, RequestContext } from '@reactionary/core';
2
2
  import { type CommercetoolsCapabilities } from '../schema/capabilities.schema.js';
3
3
  import { type CommercetoolsConfiguration } from '../schema/configuration.schema.js';
4
- export declare function withCommercetoolsCapabilities<T extends CommercetoolsCapabilities>(configuration: CommercetoolsConfiguration, capabilities: T): (cache: Cache, context: RequestContext) => ClientFromCapabilities<T>;
4
+ import { type CommercetoolsClientFromCapabilities } from './initialize.types.js';
5
+ export declare function withCommercetoolsCapabilities<T extends CommercetoolsCapabilities>(configuration: CommercetoolsConfiguration, capabilities: T): (cache: Cache, context: RequestContext) => CommercetoolsClientFromCapabilities<T>;
@@ -0,0 +1,118 @@
1
+ import type { CartFactory, CategoryFactory, CheckoutFactory, ClientFromCapabilities, IdentityFactory, InventoryFactory, OrderFactory, OrderSearchFactory, PriceFactory, ProductAssociationsFactory, ProductFactory, ProductListFactory, ProductReviewsFactory, ProductSearchFactory, ProfileFactory, StoreFactory } from '@reactionary/core';
2
+ import type { CommercetoolsCapabilities } from '../schema/capabilities.schema.js';
3
+ import type { OverridableCapabilityKey } from './capability-descriptors.js';
4
+ import type { CommercetoolsCartFactory } from '../factories/cart/cart.factory.js';
5
+ import type { CommercetoolsCategoryFactory } from '../factories/category/category.factory.js';
6
+ import type { CommercetoolsCheckoutFactory } from '../factories/checkout/checkout.factory.js';
7
+ import type { CommercetoolsIdentityFactory } from '../factories/identity/identity.factory.js';
8
+ import type { CommercetoolsInventoryFactory } from '../factories/inventory/inventory.factory.js';
9
+ import type { CommercetoolsOrderFactory } from '../factories/order/order.factory.js';
10
+ import type { CommercetoolsOrderSearchFactory } from '../factories/order-search/order-search.factory.js';
11
+ import type { CommercetoolsPriceFactory } from '../factories/price/price.factory.js';
12
+ import type { CommercetoolsProductAssociationsFactory } from '../factories/product-associations/product-associations.factory.js';
13
+ import type { CommercetoolsProductFactory } from '../factories/product/product.factory.js';
14
+ import type { CommercetoolsProductListFactory } from '../factories/product-list/product-list.factory.js';
15
+ import type { CommercetoolsProductReviewsFactory } from '../factories/product-reviews/product-reviews.factory.js';
16
+ import type { CommercetoolsProductSearchFactory } from '../factories/product-search/product-search.factory.js';
17
+ import type { CommercetoolsProfileFactory } from '../factories/profile/profile.factory.js';
18
+ import type { CommercetoolsStoreFactory } from '../factories/store/store.factory.js';
19
+ import type { CommercetoolsCartProvider } from '../providers/cart.provider.js';
20
+ import type { CommercetoolsCategoryProvider } from '../providers/category.provider.js';
21
+ import type { CommercetoolsCheckoutProvider } from '../providers/checkout.provider.js';
22
+ import type { CommercetoolsIdentityProvider } from '../providers/identity.provider.js';
23
+ import type { CommercetoolsInventoryProvider } from '../providers/inventory.provider.js';
24
+ import type { CommercetoolsOrderSearchProvider } from '../providers/order-search.provider.js';
25
+ import type { CommercetoolsOrderProvider } from '../providers/order.provider.js';
26
+ import type { CommercetoolsPriceProvider } from '../providers/price.provider.js';
27
+ import type { CommercetoolsProductAssociationsProvider } from '../providers/product-associations.provider.js';
28
+ import type { CommercetoolsProductListProvider } from '../providers/product-list.provider.js';
29
+ import type { CommercetoolsProductProvider } from '../providers/product.provider.js';
30
+ import type { CommercetoolsProductReviewsProvider } from '../providers/product-reviews.provider.js';
31
+ import type { CommercetoolsSearchProvider } from '../providers/product-search.provider.js';
32
+ import type { CommercetoolsProfileProvider } from '../providers/profile.provider.js';
33
+ import type { CommercetoolsStoreProvider } from '../providers/store.provider.js';
34
+ type EnabledCapability<TCapability> = TCapability extends {
35
+ enabled: true;
36
+ } ? true : false;
37
+ type NormalizeConfiguredCapabilities<T extends CommercetoolsCapabilities> = Omit<T, OverridableCapabilityKey> & {
38
+ [K in OverridableCapabilityKey]?: EnabledCapability<T[K]>;
39
+ };
40
+ type ExtractCapabilityFactory<TCapability, TContract, TDefaultFactory> = TCapability extends {
41
+ enabled: true;
42
+ factory?: infer TFactory;
43
+ } ? TFactory extends TContract ? TFactory : TDefaultFactory : TDefaultFactory;
44
+ type ExtractCapabilityProvider<TCapability, TDefaultProvider> = TCapability extends {
45
+ enabled: true;
46
+ provider?: infer TProviderFactory;
47
+ } ? TProviderFactory extends (...args: any[]) => infer TProvider ? TProvider : TDefaultProvider : TDefaultProvider;
48
+ type FactoryContractMap = {
49
+ product: ProductFactory;
50
+ productSearch: ProductSearchFactory;
51
+ productAssociations: ProductAssociationsFactory;
52
+ productReviews: ProductReviewsFactory;
53
+ productList: ProductListFactory;
54
+ identity: IdentityFactory;
55
+ cart: CartFactory;
56
+ checkout: CheckoutFactory;
57
+ order: OrderFactory;
58
+ orderSearch: OrderSearchFactory;
59
+ inventory: InventoryFactory;
60
+ price: PriceFactory;
61
+ category: CategoryFactory;
62
+ store: StoreFactory;
63
+ profile: ProfileFactory;
64
+ };
65
+ type DefaultFactoryMap = {
66
+ product: CommercetoolsProductFactory;
67
+ productSearch: CommercetoolsProductSearchFactory;
68
+ productAssociations: CommercetoolsProductAssociationsFactory;
69
+ productReviews: CommercetoolsProductReviewsFactory;
70
+ productList: CommercetoolsProductListFactory;
71
+ identity: CommercetoolsIdentityFactory;
72
+ cart: CommercetoolsCartFactory;
73
+ checkout: CommercetoolsCheckoutFactory;
74
+ order: CommercetoolsOrderFactory;
75
+ orderSearch: CommercetoolsOrderSearchFactory;
76
+ inventory: CommercetoolsInventoryFactory;
77
+ price: CommercetoolsPriceFactory;
78
+ category: CommercetoolsCategoryFactory;
79
+ store: CommercetoolsStoreFactory;
80
+ profile: CommercetoolsProfileFactory;
81
+ };
82
+ type ResolvedFactoryMap<T extends CommercetoolsCapabilities> = {
83
+ [K in OverridableCapabilityKey]: ExtractCapabilityFactory<T[K], FactoryContractMap[K], DefaultFactoryMap[K]>;
84
+ };
85
+ type DefaultProviderMap<T extends CommercetoolsCapabilities> = {
86
+ product: CommercetoolsProductProvider<ResolvedFactoryMap<T>['product']>;
87
+ productSearch: CommercetoolsSearchProvider<ResolvedFactoryMap<T>['productSearch']>;
88
+ productAssociations: CommercetoolsProductAssociationsProvider<ResolvedFactoryMap<T>['productAssociations']>;
89
+ productReviews: CommercetoolsProductReviewsProvider<ResolvedFactoryMap<T>['productReviews']>;
90
+ productList: CommercetoolsProductListProvider<ResolvedFactoryMap<T>['productList']>;
91
+ identity: CommercetoolsIdentityProvider<ResolvedFactoryMap<T>['identity']>;
92
+ cart: CommercetoolsCartProvider<ResolvedFactoryMap<T>['cart']>;
93
+ checkout: CommercetoolsCheckoutProvider<ResolvedFactoryMap<T>['checkout']>;
94
+ order: CommercetoolsOrderProvider<ResolvedFactoryMap<T>['order']>;
95
+ orderSearch: CommercetoolsOrderSearchProvider<ResolvedFactoryMap<T>['orderSearch']>;
96
+ inventory: CommercetoolsInventoryProvider<ResolvedFactoryMap<T>['inventory']>;
97
+ price: CommercetoolsPriceProvider<ResolvedFactoryMap<T>['price']>;
98
+ category: CommercetoolsCategoryProvider<ResolvedFactoryMap<T>['category']>;
99
+ store: CommercetoolsStoreProvider<ResolvedFactoryMap<T>['store']>;
100
+ profile: CommercetoolsProfileProvider<ResolvedFactoryMap<T>['profile']>;
101
+ };
102
+ type CapabilityProviderTypeMap<T extends CommercetoolsCapabilities> = {
103
+ [K in OverridableCapabilityKey]: ExtractCapabilityProvider<T[K], DefaultProviderMap<T>[K]>;
104
+ };
105
+ type EnabledCapabilityOverrideMap<T extends CommercetoolsCapabilities> = {
106
+ [K in OverridableCapabilityKey as T[K] extends {
107
+ enabled: true;
108
+ } ? K : never]: CapabilityProviderTypeMap<T>[K];
109
+ };
110
+ export type CommercetoolsClientFromCapabilities<T extends CommercetoolsCapabilities> = Omit<ClientFromCapabilities<NormalizeConfiguredCapabilities<T>>, OverridableCapabilityKey> & EnabledCapabilityOverrideMap<T>;
111
+ export declare function resolveCapabilityProvider<TFactory, TProvider, TProviderArgs>(capability: {
112
+ factory?: TFactory;
113
+ provider?: (args: TProviderArgs) => TProvider;
114
+ } | undefined, defaults: {
115
+ factory: TFactory;
116
+ provider: (args: TProviderArgs) => TProvider;
117
+ }, buildProviderArgs: (factory: TFactory) => TProviderArgs): TProvider;
118
+ export {};