@reactionary/provider-medusa 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 (57) hide show
  1. package/core/initialize.js +251 -28
  2. package/core/initialize.types.js +13 -0
  3. package/factories/cart/cart.factory.js +15 -0
  4. package/factories/category/category.factory.js +15 -0
  5. package/factories/checkout/checkout.factory.js +19 -0
  6. package/factories/identity/identity.factory.js +11 -0
  7. package/factories/index.js +12 -0
  8. package/factories/inventory/inventory.factory.js +11 -0
  9. package/factories/order/order.factory.js +11 -0
  10. package/factories/order-search/order-search.factory.js +11 -0
  11. package/factories/price/price.factory.js +11 -0
  12. package/factories/product/product.factory.js +11 -0
  13. package/factories/product-associations/product-associations.factory.js +11 -0
  14. package/factories/product-search/product-search.factory.js +11 -0
  15. package/factories/profile/profile.factory.js +11 -0
  16. package/index.js +2 -0
  17. package/package.json +2 -2
  18. package/providers/cart.provider.js +8 -9
  19. package/providers/category.provider.js +4 -3
  20. package/providers/checkout.provider.js +14 -5
  21. package/providers/inventory.provider.js +4 -3
  22. package/providers/order-search.provider.js +3 -2
  23. package/providers/order.provider.js +4 -2
  24. package/providers/price.provider.js +3 -2
  25. package/providers/product-associations.provider.js +5 -4
  26. package/providers/product-search.provider.js +6 -20
  27. package/providers/product.provider.js +3 -2
  28. package/providers/profile.provider.js +4 -2
  29. package/schema/capabilities.schema.js +24 -0
  30. package/src/core/initialize.d.ts +5 -4
  31. package/src/core/initialize.types.d.ts +108 -0
  32. package/src/factories/cart/cart.factory.d.ts +9 -0
  33. package/src/factories/category/category.factory.d.ts +9 -0
  34. package/src/factories/checkout/checkout.factory.d.ts +11 -0
  35. package/src/factories/identity/identity.factory.d.ts +7 -0
  36. package/src/factories/index.d.ts +12 -0
  37. package/src/factories/inventory/inventory.factory.d.ts +7 -0
  38. package/src/factories/order/order.factory.d.ts +7 -0
  39. package/src/factories/order-search/order-search.factory.d.ts +7 -0
  40. package/src/factories/price/price.factory.d.ts +9 -0
  41. package/src/factories/product/product.factory.d.ts +7 -0
  42. package/src/factories/product-associations/product-associations.factory.d.ts +7 -0
  43. package/src/factories/product-search/product-search.factory.d.ts +7 -0
  44. package/src/factories/profile/profile.factory.d.ts +7 -0
  45. package/src/index.d.ts +2 -0
  46. package/src/providers/cart.provider.d.ts +15 -13
  47. package/src/providers/category.provider.d.ts +12 -28
  48. package/src/providers/checkout.provider.d.ts +15 -13
  49. package/src/providers/inventory.provider.d.ts +8 -6
  50. package/src/providers/order-search.provider.d.ts +6 -4
  51. package/src/providers/order.provider.d.ts +7 -5
  52. package/src/providers/price.provider.d.ts +9 -7
  53. package/src/providers/product-associations.provider.d.ts +9 -6
  54. package/src/providers/product-search.provider.d.ts +21 -10
  55. package/src/providers/product.provider.d.ts +9 -7
  56. package/src/providers/profile.provider.d.ts +13 -11
  57. package/src/schema/capabilities.schema.d.ts +114 -15
@@ -1,3 +1,20 @@
1
+ import {
2
+ CartIdentifierSchema,
3
+ CartSchema,
4
+ CategoryPaginatedResultSchema,
5
+ CategorySchema,
6
+ CheckoutSchema,
7
+ InventorySchema,
8
+ OrderSchema,
9
+ OrderSearchResultSchema,
10
+ PaymentMethodSchema,
11
+ PriceSchema,
12
+ ProductAssociationSchema,
13
+ ProductSchema,
14
+ ProductSearchResultSchema,
15
+ ProfileSchema,
16
+ ShippingMethodSchema
17
+ } from "@reactionary/core";
1
18
  import { MedusaCartProvider } from "../providers/cart.provider.js";
