-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathcreateApiResponse.ts
63 lines (57 loc) · 1.3 KB
/
createApiResponse.ts
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
import type { SearchResponse } from '@algolia/autocomplete-shared';
import { SearchForFacetValuesResponse } from '@algolia/client-search';
export function createSingleSearchResponse<THit = any>(
subset: Partial<SearchResponse<THit>> = {}
): SearchResponse<THit> {
const {
query = '',
page = 0,
hitsPerPage = 20,
hits = [],
nbHits = hits.length,
nbPages = Math.ceil(nbHits / hitsPerPage),
params = '',
exhaustiveNbHits = true,
exhaustiveFacetsCount = true,
processingTimeMS = 0,
...rest
} = subset;
return {
page,
hitsPerPage,
nbHits,
nbPages,
processingTimeMS,
hits,
query,
params,
exhaustiveNbHits,
exhaustiveFacetsCount,
...rest,
};
}
type MultiResponse<THit = any> = {
results: Array<SearchResponse<THit>>;
};
export function createMultiSearchResponse<THit = any>(
...args: Array<Partial<SearchResponse<THit>>>
): MultiResponse {
if (!args.length) {
return {
results: [createSingleSearchResponse()],
};
}
return {
results: args.map(createSingleSearchResponse),
};
}
export function createSFFVResponse(
args: Partial<SearchForFacetValuesResponse> = {}
): SearchForFacetValuesResponse {
return {
facetHits: [],
exhaustiveFacetsCount: true,
processingTimeMS: 1,
...args,
};
}