@reactionary/provider-medusa 0.1.4 → 0.1.6

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.
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@reactionary/provider-medusa",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "src/index.d.ts",
7
7
  "dependencies": {
8
8
  "zod": "4.1.9",
9
- "@reactionary/core": "0.1.4",
9
+ "@reactionary/core": "0.1.6",
10
10
  "@medusajs/js-sdk": "^2.0.0",
11
11
  "debug": "^4.3.4",
12
12
  "@medusajs/types": "^2.11.0",
@@ -19,7 +19,9 @@ import {
19
19
  ProductSearchResultItemVariantSchema,
20
20
  createPaginatedResponseSchema,
21
21
  Reactionary,
22
- ProductSearchResultSchema
22
+ ProductSearchResultSchema,
23
+ FacetValueIdentifierSchema,
24
+ FacetIdentifierSchema
23
25
  } from "@reactionary/core";
24
26
  import createDebug from "debug";
25
27
  const debug = createDebug("reactionary:medusa:search");
@@ -29,10 +31,53 @@ class MedusaSearchProvider extends ProductSearchProvider {
29
31
  this.client = client;
30
32
  this.config = config;
31
33
  }
34
+ async resolveCategoryIdByExternalId(externalId) {
35
+ const sdk = await this.client.getClient();
36
+ let offset = 0;
37
+ const limit = 50;
38
+ let candidate = void 0;
39
+ while (true) {
40
+ try {
41
+ const categoryResult = await sdk.store.category.list({
42
+ offset,
43
+ limit
44
+ });
45
+ if (categoryResult.product_categories.length === 0) {
46
+ break;
47
+ }
48
+ candidate = categoryResult.product_categories.find(
49
+ (cat) => cat.metadata?.["external_id"] === externalId
50
+ );
51
+ if (candidate) {
52
+ break;
53
+ }
54
+ offset += limit;
55
+ } catch (error) {
56
+ throw new Error(
57
+ "Category not found " + externalId + " due to error: " + error
58
+ );
59
+ break;
60
+ }
61
+ }
62
+ return candidate || null;
63
+ }
32
64
  async queryByTerm(payload) {
33
65
  const client = await this.client.getClient();
66
+ let categoryIdToFind = null;
67
+ if (payload.search.categoryFilter?.key) {
68
+ debug(`Resolving category filter for key: ${payload.search.categoryFilter.key}`);
69
+ const category = await this.resolveCategoryIdByExternalId(payload.search.categoryFilter.key);
70
+ if (category) {
71
+ categoryIdToFind = category.id;
72
+ debug(`Resolved category filter key ${payload.search.categoryFilter.key} to id: ${categoryIdToFind}`);
73
+ } else {
74
+ debug(`Could not resolve category filter for key: ${payload.search.categoryFilter.key}`);
75
+ }
76
+ }
77
+ const finalSearch = (payload.search.term || "").trim().replace("*", "");
34
78
  const response = await client.store.product.list({
35
- q: payload.search.term,
79
+ q: finalSearch,
80
+ ...categoryIdToFind ? { category_id: categoryIdToFind } : {},
36
81
  limit: payload.search.paginationOptions.pageSize,
37
82
  offset: (payload.search.paginationOptions.pageNumber - 1) * payload.search.paginationOptions.pageSize
38
83
  });
@@ -121,6 +166,16 @@ class MedusaSearchProvider extends ProductSearchProvider {
121
166
  image: img
122
167
  });
123
168
  }
169
+ async createCategoryNavigationFilter(payload) {
170
+ const facetIdentifier = FacetIdentifierSchema.parse({
171
+ key: "categories"
172
+ });
173
+ const facetValueIdentifier = FacetValueIdentifierSchema.parse({
174
+ facet: facetIdentifier,
175
+ key: payload.categoryPath[payload.categoryPath.length - 1].identifier.key
176
+ });
177
+ return facetValueIdentifier;
178
+ }
124
179
  parseFacetValue(facetValueIdentifier, label, count) {
125
180
  throw new Error("Method not implemented.");
126
181
  }
@@ -93,6 +93,12 @@ class MedusaProductProvider extends ProductProvider {
93
93
  throw new Error("Product has no variants " + _body.id);
94
94
  }
