-
Notifications
You must be signed in to change notification settings - Fork 222
/
liteClient.ts
231 lines (208 loc) · 6.32 KB
/
liteClient.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
import {
createAuth,
createTransporter,
getAlgoliaAgent,
shuffle,
} from '@algolia/client-common';
import type {
CreateClientOptions,
Headers,
Host,
QueryParameters,
Request,
RequestOptions,
} from '@algolia/client-common';
import type {
CustomPostProps,
LegacySearchMethodProps,
} from '../model/clientMethodProps';
import type { SearchMethodParams } from '../model/searchMethodParams';
import type { SearchResponses } from '../model/searchResponses';
export const apiClientVersion = '5.0.0-alpha.102';
function getDefaultHosts(appId: string): Host[] {
return (
[
{
url: `${appId}-dsn.algolia.net`,
accept: 'read',
protocol: 'https',
},
{
url: `${appId}.algolia.net`,
accept: 'write',
protocol: 'https',
},
] as Host[]
).concat(
shuffle([
{
url: `${appId}-1.algolianet.com`,
accept: 'readWrite',
protocol: 'https',
},
{
url: `${appId}-2.algolianet.com`,
accept: 'readWrite',
protocol: 'https',
},
{
url: `${appId}-3.algolianet.com`,
accept: 'readWrite',
protocol: 'https',
},
])
);
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createLiteClient({
appId: appIdOption,
apiKey: apiKeyOption,
authMode,
algoliaAgents,
...options
}: CreateClientOptions) {
const auth = createAuth(appIdOption, apiKeyOption, authMode);
const transporter = createTransporter({
hosts: getDefaultHosts(appIdOption),
...options,
algoliaAgent: getAlgoliaAgent({
algoliaAgents,
client: 'Lite',
version: apiClientVersion,
}),
baseHeaders: {
'content-type': 'text/plain',
...auth.headers(),
...options.baseHeaders,
},
baseQueryParameters: {
...auth.queryParameters(),
...options.baseQueryParameters,
},
});
return {
transporter,
/**
* The `appId` currently in use.
*/
appId: appIdOption,
/**
* Clears the cache of the transporter for the `requestsCache` and `responsesCache` properties.
*/
clearCache(): Promise<void> {
return Promise.all([
transporter.requestsCache.clear(),
transporter.responsesCache.clear(),
]).then(() => undefined);
},
/**
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
*/
get _ua(): string {
return transporter.algoliaAgent.value;
},
/**
* Adds a `segment` to the `x-algolia-agent` sent with every requests.
*
* @param segment - The algolia agent (user-agent) segment to add.
* @param version - The version of the agent.
*/
addAlgoliaAgent(segment: string, version?: string): void {
transporter.algoliaAgent.add({ segment, version });
},
/**
* This method allow you to send requests to the Algolia REST API.
*
* @param customPost - The customPost object.
* @param customPost.path - Path of the endpoint, anything after \"/1\" must be specified.
* @param customPost.parameters - Query parameters to apply to the current query.
* @param customPost.body - Parameters to send with the custom request.
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
*/
customPost(
{ path, parameters, body }: CustomPostProps,
requestOptions?: RequestOptions
): Promise<Record<string, any>> {
if (!path) {
throw new Error(
'Parameter `path` is required when calling `customPost`.'
);
}
const requestPath = '/1{path}'.replace('{path}', path);
const headers: Headers = {};
const queryParameters: QueryParameters = parameters ? parameters : {};
const request: Request = {
method: 'POST',
path: requestPath,
queryParameters,
headers,
data: body ? body : {},
};
return transporter.request(request, requestOptions);
},
/**
* Send multiple search queries to one or more indices.
*
* Required API Key ACLs:
* - search.
*
* @param searchMethodParams - Query requests and strategies. Results will be received in the same order as the queries.
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
*/
search<T>(
searchMethodParams: LegacySearchMethodProps | SearchMethodParams,
requestOptions?: RequestOptions
): Promise<SearchResponses<T>> {
if (searchMethodParams && Array.isArray(searchMethodParams)) {
const newSignatureRequest: SearchMethodParams = {
requests: searchMethodParams.map(({ params, ...legacyRequest }) => {
if (legacyRequest.type === 'facet') {
return {
...legacyRequest,
...params,
type: 'facet',
};
}
return {
...legacyRequest,
...params,
facet: undefined,
maxFacetHits: undefined,
facetQuery: undefined,
};
}),
};
// eslint-disable-next-line no-param-reassign
searchMethodParams = newSignatureRequest;
}
if (!searchMethodParams) {
throw new Error(
'Parameter `searchMethodParams` is required when calling `search`.'
);
}
if (!searchMethodParams.requests) {
throw new Error(
'Parameter `searchMethodParams.requests` is required when calling `search`.'
);
}
const requestPath = '/1/indexes/*/queries';
const headers: Headers = {};
const queryParameters: QueryParameters = {};
const request: Request = {
method: 'POST',
path: requestPath,
queryParameters,
headers,
data: searchMethodParams,
useReadTransporter: true,
cacheable: true,
};
return transporter.request(request, requestOptions);
},
};
}
/**
* The client type.
*/
export type LiteClient = ReturnType<typeof createLiteClient>;