-
Notifications
You must be signed in to change notification settings - Fork 215
/
createSearchClient.ts
47 lines (40 loc) · 1.38 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
import { addMethods, AuthMode, createAuth, CreateClient, shuffle } from '@algolia/client-common';
import { CallEnum, createTransporter, TransporterOptions } from '@algolia/transporter';
import { SearchClient, SearchClientOptions } from './types';
export const createSearchClient: CreateClient<
SearchClient,
SearchClientOptions & TransporterOptions
> = options => {
const appId = options.appId;
const transporter = createTransporter(options);
transporter.setHosts(
[
{ url: `${appId}-dsn.algolia.net`, accept: CallEnum.Read },
{ url: `${appId}.algolia.net`, accept: CallEnum.Write },
].concat(
shuffle([
{ url: `${appId}-1.algolianet.com`, accept: CallEnum.Any },
{ url: `${appId}-2.algolianet.com`, accept: CallEnum.Any },
{ url: `${appId}-3.algolianet.com`, accept: CallEnum.Any },
])
)
);
const auth = createAuth(
options.authMode !== undefined ? options.authMode : AuthMode.WithinHeaders,
appId,
options.apiKey
);
transporter.addHeaders({
...auth.headers(),
...{ 'content-type': 'application/x-www-form-urlencoded' },
});
transporter.addQueryParameters(auth.queryParameters());
const base = {
transporter,
appId,
addAlgoliaAgent(segment: string, version?: string): void {
transporter.userAgent.add({ segment, version });
},
};
return addMethods(base, options.methods);
};