@algolia/client-common 5.2.2 → 5.2.4-beta.2

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.
Files changed (61) hide show
  1. package/dist/common.cjs +714 -0
  2. package/dist/common.cjs.map +1 -0
  3. package/dist/common.d.cts +438 -0
  4. package/dist/common.d.ts +438 -0
  5. package/dist/common.esm.js +653 -0
  6. package/dist/common.esm.js.map +1 -0
  7. package/package.json +17 -6
  8. package/dist/client-common.cjs +0 -783
  9. package/dist/client-common.esm.node.js +0 -747
  10. package/dist/index.d.ts +0 -10
  11. package/dist/index.d.ts.map +0 -1
  12. package/dist/src/cache/createBrowserLocalStorageCache.d.ts +0 -3
  13. package/dist/src/cache/createBrowserLocalStorageCache.d.ts.map +0 -1
  14. package/dist/src/cache/createFallbackableCache.d.ts +0 -3
  15. package/dist/src/cache/createFallbackableCache.d.ts.map +0 -1
  16. package/dist/src/cache/createMemoryCache.d.ts +0 -3
  17. package/dist/src/cache/createMemoryCache.d.ts.map +0 -1
  18. package/dist/src/cache/createNullCache.d.ts +0 -3
  19. package/dist/src/cache/createNullCache.d.ts.map +0 -1
  20. package/dist/src/cache/index.d.ts +0 -5
  21. package/dist/src/cache/index.d.ts.map +0 -1
  22. package/dist/src/constants.d.ts +0 -7
  23. package/dist/src/constants.d.ts.map +0 -1
  24. package/dist/src/createAlgoliaAgent.d.ts +0 -3
  25. package/dist/src/createAlgoliaAgent.d.ts.map +0 -1
  26. package/dist/src/createAuth.d.ts +0 -6
  27. package/dist/src/createAuth.d.ts.map +0 -1
  28. package/dist/src/createEchoRequester.d.ts +0 -7
  29. package/dist/src/createEchoRequester.d.ts.map +0 -1
  30. package/dist/src/createIterablePromise.d.ts +0 -13
  31. package/dist/src/createIterablePromise.d.ts.map +0 -1
  32. package/dist/src/getAlgoliaAgent.d.ts +0 -8
  33. package/dist/src/getAlgoliaAgent.d.ts.map +0 -1
  34. package/dist/src/transporter/createStatefulHost.d.ts +0 -3
  35. package/dist/src/transporter/createStatefulHost.d.ts.map +0 -1
  36. package/dist/src/transporter/createTransporter.d.ts +0 -3
  37. package/dist/src/transporter/createTransporter.d.ts.map +0 -1
  38. package/dist/src/transporter/errors.d.ts +0 -38
  39. package/dist/src/transporter/errors.d.ts.map +0 -1
  40. package/dist/src/transporter/helpers.d.ts +0 -9
  41. package/dist/src/transporter/helpers.d.ts.map +0 -1
  42. package/dist/src/transporter/index.d.ts +0 -7
  43. package/dist/src/transporter/index.d.ts.map +0 -1
  44. package/dist/src/transporter/responses.d.ts +0 -5
  45. package/dist/src/transporter/responses.d.ts.map +0 -1
  46. package/dist/src/transporter/stackTrace.d.ts +0 -4
  47. package/dist/src/transporter/stackTrace.d.ts.map +0 -1
  48. package/dist/src/types/cache.d.ts +0 -61
  49. package/dist/src/types/cache.d.ts.map +0 -1
  50. package/dist/src/types/createClient.d.ts +0 -12
  51. package/dist/src/types/createClient.d.ts.map +0 -1
  52. package/dist/src/types/createIterablePromise.d.ts +0 -36
  53. package/dist/src/types/createIterablePromise.d.ts.map +0 -1
  54. package/dist/src/types/host.d.ts +0 -37
  55. package/dist/src/types/host.d.ts.map +0 -1
  56. package/dist/src/types/index.d.ts +0 -7
  57. package/dist/src/types/index.d.ts.map +0 -1
  58. package/dist/src/types/requester.d.ts +0 -66
  59. package/dist/src/types/requester.d.ts.map +0 -1
  60. package/dist/src/types/transporter.d.ts +0 -128
  61. package/dist/src/types/transporter.d.ts.map +0 -1
