-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathcreateSearchClient.ts
66 lines (58 loc) · 1.65 KB
/
createSearchClient.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
64
65
66
import {
addMethods,
AuthMode,
ClientTransporterOptions,
createAuth,
CreateClient,
shuffle,
} from '@algolia/client-common';
import { CallEnum, createTransporter, HostOptions } from '@algolia/transporter';
import { SearchClient, SearchClientOptions } from './types';
export const createSearchClient: CreateClient<
SearchClient,
SearchClientOptions & ClientTransporterOptions
> = options => {
const appId = options.appId;
const auth = createAuth(
options.authMode !== undefined ? options.authMode : AuthMode.WithinHeaders,
appId,
options.apiKey
);
const transporter = createTransporter({
hosts: ([
{ url: `${appId}-dsn.algolia.net`, accept: CallEnum.Read },
{ url: `${appId}.algolia.net`, accept: CallEnum.Write },
] as readonly HostOptions[]).concat(
shuffle([
{ url: `${appId}-1.algolianet.com` },
{ url: `${appId}-2.algolianet.com` },
{ url: `${appId}-3.algolianet.com` },
])
),
...options,
headers: {
...auth.headers(),
...{ 'content-type': 'application/x-www-form-urlencoded' },
...options.headers,
},
queryParameters: {
...auth.queryParameters(),
...options.queryParameters,
},
data: auth.data(),
});
const base = {
transporter,
appId,
addAlgoliaAgent(segment: string, version?: string): void {
transporter.userAgent.add({ segment, version });
},
clearCache(): Readonly<Promise<void>> {
return Promise.all([
transporter.requestsCache.clear(),
transporter.responsesCache.clear(),
]).then(() => undefined);
},
};
return addMethods(base, options.methods);
};