@reactionary/medusa 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +11 -0
  2. package/capabilities/cart.capability.js +517 -0
  3. package/capabilities/category.capability.js +216 -0
  4. package/capabilities/checkout.capability.js +533 -0
  5. package/capabilities/identity.capability.js +128 -0
  6. package/capabilities/inventory.capability.js +142 -0
  7. package/capabilities/order-search.capability.js +161 -0
  8. package/capabilities/order.capability.js +179 -0
  9. package/capabilities/price.capability.js +120 -0
  10. package/capabilities/product-associations.capability.js +148 -0
  11. package/capabilities/product-recommendations.capability.js +116 -0
  12. package/capabilities/product-search.capability.js +177 -0
  13. package/capabilities/product.capability.js +252 -0
  14. package/capabilities/profile.capability.js +333 -0
  15. package/core/client.js +269 -0
  16. package/core/initialize.js +290 -0
  17. package/core/initialize.types.js +13 -0
  18. package/factories/cart/cart.factory.js +17 -0
  19. package/factories/category/category.factory.js +17 -0
  20. package/factories/checkout/checkout.factory.js +22 -0
  21. package/factories/identity/identity.factory.js +12 -0
  22. package/factories/index.js +12 -0
  23. package/factories/inventory/inventory.factory.js +12 -0
  24. package/factories/order/order.factory.js +12 -0
  25. package/factories/order-search/order-search.factory.js +12 -0
  26. package/factories/price/price.factory.js +12 -0
  27. package/factories/product/product.factory.js +12 -0
  28. package/factories/product-associations/product-associations.factory.js +12 -0
  29. package/factories/product-search/product-search.factory.js +12 -0
  30. package/factories/profile/profile.factory.js +12 -0
  31. package/index.js +17 -0
  32. package/package.json +17 -0
  33. package/schema/capabilities.schema.js +43 -0
  34. package/schema/configuration.schema.js +11 -0
  35. package/schema/medusa.schema.js +26 -0
  36. package/src/capabilities/cart.capability.d.ts +100 -0
  37. package/src/capabilities/category.capability.d.ts +36 -0
  38. package/src/capabilities/checkout.capability.d.ts +88 -0
  39. package/src/capabilities/identity.capability.d.ts +13 -0
  40. package/src/capabilities/inventory.capability.d.ts +21 -0
  41. package/src/capabilities/order-search.capability.d.ts +53 -0
  42. package/src/capabilities/order.capability.d.ts +49 -0
  43. package/src/capabilities/price.capability.d.ts +16 -0
  44. package/src/capabilities/product-associations.capability.d.ts +19 -0
  45. package/src/capabilities/product-recommendations.capability.d.ts +41 -0
  46. package/src/capabilities/product-search.capability.d.ts +45 -0
  47. package/src/capabilities/product.capability.d.ts +32 -0
  48. package/src/capabilities/profile.capability.d.ts +39 -0
  49. package/src/core/client.d.ts +81 -0
  50. package/src/core/initialize.d.ts +5 -0
  51. package/src/core/initialize.types.d.ts +108 -0
  52. package/src/factories/cart/cart.factory.d.ts +9 -0
  53. package/src/factories/category/category.factory.d.ts +9 -0
  54. package/src/factories/checkout/checkout.factory.d.ts +11 -0
  55. package/src/factories/identity/identity.factory.d.ts +7 -0
  56. package/src/factories/index.d.ts +12 -0
  57. package/src/factories/inventory/inventory.factory.d.ts +7 -0
  58. package/src/factories/order/order.factory.d.ts +7 -0
  59. package/src/factories/order-search/order-search.factory.d.ts +7 -0
  60. package/src/factories/price/price.factory.d.ts +9 -0
  61. package/src/factories/product/product.factory.d.ts +7 -0
  62. package/src/factories/product-associations/product-associations.factory.d.ts +7 -0
  63. package/src/factories/product-search/product-search.factory.d.ts +7 -0
  64. package/src/factories/profile/profile.factory.d.ts +7 -0
  65. package/src/index.d.ts +17 -0
  66. package/src/schema/capabilities.schema.d.ts +116 -0
  67. package/src/schema/configuration.schema.d.ts +9 -0
  68. package/src/schema/medusa.schema.d.ts +33 -0
  69. package/src/test/test-utils.d.ts +2 -0
  70. package/src/utils/medusa-helpers.d.ts +35 -0
  71. package/test/test-utils.js +12 -0
  72. package/utils/medusa-helpers.js +72 -0
