File size: 2,810 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type algoliasearch from 'algoliasearch/lite';
import type * as AlgoliaSearch from 'algoliasearch/lite';
/** @ts-ignore */
import type * as ClientSearch from '@algolia/client-search';

/** @ts-ignore */
type SearchResponseV3<TObject> = AlgoliaSearch.Response<TObject>;
/** @ts-ignore */
type SearchResponseV4<TObject> = ClientSearch.SearchResponse<TObject>;

type SearchForFacetValuesResponseV3 =
  /** @ts-ignore */
  AlgoliaSearch.SearchForFacetValues.Response;
/** @ts-ignore */
type SearchForFacetValuesResponseV4 = ClientSearch.SearchForFacetValuesResponse;

type DummySearchClientV4 = {
  readonly transporter: any;
};

type DefaultSearchClient = ReturnType<typeof algoliasearch>;

type SearchResponse<THit> = DefaultSearchClient extends DummySearchClientV4
  ? SearchResponseV4<THit>
  : SearchResponseV3<THit>;

type SearchForFacetValuesResponse =
  DefaultSearchClient extends DummySearchClientV4
    ? SearchForFacetValuesResponseV4
    : SearchForFacetValuesResponseV3;

export function createSingleSearchResponse<THit = any>(
  options: 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,
    renderingContent = {
      facetOrdering: {
        facets: {
          order: ['brand', 'hierarchicalCategories.lvl0', 'categories'],
        },
        values: {
          brand: {
            sortRemainingBy: 'count',
          },
          categories: {
            sortRemainingBy: 'count',
          },
          'hierarchicalCategories.lvl0': {
            sortRemainingBy: 'count',
          },
        },
      },
    },
    userData = [
      {
        title: 'Banner title',
        banner: 'https://banner.jpg',
        link: 'https://banner.com/link/',
      },
    ],
    ...rest
  } = options;

  return {
    page,
    hitsPerPage,
    nbHits,
    nbPages,
    processingTimeMS,
    hits,
    query,
    params,
    exhaustiveNbHits,
    exhaustiveFacetsCount,
    renderingContent,
    userData,
    ...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(
  options: Partial<SearchForFacetValuesResponse> = {}
): SearchForFacetValuesResponse {
  return {
    facetHits: [],
    exhaustiveFacetsCount: true,
    processingTimeMS: 1,
    ...options,
  };
}