@algolia/client-common 5.8.1 → 5.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/common.d.cts +562 -426
- package/dist/common.d.ts +562 -426
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +5 -5
- package/index.ts +0 -9
- package/src/__tests__/cache/browser-local-storage-cache.test.ts +0 -171
- package/src/__tests__/cache/fallbackable-cache.test.ts +0 -126
- package/src/__tests__/cache/memory-cache.test.ts +0 -82
- package/src/__tests__/cache/null-cache.test.ts +0 -47
- package/src/__tests__/create-iterable-promise.test.ts +0 -236
- package/src/__tests__/logger/null-logger.test.ts +0 -22
- package/src/cache/createBrowserLocalStorageCache.ts +0 -106
- package/src/cache/createFallbackableCache.ts +0 -43
- package/src/cache/createMemoryCache.ts +0 -43
- package/src/cache/createNullCache.ts +0 -29
- package/src/cache/index.ts +0 -4
- package/src/constants.ts +0 -7
- package/src/createAlgoliaAgent.ts +0 -18
- package/src/createAuth.ts +0 -25
- package/src/createIterablePromise.ts +0 -47
- package/src/getAlgoliaAgent.ts +0 -19
- package/src/logger/createNullLogger.ts +0 -15
- package/src/logger/index.ts +0 -1
- package/src/transporter/createStatefulHost.ts +0 -19
- package/src/transporter/createTransporter.ts +0 -315
- package/src/transporter/errors.ts +0 -77
- package/src/transporter/helpers.ts +0 -96
- package/src/transporter/index.ts +0 -6
- package/src/transporter/responses.ts +0 -13
- package/src/transporter/stackTrace.ts +0 -22
- package/src/types/cache.ts +0 -75
- package/src/types/createClient.ts +0 -15
- package/src/types/createIterablePromise.ts +0 -40
- package/src/types/host.ts +0 -43
- package/src/types/index.ts +0 -7
- package/src/types/logger.ts +0 -24
- package/src/types/requester.ts +0 -67
- package/src/types/transporter.ts +0 -153
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
EndRequest,
|
|
3
|
-
Host,
|
|
4
|
-
QueryParameters,
|
|
5
|
-
Request,
|
|
6
|
-
RequestOptions,
|
|
7
|
-
Response,
|
|
8
|
-
StackFrame,
|
|
9
|
-
Transporter,
|
|
10
|
-
TransporterOptions,
|
|
11
|
-
} from '../types';
|
|
12
|
-
import { createStatefulHost } from './createStatefulHost';
|
|
13
|
-
import { RetryError } from './errors';
|
|
14
|
-
import { deserializeFailure, deserializeSuccess, serializeData, serializeHeaders, serializeUrl } from './helpers';
|
|
15
|
-
import { isRetryable, isSuccess } from './responses';
|
|
16
|
-
import { stackFrameWithoutCredentials, stackTraceWithoutCredentials } from './stackTrace';
|
|
17
|
-
|
|
18
|
-
type RetryableOptions = {
|
|
19
|
-
hosts: Host[];
|
|
20
|
-
getTimeout: (retryCount: number, timeout: number) => number;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export function createTransporter({
|
|
24
|
-
hosts,
|
|
25
|
-
hostsCache,
|
|
26
|
-
baseHeaders,
|
|
27
|
-
logger,
|
|
28
|
-
baseQueryParameters,
|
|
29
|
-
algoliaAgent,
|
|
30
|
-
timeouts,
|
|
31
|
-
requester,
|
|
32
|
-
requestsCache,
|
|
33
|
-
responsesCache,
|
|
34
|
-
}: TransporterOptions): Transporter {
|
|
35
|
-
async function createRetryableOptions(compatibleHosts: Host[]): Promise<RetryableOptions> {
|
|
36
|
-
const statefulHosts = await Promise.all(
|
|
37
|
-
compatibleHosts.map((compatibleHost) => {
|
|
38
|
-
return hostsCache.get(compatibleHost, () => {
|
|
39
|
-
return Promise.resolve(createStatefulHost(compatibleHost));
|
|
40
|
-
});
|
|
41
|
-
}),
|
|
42
|
-
);
|
|
43
|
-
const hostsUp = statefulHosts.filter((host) => host.isUp());
|
|
44
|
-
const hostsTimedOut = statefulHosts.filter((host) => host.isTimedOut());
|
|
45
|
-
|
|
46
|
-
// Note, we put the hosts that previously timed out on the end of the list.
|
|
47
|
-
const hostsAvailable = [...hostsUp, ...hostsTimedOut];
|
|
48
|
-
const compatibleHostsAvailable = hostsAvailable.length > 0 ? hostsAvailable : compatibleHosts;
|
|
49
|
-
|
|
50
|
-
return {
|
|
51
|
-
hosts: compatibleHostsAvailable,
|
|
52
|
-
getTimeout(timeoutsCount: number, baseTimeout: number): number {
|
|
53
|
-
/**
|
|
54
|
-
* Imagine that you have 4 hosts, if timeouts will increase
|
|
55
|
-
* on the following way: 1 (timed out) > 4 (timed out) > 5 (200).
|
|
56
|
-
*
|
|
57
|
-
* Note that, the very next request, we start from the previous timeout.
|
|
58
|
-
*
|
|
59
|
-
* 5 (timed out) > 6 (timed out) > 7 ...
|
|
60
|
-
*
|
|
61
|
-
* This strategy may need to be reviewed, but is the strategy on the our
|
|
62
|
-
* current v3 version.
|
|
63
|
-
*/
|
|
64
|
-
const timeoutMultiplier =
|
|
65
|
-
hostsTimedOut.length === 0 && timeoutsCount === 0 ? 1 : hostsTimedOut.length + 3 + timeoutsCount;
|
|
66
|
-
|
|
67
|
-
return timeoutMultiplier * baseTimeout;
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function retryableRequest<TResponse>(
|
|
73
|
-
request: Request,
|
|
74
|
-
requestOptions: RequestOptions,
|
|
75
|
-
isRead = true,
|
|
76
|
-
): Promise<TResponse> {
|
|
77
|
-
const stackTrace: StackFrame[] = [];
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* First we prepare the payload that do not depend from hosts.
|
|
81
|
-
*/
|
|
82
|
-
const data = serializeData(request, requestOptions);
|
|
83
|
-
const headers = serializeHeaders(baseHeaders, request.headers, requestOptions.headers);
|
|
84
|
-
|
|
85
|
-
// On `GET`, the data is proxied to query parameters.
|
|
86
|
-
const dataQueryParameters: QueryParameters =
|
|
87
|
-
request.method === 'GET'
|
|
88
|
-
? {
|
|
89
|
-
...request.data,
|
|
90
|
-
...requestOptions.data,
|
|
91
|
-
}
|
|
92
|
-
: {};
|
|
93
|
-
|
|
94
|
-
const queryParameters: QueryParameters = {
|
|
95
|
-
...baseQueryParameters,
|
|
96
|
-
...request.queryParameters,
|
|
97
|
-
...dataQueryParameters,
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
if (algoliaAgent.value) {
|
|
101
|
-
queryParameters['x-algolia-agent'] = algoliaAgent.value;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (requestOptions && requestOptions.queryParameters) {
|
|
105
|
-
for (const key of Object.keys(requestOptions.queryParameters)) {
|
|
106
|
-
// We want to keep `undefined` and `null` values,
|
|
107
|
-
// but also avoid stringifying `object`s, as they are
|
|
108
|
-
// handled in the `serializeUrl` step right after.
|
|
109
|
-
if (
|
|
110
|
-
!requestOptions.queryParameters[key] ||
|
|
111
|
-
Object.prototype.toString.call(requestOptions.queryParameters[key]) === '[object Object]'
|
|
112
|
-
) {
|
|
113
|
-
queryParameters[key] = requestOptions.queryParameters[key];
|
|
114
|
-
} else {
|
|
115
|
-
queryParameters[key] = requestOptions.queryParameters[key].toString();
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
let timeoutsCount = 0;
|
|
121
|
-
|
|
122
|
-
const retry = async (
|
|
123
|
-
retryableHosts: Host[],
|
|
124
|
-
getTimeout: (timeoutsCount: number, timeout: number) => number,
|
|
125
|
-
): Promise<TResponse> => {
|
|
126
|
-
/**
|
|
127
|
-
* We iterate on each host, until there is no host left.
|
|
128
|
-
*/
|
|
129
|
-
const host = retryableHosts.pop();
|
|
130
|
-
if (host === undefined) {
|
|
131
|
-
throw new RetryError(stackTraceWithoutCredentials(stackTrace));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const timeout = { ...timeouts, ...requestOptions.timeouts };
|
|
135
|
-
|
|
136
|
-
const payload: EndRequest = {
|
|
137
|
-
data,
|
|
138
|
-
headers,
|
|
139
|
-
method: request.method,
|
|
140
|
-
url: serializeUrl(host, request.path, queryParameters),
|
|
141
|
-
connectTimeout: getTimeout(timeoutsCount, timeout.connect),
|
|
142
|
-
responseTimeout: getTimeout(timeoutsCount, isRead ? timeout.read : timeout.write),
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* The stackFrame is pushed to the stackTrace so we
|
|
147
|
-
* can have information about onRetry and onFailure
|
|
148
|
-
* decisions.
|
|
149
|
-
*/
|
|
150
|
-
const pushToStackTrace = (response: Response): StackFrame => {
|
|
151
|
-
const stackFrame: StackFrame = {
|
|
152
|
-
request: payload,
|
|
153
|
-
response,
|
|
154
|
-
host,
|
|
155
|
-
triesLeft: retryableHosts.length,
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
stackTrace.push(stackFrame);
|
|
159
|
-
|
|
160
|
-
return stackFrame;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
const response = await requester.send(payload);
|
|
164
|
-
|
|
165
|
-
if (isRetryable(response)) {
|
|
166
|
-
const stackFrame = pushToStackTrace(response);
|
|
167
|
-
|
|
168
|
-
// If response is a timeout, we increase the number of timeouts so we can increase the timeout later.
|
|
169
|
-
if (response.isTimedOut) {
|
|
170
|
-
timeoutsCount++;
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Failures are individually sent to the logger, allowing
|
|
174
|
-
* the end user to debug / store stack frames even
|
|
175
|
-
* when a retry error does not happen.
|
|
176
|
-
*/
|
|
177
|
-
logger.info('Retryable failure', stackFrameWithoutCredentials(stackFrame));
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* We also store the state of the host in failure cases. If the host, is
|
|
181
|
-
* down it will remain down for the next 2 minutes. In a timeout situation,
|
|
182
|
-
* this host will be added end of the list of hosts on the next request.
|
|
183
|
-
*/
|
|
184
|
-
await hostsCache.set(host, createStatefulHost(host, response.isTimedOut ? 'timed out' : 'down'));
|
|
185
|
-
|
|
186
|
-
return retry(retryableHosts, getTimeout);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (isSuccess(response)) {
|
|
190
|
-
return deserializeSuccess(response);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
pushToStackTrace(response);
|
|
194
|
-
throw deserializeFailure(response, stackTrace);
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Finally, for each retryable host perform request until we got a non
|
|
199
|
-
* retryable response. Some notes here:
|
|
200
|
-
*
|
|
201
|
-
* 1. The reverse here is applied so we can apply a `pop` later on => more performant.
|
|
202
|
-
* 2. We also get from the retryable options a timeout multiplier that is tailored
|
|
203
|
-
* for the current context.
|
|
204
|
-
*/
|
|
205
|
-
const compatibleHosts = hosts.filter(
|
|
206
|
-
(host) => host.accept === 'readWrite' || (isRead ? host.accept === 'read' : host.accept === 'write'),
|
|
207
|
-
);
|
|
208
|
-
const options = await createRetryableOptions(compatibleHosts);
|
|
209
|
-
|
|
210
|
-
return retry([...options.hosts].reverse(), options.getTimeout);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function createRequest<TResponse>(request: Request, requestOptions: RequestOptions = {}): Promise<TResponse> {
|
|
214
|
-
/**
|
|
215
|
-
* A read request is either a `GET` request, or a request that we make
|
|
216
|
-
* via the `read` transporter (e.g. `search`).
|
|
217
|
-
*/
|
|
218
|
-
const isRead = request.useReadTransporter || request.method === 'GET';
|
|
219
|
-
if (!isRead) {
|
|
220
|
-
/**
|
|
221
|
-
* On write requests, no cache mechanisms are applied, and we
|
|
222
|
-
* proxy the request immediately to the requester.
|
|
223
|
-
*/
|
|
224
|
-
return retryableRequest<TResponse>(request, requestOptions, isRead);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const createRetryableRequest = (): Promise<TResponse> => {
|
|
228
|
-
/**
|
|
229
|
-
* Then, we prepare a function factory that contains the construction of
|
|
230
|
-
* the retryable request. At this point, we may *not* perform the actual
|
|
231
|
-
* request. But we want to have the function factory ready.
|
|
232
|
-
*/
|
|
233
|
-
return retryableRequest<TResponse>(request, requestOptions);
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Once we have the function factory ready, we need to determine of the
|
|
238
|
-
* request is "cacheable" - should be cached. Note that, once again,
|
|
239
|
-
* the user can force this option.
|
|
240
|
-
*/
|
|
241
|
-
const cacheable = requestOptions.cacheable || request.cacheable;
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* If is not "cacheable", we immediately trigger the retryable request, no
|
|
245
|
-
* need to check cache implementations.
|
|
246
|
-
*/
|
|
247
|
-
if (cacheable !== true) {
|
|
248
|
-
return createRetryableRequest();
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* If the request is "cacheable", we need to first compute the key to ask
|
|
253
|
-
* the cache implementations if this request is on progress or if the
|
|
254
|
-
* response already exists on the cache.
|
|
255
|
-
*/
|
|
256
|
-
const key = {
|
|
257
|
-
request,
|
|
258
|
-
requestOptions,
|
|
259
|
-
transporter: {
|
|
260
|
-
queryParameters: baseQueryParameters,
|
|
261
|
-
headers: baseHeaders,
|
|
262
|
-
},
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* With the computed key, we first ask the responses cache
|
|
267
|
-
* implementation if this request was been resolved before.
|
|
268
|
-
*/
|
|
269
|
-
return responsesCache.get(
|
|
270
|
-
key,
|
|
271
|
-
() => {
|
|
272
|
-
/**
|
|
273
|
-
* If the request has never resolved before, we actually ask if there
|
|
274
|
-
* is a current request with the same key on progress.
|
|
275
|
-
*/
|
|
276
|
-
return requestsCache.get(key, () =>
|
|
277
|
-
/**
|
|
278
|
-
* Finally, if there is no request in progress with the same key,
|
|
279
|
-
* this `createRetryableRequest()` will actually trigger the
|
|
280
|
-
* retryable request.
|
|
281
|
-
*/
|
|
282
|
-
requestsCache
|
|
283
|
-
.set(key, createRetryableRequest())
|
|
284
|
-
.then(
|
|
285
|
-
(response) => Promise.all([requestsCache.delete(key), response]),
|
|
286
|
-
(err) => Promise.all([requestsCache.delete(key), Promise.reject(err)]),
|
|
287
|
-
)
|
|
288
|
-
.then(([_, response]) => response),
|
|
289
|
-
);
|
|
290
|
-
},
|
|
291
|
-
{
|
|
292
|
-
/**
|
|
293
|
-
* Of course, once we get this response back from the server, we
|
|
294
|
-
* tell response cache to actually store the received response
|
|
295
|
-
* to be used later.
|
|
296
|
-
*/
|
|
297
|
-
miss: (response) => responsesCache.set(key, response),
|
|
298
|
-
},
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return {
|
|
303
|
-
hostsCache,
|
|
304
|
-
requester,
|
|
305
|
-
timeouts,
|
|
306
|
-
logger,
|
|
307
|
-
algoliaAgent,
|
|
308
|
-
baseHeaders,
|
|
309
|
-
baseQueryParameters,
|
|
310
|
-
hosts,
|
|
311
|
-
request: createRequest,
|
|
312
|
-
requestsCache,
|
|
313
|
-
responsesCache,
|
|
314
|
-
};
|
|
315
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import type { Response, StackFrame } from '../types';
|
|
2
|
-
|
|
3
|
-
export class AlgoliaError extends Error {
|
|
4
|
-
override name: string = 'AlgoliaError';
|
|
5
|
-
|
|
6
|
-
constructor(message: string, name: string) {
|
|
7
|
-
super(message);
|
|
8
|
-
|
|
9
|
-
if (name) {
|
|
10
|
-
this.name = name;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class ErrorWithStackTrace extends AlgoliaError {
|
|
16
|
-
stackTrace: StackFrame[];
|
|
17
|
-
|
|
18
|
-
constructor(message: string, stackTrace: StackFrame[], name: string) {
|
|
19
|
-
super(message, name);
|
|
20
|
-
// the array and object should be frozen to reflect the stackTrace at the time of the error
|
|
21
|
-
this.stackTrace = stackTrace;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class RetryError extends ErrorWithStackTrace {
|
|
26
|
-
constructor(stackTrace: StackFrame[]) {
|
|
27
|
-
super(
|
|
28
|
-
'Unreachable hosts - your application id may be incorrect. If the error persists, please reach out to the Algolia Support team: https://alg.li/support.',
|
|
29
|
-
stackTrace,
|
|
30
|
-
'RetryError',
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export class ApiError extends ErrorWithStackTrace {
|
|
36
|
-
status: number;
|
|
37
|
-
|
|
38
|
-
constructor(message: string, status: number, stackTrace: StackFrame[], name = 'ApiError') {
|
|
39
|
-
super(message, stackTrace, name);
|
|
40
|
-
this.status = status;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export class DeserializationError extends AlgoliaError {
|
|
45
|
-
response: Response;
|
|
46
|
-
|
|
47
|
-
constructor(message: string, response: Response) {
|
|
48
|
-
super(message, 'DeserializationError');
|
|
49
|
-
this.response = response;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export type DetailedErrorWithMessage = {
|
|
54
|
-
message: string;
|
|
55
|
-
label: string;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export type DetailedErrorWithTypeID = {
|
|
59
|
-
id: string;
|
|
60
|
-
type: string;
|
|
61
|
-
name?: string;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export type DetailedError = {
|
|
65
|
-
code: string;
|
|
66
|
-
details?: DetailedErrorWithMessage[] | DetailedErrorWithTypeID[];
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
// DetailedApiError is only used by the ingestion client to return more informative error, other clients will use ApiClient.
|
|
70
|
-
export class DetailedApiError extends ApiError {
|
|
71
|
-
error: DetailedError;
|
|
72
|
-
|
|
73
|
-
constructor(message: string, status: number, error: DetailedError, stackTrace: StackFrame[]) {
|
|
74
|
-
super(message, status, stackTrace, 'DetailedApiError');
|
|
75
|
-
this.error = error;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { Headers, Host, QueryParameters, Request, RequestOptions, Response, StackFrame } from '../types';
|
|
2
|
-
import { ApiError, DeserializationError, DetailedApiError } from './errors';
|
|
3
|
-
|
|
4
|
-
export function shuffle<TData>(array: TData[]): TData[] {
|
|
5
|
-
const shuffledArray = array;
|
|
6
|
-
|
|
7
|
-
for (let c = array.length - 1; c > 0; c--) {
|
|
8
|
-
const b = Math.floor(Math.random() * (c + 1));
|
|
9
|
-
const a = array[c];
|
|
10
|
-
|
|
11
|
-
shuffledArray[c] = array[b];
|
|
12
|
-
shuffledArray[b] = a;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return shuffledArray;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function serializeUrl(host: Host, path: string, queryParameters: QueryParameters): string {
|
|
19
|
-
const queryParametersAsString = serializeQueryParameters(queryParameters);
|
|
20
|
-
let url = `${host.protocol}://${host.url}${host.port ? `:${host.port}` : ''}/${
|
|
21
|
-
path.charAt(0) === '/' ? path.substring(1) : path
|
|
22
|
-
}`;
|
|
23
|
-
|
|
24
|
-
if (queryParametersAsString.length) {
|
|
25
|
-
url += `?${queryParametersAsString}`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return url;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function serializeQueryParameters(parameters: QueryParameters): string {
|
|
32
|
-
return Object.keys(parameters)
|
|
33
|
-
.filter((key) => parameters[key] !== undefined)
|
|
34
|
-
.sort()
|
|
35
|
-
.map(
|
|
36
|
-
(key) =>
|
|
37
|
-
`${key}=${encodeURIComponent(
|
|
38
|
-
Object.prototype.toString.call(parameters[key]) === '[object Array]'
|
|
39
|
-
? parameters[key].join(',')
|
|
40
|
-
: parameters[key],
|
|
41
|
-
).replace(/\+/g, '%20')}`,
|
|
42
|
-
)
|
|
43
|
-
.join('&');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function serializeData(request: Request, requestOptions: RequestOptions): string | undefined {
|
|
47
|
-
if (request.method === 'GET' || (request.data === undefined && requestOptions.data === undefined)) {
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const data = Array.isArray(request.data) ? request.data : { ...request.data, ...requestOptions.data };
|
|
52
|
-
|
|
53
|
-
return JSON.stringify(data);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function serializeHeaders(
|
|
57
|
-
baseHeaders: Headers,
|
|
58
|
-
requestHeaders: Headers,
|
|
59
|
-
requestOptionsHeaders?: Headers,
|
|
60
|
-
): Headers {
|
|
61
|
-
const headers: Headers = {
|
|
62
|
-
Accept: 'application/json',
|
|
63
|
-
...baseHeaders,
|
|
64
|
-
...requestHeaders,
|
|
65
|
-
...requestOptionsHeaders,
|
|
66
|
-
};
|
|
67
|
-
const serializedHeaders: Headers = {};
|
|
68
|
-
|
|
69
|
-
Object.keys(headers).forEach((header) => {
|
|
70
|
-
const value = headers[header];
|
|
71
|
-
serializedHeaders[header.toLowerCase()] = value;
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
return serializedHeaders;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function deserializeSuccess<TObject>(response: Response): TObject {
|
|
78
|
-
try {
|
|
79
|
-
return JSON.parse(response.content);
|
|
80
|
-
} catch (e) {
|
|
81
|
-
throw new DeserializationError((e as Error).message, response);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function deserializeFailure({ content, status }: Response, stackFrame: StackFrame[]): Error {
|
|
86
|
-
try {
|
|
87
|
-
const parsed = JSON.parse(content);
|
|
88
|
-
if ('error' in parsed) {
|
|
89
|
-
return new DetailedApiError(parsed.message, status, parsed.error, stackFrame);
|
|
90
|
-
}
|
|
91
|
-
return new ApiError(parsed.message, status, stackFrame);
|
|
92
|
-
} catch {
|
|
93
|
-
// ..
|
|
94
|
-
}
|
|
95
|
-
return new ApiError(content, status, stackFrame);
|
|
96
|
-
}
|
package/src/transporter/index.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Response } from '../types';
|
|
2
|
-
|
|
3
|
-
export function isNetworkError({ isTimedOut, status }: Omit<Response, 'content'>): boolean {
|
|
4
|
-
return !isTimedOut && ~~status === 0;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function isRetryable({ isTimedOut, status }: Omit<Response, 'content'>): boolean {
|
|
8
|
-
return isTimedOut || isNetworkError({ isTimedOut, status }) || (~~(status / 100) !== 2 && ~~(status / 100) !== 4);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function isSuccess({ status }: Pick<Response, 'status'>): boolean {
|
|
12
|
-
return ~~(status / 100) === 2;
|
|
13
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { Headers, StackFrame } from '../types';
|
|
2
|
-
|
|
3
|
-
export function stackTraceWithoutCredentials(stackTrace: StackFrame[]): StackFrame[] {
|
|
4
|
-
return stackTrace.map((stackFrame) => stackFrameWithoutCredentials(stackFrame));
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame {
|
|
8
|
-
const modifiedHeaders: Headers = stackFrame.request.headers['x-algolia-api-key']
|
|
9
|
-
? { 'x-algolia-api-key': '*****' }
|
|
10
|
-
: {};
|
|
11
|
-
|
|
12
|
-
return {
|
|
13
|
-
...stackFrame,
|
|
14
|
-
request: {
|
|
15
|
-
...stackFrame.request,
|
|
16
|
-
headers: {
|
|
17
|
-
...stackFrame.request.headers,
|
|
18
|
-
...modifiedHeaders,
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
}
|
package/src/types/cache.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
export type Cache = {
|
|
2
|
-
/**
|
|
3
|
-
* Gets the value of the given `key`.
|
|
4
|
-
*/
|
|
5
|
-
get: <TValue>(
|
|
6
|
-
key: Record<string, any> | string,
|
|
7
|
-
defaultValue: () => Promise<TValue>,
|
|
8
|
-
events?: CacheEvents<TValue>,
|
|
9
|
-
) => Promise<TValue>;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Sets the given value with the given `key`.
|
|
13
|
-
*/
|
|
14
|
-
set: <TValue>(key: Record<string, any> | string, value: TValue) => Promise<TValue>;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Deletes the given `key`.
|
|
18
|
-
*/
|
|
19
|
-
delete: (key: Record<string, any> | string) => Promise<void>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Clears the cache.
|
|
23
|
-
*/
|
|
24
|
-
clear: () => Promise<void>;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export type CacheEvents<TValue> = {
|
|
28
|
-
/**
|
|
29
|
-
* The callback when the given `key` is missing from the cache.
|
|
30
|
-
*/
|
|
31
|
-
miss: (value: TValue) => Promise<any>;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type MemoryCacheOptions = {
|
|
35
|
-
/**
|
|
36
|
-
* If keys and values should be serialized using `JSON.stringify`.
|
|
37
|
-
*/
|
|
38
|
-
serializable?: boolean;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export type BrowserLocalStorageOptions = {
|
|
42
|
-
/**
|
|
43
|
-
* The cache key.
|
|
44
|
-
*/
|
|
45
|
-
key: string;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The time to live for each cached item in seconds.
|
|
49
|
-
*/
|
|
50
|
-
timeToLive?: number;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* The native local storage implementation.
|
|
54
|
-
*/
|
|
55
|
-
localStorage?: Storage;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export type BrowserLocalStorageCacheItem = {
|
|
59
|
-
/**
|
|
60
|
-
* The cache item creation timestamp.
|
|
61
|
-
*/
|
|
62
|
-
timestamp: number;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* The cache item value.
|
|
66
|
-
*/
|
|
67
|
-
value: any;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export type FallbackableCacheOptions = {
|
|
71
|
-
/**
|
|
72
|
-
* List of caches order by priority.
|
|
73
|
-
*/
|
|
74
|
-
caches: Cache[];
|
|
75
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { AlgoliaAgentOptions, TransporterOptions } from './transporter';
|
|
2
|
-
|
|
3
|
-
export type AuthMode = 'WithinHeaders' | 'WithinQueryParameters';
|
|
4
|
-
|
|
5
|
-
type OverriddenTransporterOptions = 'baseHeaders' | 'baseQueryParameters' | 'hosts';
|
|
6
|
-
|
|
7
|
-
export type CreateClientOptions = Omit<TransporterOptions, OverriddenTransporterOptions | 'algoliaAgent'> &
|
|
8
|
-
Partial<Pick<TransporterOptions, OverriddenTransporterOptions>> & {
|
|
9
|
-
appId: string;
|
|
10
|
-
apiKey: string;
|
|
11
|
-
authMode?: AuthMode;
|
|
12
|
-
algoliaAgents: AlgoliaAgentOptions[];
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type ClientOptions = Partial<Omit<CreateClientOptions, 'apiKey' | 'appId'>>;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
export type IterableOptions<TResponse> = Partial<{
|
|
2
|
-
/**
|
|
3
|
-
* The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
|
|
4
|
-
*/
|
|
5
|
-
aggregator: (response: TResponse) => void;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The `validate` condition to throw an error and its message.
|
|
9
|
-
*/
|
|
10
|
-
error: {
|
|
11
|
-
/**
|
|
12
|
-
* The function to validate the error condition.
|
|
13
|
-
*/
|
|
14
|
-
validate: (response: TResponse) => boolean;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The error message to throw.
|
|
18
|
-
*/
|
|
19
|
-
message: (response: TResponse) => string;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The function to decide how long to wait between iterations.
|
|
24
|
-
*/
|
|
25
|
-
timeout: () => number;
|
|
26
|
-
}>;
|
|
27
|
-
|
|
28
|
-
export type CreateIterablePromise<TResponse> = IterableOptions<TResponse> & {
|
|
29
|
-
/**
|
|
30
|
-
* The function to run, which returns a promise.
|
|
31
|
-
*
|
|
32
|
-
* The `previousResponse` parameter (`undefined` on the first call) allows you to build your request with incremental logic, to iterate on `page` or `cursor` for example.
|
|
33
|
-
*/
|
|
34
|
-
func: (previousResponse?: TResponse) => Promise<TResponse>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* The validator function. It receive the resolved return of the API call.
|
|
38
|
-
*/
|
|
39
|
-
validate: (response: TResponse) => boolean;
|
|
40
|
-
};
|