@reactionary/medusa 0.7.5 → 0.8.0

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.
@@ -134,7 +134,27 @@ class MedusaCartCapability extends CartCapability {
134
134
  payload.quantity
135
135
  );
136
136
  }
137
- const variantId = await this.medusaApi.resolveVariantId(payload.variant.sku);
137
+ const productResponse = await client.store.product.list({
138
+ variants: {
139
+ sku: payload.variant.sku
140
+ },
141
+ limit: 1
142
+ });
143
+ if (productResponse.products.length === 0) {
144
+ return error({
145
+ type: "NotFound",
146
+ identifier: payload
147
+ });
148
+ }
149
+ const product = productResponse.products[0];
150
+ const variant = product.variants?.find((v) => v.sku === payload.variant.sku);
151
+ if (!variant) {
152
+ return error({
153
+ type: "NotFound",
154
+ identifier: payload
155
+ });
156
+ }
157
+ const variantId = variant.id;
138
158
  const response = await client.store.cart.createLineItem(
139
159
  medusaId.key,
140
160
  this.addPayload(payload, variantId),
@@ -190,7 +210,7 @@ class MedusaCartCapability extends CartCapability {
190
210
  createCartPayload(payload) {
191
211
  return {
192
212
  currency_code: (this.context.languageContext.currencyCode || "EUR").toLowerCase(),
193
- locale: this.context.languageContext.locale || "en",
213
+ locale: this.context.languageContext.locale,
194
214
  metadata: {
195
215
  name: payload.name
196
216
  }
@@ -32,35 +32,18 @@ class MedusaCategoryCapability extends CategoryCapability {
32
32
  }
33
33
  config;
34
34
  factory;
35
+ alwaysIncludedFields = ["+metadata", "+external_id", "+parent_category.external_id"];
35
36
  async resolveCategoryIdByExternalId(externalId) {
36
37
  const sdk = await this.medusaApi.getClient();
37
- let offset = 0;
38
- const limit = 50;
39
- let candidate = void 0;
40
- while (true) {
41
- try {
42
- const categoryResult = await sdk.store.category.list({
43
- offset,
44
- limit
45
- });
46
- if (categoryResult.product_categories.length === 0) {
47
- break;
48
- }
49
- candidate = categoryResult.product_categories.find(
50
- (cat) => cat.metadata?.["external_id"] === externalId
51
- );
52
- if (candidate) {
53
- break;
54
- }
55
- offset += limit;
56
- } catch (error2) {
57
- throw new Error(
58
- "Category not found " + externalId + " due to error: " + error2
59
- );
60
- break;
61
- }
38
+ const categoryResult = await sdk.store.category.list({
39
+ fields: this.alwaysIncludedFields.join(","),
40
+ external_id: externalId,
41
+ limit: 1
42
+ });
43
+ if (categoryResult.count === 0) {
44
+ return null;
62
45
  }
63
- return candidate || null;
46
+ return categoryResult.product_categories[0];
64
47
  }
65
48
  async getById(payload) {
66
49
  const candidate = await this.resolveCategoryIdByExternalId(payload.id.key);
@@ -75,6 +58,7 @@ class MedusaCategoryCapability extends CategoryCapability {
75
58
  getBySlugPayload(payload) {
76
59
  return {
77
60
  handle: payload.slug,
61
+ fields: this.alwaysIncludedFields.join(","),
78
62
  limit: 1,
79
63
  offset: 0
80
64
  };
@@ -106,7 +90,7 @@ class MedusaCategoryCapability extends CategoryCapability {
106
90
  }
107
91
  const sdk = await this.medusaApi.getClient();
108
92
  const path = await sdk.store.category.retrieve(actualCategoryId.id, {
109
- fields: "+metadata,+parent_category.metadata",
93
+ fields: this.alwaysIncludedFields.join(","),
110
94
  include_ancestors_tree: true
111
95
  });
112
96
  let results = new Array();
@@ -120,7 +104,7 @@ class MedusaCategoryCapability extends CategoryCapability {
120
104
  }
121
105
  findChildCategoriesPayload(payload, actualParent) {
122
106
  return {
123
- fields: "+metadata,+parent_category.metadata",
107
+ fields: this.alwaysIncludedFields.join(","),
124
108
  parent_category_id: actualParent.id,
125
109
  limit: payload.paginationOptions.pageSize,
126
110
  offset: (payload.paginationOptions.pageNumber - 1) * payload.paginationOptions.pageSize
@@ -146,7 +130,7 @@ class MedusaCategoryCapability extends CategoryCapability {
146
130
  }
147
131
  findTopCategoriesPayload(payload) {
148
132
  return {
149
- fields: "+metadata",
133
+ fields: this.alwaysIncludedFields.join(","),
150
134
  parent_category_id: "null",
151
135
  limit: payload.paginationOptions.pageSize,
152
136
  offset: (payload.paginationOptions.pageNumber - 1) * payload.paginationOptions.pageSize
@@ -42,12 +42,19 @@ class MedusaPriceCapability extends PriceCapability {
42
42
  debug(`Fetching price for SKU: ${sku}`);
43
43
  }
44
44
  try {
45
- const productForSKU = await this.medusaApi.resolveProductForSKU(payload.variant.sku);
46
45
  const client = await this.medusaApi.getClient();
47
- const product = (await client.store.product.retrieve(
48
- productForSKU.id || "",
49
- { region_id: (await this.medusaApi.getActiveRegion()).id }
50
- )).product;
46
+ const regionId = await this.medusaApi.getActiveRegion();
47
+ const productResponse = await client.store.product.list({
48
+ variants: {
49
+ sku: payload.variant.sku
50
+ },
51
+ limit: 1,
52
+ region_id: regionId.id
53
+ });
54
+ if (productResponse.products.length === 0) {
55
+ return this.createEmptyPriceResult(sku);
56
+ }
57
+ const product = productResponse.products[0];
51
58
  const variant = product.variants?.find((v) => v.sku === sku);
52
59
  if (!variant) {
53
60
  if (debug.enabled) {
@@ -95,7 +95,21 @@ class MedusaProductCapability extends ProductCapability {
95
95
  );
96
96
  }
97
97
  const sku = payload.variant.sku;
98
- const product = await this.medusaApi.resolveProductForSKU(sku);
98
+ const client = await this.medusaApi.getClient();
99
+ const productResponse = await client.store.product.list({
100
+ variants: {
101
+ sku
102
+ },
103
+ limit: 1,
104
+ fields: this.alwaysIncludedFields.join(",")
105
+ });
106
+ if (productResponse.products.length === 0) {
107
+ return error({
108
+ type: "NotFound",
109
+ identifier: payload
110
+ });
111
+ }
112
+ const product = productResponse.products[0];
99
113
  const variant = product.variants?.find((v) => v.sku === sku);
100
114
  if (!variant) {
101
115
  throw new Error(`Variant with SKU ${sku} not found`);
package/core/client.js CHANGED
@@ -70,16 +70,13 @@ class MedusaAdminAPI {
70
70
  constructor(config, context) {
71
71
  this.config = config;
72
72
  this.context = context;
73
- console.log(
74
- "MedusaAdminClient config:",
75
- this.config,
76
- "Debug enabled:",
77
- debug.enabled
78
- );
79
73
  this.client = new Medusa({
80
74
  baseUrl: this.config.apiUrl,
81
75
  apiKey: this.config.adminApiKey,
82
- debug: true
76
+ debug: true,
77
+ globalHeaders: {
78
+ "x-medusa-locale": this.context.languageContext.locale
79
+ }
83
80
  });
84
81
  }
85
82
  async getClient() {
@@ -93,18 +90,14 @@ class MedusaAPI {
93
90
  constructor(config, context) {
94
91
  this.config = config;
95
92
  this.context = context;
96
- console.log(
97
- "MedusaClient config:",
98
- this.config,
99
- "Debug enabled:",
100
- debug.enabled
101
- );
102
93
  this.client = void 0;
103
94
  }
104
95
  async getActiveRegion() {
105
96
  const session = this.getSessionData();
106
97
  if (session.selectedRegion) {
107
- return session.selectedRegion;
98
+ if (session.selectedRegion.currency_code === this.context.languageContext.currencyCode.toLowerCase()) {
99
+ return session.selectedRegion;
100
+ }
108
101
  }
109
102
  const regions = await (await this.getClient()).store.region.list();
110
103
  const allRegions = [];
@@ -125,39 +118,6 @@ class MedusaAPI {
125
118
  });
126
119
  return selectedRegion;
127
120
  }
128
- async resolveProductForSKU(sku) {
129
- const adminClient = await new MedusaAdminAPI(
130
- this.config,
131
- this.context
132
- ).getClient();
133
- const productsResponse = await adminClient.admin.product.list({
134
- limit: 1,
135
- offset: 0,
136
- fields: "+metadata.*,+categories.metadata.*,+external_id",
137
- variants: {
138
- $or: [{ ean: sku }, { upc: sku }, { barcode: sku }]
139
- }
140
- });
141
- const product = productsResponse.products[0];
142
- if (!product) {
143
- throw new Error(`Product with SKU ${sku} not found`);
144
- }
145
- return product;
146
- }
147
- /**
148
- * This function should not need to exist.
149
- * It returns a product-id, given a SKU.
150
- * @param sku
151
- * @returns
152
- */
153
- async resolveVariantId(sku) {
154
- const product = await this.resolveProductForSKU(sku);
155
- const variant = product.variants?.find((v) => v.sku === sku);
156
- if (!variant) {
157
- throw new Error(`Variant with SKU ${sku} not found`);
158
- }
159
- return variant.id;
160
- }
161
121
  async getClient() {
162
122
  if (!this.client) {
163
123
  this.client = this.createAuthenticatedClient(this.context);
@@ -253,6 +213,9 @@ class MedusaAPI {
253
213
  baseUrl: this.config.apiUrl,
254
214
  publishableKey: this.config.publishable_key,
255
215
  debug: true,
216
+ globalHeaders: {
217
+ "x-medusa-locale": this.context.languageContext.locale
218
+ },
256
219
  auth: {
257
220
  type: "jwt",
258
221
  jwtTokenStorageMethod: "custom",
@@ -10,12 +10,12 @@ class MedusaCategoryFactory {
10
10
  }
11
11
  parseCategory(_context, data) {
12
12
  const identifier = CategoryIdentifierSchema.parse({
13
- key: data.metadata?.["external_id"] || ""
13
+ key: data.external_id || ""
14
14
  });
15
15
  const name = data.name;
16
16
  const slug = data.handle;
17
- const text = data.description || data.name || "";
18
- const parentCategory = data.parent_category_id ? { key: data.parent_category?.metadata?.["external_id"] + "" || "" } : void 0;
17
+ const text = data.description || "";
18
+ const parentCategory = data.parent_category_id ? { key: data.parent_category?.external_id || "" } : void 0;
19
19
  const result = {
20
20
  identifier,
21
21
  name,
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@reactionary/medusa",
3
- "version": "0.7.5",
3
+ "version": "0.8.0",
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.3",
9
- "@reactionary/core": "0.7.5",
9
+ "@reactionary/core": "0.8.0",
10
10
  "debug": "^4.4.3",
11
11
  "@medusajs/types": "^2.13.3",
12
12
  "zod": "4.1.9",
@@ -7,11 +7,13 @@ export declare class MedusaCategoryCapability<TFactory extends CategoryFactory =
7
7
  medusaApi: MedusaAPI;
8
8
  protected config: MedusaConfiguration;
9
9
  protected factory: CategoryFactoryWithOutput<TFactory>;
10
+ protected alwaysIncludedFields: string[];
10
11
  constructor(config: MedusaConfiguration, cache: Cache, context: RequestContext, medusaApi: MedusaAPI, factory: CategoryFactoryWithOutput<TFactory>);
11
12
  protected resolveCategoryIdByExternalId(externalId: string): Promise<StoreProductCategory | null>;
12
13
  getById(payload: CategoryQueryById): Promise<Result<CategoryFactoryCategoryOutput<TFactory>, NotFoundError>>;
13
14
  protected getBySlugPayload(payload: CategoryQueryBySlug): {
14
15
  handle: string;
16
+ fields: string;
15
17
  limit: number;
16
18
  offset: number;
17
19
  };
@@ -2,7 +2,6 @@ import { Admin, Auth, Client, type Config, Store } from '@medusajs/js-sdk';
2
2
  import type { MedusaConfiguration } from '../schema/configuration.schema.js';
3
3
  import { type RequestContext } from '@reactionary/core';
4
4
  import { type MedusaSession } from '../index.js';
5
- import type { StoreProduct } from '@medusajs/types';
6
5
  export declare const SESSION_KEY = "MEDUSA_PROVIDER";
7
6
  export interface MedusaAuthToken {
8
7
  token: string;
@@ -46,14 +45,6 @@ export declare class MedusaAPI {
46
45
  name: string;
47
46
  currency_code: string;
48
47
  }>;
49
- resolveProductForSKU(sku: string): Promise<StoreProduct>;
50
- /**
51
- * This function should not need to exist.
52
- * It returns a product-id, given a SKU.
53
- * @param sku
54
- * @returns
55
- */
56
- resolveVariantId(sku: string): Promise<string>;
57
48
  getClient(): Promise<Medusa>;
58
49
  getSessionData(): MedusaSession;
59
50
  setSessionData(sessionData: Partial<MedusaSession>): void;