@@ -0,0 +1,438 @@
1
+ type Cache = {
2
+ /**
3
+ * Gets the value of the given `key`.
4
+ */
5
+ get: <TValue>(key: Record<string, any> | string, defaultValue: () => Promise<TValue>, events?: CacheEvents<TValue>) => Promise<TValue>;
6
+ /**
7
+ * Sets the given value with the given `key`.
8
+ */
9
+ set: <TValue>(key: Record<string, any> | string, value: TValue) => Promise<TValue>;
10
+ /**
11
+ * Deletes the given `key`.
12
+ */
13
+ delete: (key: Record<string, any> | string) => Promise<void>;
14
+ /**
15
+ * Clears the cache.
16
+ */
17
+ clear: () => Promise<void>;
18
+ };
19
+ type CacheEvents<TValue> = {
20
+ /**
21
+ * The callback when the given `key` is missing from the cache.
22
+ */
23
+ miss: (value: TValue) => Promise<any>;
24
+ };
25
+ type MemoryCacheOptions = {
26
+ /**
27
+ * If keys and values should be serialized using `JSON.stringify`.
28
+ */
29
+ serializable?: boolean;
30
+ };
31
+ type BrowserLocalStorageOptions = {
32
+ /**
33
+ * The cache key.
34
+ */
35
+ key: string;
36
+ /**
37
+ * The time to live for each cached item in seconds.
38
+ */
39
+ timeToLive?: number;
40
+ /**
41
+ * The native local storage implementation.
42
+ */
43
+ localStorage?: Storage;
44
+ };
45
+ type BrowserLocalStorageCacheItem = {
46
+ /**
47
+ * The cache item creation timestamp.
48
+ */
49
+ timestamp: number;
50
+ /**
51
+ * The cache item value.
52
+ */
53
+ value: any;
54
+ };
55
+ type FallbackableCacheOptions = {
56
+ /**
57
+ * List of caches order by priority.
58
+ */
59
+ caches: Cache[];
60
+ };
61
+
62
+ type Host = {
63
+ /**
64
+ * The host URL.
65
+ */
66
+ url: string;
67
+ /**
68
+ * The accepted transporter.
69
+ */
70
+ accept: 'read' | 'readWrite' | 'write';
71
+ /**
72
+ * The protocol of the host URL.
73
+ */
74
+ protocol: 'http' | 'https';
75
+ /**
76
+ * The port of the host URL.
77
+ */
78
+ port?: number;
79
+ };
80
+ type StatefulHost = Host & {
81
+ /**
82
+ * The status of the host.
83
+ */
84
+ status: 'down' | 'timed out' | 'up';
85
+ /**
86
+ * The last update of the host status, used to compare with the expiration delay.
87
+ */
88
+ lastUpdate: number;
89
+ /**
90
+ * Returns whether the host is up or not.
91
+ */
92
+ isUp: () => boolean;
93
+ /**
94
+ * Returns whether the host is timed out or not.
95
+ */
96
+ isTimedOut: () => boolean;
97
+ };
98
+
99
+ /**
100
+ * The method of the request.
101
+ */
102
+ type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
103
+ type Request = {
104
+ method: Method;
105
+ /**
106
+ * The path of the REST API to send the request to.
107
+ */
108
+ path: string;
109
+ queryParameters: QueryParameters;
110
+ data?: Array<Record<string, any>> | Record<string, any>;
111
+ headers: Headers;
112
+ /**
113
+ * If the given request should persist on the cache. Keep in mind,
114
+ * that some methods may have this option enabled by default.
115
+ */
116
+ cacheable?: boolean;
117
+ /**
118
+ * Some POST methods in the Algolia REST API uses the `read` transporter.
119
+ * This information is defined at the spec level.
120
+ */
121
+ useReadTransporter?: boolean;
122
+ };
123
+ type EndRequest = Pick<Request, 'headers' | 'method'> & {
124
+ /**
125
+ * The full URL of the REST API.
126
+ */
127
+ url: string;
128
+ /**
129
+ * The connection timeout, in milliseconds.
130
+ */
131
+ connectTimeout: number;
132
+ /**
133
+ * The response timeout, in milliseconds.
134
+ */
135
+ responseTimeout: number;
136
+ data?: string;
137
+ };
138
+ type Response = {
139
+ /**
140
+ * The body of the response.
141
+ */
142
+ content: string;
143
+ /**
144
+ * Whether the API call is timed out or not.
145
+ */
146
+ isTimedOut: boolean;
147
+ /**
148
+ * The HTTP status code of the response.
149
+ */
150
+ status: number;
151
+ };
152
+ type Requester = {
153
+ /**
154
+ * Sends the given `request` to the server.
155
+ */
156
+ send: (request: EndRequest) => Promise<Response>;
157
+ };
158
+ type EchoResponse = Omit<EndRequest, 'data'> & Pick<Request, 'data' | 'path'> & {
159
+ host: string;
160
+ algoliaAgent: string;
161
+ searchParams?: Record<string, string>;
162
+ };
163
+
164
+ type Headers = Record<string, string>;
165
+ type QueryParameters = Record<string, any>;
166
+ type RequestOptions = Pick<Request, 'cacheable'> & {
167
+ /**
168
+ * Custom timeout for the request. Note that, in normal situations
169
+ * the given timeout will be applied. But the transporter layer may
170
+ * increase this timeout if there is need for it.
171
+ */
172
+ timeouts?: Partial<Timeouts>;
173
+ /**
174
+ * Custom headers for the request. This headers are
175
+ * going to be merged the transporter headers.
176
+ */
177
+ headers?: Headers;
178
+ /**
179
+ * Custom query parameters for the request. This query parameters are
180
+ * going to be merged the transporter query parameters.
181
+ */
182
+ queryParameters?: QueryParameters;
183
+ /**
184
+ * Custom data for the request. This data is
185
+ * going to be merged the transporter data.
186
+ */
187
+ data?: Array<Record<string, any>> | Record<string, any>;
188
+ };
189
+ type StackFrame = {
190
+ request: EndRequest;
191
+ response: Response;
192
+ host: Host;
193
+ triesLeft: number;
194
+ };
195
+ type AlgoliaAgentOptions = {
196
+ /**
197
+ * The segment. Usually the integration name.
198
+ */
199
+ segment: string;
200
+ /**
201
+ * The version. Usually the integration version.
202
+ */
203
+ version?: string;
204
+ };
205
+ type AlgoliaAgent = {
206
+ /**
207
+ * The raw value of the user agent.
208
+ */
209
+ value: string;
210
+ /**
211
+ * Mutates the current user agent adding the given user agent options.
212
+ */
213
+ add: (options: AlgoliaAgentOptions) => AlgoliaAgent;
214
+ };
215
+ type Timeouts = {
216
+ /**
217
+ * Timeout in milliseconds before the connection is established.
218
+ */
219
+ connect: number;
220
+ /**
221
+ * Timeout in milliseconds before reading the response on a read request.
222
+ */
223
+ read: number;
224
+ /**
225
+ * Timeout in milliseconds before reading the response on a write request.
226
+ */
227
+ write: number;
228
+ };
229
+ type TransporterOptions = {
230
+ /**
231
+ * The cache of the hosts. Usually used to persist
232
+ * the state of the host when its down.
233
+ */
234
+ hostsCache: Cache;
235
+ /**
236
+ * The underlying requester used. Should differ
237
+ * depending of the environment where the client
238
+ * will be used.
239
+ */
240
+ requester: Requester;
241
+ /**
242
+ * The cache of the requests. When requests are
243
+ * `cacheable`, the returned promised persists
244
+ * in this cache to shared in similar requests
245
+ * before being resolved.
246
+ */
247
+ requestsCache: Cache;
248
+ /**
249
+ * The cache of the responses. When requests are
250
+ * `cacheable`, the returned responses persists
251
+ * in this cache to shared in similar requests.
252
+ */
253
+ responsesCache: Cache;
254
+ /**
255
+ * The timeouts used by the requester. The transporter
256
+ * layer may increase this timeouts as defined on the
257
+ * retry strategy.
258
+ */
259
+ timeouts: Timeouts;
260
+ /**
261
+ * The hosts used by the requester.
262
+ */
263
+ hosts: Host[];
264
+ /**
265
+ * The headers used by the requester. The transporter
266
+ * layer may add some extra headers during the request
267
+ * for the user agent, and others.
268
+ */
269
+ baseHeaders: Headers;
270
+ /**
271
+ * The query parameters used by the requester. The transporter
272
+ * layer may add some extra headers during the request
273
+ * for the user agent, and others.
274
+ */
275
+ baseQueryParameters: QueryParameters;
276
+ /**
277
+ * The user agent used. Sent on query parameters.
278
+ */
279
+ algoliaAgent: AlgoliaAgent;
280
+ };
281
+ type Transporter = TransporterOptions & {
282
+ /**
283
+ * Performs a request.
284
+ * The `baseRequest` and `baseRequestOptions` will be merged accordingly.
285
+ */
286
+ request: <TResponse>(baseRequest: Request, baseRequestOptions?: RequestOptions) => Promise<TResponse>;
287
+ };
288
+
289
+ type AuthMode = 'WithinHeaders' | 'WithinQueryParameters';
290
+ type OverriddenTransporterOptions = 'baseHeaders' | 'baseQueryParameters' | 'hosts';
291
+ type CreateClientOptions = Omit<TransporterOptions, OverriddenTransporterOptions | 'algoliaAgent'> & Partial<Pick<TransporterOptions, OverriddenTransporterOptions>> & {
292
+ appId: string;
293
+ apiKey: string;
294
+ authMode?: AuthMode;
295
+ algoliaAgents: AlgoliaAgentOptions[];
296
+ };
297
+ type ClientOptions = Partial<Omit<CreateClientOptions, 'apiKey' | 'appId'>>;
298
+
299
+ type IterableOptions<TResponse> = Partial<{
300
+ /**
301
+ * The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
302
+ */
303
+ aggregator: (response: TResponse) => void;
304
+ /**
305
+ * The `validate` condition to throw an error and its message.
306
+ */
307
+ error: {
308
+ /**
309
+ * The function to validate the error condition.
310
+ */
311
+ validate: (response: TResponse) => boolean;
312
+ /**
313
+ * The error message to throw.
314
+ */
315
+ message: (response: TResponse) => string;
316
+ };
317
+ /**
318
+ * The function to decide how long to wait between iterations.
319
+ */
320
+ timeout: () => number;
321
+ }>;
322
+ type CreateIterablePromise<TResponse> = IterableOptions<TResponse> & {
323
+ /**
324
+ * The function to run, which returns a promise.
325
+ *
326
+ * 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.
327
+ */
328
+ func: (previousResponse?: TResponse) => Promise<TResponse>;
329
+ /**
330
+ * The validator function. It receive the resolved return of the API call.
331
+ */
332
+ validate: (response: TResponse) => boolean;
333
+ };
334
+
335
+ declare function createAuth(appId: string, apiKey: string, authMode?: AuthMode): {
336
+ readonly headers: () => Headers;
337
+ readonly queryParameters: () => QueryParameters;
338
+ };
339
+
340
+ type EchoRequesterParams = {
341
+ getURL: (url: string) => URL;
342
+ status?: number;
343
+ };
344
+ declare function createEchoRequester({ getURL, status }: EchoRequesterParams): Requester;
345
+
346
+ /**
347
+ * Helper: Returns the promise of a given `func` to iterate on, based on a given `validate` condition.
348
+ *
349
+ * @param createIterator - The createIterator options.
350
+ * @param createIterator.func - The function to run, which returns a promise.
351
+ * @param createIterator.validate - The validator function. It receives the resolved return of `func`.
352
+ * @param createIterator.aggregator - The function that runs right after the `func` method has been executed, allows you to do anything with the response before `validate`.
353
+ * @param createIterator.error - The `validate` condition to throw an error, and its message.
354
+ * @param createIterator.timeout - The function to decide how long to wait between iterations.
355
+ */
356
+ declare function createIterablePromise<TResponse>({ func, validate, aggregator, error, timeout, }: CreateIterablePromise<TResponse>): Promise<TResponse>;
357
+
358
+ declare function createBrowserLocalStorageCache(options: BrowserLocalStorageOptions): Cache;
359
+
360
+ declare function createFallbackableCache(options: FallbackableCacheOptions): Cache;
361
+
362
+ declare function createMemoryCache(options?: MemoryCacheOptions): Cache;
363
+
364
+ declare function createNullCache(): Cache;
365
+
366
+ declare function createTransporter({ hosts, hostsCache, baseHeaders, baseQueryParameters, algoliaAgent, timeouts, requester, requestsCache, responsesCache, }: TransporterOptions): Transporter;
367
+
368
+ declare function createStatefulHost(host: Host, status?: StatefulHost['status']): StatefulHost;
369
+
370
+ declare class AlgoliaError extends Error {
371
+ name: string;
372
+ constructor(message: string, name: string);
373
+ }
374
+ declare class ErrorWithStackTrace extends AlgoliaError {
375
+ stackTrace: StackFrame[];
376
+ constructor(message: string, stackTrace: StackFrame[], name: string);
377
+ }
378
+ declare class RetryError extends ErrorWithStackTrace {
379
+ constructor(stackTrace: StackFrame[]);
380
+ }
381
+ declare class ApiError extends ErrorWithStackTrace {
382
+ status: number;
383
+ constructor(message: string, status: number, stackTrace: StackFrame[], name?: string);
384
+ }
385
+ declare class DeserializationError extends AlgoliaError {
386
+ response: Response;
387
+ constructor(message: string, response: Response);
388
+ }
389
+ type DetailedErrorWithMessage = {
390
+ message: string;
391
+ label: string;
392
+ };
393
+ type DetailedErrorWithTypeID = {
394
+ id: string;
395
+ type: string;
396
+ name?: string;
397
+ };
398
+ type DetailedError = {
399
+ code: string;
400
+ details?: DetailedErrorWithMessage[] | DetailedErrorWithTypeID[];
401
+ };
402
+ declare class DetailedApiError extends ApiError {
403
+ error: DetailedError;
404
+ constructor(message: string, status: number, error: DetailedError, stackTrace: StackFrame[]);
405
+ }
406
+
407
+ declare function shuffle<TData>(array: TData[]): TData[];
408
+ declare function serializeUrl(host: Host, path: string, queryParameters: QueryParameters): string;
409
+ declare function serializeQueryParameters(parameters: QueryParameters): string;
410
+ declare function serializeData(request: Request, requestOptions: RequestOptions): string | undefined;
411
+ declare function serializeHeaders(baseHeaders: Headers, requestHeaders: Headers, requestOptionsHeaders?: Headers): Headers;
412
+ declare function deserializeSuccess<TObject>(response: Response): TObject;
413
+ declare function deserializeFailure({ content, status }: Response, stackFrame: StackFrame[]): Error;
414
+
415
+ declare function isNetworkError({ isTimedOut, status }: Omit<Response, 'content'>): boolean;
416
+ declare function isRetryable({ isTimedOut, status }: Omit<Response, 'content'>): boolean;
417
+ declare function isSuccess({ status }: Pick<Response, 'status'>): boolean;
418
+
419
+ declare function stackTraceWithoutCredentials(stackTrace: StackFrame[]): StackFrame[];
420
+ declare function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame;
421
+
422
+ declare function createAlgoliaAgent(version: string): AlgoliaAgent;
423
+
424
+ type GetAlgoliaAgent = {
425
+ algoliaAgents: AlgoliaAgentOptions[];
426
+ client: string;
427
+ version: string;
428
+ };
429
+ declare function getAlgoliaAgent({ algoliaAgents, client, version }: GetAlgoliaAgent): AlgoliaAgent;
430
+
431
+ declare const DEFAULT_CONNECT_TIMEOUT_BROWSER = 1000;
432
+ declare const DEFAULT_READ_TIMEOUT_BROWSER = 2000;
433
+ declare const DEFAULT_WRITE_TIMEOUT_BROWSER = 30000;
434
+ declare const DEFAULT_CONNECT_TIMEOUT_NODE = 2000;
435
+ declare const DEFAULT_READ_TIMEOUT_NODE = 5000;
436
+ declare const DEFAULT_WRITE_TIMEOUT_NODE = 30000;
437
+
438
+ export { type AlgoliaAgent, type AlgoliaAgentOptions, AlgoliaError, ApiError, type AuthMode, type BrowserLocalStorageCacheItem, type BrowserLocalStorageOptions, type Cache, type CacheEvents, type ClientOptions, type CreateClientOptions, type CreateIterablePromise, DEFAULT_CONNECT_TIMEOUT_BROWSER, DEFAULT_CONNECT_TIMEOUT_NODE, DEFAULT_READ_TIMEOUT_BROWSER, DEFAULT_READ_TIMEOUT_NODE, DEFAULT_WRITE_TIMEOUT_BROWSER, DEFAULT_WRITE_TIMEOUT_NODE, DeserializationError, DetailedApiError, type DetailedError, type DetailedErrorWithMessage, type DetailedErrorWithTypeID, type EchoRequesterParams, type EchoResponse, type EndRequest, ErrorWithStackTrace, type FallbackableCacheOptions, type GetAlgoliaAgent, type Headers, type Host, type IterableOptions, type MemoryCacheOptions, type Method, type QueryParameters, type Request, type RequestOptions, type Requester, type Response, RetryError, type StackFrame, type StatefulHost, type Timeouts, type Transporter, type TransporterOptions, createAlgoliaAgent, createAuth, createBrowserLocalStorageCache, createEchoRequester, createFallbackableCache, createIterablePromise, createMemoryCache, createNullCache, createStatefulHost, createTransporter, deserializeFailure, deserializeSuccess, getAlgoliaAgent, isNetworkError, isRetryable, isSuccess, serializeData, serializeHeaders, serializeQueryParameters, serializeUrl, shuffle, stackFrameWithoutCredentials, stackTraceWithoutCredentials };