|
1
|
import { |
|
2
|
adaptFacetSearchRequest, |
|
3
|
adaptSearchRequest, |
|
4
|
parseSearchParams, |
|
5
|
} from "./request" |
|
6
|
import { |
|
7
|
adaptFacetSearchResponse, |
|
8
|
adaptSearchResponse, |
|
9
|
createEmptyFacetSearchResponse, |
|
10
|
createEmptySearchResponse, |
|
11
|
} from "./response" |
|
12
|
import { assertAdapterTransport, createSearchRequester } from "./http" |
|
13
|
import type { |
|
14
|
InstantSearchAdapterOptions, |
|
15
|
InstantSearchFacetSearchResponse, |
|
16
|
InstantSearchRequest, |
|
17
|
InstantSearchSearchResponse, |
|
18
|
SearchClient, |
|
19
|
SearchQuery, |
|
20
|
SearchResult, |
|
21
|
} from "./types" |
|
22
|
|
|
23
|
class TtlCache<T> { |
|
24
|
private store = new Map<string, { value: T; expiresAt: number }>() |
|
25
|
|
|
26
|
constructor(private ttlMs: number) {} |
|
27
|
|
|
28
|
get(key: string): T | undefined { |
|
29
|
if (this.ttlMs <= 0) { |
|
30
|
return undefined |
|
31
|
} |
|
32
|
const entry = this.store.get(key) |
|
33
|
if (!entry) { |
|
34
|
return undefined |
|
35
|
} |
|
36
|
if (Date.now() > entry.expiresAt) { |
|
37
|
this.store.delete(key) |
|
38
|
return undefined |
|
39
|
} |
|
40
|
return entry.value |
|
41
|
} |
|
42
|
|
|
43
|
set(key: string, value: T): void { |
|
44
|
if (this.ttlMs <= 0) { |
|
45
|
return |
|
46
|
} |
|
47
|
this.store.set(key, { value, expiresAt: Date.now() + this.ttlMs }) |
|
48
|
} |
|
49
|
|
|
50
|
clear(): void { |
|
51
|
this.store.clear() |
|
52
|
} |
|
53
|
} |
|
54
|
|
|
55
|
function isPlaceholderDisabled( |
|
56
|
request: InstantSearchRequest, |
|
57
|
placeholderSearch: boolean |
|
58
|
): boolean { |
|
59
|
if (placeholderSearch) { |
|
60
|
return false |
|
61
|
} |
|
62
|
const query = parseSearchParams(request.params).query |
|
63
|
return !query || !query.trim() |
|
64
|
} |
|
65
|
|
|
66
|
function cacheKey(kind: string, queries: SearchQuery[]): string { |
|
67
|
return `${kind}:${JSON.stringify(queries)}` |
|
68
|
} |
|
69
|
|
|
70
|
export function createInstantSearchAdapter( |
|
71
|
options: InstantSearchAdapterOptions |
|
72
|
): { searchClient: SearchClient } { |
|
73
|
assertAdapterTransport(options) |
|
74
|
|
|
75
|
const requester = createSearchRequester(options) |
|
76
|
const placeholderSearch = options.placeholderSearch !== false |
|
77
|
const cache = new TtlCache<unknown>( |
|
78
|
(options.cacheSearchResultsForSeconds ?? 0) * 1000 |
|
79
|
) |
|
80
|
|
|
81
|
const toSearchQuery = ( |
|
82
|
request: InstantSearchRequest, |
|
83
|
facetSearch = false |
|
84
|
): SearchQuery => { |
|
85
|
const adapted = facetSearch |
|
86
|
? adaptFacetSearchRequest(request, options) |
|
87
|
: adaptSearchRequest(request, options) |
|
88
|
return options.transformQuery |
|
89
|
? options.transformQuery(adapted, request) |
|
90
|
: adapted |
|
91
|
} |
|
92
|
|
|
93
|
const searchClient: SearchClient = { |
|
94
|
async search(requests: InstantSearchRequest[]) { |
|
95
|
const results: InstantSearchSearchResponse[] = new Array(requests.length) |
|
96
|
const pending: { index: number; query: SearchQuery }[] = [] |
|
97
|
|
|
98
|
requests.forEach((request, index) => { |
|
99
|
if (isPlaceholderDisabled(request, placeholderSearch)) { |
|
100
|
results[index] = createEmptySearchResponse(request) |
|
101
|
return |
|
102
|
} |
|
103
|
pending.push({ index, query: toSearchQuery(request) }) |
|
104
|
}) |
|
105
|
|
|
106
|
if (pending.length) { |
|
107
|
const queries = pending.map((item) => item.query) |
|
108
|
const key = cacheKey("search", queries) |
|
109
|
const cached = cache.get(key) as SearchResult[] | undefined |
|
110
|
const fetched = cached ?? (await requester(queries)) |
|
111
|
if (!cached) { |
|
112
|
cache.set(key, fetched) |
|
113
|
} |
|
114
|
|
|
115
|
pending.forEach((item, queryIndex) => { |
|
116
|
const request = requests[item.index] |
|
117
|
const adapted = adaptSearchResponse(fetched[queryIndex], request, { |
|
118
|
primaryKey: options.primaryKey, |
|
119
|
}) |
|
120
|
results[item.index] = options.transformResponse |
|
121
|
? options.transformResponse(adapted, fetched[queryIndex], request) |
|
122
|
: adapted |
|
123
|
}) |
|
124
|
} |
|
125
|
|
|
126
|
return { results } |
|
127
|
}, |
|
128
|
|
|
129
|
async searchForFacetValues(requests: InstantSearchRequest[]) { |
|
130
|
const results: InstantSearchFacetSearchResponse[] = new Array( |
|
131
|
requests.length |
|
132
|
) |
|
133
|
const pending: { index: number; query: SearchQuery }[] = [] |
|
134
|
|
|
135
|
requests.forEach((request, index) => { |
|
136
|
if (isPlaceholderDisabled(request, placeholderSearch)) { |
|
137
|
results[index] = createEmptyFacetSearchResponse() |
|
138
|
return |
|
139
|
} |
|
140
|
pending.push({ index, query: toSearchQuery(request, true) }) |
|
141
|
}) |
|
142
|
|
|
143
|
if (pending.length) { |
|
144
|
const queries = pending.map((item) => item.query) |
|
145
|
const key = cacheKey("facet", queries) |
|
146
|
const cached = cache.get(key) as SearchResult[] | undefined |
|
147
|
const fetched = cached ?? (await requester(queries)) |
|
148
|
if (!cached) { |
|
149
|
cache.set(key, fetched) |
|
150
|
} |
|
151
|
|
|
152
|
pending.forEach((item, queryIndex) => { |
|
153
|
results[item.index] = adaptFacetSearchResponse( |
|
154
|
fetched[queryIndex], |
|
155
|
requests[item.index] |
|
156
|
) |
|
157
|
}) |
|
158
|
} |
|
159
|
|
|
160
|
return results |
|
161
|
}, |
|
162
|
|
|
163
|
clearCache() { |
|
164
|
cache.clear() |
|
165
|
}, |
|
166
|
} |
|
167
|
|
|
168
|
return { searchClient } |
|
169
|
} |