@algolia/client-common 4.14.2 → 5.0.0-alpha.11

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 (89) hide show
  1. package/dist/client-common.cjs.js +804 -92
  2. package/dist/client-common.esm.node.js +776 -0
  3. package/dist/index.d.ts +10 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/src/cache/createBrowserLocalStorageCache.d.ts +3 -0
  6. package/dist/src/cache/createBrowserLocalStorageCache.d.ts.map +1 -0
  7. package/dist/src/cache/createFallbackableCache.d.ts +3 -0
  8. package/dist/src/cache/createFallbackableCache.d.ts.map +1 -0
  9. package/dist/src/cache/createMemoryCache.d.ts +3 -0
  10. package/dist/src/cache/createMemoryCache.d.ts.map +1 -0
  11. package/dist/src/cache/createNullCache.d.ts +3 -0
  12. package/dist/src/cache/createNullCache.d.ts.map +1 -0
  13. package/dist/src/cache/index.d.ts +5 -0
  14. package/dist/src/cache/index.d.ts.map +1 -0
  15. package/dist/src/constants.d.ts +7 -0
  16. package/dist/src/constants.d.ts.map +1 -0
  17. package/dist/src/createAlgoliaAgent.d.ts +3 -0
  18. package/dist/src/createAlgoliaAgent.d.ts.map +1 -0
  19. package/dist/src/createAuth.d.ts +6 -0
  20. package/dist/src/createAuth.d.ts.map +1 -0
  21. package/dist/src/createEchoRequester.d.ts +7 -0
  22. package/dist/src/createEchoRequester.d.ts.map +1 -0
  23. package/dist/src/createIterablePromise.d.ts +13 -0
  24. package/dist/src/createIterablePromise.d.ts.map +1 -0
  25. package/dist/src/getAlgoliaAgent.d.ts +8 -0
  26. package/dist/src/getAlgoliaAgent.d.ts.map +1 -0
  27. package/dist/src/transporter/createStatefulHost.d.ts +3 -0
  28. package/dist/src/transporter/createStatefulHost.d.ts.map +1 -0
  29. package/dist/src/transporter/createTransporter.d.ts +3 -0
  30. package/dist/src/transporter/createTransporter.d.ts.map +1 -0
  31. package/dist/src/transporter/errors.d.ts +21 -0
  32. package/dist/src/transporter/errors.d.ts.map +1 -0
  33. package/dist/src/transporter/helpers.d.ts +9 -0
  34. package/dist/src/transporter/helpers.d.ts.map +1 -0
  35. package/dist/src/transporter/index.d.ts +7 -0
  36. package/dist/src/transporter/index.d.ts.map +1 -0
  37. package/dist/src/transporter/responses.d.ts +5 -0
  38. package/dist/src/transporter/responses.d.ts.map +1 -0
  39. package/dist/src/transporter/stackTrace.d.ts +4 -0
  40. package/dist/src/transporter/stackTrace.d.ts.map +1 -0
  41. package/dist/src/types/cache.d.ts +47 -0
  42. package/dist/src/types/cache.d.ts.map +1 -0
  43. package/dist/src/types/createClient.d.ts +12 -0
  44. package/dist/src/types/createClient.d.ts.map +1 -0
  45. package/dist/src/types/createIterablePromise.d.ts +36 -0
  46. package/dist/src/types/createIterablePromise.d.ts.map +1 -0
  47. package/dist/src/types/host.d.ts +33 -0
  48. package/dist/src/types/host.d.ts.map +1 -0
  49. package/dist/src/types/index.d.ts +7 -0
  50. package/dist/src/types/index.d.ts.map +1 -0
  51. package/dist/src/types/requester.d.ts +66 -0
  52. package/dist/src/types/requester.d.ts.map +1 -0
  53. package/dist/src/types/transporter.d.ts +128 -0
  54. package/dist/src/types/transporter.d.ts.map +1 -0
  55. package/index.ts +9 -0
  56. package/package.json +25 -15
  57. package/src/__tests__/cache/browser-local-storage-cache.test.ts +134 -0
  58. package/src/__tests__/cache/fallbackable-cache.test.ts +126 -0
  59. package/src/__tests__/cache/memory-cache.test.ts +90 -0
  60. package/src/__tests__/cache/null-cache.test.ts +49 -0
  61. package/src/__tests__/create-iterable-promise.test.ts +238 -0
  62. package/src/cache/createBrowserLocalStorageCache.ts +74 -0
  63. package/src/cache/createFallbackableCache.ts +53 -0
  64. package/src/cache/createMemoryCache.ts +56 -0
  65. package/src/cache/createNullCache.ts +34 -0
  66. package/src/cache/index.ts +4 -0
  67. package/src/constants.ts +7 -0
  68. package/src/createAlgoliaAgent.ts +20 -0
  69. package/src/createAuth.ts +25 -0
  70. package/src/createEchoRequester.ts +59 -0
  71. package/src/createIterablePromise.ts +47 -0
  72. package/src/getAlgoliaAgent.ts +25 -0
  73. package/src/transporter/createStatefulHost.ts +24 -0
  74. package/src/transporter/createTransporter.ts +350 -0
  75. package/src/transporter/errors.ts +51 -0
  76. package/src/transporter/helpers.ts +119 -0
  77. package/src/transporter/index.ts +6 -0
  78. package/src/transporter/responses.ts +23 -0
  79. package/src/transporter/stackTrace.ts +30 -0
  80. package/src/types/cache.ts +61 -0
  81. package/src/types/createClient.ts +23 -0
  82. package/src/types/createIterablePromise.ts +40 -0
  83. package/src/types/host.ts +38 -0
  84. package/src/types/index.ts +6 -0
  85. package/src/types/requester.ts +72 -0
  86. package/src/types/transporter.ts +153 -0
  87. package/dist/client-common.d.ts +0 -102
  88. package/dist/client-common.esm.js +0 -89
  89. package/index.js +0 -2