95
95
  const mainVariant = this.parseVariant(_body.variants[0], _body);
96
+ const otherVariants = [];
97
+ if (_body.variants.length > 1) {
98
+ otherVariants.push(
99
+ ..._body.variants.slice(1).map((variant) => this.parseVariant(variant, _body))
100
+ );
101
+ }
96
102
  const meta = {
97
103
  cache: { hit: false, key: this.generateCacheKeySingle(identifier) },
98
104
  placeholder: false
@@ -110,7 +116,8 @@ class MedusaProductProvider extends ProductProvider {
110
116
  parentCategories,
111
117
  published: true,
112
118
  sharedAttributes,
113
- slug
119
+ slug,
120
+ variants: otherVariants
114
121
  };
115
122
  return result;
116
123
  }
@@ -1,11 +1,12 @@
1
- import { ProductSearchProvider, type Cache, type RequestContext, type ProductSearchQueryByTerm, type ProductSearchResult, type ProductSearchResultItem, type ProductSearchResultItemVariant, type FacetIdentifier, type FacetValueIdentifier, type ProductSearchResultFacet, type ProductSearchResultFacetValue } from '@reactionary/core';
1
+ import { ProductSearchProvider, type Cache, type RequestContext, type ProductSearchQueryByTerm, type ProductSearchResult, type ProductSearchResultItem, type ProductSearchResultItemVariant, type FacetIdentifier, type FacetValueIdentifier, type ProductSearchResultFacet, type ProductSearchResultFacetValue, type ProductSearchQueryCreateNavigationFilter } from '@reactionary/core';
2
2
  import type { MedusaConfiguration } from '../schema/configuration.schema.js';
3
3
  import type { MedusaClient } from '../core/client.js';
