@reactionary/provider-medusa 0.1.5 → 0.1.7

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.5",
3
+ "version": "0.1.7",
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.5",
9
+ "@reactionary/core": "0.1.7",
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
  }
@@ -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
  }
@@ -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,62 @@ 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
+ let candidate = categories.items[0];
72
+ while (candidate) {
73
+ const children = await categoryProvider.findChildCategories({
74
+ parentId: candidate.identifier,
75
+ paginationOptions: {
76
+ pageNumber: 1,
77
+ pageSize: 10
78
+ }
79
+ });
80
+ if (children.items.length > 0) {
81
+ candidate = children.items[0];
82
+ } else {
83
+ break;
84
+ }
85
+ }
86
+ const unfilteredSearch = await provider.queryByTerm({
87
+ search: {
88
+ term: "",
89
+ paginationOptions: {
90
+ pageNumber: 1,
91
+ pageSize: 1
92
+ },
93
+ facets: [],
94
+ filters: []
95
+ }
96
+ });
97
+ expect(unfilteredSearch.totalCount).toBeGreaterThan(0);
98
+ const breadCrumb = await categoryProvider.getBreadcrumbPathToCategory({
99
+ id: candidate.identifier
100
+ });
101
+ expect(breadCrumb.length).toBeGreaterThan(0);
102
+ const categoryFilter = await provider.createCategoryNavigationFilter({
103
+ categoryPath: breadCrumb
104
+ });
105
+ const filteredSearch = await provider.queryByTerm({
106
+ search: {
107
+ term: "",
108
+ categoryFilter,
109
+ paginationOptions: {
110
+ pageNumber: 1,
111
+ pageSize: 1
112
+ },
113
+ facets: [],
114
+ filters: []
115
+ }
116
+ });
117
+ expect(filteredSearch.totalCount).toBeLessThan(unfilteredSearch.totalCount);
118
+ expect(filteredSearch.totalCount).toBeGreaterThan(0);
119
+ });
57
120
  it("should be able to change page size", async () => {
58
121
  const smallPage = await provider.queryByTerm(ProductSearchQueryByTermSchema.parse({ search: {
59
122
  term: testData.searchTerm,