@@ -0,0 +1,38 @@
1
+ export type Host = {
2
+ /**
3
+ * The host URL.
4
+ */
5
+ url: string;
6
+
7
+ /**
8
+ * The accepted transporter.
9
+ */
10
+ accept: 'read' | 'readWrite' | 'write';
11
+
12
+ /**
13
+ * The protocol of the host URL.
14
+ */
15
+ protocol: 'http' | 'https';
16
+ };
17
+
18
+ export type StatefulHost = Host & {
19
+ /**
20
+ * The status of the host.
21
+ */
22
+ status: 'down' | 'timed out' | 'up';
23
+
24
+ /**
25
+ * The last update of the host status, used to compare with the expiration delay.
26
+ */
27
+ lastUpdate: number;
28
+
29
+ /**
30
+ * Returns whether the host is up or not.
31
+ */
32
+ isUp: () => boolean;
33
+
34
+ /**
35
+ * Returns whether the host is timed out or not.
36
+ */
37
+ isTimedOut: () => boolean;
38
+ };
@@ -0,0 +1,6 @@
1
+ export * from './cache';
2
+ export * from './createClient';
3
+ export * from './createIterablePromise';
4
+ export * from './host';
5
+ export * from './requester';
6
+ export * from './transporter';
@@ -0,0 +1,72 @@
1
+ import type { Headers, QueryParameters } from './transporter';
2
+
3
+ /**
4
+ * The method of the request.
5
+ */
6
+ export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
7
+
8
+ export type Request = {
9
+ method: Method;
10
+ /**
11
+ * The path of the REST API to send the request to.
12
+ */
13
+ path: string;
14
+ queryParameters: QueryParameters;
15
+ data?: Array<Record<string, any>> | Record<string, any>;
16
+ headers: Headers;
17
+ /**
18
+ * If the given request should persist on the cache. Keep in mind,
19
+ * that some methods may have this option enabled by default.
20
+ */
21
+ cacheable?: boolean;
22
+ /**
23
+ * Some POST methods in the Algolia REST API uses the `read` transporter.
24
+ * This information is defined at the spec level.
25
+ */
26
+ useReadTransporter?: boolean;
27
+ };
28
+
29
+ export type EndRequest = Pick<Request, 'headers' | 'method'> & {
30
+ /**
31
+ * The full URL of the REST API.
32
+ */
33
+ url: string;
34
+ /**
35
+ * The connection timeout, in milliseconds.
36
+ */
37
+ connectTimeout: number;
38
+ /**
39
+ * The response timeout, in milliseconds.
40
+ */
41
+ responseTimeout: number;
42
+ data?: string;
43
+ };
44
+
45
+ export type Response = {
46
+ /**
47
+ * The body of the response.
48
+ */
49
+ content: string;
50
+ /**
51
+ * Whether the API call is timed out or not.
52
+ */
53
+ isTimedOut: boolean;
54
+ /**
55
+ * The HTTP status code of the response.
56
+ */
57
+ status: number;
58
+ };
59
+
60
+ export type Requester = {
61
+ /**
62
+ * Sends the given `request` to the server.
63
+ */
64
+ send: (request: EndRequest) => Promise<Response>;
65
+ };
66
+
67
+ export type EchoResponse = Omit<EndRequest, 'data'> &
68
+ Pick<Request, 'data' | 'path'> & {
69
+ host: string;
70
+ algoliaAgent: string;
71
+ searchParams?: Record<string, string>;
72
+ };
@@ -0,0 +1,153 @@
1
+ import type { Cache } from './cache';
2
+ import type { Host } from './host';
3
+ import type { Request, Requester, EndRequest, Response } from './requester';
4
+
5
+ export type Headers = Record<string, string>;
6
+ export type QueryParameters = Record<string, any>;
7
+
8
+ export type RequestOptions = Pick<Request, 'cacheable'> & {
9
+ /**
10
+ * Custom timeout for the request. Note that, in normal situations
11
+ * the given timeout will be applied. But the transporter layer may
12
+ * increase this timeout if there is need for it.
13
+ */
14
+ timeout?: number;
15
+
16
+ /**
17
+ * Custom headers for the request. This headers are
18
+ * going to be merged the transporter headers.
19
+ */
20
+ headers?: Headers;
21
+
22
+ /**
23
+ * Custom query parameters for the request. This query parameters are
24
+ * going to be merged the transporter query parameters.
25
+ */
26
+ queryParameters?: QueryParameters;
27
+
28
+ /**
29
+ * Custom data for the request. This data is
30
+ * going to be merged the transporter data.
31
+ */
32
+ data?: Array<Record<string, any>> | Record<string, any>;
33
+ };
34
+
35
+ export type StackFrame = {
36
+ request: EndRequest;
37
+ response: Response;
38
+ host: Host;
39
+ triesLeft: number;
40
+ };
41
+
42
+ export type AlgoliaAgentOptions = {
43
+ /**
44
+ * The segment. Usually the integration name.
45
+ */
46
+ segment: string;
47
+
48
+ /**
49
+ * The version. Usually the integration version.
50
+ */
51
+ version?: string;
52
+ };
53
+
54
+ export type AlgoliaAgent = {
55
+ /**
56
+ * The raw value of the user agent.
57
+ */
58
+ value: string;
59
+
60
+ /**
61
+ * Mutates the current user agent adding the given user agent options.
62
+ */
63
+ add: (options: AlgoliaAgentOptions) => AlgoliaAgent;
64
+ };
65
+
66
+ export type Timeouts = {
67
+ /**
68
+ * Timeout in milliseconds before the connection is established.
69
+ */
70
+ connect: number;
71
+
72
+ /**
73
+ * Timeout in milliseconds before reading the response on a read request.
74
+ */
75
+ read: number;
76
+
77
+ /**
78
+ * Timeout in milliseconds before reading the response on a write request.
79
+ */
80
+ write: number;
81
+ };
82
+
83
+ export type TransporterOptions = {
84
+ /**
85
+ * The cache of the hosts. Usually used to persist
86
+ * the state of the host when its down.
87
+ */
88
+ hostsCache: Cache;
89
+
90
+ /**
91
+ * The underlying requester used. Should differ
92
+ * depending of the environment where the client
93
+ * will be used.
94
+ */
95
+ requester: Requester;
96
+
97
+ /**
98
+ * The cache of the requests. When requests are
99
+ * `cacheable`, the returned promised persists
100
+ * in this cache to shared in similar requests
101
+ * before being resolved.
102
+ */
103
+ requestsCache: Cache;
104
+
105
+ /**
106
+ * The cache of the responses. When requests are
107
+ * `cacheable`, the returned responses persists
108
+ * in this cache to shared in similar requests.
109
+ */
110
+ responsesCache: Cache;
111
+
112
+ /**
113
+ * The timeouts used by the requester. The transporter
114
+ * layer may increase this timeouts as defined on the
115
+ * retry strategy.
116
+ */
117
+ timeouts: Timeouts;
118
+
119
+ /**
120
+ * The hosts used by the requester.
121
+ */
122
+ hosts: Host[];
123
+
124
+ /**
125
+ * The headers used by the requester. The transporter
126
+ * layer may add some extra headers during the request
127
+ * for the user agent, and others.
128
+ */
129
+ baseHeaders: Headers;
130
+
131
+ /**
132
+ * The query parameters used by the requester. The transporter
133
+ * layer may add some extra headers during the request
134
+ * for the user agent, and others.
135
+ */
136
+ baseQueryParameters: QueryParameters;
137
+
138
+ /**
139
+ * The user agent used. Sent on query parameters.
140
+ */
141
+ algoliaAgent: AlgoliaAgent;
142
+ };
143
+
144
+ export type Transporter = TransporterOptions & {
145
+ /**
146
+ * Performs a request.
147
+ * The `baseRequest` and `baseRequestOptions` will be merged accordingly.
148
+ */
149
+ request: <TResponse>(
150
+ baseRequest: Request,
151
+ baseRequestOptions?: RequestOptions
152
+ ) => Promise<TResponse>;
153
+ };
@@ -1,102 +0,0 @@
1
- import { Headers } from '@algolia/transporter';
2
- import { HostOptions } from '@algolia/transporter';
3
- import { QueryParameters } from '@algolia/transporter';
4
- import { RequestOptions } from '@algolia/transporter';
5
- import { Transporter } from '@algolia/transporter';
6
- import { TransporterOptions } from '@algolia/transporter';
7
-
8
- declare type AddedMethods<TBase, TMethods extends Methods<TBase>> = TBase & {
9
- [TKey in keyof TMethods extends string ? keyof TMethods : never]: ReturnType<TMethods[TKey]>;
10
- };
11
-
12
- export declare function addMethods<TBase extends {}, TMethods extends Methods<TBase>>(base: TBase, methods?: TMethods): AddedMethods<TBase, TMethods>;
13
-
14
- export declare type Auth = {
15
- /**
16
- * Returns the headers related to auth. Should be
17
- * merged to the transporter headers.
18
- */
19
- readonly headers: () => Readonly<Record<string, string>>;
20
- /**
21
- * Returns the query parameters related to auth. Should be
22
- * merged to the query parameters headers.
23
- */
24
- readonly queryParameters: () => Readonly<Record<string, string>>;
25
- };
26
-
27
- export declare const AuthMode: Readonly<Record<string, AuthModeType>>;
28
-
29
- export declare type AuthModeType = 0 | 1;
30
-
31
- export declare type ClientTransporterOptions = Pick<TransporterOptions, Exclude<keyof TransporterOptions, 'headers'> & Exclude<keyof TransporterOptions, 'queryParameters'> & Exclude<keyof TransporterOptions, 'hosts'>> & {
32
- /**
33
- * The hosts used by the requester.
34
- */
35
- readonly hosts?: readonly HostOptions[];
36
- /**
37
- * The headers used by the requester. The transporter
38
- * layer may add some extra headers during the request
39
- * for the user agent, and others.
40
- */
41
- readonly headers?: Headers;
42
- /**
43
- * The query parameters used by the requester. The transporter
44
- * layer may add some extra headers during the request
45
- * for the user agent, and others.
46
- */
47
- readonly queryParameters?: QueryParameters;
48
- };
49
-
50
- export declare function createAuth(authMode: AuthModeType, appId: string, apiKey: string): Auth;
51
-
52
- export declare type CreateClient<TClient, TOptions> = <TMethods extends {
53
- readonly [key: string]: (base: TClient) => (...args: any) => any;
54
- }>(options: TOptions & {
55
- readonly methods?: TMethods;
56
- }) => TClient & {
57
- [key in keyof TMethods extends string ? keyof TMethods : never]: ReturnType<TMethods[key]>;
58
- };
59
-
60
- export declare function createRetryablePromise<TResponse>(callback: (retry: () => Promise<TResponse>) => Promise<TResponse>): Promise<TResponse>;
61
-
62
- export declare function createWaitablePromise<TResponse>(promise: Readonly<Promise<TResponse>>, wait?: Wait<TResponse>): Readonly<WaitablePromise<TResponse>>;
63
-
64
- export declare const destroy: (base: {
65
- readonly transporter: Transporter;
66
- }) => () => Readonly<Promise<void>>;
67
-
68
- export declare function encode(format: string, ...args: readonly any[]): string;
69
-
70
- declare type Methods<TBase> = {
71
- readonly [key: string]: (base: TBase) => (...args: any[]) => any;
72
- };
73
-
74
- export declare function shuffle<TData>(array: TData[]): TData[];
75
-
76
- export declare const version = "4.14.2";
77
-
78
- export declare type Wait<TResponse> = (
79
- /**
80
- * The original response.
81
- */
82
- response: TResponse,
83
- /**
84
- * The custom request options.
85
- */
86
- requestOptions?: RequestOptions) => Readonly<Promise<any>>;
87
-
88
- export declare type WaitablePromise<TResponse> = Readonly<Promise<TResponse>> & {
89
- /**
90
- * Wait for a task to complete before executing the next line of code, to synchronize index updates.
91
- *
92
- * All write operations in Algolia are asynchronous by design. It means that when you add or
93
- * update an object to your index, our servers will reply to your request with a taskID as
94
- * soon as they understood the write operation. The actual insert and indexing will be
95
- * done after replying to your code.
96
- *
97
- * You can wait for a task to complete by using this method.
98
- */
99
- readonly wait: (requestOptions?: RequestOptions) => Readonly<WaitablePromise<TResponse>>;
100
- };
101
-
102
- export { }
@@ -1,89 +0,0 @@
1
- function createAuth(authMode, appId, apiKey) {
2
- const credentials = {
3
- 'x-algolia-api-key': apiKey,
4
- 'x-algolia-application-id': appId,
5
- };
6
- return {
7
- headers() {
8
- return authMode === AuthMode.WithinHeaders ? credentials : {};
9
- },
10
- queryParameters() {
11
- return authMode === AuthMode.WithinQueryParameters ? credentials : {};
12
- },
13
- };
14
- }
15
-
16
- function createRetryablePromise(callback) {
17
- let retriesCount = 0; // eslint-disable-line functional/no-let
18
- const retry = () => {
19
- retriesCount++;
20
- return new Promise((resolve) => {
21
- setTimeout(() => {
22
- resolve(callback(retry));
23
- }, Math.min(100 * retriesCount, 1000));
24
- });
25
- };
26
- return callback(retry);
27
- }
28
-
29
- function createWaitablePromise(promise, wait = (_response, _requestOptions) => {
30
- return Promise.resolve();
31
- }) {
32
- // eslint-disable-next-line functional/immutable-data
33
- return Object.assign(promise, {
34
- wait(requestOptions) {
35
- return createWaitablePromise(promise
36
- .then(response => Promise.all([wait(response, requestOptions), response]))
37
- .then(promiseResults => promiseResults[1]));
38
- },
39
- });
40
- }
41
-
42
- // eslint-disable-next-line functional/prefer-readonly-type
43
- function shuffle(array) {
44
- let c = array.length - 1; // eslint-disable-line functional/no-let
45
- // eslint-disable-next-line functional/no-loop-statement
46
- for (c; c > 0; c--) {
47
- const b = Math.floor(Math.random() * (c + 1));
48
- const a = array[c];
49
- array[c] = array[b]; // eslint-disable-line functional/immutable-data, no-param-reassign
50
- array[b] = a; // eslint-disable-line functional/immutable-data, no-param-reassign
51
- }
52
- return array;
53
- }
54
- function addMethods(base, methods) {
55
- if (!methods) {
56
- return base;
57
- }
58
- Object.keys(methods).forEach(key => {
59
- // eslint-disable-next-line functional/immutable-data, no-param-reassign
60
- base[key] = methods[key](base);
61
- });
62
- return base;
63
- }
64
- function encode(format, ...args) {
65
- // eslint-disable-next-line functional/no-let
66
- let i = 0;
67
- return format.replace(/%s/g, () => encodeURIComponent(args[i++]));
68
- }
69
-
70
- const version = '4.14.2';
71
-
72
- const destroy = (base) => {
73
- return () => {
74
- return base.transporter.requester.destroy();
75
- };
76
- };
77
-
78
- const AuthMode = {
79
- /**
80
- * If auth credentials should be in query parameters.
81
- */
82
- WithinQueryParameters: 0,
83
- /**
84
- * If auth credentials should be in headers.
85
- */
86
- WithinHeaders: 1,
87
- };
88
-
89
- export { AuthMode, addMethods, createAuth, createRetryablePromise, createWaitablePromise, destroy, encode, shuffle, version };
package/index.js DELETED
@@ -1,2 +0,0 @@
1
- // eslint-disable-next-line functional/immutable-data, import/no-commonjs
2
- module.exports = require('./dist/client-common.cjs.js');