forked from typesense/typesense-instantsearch-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypesenseInstantsearchAdapter.js
104 lines (91 loc) · 3.34 KB
/
TypesenseInstantsearchAdapter.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"use strict";
import { Configuration } from "./Configuration";
import { SearchClient as TypesenseSearchClient } from "typesense";
import { SearchRequestAdapter } from "./SearchRequestAdapter";
import { SearchResponseAdapter } from "./SearchResponseAdapter";
import { FacetSearchResponseAdapter } from "./FacetSearchResponseAdapter";
export default class TypesenseInstantsearchAdapter {
constructor(options) {
this.updateConfiguration(options);
this.searchClient = {
clearCache: () => this.clearCache(),
search: (instantsearchRequests) => this.searchTypesenseAndAdapt(instantsearchRequests),
searchForFacetValues: (instantsearchRequests) =>
this.searchTypesenseForFacetValuesAndAdapt(instantsearchRequests),
};
}
async searchTypesenseAndAdapt(instantsearchRequests) {
let typesenseResponse;
try {
typesenseResponse = await this._adaptAndPerformTypesenseRequest(instantsearchRequests);
const adaptedResponses = typesenseResponse.results.map((typesenseResult, index) => {
this._validateTypesenseResult(typesenseResult);
const responseAdapter = new SearchResponseAdapter(
typesenseResult,
instantsearchRequests[index],
this.configuration,
typesenseResponse.results,
typesenseResponse,
);
let adaptedResponse = responseAdapter.adapt();
return adaptedResponse;
});
return {
results: adaptedResponses,
};
} catch (error) {
console.error(error);
throw error;
}
}
async searchTypesenseForFacetValuesAndAdapt(instantsearchRequests) {
let typesenseResponse;
try {
typesenseResponse = await this._adaptAndPerformTypesenseRequest(instantsearchRequests);
const adaptedResponses = typesenseResponse.results.map((typesenseResult, index) => {
this._validateTypesenseResult(typesenseResult);
const responseAdapter = new FacetSearchResponseAdapter(
typesenseResult,
instantsearchRequests[index],
this.configuration,
);
return responseAdapter.adapt();
});
return adaptedResponses;
} catch (error) {
console.error(error);
throw error;
}
}
async _adaptAndPerformTypesenseRequest(instantsearchRequests) {
const requestAdapter = new SearchRequestAdapter(instantsearchRequests, this.typesenseClient, this.configuration);
const typesenseResponse = await requestAdapter.request();
if (typeof typesenseResponse === "string") {
try {
return JSON.parse(typesenseResponse);
} catch (error) {
return typesenseResponse;
}
} else {
return typesenseResponse;
}
}
clearCache() {
this.typesenseClient = new TypesenseSearchClient(this.configuration.server);
return this.searchClient;
}
updateConfiguration(options) {
this.configuration = new Configuration(options);
this.configuration.validate();
this.typesenseClient = new TypesenseSearchClient(this.configuration.server);
return true;
}
_validateTypesenseResult(typesenseResult) {
if (typesenseResult.error) {
throw new Error(`${typesenseResult.code} - ${typesenseResult.error}`);
}
if (!typesenseResult.hits && !typesenseResult.grouped_hits) {
throw new Error(`Did not find any hits. ${typesenseResult.code} - ${typesenseResult.error}`);
}
}
}