@reactionary/provider-algolia 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,10 +1,10 @@
1
1
  {
2
2
  "name": "@reactionary/provider-algolia",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
7
- "@reactionary/core": "0.1.5",
7
+ "@reactionary/core": "0.1.7",
8
8
  "algoliasearch": "^5.23.4",
9
9
  "zod": "4.1.9"
10
10
  },
@@ -29,6 +29,17 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
29
29
  }
30
30
  async queryByTerm(payload) {
31
31
  const client = algoliasearch(this.config.appId, this.config.apiKey);
32
+ const facetsThatAreNotCategory = payload.search.facets.filter((x) => x.facet.key !== "categories");
33
+ const categoryFacet = payload.search.facets.find((x) => x.facet.key === "categories") || payload.search.categoryFilter;
34
+ const finalFilters = [...payload.search.filters || []];
35
+ const finalFacetFilters = [
36
+ ...facetsThatAreNotCategory.map(
37
+ (x) => `${encodeURIComponent(x.facet.key)}:"${x.key}"`
38
+ )
39
+ ];
40
+ if (categoryFacet) {
41
+ finalFilters.push(`categories:"${categoryFacet.key}"`);
42
+ }
32
43
  const remote = await client.search({
33
44
  requests: [
34
45
  {
@@ -39,10 +50,8 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
39
50
  facets: ["*"],
40
51
  analytics: true,
41
52
  clickAnalytics: true,
42
- facetFilters: payload.search.facets.map(
43
- (x) => `${encodeURIComponent(x.facet.key)}:${x.key}`
44
- ),
45
- filters: (payload.search.filters || []).map((x) => `${encodeURIComponent(x)}`).join(" AND ")
53
+ facetFilters: finalFacetFilters,
54
+ filters: (finalFilters || []).join(" AND ")
46
55
  }
47
56
  ]
48
57
  });
@@ -59,6 +68,16 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
59
68
  }
60
69
  return result;
61
70
  }
71
+ createCategoryNavigationFilter(payload) {
72
+ const facetIdentifier = FacetIdentifierSchema.parse({
73
+ key: "categories"
74
+ });
75
+ const facetValueIdentifier = FacetValueIdentifierSchema.parse({
76
+ facet: facetIdentifier,
77
+ key: payload.categoryPath.map((c) => c.name).join(" > ")
78
+ });
79
+ return Promise.resolve(facetValueIdentifier);
80
+ }
62
81
  parseSingle(body) {
63
82
  const product = {
64
83
  identifier: { key: body.objectID },
@@ -89,7 +108,7 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
89
108
  }
90
109
  parsePaginatedResult(body, query) {
91
110
  const items = body.hits.map((hit) => this.parseSingle(hit));
92
- const facets = [];
111
+ let facets = [];
93
112
  for (const id in body.facets) {
94
113
  const f = body.facets[id];
95
114
  const facetId = FacetIdentifierSchema.parse({
@@ -98,6 +117,26 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
98
117
  const facet = this.parseFacet(facetId, f);
99
118
  facets.push(facet);
100
119
  }
120
+ const selectedCategoryFacet = query.search.facets.find((x) => x.facet.key === "categories") || query.search.categoryFilter;
121
+ let subCategoryFacet;
122
+ if (selectedCategoryFacet) {
123
+ const valueDepth = selectedCategoryFacet.key.split(" > ").length;
124
+ subCategoryFacet = facets.find((f) => f.identifier.key === `hierarchy.lvl${valueDepth}`);
125
+ } else {
126
+ subCategoryFacet = facets.find((f) => f.identifier.key === "hierarchy.lvl0");
127
+ }
128
+ if (subCategoryFacet) {
129
+ subCategoryFacet.identifier = FacetIdentifierSchema.parse({
130
+ key: "categories"
131
+ });
132
+ subCategoryFacet.name = "Categories";
133
+ for (const v of subCategoryFacet.values) {
134
+ const pathParts = v.identifier.key.split(" > ");
135
+ v.identifier.facet = subCategoryFacet.identifier;
136
+ v.name = pathParts[pathParts.length - 1];
137
+ }
138
+ }
139
+ facets = facets.filter((f) => !f.identifier.key.startsWith("hierarchy.lvl"));
101
140
  const result = {
102
141
  identifier: {
103
142
  term: query.search.term,
@@ -1,4 +1,4 @@
1
- import { type Cache, type FacetIdentifier, type FacetValueIdentifier, ProductSearchProvider, type ProductSearchQueryByTerm, type ProductSearchResult, type ProductSearchResultFacet, type ProductSearchResultFacetValue, type ProductSearchResultItemVariant, type RequestContext } from '@reactionary/core';
1
+ import { type Cache, type FacetIdentifier, type FacetValueIdentifier, ProductSearchProvider, type ProductSearchQueryByTerm, type ProductSearchQueryCreateNavigationFilter, type ProductSearchResult, type ProductSearchResultFacet, type ProductSearchResultFacetValue, type ProductSearchResultItemVariant, type RequestContext } from '@reactionary/core';
2
2
  import { type SearchResponse } from 'algoliasearch';
3
3
  import type { AlgoliaConfiguration } from '../schema/configuration.schema.js';
4
4
  interface AlgoliaNativeVariant {
@@ -15,6 +15,7 @@ export declare class AlgoliaSearchProvider extends ProductSearchProvider {
15
15
  protected config: AlgoliaConfiguration;
16
16
  constructor(config: AlgoliaConfiguration, cache: Cache, context: RequestContext);
17
17
  queryByTerm(payload: ProductSearchQueryByTerm): Promise<ProductSearchResult>;
18
+ createCategoryNavigationFilter(payload: ProductSearchQueryCreateNavigationFilter): Promise<FacetValueIdentifier>;
18
19
  protected parseSingle(body: AlgoliaNativeRecord): {
19
20
  identifier: {
20
21
  key: string;
@@ -12,6 +12,12 @@ export declare const AlgoliaProductSearchIdentifierSchema: z.ZodObject<{
12
12
  pageNumber: z.ZodDefault<z.ZodNumber>;
13
13
  pageSize: z.ZodDefault<z.ZodNumber>;
14
14
  }, z.core.$loose>;
15
+ categoryFilter: z.ZodOptional<z.ZodObject<{
16
+ facet: z.ZodObject<{
17
+ key: z.ZodString;
18
+ }, z.core.$loose>;
19
+ key: z.ZodString;
20
+ }, z.core.$strip>>;
15
21
  key: z.ZodString;
16
22
  index: z.ZodString;
17
23
  }, z.core.$loose>;
@@ -97,6 +103,12 @@ export declare const AlgoliaProductSearchResultSchema: z.ZodObject<{
97
103
  pageNumber: z.ZodDefault<z.ZodNumber>;
98
104
  pageSize: z.ZodDefault<z.ZodNumber>;
99
105
  }, z.core.$loose>;
106
+ categoryFilter: z.ZodOptional<z.ZodObject<{
107
+ facet: z.ZodObject<{
108
+ key: z.ZodString;
109
+ }, z.core.$loose>;
110
+ key: z.ZodString;
111
+ }, z.core.$strip>>;
100
112
  key: z.ZodString;
101
113
  index: z.ZodString;
102
114
  }, z.core.$loose>>;