4
- import type { StoreProduct, StoreProductListResponse, StoreProductVariant } from '@medusajs/types';
4
+ import type { StoreProduct, StoreProductCategory, StoreProductListResponse, StoreProductVariant } from '@medusajs/types';
5
5
  export declare class MedusaSearchProvider extends ProductSearchProvider {
6
6
  client: MedusaClient;
7
7
  protected config: MedusaConfiguration;
8
8
  constructor(config: MedusaConfiguration, cache: Cache, context: RequestContext, client: MedusaClient);
9
+ protected resolveCategoryIdByExternalId(externalId: string): Promise<StoreProductCategory | null>;
9
10
  queryByTerm(payload: ProductSearchQueryByTerm): Promise<ProductSearchResult>;
10
11
  protected parsePaginatedResult(remote: StoreProductListResponse): {
11
12
  identifier: {
@@ -72,6 +73,7 @@ export declare class MedusaSearchProvider extends ProductSearchProvider {
72
73
  };
73
74
  protected parseSingle(_body: StoreProduct): ProductSearchResultItem;
74
75
  protected parseVariant(variant: StoreProductVariant, product: StoreProduct): ProductSearchResultItemVariant;
76
+ createCategoryNavigationFilter(payload: ProductSearchQueryCreateNavigationFilter): Promise<FacetValueIdentifier>;
75
77
  protected parseFacetValue(facetValueIdentifier: FacetValueIdentifier, label: string, count: number): ProductSearchResultFacetValue;
76
78
  protected parseFacet(facetIdentifier: FacetIdentifier, facetValue: unknown): ProductSearchResultFacet;
77
79
  }
@@ -10,6 +10,9 @@ const testData = {
10
10
  slug: "lv-ca31-scart-cable-101080",
11
11
  image: "https://images.icecat.biz/img/norm/high/101080-3513.jpg",
12
12
  sku: "4960999194479"
13
+ },
14
+ productWithMultiVariants: {
15
+ slug: "hp-gk859aa-mouse-office-bluetooth-laser-1600-dpi-1377612"
13
16
  }
14
17
  };
15
18
  describe("Medusa Product Provider", () => {
@@ -35,16 +38,20 @@ describe("Medusa Product Provider", () => {
35
38
  expect(result.sharedAttributes[1].values.length).toBeGreaterThan(0);
36
39
  expect(result.sharedAttributes[1].values[0].value).toBeTruthy();
37
40
  });
38
- it("should be able to get a product by slug", async () => {
39
- const result = await provider.getBySlug({ slug: testData.product.slug });
41
+ it("should be able to get a product with multiple variants by slug", async () => {
42
+ const result = await provider.getBySlug({ slug: testData.productWithMultiVariants.slug });
40
43
  expect(result).toBeTruthy();
41
44
  if (result) {
42
45
  expect(result.meta.placeholder).toBe(false);
43
46
  expect(result.identifier.key).toBeTruthy();
44
- expect(result.name).toBe(testData.product.name);
47
+ expect(result.slug).toBe(testData.productWithMultiVariants.slug);
45
48
  expect(result.mainVariant).toBeDefined();
46
- expect(result.mainVariant.identifier.sku).toBe(testData.product.sku);
47
- expect(result.mainVariant.images[0].sourceUrl).toBe(testData.product.image);
49
+ expect(result.variants.length).toBeGreaterThan(0);
50
+ expect(result.variants[0].identifier.sku).toBeTruthy();
51
+ expect(result.variants[0].identifier.sku).not.toBe(result.mainVariant.identifier.sku);
52
+ expect(result.sharedAttributes.length).toBeGreaterThan(1);
53
+ expect(result.sharedAttributes[1].values.length).toBeGreaterThan(0);
54
+ expect(result.sharedAttributes[1].values[0].value).toBeTruthy();
48
55
  }
49
56
  });
50
57
  it("should be able to get a product by sku", async () => {
@@ -4,6 +4,7 @@ import { describe, expect, it } from "vitest";
4
4
  import { MedusaSearchProvider } from "../providers/product-search.provider.js";
5
5
  import { getMedusaTestConfiguration } from "./test-utils.js";
6
6
  import { MedusaClient } from "../index.js";
7
+ import { MedusaCategoryProvider } from "../providers/category.provider.js";
7
8
  const testData = {
8
9
  searchTerm: "printer"
9
10
  };
@@ -16,6 +17,12 @@ describe("Medusa Search Provider", () => {
16
17
  reqCtx,
17
18
  client
18
19
  );
20
+ const categoryProvider = new MedusaCategoryProvider(
21
+ getMedusaTestConfiguration(),
22
+ new NoOpCache(),
23
+ reqCtx,
24
+ client
25
+ );
19
26
  it("should be able to get a result by term", async () => {
20
27
  const result = await provider.queryByTerm(ProductSearchQueryByTermSchema.parse({ search: {
21
28
  term: testData.searchTerm,
@@ -54,6 +61,47 @@ describe("Medusa Search Provider", () => {
54
61
  secondPage.items[0].identifier.key
55
62
  );
56
63
  });
64
+ it("should be able to apply a top level category filter", async () => {
65
+ const categories = await categoryProvider.findTopCategories({
66
+ paginationOptions: {
67
+ pageNumber: 1,
68
+ pageSize: 2
69
+ }
70
+ });
71
+ const unfilteredSearch = await provider.queryByTerm({
72
+ search: {
73
+ term: "",
74
+ paginationOptions: {
75
+ pageNumber: 1,
76
+ pageSize: 1
77
+ },
78
+ facets: [],
79
+ filters: []
80
+ }
81
+ });
82
+ expect(unfilteredSearch.totalCount).toBeGreaterThan(0);
83
+ const breadCrumb = await categoryProvider.getBreadcrumbPathToCategory({
84
+ id: categories.items[1].identifier
85
+ });
86
+ expect(breadCrumb.length).toBeGreaterThan(0);
87
+ const categoryFilter = await provider.createCategoryNavigationFilter({
88
+ categoryPath: breadCrumb
89
+ });
90
+ const filteredSearch = await provider.queryByTerm({
91
+ search: {
92
+ term: "",
93
+ categoryFilter,
94
+ paginationOptions: {
95
+ pageNumber: 1,
96
+ pageSize: 1
97
+ },
98
+ facets: [],
99
+ filters: []
100
+ }
101
+ });
102
+ expect(filteredSearch.totalCount).toBeLessThan(unfilteredSearch.totalCount);
103
+ expect(filteredSearch.totalCount).toBeGreaterThan(0);
104
+ });
57
105
  it("should be able to change page size", async () => {
58
106
  const smallPage = await provider.queryByTerm(ProductSearchQueryByTermSchema.parse({ search: {
59
107
  term: testData.searchTerm,