@reactionary/provider-algolia 0.0.81 → 0.0.82
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/core/initialize.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { ProductSearchResultItemSchema } from "@reactionary/core";
|
|
2
1
|
import { AlgoliaSearchProvider } from "../providers/product-search.provider.js";
|
|
3
2
|
function withAlgoliaCapabilities(configuration, capabilities) {
|
|
4
3
|
return (cache, context) => {
|
|
5
4
|
const client = {};
|
|
6
5
|
if (capabilities.productSearch) {
|
|
7
|
-
client.productSearch = new AlgoliaSearchProvider(configuration,
|
|
6
|
+
client.productSearch = new AlgoliaSearchProvider(configuration, cache, context);
|
|
8
7
|
}
|
|
9
8
|
return client;
|
|
10
9
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/provider-algolia",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.82",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "src/index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@reactionary/core": "0.0.
|
|
7
|
+
"@reactionary/core": "0.0.82",
|
|
8
8
|
"algoliasearch": "^5.23.4",
|
|
9
9
|
"zod": "4.1.9"
|
|
10
10
|
},
|
|
@@ -10,7 +10,6 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
10
10
|
return result;
|
|
11
11
|
};
|
|
12
12
|
import {
|
|
13
|
-
createPaginatedResponseSchema,
|
|
14
13
|
FacetIdentifierSchema,
|
|
15
14
|
FacetValueIdentifierSchema,
|
|
16
15
|
ImageSchema,
|
|
@@ -23,10 +22,9 @@ import {
|
|
|
23
22
|
Reactionary
|
|
24
23
|
} from "@reactionary/core";
|
|
25
24
|
import { algoliasearch } from "algoliasearch";
|
|
26
|
-
import { AlgoliaSearchIdentifierSchema } from "../schema/search.schema.js";
|
|
27
25
|
class AlgoliaSearchProvider extends ProductSearchProvider {
|
|
28
|
-
constructor(config,
|
|
29
|
-
super(
|
|
26
|
+
constructor(config, cache, context) {
|
|
27
|
+
super(cache, context);
|
|
30
28
|
this.config = config;
|
|
31
29
|
}
|
|
32
30
|
async queryByTerm(payload) {
|
|
@@ -49,13 +47,7 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
|
|
|
49
47
|
]
|
|
50
48
|
});
|
|
51
49
|
const input = remote.results[0];
|
|
52
|
-
const
|
|
53
|
-
...payload.search,
|
|
54
|
-
index: input.index,
|
|
55
|
-
key: input.queryID
|
|
56
|
-
});
|
|
57
|
-
const result = this.parsePaginatedResult(input);
|
|
58
|
-
result.identifier = identifier;
|
|
50
|
+
const result = this.parsePaginatedResult(input, payload);
|
|
59
51
|
for (const selectedFacet of payload.search.facets) {
|
|
60
52
|
const facet = result.facets.find((f) => f.identifier.key === selectedFacet.facet.key);
|
|
61
53
|
if (facet) {
|
|
@@ -65,19 +57,23 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
|
|
|
65
57
|
}
|
|
66
58
|
}
|
|
67
59
|
}
|
|
68
|
-
result.meta = {
|
|
69
|
-
cache: { hit: false, key: "" },
|
|
70
|
-
placeholder: false
|
|
71
|
-
};
|
|
72
60
|
return result;
|
|
73
61
|
}
|
|
74
62
|
parseSingle(body) {
|
|
75
|
-
const product =
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
63
|
+
const product = {
|
|
64
|
+
identifier: { key: body.objectID },
|
|
65
|
+
name: body.name || body.objectID,
|
|
66
|
+
slug: body.slug || body.objectID,
|
|
67
|
+
variants: [...body.variants || []].map((variant) => this.parseVariant(variant, body)),
|
|
68
|
+
meta: {
|
|
69
|
+
placeholder: false,
|
|
70
|
+
cache: {
|
|
71
|
+
hit: false,
|
|
72
|
+
key: ""
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
return product;
|
|
81
77
|
}
|
|
82
78
|
parseVariant(variant, product) {
|
|
83
79
|
const result = ProductSearchResultItemVariantSchema.parse({
|
|
@@ -91,7 +87,7 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
|
|
|
91
87
|
});
|
|
92
88
|
return result;
|
|
93
89
|
}
|
|
94
|
-
parsePaginatedResult(body) {
|
|
90
|
+
parsePaginatedResult(body, query) {
|
|
95
91
|
const items = body.hits.map((hit) => this.parseSingle(hit));
|
|
96
92
|
const facets = [];
|
|
97
93
|
for (const id in body.facets) {
|
|
@@ -102,18 +98,24 @@ class AlgoliaSearchProvider extends ProductSearchProvider {
|
|
|
102
98
|
const facet = this.parseFacet(facetId, f);
|
|
103
99
|
facets.push(facet);
|
|
104
100
|
}
|
|
105
|
-
const result =
|
|
101
|
+
const result = {
|
|
102
|
+
identifier: {
|
|
103
|
+
term: query.search.term,
|
|
104
|
+
facets: query.search.facets,
|
|
105
|
+
filters: query.search.filters,
|
|
106
|
+
paginationOptions: query.search.paginationOptions
|
|
107
|
+
},
|
|
106
108
|
meta: {
|
|
107
109
|
cache: { hit: false, key: "unknown" },
|
|
108
110
|
placeholder: false
|
|
109
111
|
},
|
|
110
112
|
pageNumber: (body.page || 0) + 1,
|
|
111
|
-
pageSize: body.hitsPerPage,
|
|
112
|
-
totalCount: body.nbHits,
|
|
113
|
-
totalPages: body.nbPages,
|
|
114
|
-
items
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
pageSize: body.hitsPerPage || 0,
|
|
114
|
+
totalCount: body.nbHits || 0,
|
|
115
|
+
totalPages: body.nbPages || 0,
|
|
116
|
+
items,
|
|
117
|
+
facets
|
|
118
|
+
};
|
|
117
119
|
return result;
|
|
118
120
|
}
|
|
119
121
|
parseFacet(facetIdentifier, facetValues) {
|
package/schema/search.schema.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { ProductSearchIdentifierSchema, ProductSearchResultSchema } from "@reactionary/core";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
const
|
|
4
|
-
key: z.string()
|
|
5
|
-
index: z.string()
|
|
3
|
+
const AlgoliaProductSearchIdentifierSchema = ProductSearchIdentifierSchema.extend({
|
|
4
|
+
key: z.string(),
|
|
5
|
+
index: z.string()
|
|
6
6
|
});
|
|
7
|
-
const
|
|
8
|
-
identifier:
|
|
7
|
+
const AlgoliaProductSearchResultSchema = ProductSearchResultSchema.extend({
|
|
8
|
+
identifier: AlgoliaProductSearchIdentifierSchema.default(() => AlgoliaProductSearchIdentifierSchema.parse({}))
|
|
9
9
|
});
|
|
10
10
|
export {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
AlgoliaProductSearchIdentifierSchema,
|
|
12
|
+
AlgoliaProductSearchResultSchema
|
|
13
13
|
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { type Cache, type FacetIdentifier, type FacetValueIdentifier, ProductSearchProvider, type ProductSearchQueryByTerm, type ProductSearchResult, type ProductSearchResultFacet, type ProductSearchResultFacetValue, type
|
|
1
|
+
import { type Cache, type FacetIdentifier, type FacetValueIdentifier, ProductSearchProvider, type ProductSearchQueryByTerm, type ProductSearchResult, type ProductSearchResultFacet, type ProductSearchResultFacetValue, type ProductSearchResultItemVariant, type RequestContext } from '@reactionary/core';
|
|
2
2
|
import { type SearchResponse } from 'algoliasearch';
|
|
3
|
-
import type { z } from 'zod';
|
|
4
3
|
import type { AlgoliaConfiguration } from '../schema/configuration.schema.js';
|
|
5
4
|
interface AlgoliaNativeVariant {
|
|
6
5
|
sku: string;
|
|
@@ -12,27 +11,134 @@ interface AlgoliaNativeRecord {
|
|
|
12
11
|
name?: string;
|
|
13
12
|
variants: Array<AlgoliaNativeVariant>;
|
|
14
13
|
}
|
|
15
|
-
export declare class AlgoliaSearchProvider
|
|
14
|
+
export declare class AlgoliaSearchProvider extends ProductSearchProvider {
|
|
16
15
|
protected config: AlgoliaConfiguration;
|
|
17
|
-
constructor(config: AlgoliaConfiguration,
|
|
16
|
+
constructor(config: AlgoliaConfiguration, cache: Cache, context: RequestContext);
|
|
18
17
|
queryByTerm(payload: ProductSearchQueryByTerm): Promise<ProductSearchResult>;
|
|
19
|
-
protected parseSingle(body: AlgoliaNativeRecord):
|
|
18
|
+
protected parseSingle(body: AlgoliaNativeRecord): {
|
|
19
|
+
identifier: {
|
|
20
|
+
key: string;
|
|
21
|
+
};
|
|
22
|
+
name: string;
|
|
23
|
+
slug: string;
|
|
24
|
+
variants: {
|
|
25
|
+
variant: {
|
|
26
|
+
sku: string;
|
|
27
|
+
};
|
|
28
|
+
image: {
|
|
29
|
+
sourceUrl: string;
|
|
30
|
+
altText: string;
|
|
31
|
+
width?: number | undefined;
|
|
32
|
+
height?: number | undefined;
|
|
33
|
+
};
|
|
34
|
+
options?: {
|
|
35
|
+
identifier: {
|
|
36
|
+
key: string;
|
|
37
|
+
};
|
|
38
|
+
name: string;
|
|
39
|
+
value: {
|
|
40
|
+
identifier: {
|
|
41
|
+
option: {
|
|
42
|
+
key: string;
|
|
43
|
+
};
|
|
44
|
+
key: string;
|
|
45
|
+
};
|
|
46
|
+
label: string;
|
|
47
|
+
};
|
|
48
|
+
} | undefined;
|
|
49
|
+
}[];
|
|
50
|
+
meta: {
|
|
51
|
+
placeholder: false;
|
|
52
|
+
cache: {
|
|
53
|
+
hit: false;
|
|
54
|
+
key: string;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
};
|
|
20
58
|
protected parseVariant(variant: AlgoliaNativeVariant, product: AlgoliaNativeRecord): ProductSearchResultItemVariant;
|
|
21
|
-
protected parsePaginatedResult(body: SearchResponse<AlgoliaNativeRecord
|
|
59
|
+
protected parsePaginatedResult(body: SearchResponse<AlgoliaNativeRecord>, query: ProductSearchQueryByTerm): {
|
|
60
|
+
identifier: {
|
|
61
|
+
term: string;
|
|
62
|
+
facets: {
|
|
63
|
+
facet: {
|
|
64
|
+
key: string;
|
|
65
|
+
};
|
|
66
|
+
key: string;
|
|
67
|
+
}[];
|
|
68
|
+
filters: string[];
|
|
69
|
+
paginationOptions: {
|
|
70
|
+
pageNumber: number;
|
|
71
|
+
pageSize: number;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
22
74
|
meta: {
|
|
23
|
-
[x: string]: unknown;
|
|
24
75
|
cache: {
|
|
25
|
-
|
|
26
|
-
hit: boolean;
|
|
76
|
+
hit: false;
|
|
27
77
|
key: string;
|
|
28
78
|
};
|
|
29
|
-
placeholder:
|
|
79
|
+
placeholder: false;
|
|
30
80
|
};
|
|
31
81
|
pageNumber: number;
|
|
32
82
|
pageSize: number;
|
|
33
83
|
totalCount: number;
|
|
34
84
|
totalPages: number;
|
|
35
|
-
items:
|
|
85
|
+
items: {
|
|
86
|
+
identifier: {
|
|
87
|
+
key: string;
|
|
88
|
+
};
|
|
89
|
+
name: string;
|
|
90
|
+
slug: string;
|
|
91
|
+
variants: {
|
|
92
|
+
variant: {
|
|
93
|
+
sku: string;
|
|
94
|
+
};
|
|
95
|
+
image: {
|
|
96
|
+
sourceUrl: string;
|
|
97
|
+
altText: string;
|
|
98
|
+
width?: number | undefined;
|
|
99
|
+
height?: number | undefined;
|
|
100
|
+
};
|
|
101
|
+
options?: {
|
|
102
|
+
identifier: {
|
|
103
|
+
key: string;
|
|
104
|
+
};
|
|
105
|
+
name: string;
|
|
106
|
+
value: {
|
|
107
|
+
identifier: {
|
|
108
|
+
option: {
|
|
109
|
+
key: string;
|
|
110
|
+
};
|
|
111
|
+
key: string;
|
|
112
|
+
};
|
|
113
|
+
label: string;
|
|
114
|
+
};
|
|
115
|
+
} | undefined;
|
|
116
|
+
}[];
|
|
117
|
+
meta: {
|
|
118
|
+
placeholder: false;
|
|
119
|
+
cache: {
|
|
120
|
+
hit: false;
|
|
121
|
+
key: string;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
}[];
|
|
125
|
+
facets: {
|
|
126
|
+
identifier: {
|
|
127
|
+
key: string;
|
|
128
|
+
};
|
|
129
|
+
name: string;
|
|
130
|
+
values: {
|
|
131
|
+
identifier: {
|
|
132
|
+
facet: {
|
|
133
|
+
key: string;
|
|
134
|
+
};
|
|
135
|
+
key: string;
|
|
136
|
+
};
|
|
137
|
+
name: string;
|
|
138
|
+
count: number;
|
|
139
|
+
active: boolean;
|
|
140
|
+
}[];
|
|
141
|
+
}[];
|
|
36
142
|
};
|
|
37
143
|
protected parseFacet(facetIdentifier: FacetIdentifier, facetValues: Record<string, number>): ProductSearchResultFacet;
|
|
38
144
|
protected parseFacetValue(facetValueIdentifier: FacetValueIdentifier, label: string, count: number): ProductSearchResultFacetValue;
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
export declare const
|
|
3
|
-
term: z.
|
|
4
|
-
facets: z.
|
|
5
|
-
facet: z.
|
|
6
|
-
key: z.
|
|
7
|
-
}, z.core.$loose
|
|
8
|
-
key: z.
|
|
9
|
-
}, z.core.$strip
|
|
10
|
-
filters: z.
|
|
11
|
-
paginationOptions: z.
|
|
2
|
+
export declare const AlgoliaProductSearchIdentifierSchema: z.ZodObject<{
|
|
3
|
+
term: z.ZodString;
|
|
4
|
+
facets: z.ZodArray<z.ZodObject<{
|
|
5
|
+
facet: z.ZodObject<{
|
|
6
|
+
key: z.ZodString;
|
|
7
|
+
}, z.core.$loose>;
|
|
8
|
+
key: z.ZodString;
|
|
9
|
+
}, z.core.$strip>>;
|
|
10
|
+
filters: z.ZodArray<z.ZodString>;
|
|
11
|
+
paginationOptions: z.ZodObject<{
|
|
12
12
|
pageNumber: z.ZodDefault<z.ZodNumber>;
|
|
13
13
|
pageSize: z.ZodDefault<z.ZodNumber>;
|
|
14
|
-
}, z.core.$loose
|
|
15
|
-
key: z.
|
|
16
|
-
index: z.
|
|
14
|
+
}, z.core.$loose>;
|
|
15
|
+
key: z.ZodString;
|
|
16
|
+
index: z.ZodString;
|
|
17
17
|
}, z.core.$loose>;
|
|
18
|
-
export declare const
|
|
18
|
+
export declare const AlgoliaProductSearchResultSchema: z.ZodObject<{
|
|
19
19
|
meta: z.ZodDefault<z.ZodObject<{
|
|
20
20
|
cache: z.ZodDefault<z.ZodObject<{
|
|
21
21
|
hit: z.ZodDefault<z.ZodBoolean>;
|
|
@@ -35,71 +35,71 @@ export declare const AlgoliaSearchResultSchema: z.ZodObject<{
|
|
|
35
35
|
}, z.core.$loose>>;
|
|
36
36
|
placeholder: z.ZodDefault<z.ZodBoolean>;
|
|
37
37
|
}, z.core.$loose>>;
|
|
38
|
-
identifier: z.
|
|
39
|
-
key: z.
|
|
40
|
-
}, z.core.$loose
|
|
41
|
-
name: z.
|
|
42
|
-
slug: z.
|
|
43
|
-
variants: z.
|
|
44
|
-
variant: z.
|
|
45
|
-
sku: z.
|
|
46
|
-
}, z.core.$loose
|
|
47
|
-
image: z.
|
|
38
|
+
identifier: z.ZodObject<{
|
|
39
|
+
key: z.ZodString;
|
|
40
|
+
}, z.core.$loose>;
|
|
41
|
+
name: z.ZodString;
|
|
42
|
+
slug: z.ZodString;
|
|
43
|
+
variants: z.ZodArray<z.ZodObject<{
|
|
44
|
+
variant: z.ZodObject<{
|
|
45
|
+
sku: z.ZodString;
|
|
46
|
+
}, z.core.$loose>;
|
|
47
|
+
image: z.ZodObject<{
|
|
48
48
|
sourceUrl: z.ZodDefault<z.ZodString>;
|
|
49
49
|
altText: z.ZodDefault<z.ZodString>;
|
|
50
50
|
width: z.ZodOptional<z.ZodNumber>;
|
|
51
51
|
height: z.ZodOptional<z.ZodNumber>;
|
|
52
|
-
}, z.core.$loose
|
|
52
|
+
}, z.core.$loose>;
|
|
53
53
|
options: z.ZodOptional<z.ZodObject<{
|
|
54
|
-
identifier: z.
|
|
55
|
-
key: z.
|
|
56
|
-
}, z.core.$loose
|
|
54
|
+
identifier: z.ZodObject<{
|
|
55
|
+
key: z.ZodString;
|
|
56
|
+
}, z.core.$loose>;
|
|
57
57
|
name: z.ZodString;
|
|
58
|
-
value: z.
|
|
59
|
-
identifier: z.
|
|
60
|
-
option: z.
|
|
61
|
-
key: z.
|
|
62
|
-
}, z.core.$loose
|
|
63
|
-
key: z.
|
|
64
|
-
}, z.core.$loose
|
|
58
|
+
value: z.ZodObject<{
|
|
59
|
+
identifier: z.ZodObject<{
|
|
60
|
+
option: z.ZodObject<{
|
|
61
|
+
key: z.ZodString;
|
|
62
|
+
}, z.core.$loose>;
|
|
63
|
+
key: z.ZodString;
|
|
64
|
+
}, z.core.$loose>;
|
|
65
65
|
label: z.ZodString;
|
|
66
|
-
}, z.core.$loose
|
|
66
|
+
}, z.core.$loose>;
|
|
67
67
|
}, z.core.$loose>>;
|
|
68
|
-
}, z.core.$loose
|
|
68
|
+
}, z.core.$loose>>;
|
|
69
69
|
}, z.core.$loose>>;
|
|
70
|
-
facets: z.
|
|
71
|
-
identifier: z.
|
|
72
|
-
key: z.
|
|
70
|
+
facets: z.ZodArray<z.ZodObject<{
|
|
71
|
+
identifier: z.ZodObject<{
|
|
72
|
+
key: z.ZodString;
|
|
73
|
+
}, z.core.$loose>;
|
|
74
|
+
name: z.ZodString;
|
|
75
|
+
values: z.ZodArray<z.ZodObject<{
|
|
76
|
+
identifier: z.ZodObject<{
|
|
77
|
+
facet: z.ZodObject<{
|
|
78
|
+
key: z.ZodString;
|
|
79
|
+
}, z.core.$loose>;
|
|
80
|
+
key: z.ZodString;
|
|
81
|
+
}, z.core.$strip>;
|
|
82
|
+
name: z.ZodString;
|
|
83
|
+
count: z.ZodNumber;
|
|
84
|
+
active: z.ZodBoolean;
|
|
73
85
|
}, z.core.$loose>>;
|
|
74
|
-
|
|
75
|
-
values: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
76
|
-
identifier: z.ZodDefault<z.ZodObject<{
|
|
77
|
-
facet: z.ZodDefault<z.ZodObject<{
|
|
78
|
-
key: z.ZodNonOptional<z.ZodDefault<z.ZodString>>;
|
|
79
|
-
}, z.core.$loose>>;
|
|
80
|
-
key: z.ZodDefault<z.ZodString>;
|
|
81
|
-
}, z.core.$strip>>;
|
|
82
|
-
name: z.ZodDefault<z.ZodString>;
|
|
83
|
-
count: z.ZodDefault<z.ZodNumber>;
|
|
84
|
-
active: z.ZodDefault<z.ZodBoolean>;
|
|
85
|
-
}, z.core.$loose>>>;
|
|
86
|
-
}, z.core.$loose>>>;
|
|
86
|
+
}, z.core.$loose>>;
|
|
87
87
|
identifier: z.ZodDefault<z.ZodObject<{
|
|
88
|
-
term: z.
|
|
89
|
-
facets: z.
|
|
90
|
-
facet: z.
|
|
91
|
-
key: z.
|
|
92
|
-
}, z.core.$loose
|
|
93
|
-
key: z.
|
|
94
|
-
}, z.core.$strip
|
|
95
|
-
filters: z.
|
|
96
|
-
paginationOptions: z.
|
|
88
|
+
term: z.ZodString;
|
|
89
|
+
facets: z.ZodArray<z.ZodObject<{
|
|
90
|
+
facet: z.ZodObject<{
|
|
91
|
+
key: z.ZodString;
|
|
92
|
+
}, z.core.$loose>;
|
|
93
|
+
key: z.ZodString;
|
|
94
|
+
}, z.core.$strip>>;
|
|
95
|
+
filters: z.ZodArray<z.ZodString>;
|
|
96
|
+
paginationOptions: z.ZodObject<{
|
|
97
97
|
pageNumber: z.ZodDefault<z.ZodNumber>;
|
|
98
98
|
pageSize: z.ZodDefault<z.ZodNumber>;
|
|
99
|
-
}, z.core.$loose
|
|
100
|
-
key: z.
|
|
101
|
-
index: z.
|
|
99
|
+
}, z.core.$loose>;
|
|
100
|
+
key: z.ZodString;
|
|
101
|
+
index: z.ZodString;
|
|
102
102
|
}, z.core.$loose>>;
|
|
103
103
|
}, z.core.$strip>;
|
|
104
|
-
export type
|
|
105
|
-
export type
|
|
104
|
+
export type AlgoliaProductSearchResult = z.infer<typeof AlgoliaProductSearchResultSchema>;
|
|
105
|
+
export type AlgoliaProductSearchIdentifier = z.infer<typeof AlgoliaProductSearchIdentifierSchema>;
|