2
19
  import { MedusaCategoryProvider } from "../providers/category.provider.js";
3
20
  import { MedusaCheckoutProvider } from "../providers/checkout.provider.js";
@@ -11,53 +28,259 @@ import { MedusaProductRecommendationsProvider } from "../providers/product-recom
11
28
  import { MedusaProductProvider } from "../providers/product.provider.js";
12
29
  import { MedusaProductAssociationsProvider } from "../providers/product-associations.provider.js";
13
30
  import { MedusaProfileProvider } from "../providers/profile.provider.js";
14
- import { MedusaCapabilitiesSchema } from "../schema/capabilities.schema.js";
15
- import { MedusaConfigurationSchema } from "../schema/configuration.schema.js";
31
+ import {
32
+ MedusaCapabilitiesSchema
33
+ } from "../schema/capabilities.schema.js";
34
+ import {
35
+ MedusaConfigurationSchema
36
+ } from "../schema/configuration.schema.js";
16
37
  import { MedusaAPI } from "./client.js";
38
+ import {
39
+ MedusaCartFactory,
40
+ MedusaCategoryFactory,
41
+ MedusaCheckoutFactory,
42
+ MedusaInventoryFactory,
43
+ MedusaOrderFactory,
44
+ MedusaOrderSearchFactory,
45
+ MedusaPriceFactory,
46
+ MedusaProductAssociationsFactory,
47
+ MedusaProductFactory,
48
+ MedusaProductSearchFactory,
49
+ MedusaProfileFactory
50
+ } from "../factories/index.js";
51
+ import {
52
+ resolveCapabilityProvider,
53
+ resolveProviderOnlyCapability
54
+ } from "./initialize.types.js";
17
55
  function withMedusaCapabilities(configuration, capabilities) {
18
56
  return (cache, context) => {
19
57
  const client = {};
20
58
  const config = MedusaConfigurationSchema.parse(configuration);
21
59
  const caps = MedusaCapabilitiesSchema.parse(capabilities);
22
60
  const medusaApi = new MedusaAPI(config, context);
23
- if (caps.productSearch) {
24
- client.productSearch = new MedusaSearchProvider(configuration, cache, context, medusaApi);
61
+ const buildProviderArgs = (factory) => ({
62
+ cache,
63
+ context,
64
+ config,
65
+ medusaApi,
66
+ factory
67
+ });
68
+ if (caps.product?.enabled) {
69
+ client.product = resolveCapabilityProvider(
70
+ capabilities.product,
71
+ {
72
+ factory: new MedusaProductFactory(ProductSchema),
73
+ provider: (args) => new MedusaProductProvider(
74
+ args.config,
75
+ args.cache,
76
+ args.context,
77
+ args.medusaApi,
78
+ args.factory
79
+ )
80
+ },
81
+ buildProviderArgs
82
+ );
25
83
  }
26
- if (caps.productRecommendations) {
27
- client.productRecommendations = new MedusaProductRecommendationsProvider(configuration, cache, context, medusaApi);
84
+ if (caps.productSearch?.enabled) {
85
+ client.productSearch = resolveCapabilityProvider(
86
+ capabilities.productSearch,
87
+ {
88
+ factory: new MedusaProductSearchFactory(ProductSearchResultSchema),
89
+ provider: (args) => new MedusaSearchProvider(
90
+ args.config,
91
+ args.cache,
92
+ args.context,
93
+ args.medusaApi,
94
+ args.factory
95
+ )
96
+ },
97
+ buildProviderArgs
98
+ );
28
99
  }
29
- if (caps.category) {
30
- client.category = new MedusaCategoryProvider(configuration, cache, context, medusaApi);
100
+ if (caps.category?.enabled) {
101
+ client.category = resolveCapabilityProvider(
102
+ capabilities.category,
103
+ {
104
+ factory: new MedusaCategoryFactory(
105
+ CategorySchema,
106
+ CategoryPaginatedResultSchema
107
+ ),
108
+ provider: (args) => new MedusaCategoryProvider(
109
+ args.config,
110
+ args.cache,
111
+ args.context,
112
+ args.medusaApi,
113
+ args.factory
114
+ )
115
+ },
116
+ buildProviderArgs
117
+ );
31
118
  }
32
- if (caps.checkout) {
33
- client.checkout = new MedusaCheckoutProvider(configuration, cache, context, medusaApi);
119
+ if (caps.checkout?.enabled) {
120
+ client.checkout = resolveCapabilityProvider(
121
+ capabilities.checkout,
122
+ {
123
+ factory: new MedusaCheckoutFactory(
124
+ CheckoutSchema,
125
+ ShippingMethodSchema,
126
+ PaymentMethodSchema
127
+ ),
128
+ provider: (args) => new MedusaCheckoutProvider(
129
+ args.config,
130
+ args.cache,
131
+ args.context,
132
+ args.medusaApi,
133
+ args.factory
134
+ )
135
+ },
136
+ buildProviderArgs
137
+ );
34
138
  }
35
- if (caps.product) {
36
- client.product = new MedusaProductProvider(configuration, cache, context, medusaApi);
139
+ if (caps.productRecommendations?.enabled) {
140
+ client.productRecommendations = resolveProviderOnlyCapability(
141
+ capabilities.productRecommendations,
142
+ (args) => new MedusaProductRecommendationsProvider(
143
+ args.config,
144
+ args.cache,
145
+ args.context,
146
+ args.medusaApi
147
+ ),
148
+ {
149
+ cache,
150
+ context,
151
+ config,
152
+ medusaApi
153
+ }
154
+ );
37
155
  }
38
- if (caps.cart) {
39
- client.cart = new MedusaCartProvider(configuration, cache, context, medusaApi);
156
+ if (caps.cart?.enabled) {
157
+ client.cart = resolveCapabilityProvider(
158
+ capabilities.cart,
159
+ {
160
+ factory: new MedusaCartFactory(CartSchema, CartIdentifierSchema),
161
+ provider: (args) => new MedusaCartProvider(
162
+ args.config,
163
+ args.cache,
164
+ args.context,
165
+ args.medusaApi,
166
+ args.factory
167
+ )
168
+ },
169
+ buildProviderArgs
170
+ );
40
171
  }
41
- if (caps.price) {
42
- client.price = new MedusaPriceProvider(configuration, cache, context, medusaApi);
172
+ if (caps.price?.enabled) {
173
+ client.price = resolveCapabilityProvider(
174
+ capabilities.price,
175
+ {
176
+ factory: new MedusaPriceFactory(PriceSchema),
177
+ provider: (args) => new MedusaPriceProvider(
178
+ args.config,
179
+ args.cache,
180
+ args.context,
181
+ args.medusaApi,
182
+ args.factory
183
+ )
184
+ },
185
+ buildProviderArgs
186
+ );
43
187
  }
44
- if (caps.inventory) {
45
- client.inventory = new MedusaInventoryProvider(configuration, cache, context, medusaApi);
188
+ if (caps.inventory?.enabled) {
189
+ client.inventory = resolveCapabilityProvider(
190
+ capabilities.inventory,
191
+ {
192
+ factory: new MedusaInventoryFactory(InventorySchema),
193
+ provider: (args) => new MedusaInventoryProvider(
194
+ args.config,
195
+ args.cache,
196
+ args.context,
197
+ args.medusaApi,
198
+ args.factory
199
+ )
200
+ },
201
+ buildProviderArgs
202
+ );
46
203
  }
47
- if (caps.identity) {
48
- client.identity = new MedusaIdentityProvider(configuration, cache, context, medusaApi);
204
+ if (caps.identity?.enabled) {
205
+ client.identity = resolveProviderOnlyCapability(
206
+ capabilities.identity,
207
+ (args) => new MedusaIdentityProvider(
208
+ args.config,
209
+ args.cache,
210
+ args.context,
211
+ args.medusaApi
212
+ ),
213
+ {
214
+ cache,
215
+ context,
216
+ config,
217
+ medusaApi
218
+ }
219
+ );
49
220
  }
50
- if (caps.profile) {
51
- client.profile = new MedusaProfileProvider(configuration, cache, context, medusaApi);
221
+ if (caps.profile?.enabled) {
222
+ client.profile = resolveCapabilityProvider(
223
+ capabilities.profile,
224
+ {
225
+ factory: new MedusaProfileFactory(ProfileSchema),
226
+ provider: (args) => new MedusaProfileProvider(
227
+ args.config,
228
+ args.cache,
229
+ args.context,
230
+ args.medusaApi,
231
+ args.factory
232
+ )
233
+ },
234
+ buildProviderArgs
235
+ );
52
236
  }
53
- if (caps.order) {
54
- client.order = new MedusaOrderProvider(configuration, cache, context, medusaApi);
237
+ if (caps.order?.enabled) {
238
+ client.order = resolveCapabilityProvider(
239
+ capabilities.order,
240
+ {
241
+ factory: new MedusaOrderFactory(OrderSchema),
242
+ provider: (args) => new MedusaOrderProvider(
243
+ args.config,
244
+ args.cache,
245
+ args.context,
246
+ args.medusaApi,
247
+ args.factory
248
+ )
249
+ },
250
+ buildProviderArgs
251
+ );
55
252
  }
56
- if (caps.orderSearch) {
57
- client.orderSearch = new MedusaOrderSearchProvider(configuration, cache, context, medusaApi);
253
+ if (caps.orderSearch?.enabled) {
254
+ client.orderSearch = resolveCapabilityProvider(
255
+ capabilities.orderSearch,
256
+ {
257
+ factory: new MedusaOrderSearchFactory(OrderSearchResultSchema),
258
+ provider: (args) => new MedusaOrderSearchProvider(
259
+ args.config,
260
+ args.cache,
261
+ args.context,
262
+ args.medusaApi,
263
+ args.factory
264
+ )
265
+ },
266
+ buildProviderArgs
267
+ );
58
268
  }
59
- if (caps.productAssociations) {
60
- client.productAssociations = new MedusaProductAssociationsProvider(configuration, cache, context, medusaApi);
269
+ if (caps.productAssociations?.enabled) {
270
+ client.productAssociations = resolveCapabilityProvider(
271
+ capabilities.productAssociations,
272
+ {
273
+ factory: new MedusaProductAssociationsFactory(ProductAssociationSchema),
274
+ provider: (args) => new MedusaProductAssociationsProvider(
275
+ args.config,
276
+ args.cache,
277
+ args.context,
278
+ args.medusaApi,
279
+ args.factory
280
+ )
281
+ },
282
+ buildProviderArgs
283
+ );
61
284
  }
62
285
  return client;
63
286
  };
@@ -0,0 +1,13 @@
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
+ function resolveProviderOnlyCapability(capability, defaultProvider, args) {
7
+ const provider = capability?.provider ?? defaultProvider;
8
+ return provider(args);
9
+ }
10
+ export {
11
+ resolveCapabilityProvider,
12
+ resolveProviderOnlyCapability
13
+ };
@@ -0,0 +1,15 @@
1
+ class MedusaCartFactory {
2
+ constructor(cartSchema, cartIdentifierSchema) {
3
+ this.cartSchema = cartSchema;
4
+ this.cartIdentifierSchema = cartIdentifierSchema;
5
+ }
6
+ parseCart(_context, data) {
7
+ return this.cartSchema.parse(data);
8
+ }
9
+ parseCartIdentifier(_context, data) {
10
+ return this.cartIdentifierSchema.parse(data);
11
+ }
12
+ }
13
+ export {
14
+ MedusaCartFactory
15
+ };
@@ -0,0 +1,15 @@
1
+ class MedusaCategoryFactory {
2
+ constructor(categorySchema, categoryPaginatedResultSchema) {
3
+ this.categorySchema = categorySchema;
4
+ this.categoryPaginatedResultSchema = categoryPaginatedResultSchema;
5
+ }
6
+ parseCategory(_context, data) {
7
+ return this.categorySchema.parse(data);
8
+ }
9
+ parseCategoryPaginatedResult(_context, data) {
10
+ return this.categoryPaginatedResultSchema.parse(data);
11
+ }
12
+ }
13
+ export {
14
+ MedusaCategoryFactory
15
+ };
@@ -0,0 +1,19 @@
1
+ class MedusaCheckoutFactory {
2
+ constructor(checkoutSchema, shippingMethodSchema, paymentMethodSchema) {
3
+ this.checkoutSchema = checkoutSchema;
4
+ this.shippingMethodSchema = shippingMethodSchema;
5
+ this.paymentMethodSchema = paymentMethodSchema;
6
+ }
7
+ parseCheckout(_context, data) {
8
+ return this.checkoutSchema.parse(data);
9
+ }
10
+ parseShippingMethod(_context, data) {
11
+ return this.shippingMethodSchema.parse(data);
12
+ }
13
+ parsePaymentMethod(_context, data) {
14
+ return this.paymentMethodSchema.parse(data);
15
+ }
16
+ }
17
+ export {
18
+ MedusaCheckoutFactory
19
+ };
@@ -0,0 +1,11 @@
1
+ class MedusaIdentityFactory {
2
+ constructor(identitySchema) {
3
+ this.identitySchema = identitySchema;
4
+ }
5
+ parseIdentity(_context, data) {
6
+ return this.identitySchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaIdentityFactory
11
+ };
@@ -0,0 +1,12 @@
1
+ export * from "./cart/cart.factory.js";
2
+ export * from "./category/category.factory.js";
3
+ export * from "./checkout/checkout.factory.js";
4
+ export * from "./identity/identity.factory.js";
5
+ export * from "./inventory/inventory.factory.js";
6
+ export * from "./order/order.factory.js";
7
+ export * from "./order-search/order-search.factory.js";
8
+ export * from "./price/price.factory.js";
9
+ export * from "./product/product.factory.js";
10
+ export * from "./product-associations/product-associations.factory.js";
11
+ export * from "./product-search/product-search.factory.js";
12
+ export * from "./profile/profile.factory.js";
@@ -0,0 +1,11 @@
1
+ class MedusaInventoryFactory {
2
+ constructor(inventorySchema) {
3
+ this.inventorySchema = inventorySchema;
4
+ }
5
+ parseInventory(_context, data) {
6
+ return this.inventorySchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaInventoryFactory
11
+ };
@@ -0,0 +1,11 @@
1
+ class MedusaOrderFactory {
2
+ constructor(orderSchema) {
3
+ this.orderSchema = orderSchema;
4
+ }
5
+ parseOrder(_context, data) {
6
+ return this.orderSchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaOrderFactory
11
+ };
@@ -0,0 +1,11 @@
1
+ class MedusaOrderSearchFactory {
2
+ constructor(orderSearchResultSchema) {
3
+ this.orderSearchResultSchema = orderSearchResultSchema;
4
+ }
5
+ parseOrderSearchResult(_context, data, _query) {
6
+ return this.orderSearchResultSchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaOrderSearchFactory
11
+ };
@@ -0,0 +1,11 @@
1
+ class MedusaPriceFactory {
2
+ constructor(priceSchema) {
3
+ this.priceSchema = priceSchema;
4
+ }
5
+ parsePrice(_context, data, _options) {
6
+ return this.priceSchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaPriceFactory
11
+ };
@@ -0,0 +1,11 @@
1
+ class MedusaProductFactory {
2
+ constructor(productSchema) {
3
+ this.productSchema = productSchema;
4
+ }
5
+ parseProduct(_context, data) {
6
+ return this.productSchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaProductFactory
11
+ };
@@ -0,0 +1,11 @@
1
+ class MedusaProductAssociationsFactory {
2
+ constructor(productAssociationSchema) {
3
+ this.productAssociationSchema = productAssociationSchema;
4
+ }
5
+ parseAssociation(_context, data) {
6
+ return this.productAssociationSchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaProductAssociationsFactory
11
+ };
@@ -0,0 +1,11 @@
1
+ class MedusaProductSearchFactory {
2
+ constructor(productSearchResultSchema) {
3
+ this.productSearchResultSchema = productSearchResultSchema;
4
+ }
5
+ parseSearchResult(_context, data, _query) {
6
+ return this.productSearchResultSchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaProductSearchFactory
11
+ };
@@ -0,0 +1,11 @@
1
+ class MedusaProfileFactory {
2
+ constructor(profileSchema) {
3
+ this.profileSchema = profileSchema;
4
+ }
5
+ parseProfile(_context, data) {
6
+ return this.profileSchema.parse(data);
7
+ }
8
+ }
9
+ export {
10
+ MedusaProfileFactory
11
+ };
package/index.js CHANGED
@@ -2,7 +2,9 @@ export * from "./schema/configuration.schema.js";
2
2
  export * from "./schema/medusa.schema.js";
3
3
  export * from "./schema/capabilities.schema.js";
4
4
  export * from "./core/initialize.js";
5
+ export * from "./core/initialize.types.js";
5
6
  export * from "./core/client.js";
7
+ export * from "./factories/index.js";
6
8
  export * from "./providers/cart.provider.js";
7
9
  export * from "./providers/identity.provider.js";
8
10
  export * from "./providers/inventory.provider.js";
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@reactionary/provider-medusa",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "src/index.d.ts",
7
7
  "dependencies": {
8
8
  "@medusajs/js-sdk": "^2.13.0",
9
- "@reactionary/core": "0.6.1",
9
+ "@reactionary/core": "0.6.2",
10
10
  "debug": "^4.4.3",
11
11
  "@medusajs/types": "^2.11.0",
12
12
  "zod": "4.1.9"
@@ -35,7 +35,7 @@ import {
35
35
  } from "../utils/medusa-helpers.js";
36
36
  const debug = createDebug("reactionary:medusa:cart");
37
37
  class MedusaCartProvider extends CartProvider {
38
- constructor(config, cache, context, medusaApi) {
38
+ constructor(config, cache, context, medusaApi, factory) {
39
39
  super(cache, context);
40
40
  this.medusaApi = medusaApi;
41
41
  /**
@@ -46,6 +46,7 @@ class MedusaCartProvider extends CartProvider {
46
46
  */
47
47
  this.includedFields = ["+items.*"].join(",");
48
48
  this.config = config;
49
+ this.factory = factory;
49
50
  }
50
51
  async getById(payload) {
51
52
  try {
@@ -185,12 +186,10 @@ class MedusaCartProvider extends CartProvider {
185
186
  if (activeCartId) {
186
187
  try {
187
188
  await client.store.cart.retrieve(activeCartId);
188
- return success(
189
- MedusaCartIdentifierSchema.parse({
190
- key: activeCartId,
191
- region_id: (await this.medusaApi.getActiveRegion()).id
192
- })
193
- );
189
+ return success(this.factory.parseCartIdentifier(this.context, {
190
+ key: activeCartId,
191
+ region_id: (await this.medusaApi.getActiveRegion()).id
192
+ }));
194
193
  } catch {
195
194
  }
196
195
  }
@@ -348,7 +347,7 @@ class MedusaCartProvider extends CartProvider {
348
347
  }
349
348
  );
350
349
  if (response.cart) {
351
- const cartIdentifier = MedusaCartIdentifierSchema.parse({
350
+ const cartIdentifier = this.factory.parseCartIdentifier(this.context, {
352
351
  key: response.cart.id,
353
352
  region_id: response.cart.region_id
354
353
  });
@@ -455,7 +454,7 @@ class MedusaCartProvider extends CartProvider {
455
454
  userId: "???"
456
455
  }
457
456
  };
458
- return result;
457
+ return this.factory.parseCart(this.context, result);
459
458
  }
460
459
  }
461
460
  __decorateClass([
@@ -26,10 +26,11 @@ import {
26
26
  } from "@reactionary/core";
27
27
  import * as z from "zod";
28
28
  class MedusaCategoryProvider extends CategoryProvider {
29
- constructor(config, cache, context, medusaApi) {
29
+ constructor(config, cache, context, medusaApi, factory) {
30
30
  super(cache, context);
31
31
  this.medusaApi = medusaApi;
32
32
  this.config = config;
33
+ this.factory = factory;
33
34
  }
34
35
  async resolveCategoryIdByExternalId(externalId) {
35
36
  const sdk = await this.medusaApi.getClient();
@@ -160,7 +161,7 @@ class MedusaCategoryProvider extends CategoryProvider {
160
161
  parentCategory,
161
162
  images: []
162
163
  };
163
- return result;
164
+ return this.factory.parseCategory(this.context, result);
164
165
  }
165
166
  parsePaginatedResult(body) {
166
167
  const items = body.product_categories.map((x) => this.parseSingle(x));
@@ -175,7 +176,7 @@ class MedusaCategoryProvider extends CategoryProvider {
175
176
  totalPages,
176
177
  items
177
178
  };
178
- return result;
179
+ return this.factory.parseCategoryPaginatedResult(this.context, result);
179
180
  }
180
181
  }
181
182
  __decorateClass([
@@ -46,7 +46,7 @@ class CheckoutNotReadyForFinalizationError extends Error {
46
46
  }
47
47
  }
48
48
  class MedusaCheckoutProvider extends CheckoutProvider {
49
- constructor(config, cache, context, medusaApi) {
49
+ constructor(config, cache, context, medusaApi, factory) {
50
50
  super(cache, context);
51
51
  this.medusaApi = medusaApi;
52
52
  /**
@@ -57,6 +57,7 @@ class MedusaCheckoutProvider extends CheckoutProvider {
57
57
  */
58
58
  this.includedFields = ["+items.*"].join(",");
59
59
  this.config = config;
60
+ this.factory = factory;
60
61
  }
61
62
  initiateCheckoutForCartPayload(payload) {
62
63
  return {
@@ -143,7 +144,9 @@ class MedusaCheckoutProvider extends CheckoutProvider {
143
144
  shippingMethods
144
145
  );
145
146
  }
146
- return success(shippingMethods);
147
+ return success(
148
+ shippingMethods.map((x) => this.factory.parseShippingMethod(this.context, x))
149
+ );
147
150
  }
148
151
  getAvailablePaymentMethodsPayload(payload, regionId) {
149
152
  return {
@@ -185,7 +188,9 @@ class MedusaCheckoutProvider extends CheckoutProvider {
185
188
  paymentMethods
186
189
  );
187
190
  }
188
- return success(paymentMethods);
191
+ return success(
192
+ paymentMethods.map((x) => this.factory.parsePaymentMethod(this.context, x))
193
+ );
189
194
  }
190
195
  addPaymentInstructionPayload(payload) {
191
196
  return {
@@ -274,9 +279,13 @@ class MedusaCheckoutProvider extends CheckoutProvider {
274
279
  order_display_id: response.order?.display_id ? 0 : ""
275
280
  }
276
281
  });
277
- return this.getById({
282
+ const refreshedCheckout = await this.getById({
278
283
  identifier: payload.checkout
279
284
  });
285
+ if (!refreshedCheckout.success) {
286
+ throw new Error(`Unable to reload checkout ${payload.checkout.key} after completion`);
287
+ }
288
+ return success(refreshedCheckout.value);
280
289
  }
281
290
  throw new Error("Something failed during order creation");
282
291
  }
@@ -459,7 +468,7 @@ class MedusaCheckoutProvider extends CheckoutProvider {
459
468
  shippingAddress,
460
469
  shippingInstruction
461
470
  };
462
- return result;
471
+ return this.factory.parseCheckout(this.context, result);
463
472
  }
464
473
  }
465
474
  __decorateClass([