@@ -0,0 +1,216 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result)
9
+ __defProp(target, key, result);
10
+ return result;
11
+ };
12
+ import {
13
+ CategoryIdentifierSchema,
14
+ CategoryCapability,
15
+ CategorySchema,
16
+ CategoryQueryByIdSchema,
17
+ CategoryQueryBySlugSchema,
18
+ CategoryQueryForBreadcrumbSchema,
19
+ CategoryQueryForChildCategoriesSchema,
20
+ CategoryQueryForTopCategoriesSchema,
21
+ createPaginatedResponseSchema,
22
+ Reactionary,
23
+ success,
24
+ error,
25
+ CategoryPaginatedResultSchema
26
+ } from "@reactionary/core";
27
+ import * as z from "zod";
28
+ class MedusaCategoryCapability extends CategoryCapability {
29
+ constructor(config, cache, context, medusaApi, factory) {
30
+ super(cache, context);
31
+ this.medusaApi = medusaApi;
32
+ this.config = config;
33
+ this.factory = factory;
34
+ }
35
+ config;
36
+ factory;
37
+ async resolveCategoryIdByExternalId(externalId) {
38
+ const sdk = await this.medusaApi.getClient();
39
+ let offset = 0;
40
+ const limit = 50;
41
+ let candidate = void 0;
42
+ while (true) {
43
+ try {
44
+ const categoryResult = await sdk.store.category.list({
45
+ offset,
46
+ limit
47
+ });
48
+ if (categoryResult.product_categories.length === 0) {
49
+ break;
50
+ }
51
+ candidate = categoryResult.product_categories.find(
52
+ (cat) => cat.metadata?.["external_id"] === externalId
53
+ );
54
+ if (candidate) {
55
+ break;
56
+ }
57
+ offset += limit;
58
+ } catch (error2) {
59
+ throw new Error(
60
+ "Category not found " + externalId + " due to error: " + error2
61
+ );
62
+ break;
63
+ }
64
+ }
65
+ return candidate || null;
66
+ }
67
+ async getById(payload) {
68
+ const candidate = await this.resolveCategoryIdByExternalId(payload.id.key);
69
+ if (!candidate) {
70
+ return error({
71
+ type: "NotFound",
72
+ identifier: payload
73
+ });
74
+ }
75
+ return success(this.parseSingle(candidate));
76
+ }
77
+ getBySlugPayload(payload) {
78
+ return {
79
+ handle: payload.slug,
80
+ limit: 1,
81
+ offset: 0
82
+ };
83
+ }
84
+ async getBySlug(payload) {
85
+ const sdk = await this.medusaApi.getClient();
86
+ const categoryResult = await sdk.store.category.list(this.getBySlugPayload(payload));
87
+ if (categoryResult.count === 0) {
88
+ return error({
89
+ type: "NotFound",
90
+ identifier: payload
91
+ });
92
+ }
93
+ return success(this.parseSingle(categoryResult.product_categories[0]));
94
+ }
95
+ async getBreadcrumbPathToCategory(payload) {
96
+ const actualCategoryId = await this.resolveCategoryIdByExternalId(
97
+ payload.id.key
98
+ );
99
+ if (!actualCategoryId) {
100
+ throw new Error("Category not found " + payload.id.key);
101
+ }
102
+ const sdk = await this.medusaApi.getClient();
103
+ const path = await sdk.store.category.retrieve(actualCategoryId.id, {
104
+ fields: "+metadata,+parent_category.metadata",
105
+ include_ancestors_tree: true
106
+ });
107
+ let results = new Array();
108
+ let current = path.product_category;
109
+ while (current) {
110
+ results.push(this.parseSingle(current));
111
+ current = current.parent_category;
112
+ }
113
+ results = results.reverse();
114
+ return success(results);
115
+ }
116
+ findChildCategoriesPayload(payload, actualParent) {
117
+ return {
118
+ fields: "+metadata,+parent_category.metadata",
119
+ parent_category_id: actualParent.id,
120
+ limit: payload.paginationOptions.pageSize,
121
+ offset: (payload.paginationOptions.pageNumber - 1) * payload.paginationOptions.pageSize
122
+ };
123
+ }
124
+ async findChildCategories(payload) {
125
+ const sdk = await this.medusaApi.getClient();
126
+ const actualParent = await this.resolveCategoryIdByExternalId(
127
+ payload.parentId.key
128
+ );
129
+ if (!actualParent) {
130
+ throw new Error("Parent category not found " + payload.parentId.key);
131
+ }
132
+ const response = await sdk.store.category.list(this.findChildCategoriesPayload(payload, actualParent));
133
+ const result = this.parsePaginatedResult(response);
134
+ return success(result);
135
+ }
136
+ findTopCategoriesPayload(payload) {
137
+ return {
138
+ fields: "+metadata",
139
+ parent_category_id: "null",
140
+ limit: payload.paginationOptions.pageSize,
141
+ offset: (payload.paginationOptions.pageNumber - 1) * payload.paginationOptions.pageSize
142
+ };
143
+ }
144
+ async findTopCategories(payload) {
145
+ const sdk = await this.medusaApi.getClient();
146
+ const response = await sdk.store.category.list(this.findTopCategoriesPayload(payload));
147
+ const result = this.parsePaginatedResult(response);
148
+ return success(result);
149
+ }
150
+ parseSingle(_body) {
151
+ const identifier = CategoryIdentifierSchema.parse({
152
+ key: _body.metadata?.["external_id"] || ""
153
+ });
154
+ const name = _body.name;
155
+ const slug = _body.handle;
156
+ const text = _body.description || _body.name || "";
157
+ const parentCategory = _body.parent_category_id ? { key: _body.parent_category?.metadata?.["external_id"] + "" || "" } : void 0;
158
+ const result = {
159
+ identifier,
160
+ name,
161
+ slug,
162
+ text,
163
+ parentCategory,
164
+ images: []
165
+ };
166
+ return this.factory.parseCategory(this.context, result);
167
+ }
168
+ parsePaginatedResult(body) {
169
+ const items = body.product_categories.map((x) => this.parseSingle(x));
170
+ const totalPages = Math.ceil(
171
+ (body.count ?? 0) / Math.max(body.product_categories.length, 1)
172
+ );
173
+ const pageNumber = body.count === 0 ? 1 : Math.floor(body.offset / body.product_categories.length) + 1;
174
+ const result = {
175
+ pageNumber,
176
+ pageSize: Math.max(body.product_categories.length, 1),
177
+ totalCount: body.count,
178
+ totalPages,
179
+ items
180
+ };
181
+ return this.factory.parseCategoryPaginatedResult(this.context, result);
182
+ }
183
+ }
184
+ __decorateClass([
185
+ Reactionary({
186
+ inputSchema: CategoryQueryByIdSchema,
187
+ outputSchema: CategorySchema
188
+ })
189
+ ], MedusaCategoryCapability.prototype, "getById", 1);
190
+ __decorateClass([
191
+ Reactionary({
192
+ inputSchema: CategoryQueryBySlugSchema,
193
+ outputSchema: CategorySchema.nullable()
194
+ })
195
+ ], MedusaCategoryCapability.prototype, "getBySlug", 1);
196
+ __decorateClass([
197
+ Reactionary({
198
+ inputSchema: CategoryQueryForBreadcrumbSchema,
199
+ outputSchema: z.array(CategorySchema)
200
+ })
201
+ ], MedusaCategoryCapability.prototype, "getBreadcrumbPathToCategory", 1);
202
+ __decorateClass([
203
+ Reactionary({
204
+ inputSchema: CategoryQueryForChildCategoriesSchema,
205
+ outputSchema: CategoryPaginatedResultSchema
206
+ })
207
+ ], MedusaCategoryCapability.prototype, "findChildCategories", 1);
208
+ __decorateClass([
209
+ Reactionary({
210
+ inputSchema: CategoryQueryForTopCategoriesSchema,
211
+ outputSchema: CategoryPaginatedResultSchema
212
+ })
213
+ ], MedusaCategoryCapability.prototype, "findTopCategories", 1);
214
+ export {
215
+ MedusaCategoryCapability
216
+ };