forked from WPMedia/arc-themes-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algolia-assortment.js
49 lines (45 loc) · 1.18 KB
/
algolia-assortment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import algoliasearch from "algoliasearch/lite";
import { algoliaSearchKey, algoliaAppId } from "fusion:environment";
const params = {
filters: "text",
index: "text",
query: "text",
ruleContexts: "text",
hitsPerPage: "number",
page: "number",
};
const fetch = async (contentSourceParameters) => {
const { filters, index, query, ruleContexts } = contentSourceParameters;
let { hitsPerPage, page } = contentSourceParameters;
// parameters in content source are defined as null by default
if (hitsPerPage === null) {
hitsPerPage = 20;
}
if (page === null) {
page = 0;
}
const client = algoliasearch(algoliaAppId, algoliaSearchKey);
const clientSearchIndex = client.initIndex(index);
try {
const { hits } = await clientSearchIndex.search(query, {
// array of strings
// https://www.algolia.com/doc/api-reference/api-parameters/ruleContexts/
ruleContexts: [ruleContexts],
filters,
hitsPerPage,
page,
});
return hits;
} catch (err) {
const error = new Error(err);
error.statusCode = 500;
throw error;
}
};
export default {
fetch,
params,
transform: (data) => data,
// Time to live cache set to the Fusion minimum of 2 minutes (120 seconds)
ttl: 120,
};