@algolia/client-common 5.0.0-alpha.3 → 5.0.0-alpha.6
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/client-common.cjs.js +29 -31
- package/dist/client-common.esm.node.js +29 -29
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/src/createIterablePromise.d.ts +13 -0
- package/dist/src/createIterablePromise.d.ts.map +1 -0
- package/dist/src/types/{Cache.d.ts → cache.d.ts} +1 -1
- package/dist/src/types/{Cache.d.ts.map → cache.d.ts.map} +1 -1
- package/dist/src/types/{CreateClient.d.ts → createClient.d.ts} +2 -2
- package/dist/src/types/{CreateClient.d.ts.map → createClient.d.ts.map} +1 -1
- package/dist/src/types/createIterablePromise.d.ts +36 -0
- package/dist/src/types/createIterablePromise.d.ts.map +1 -0
- package/dist/src/types/{Host.d.ts → host.d.ts} +1 -1
- package/dist/src/types/{Host.d.ts.map → host.d.ts.map} +1 -1
- package/dist/src/types/index.d.ts +6 -6
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/{Requester.d.ts → requester.d.ts} +2 -2
- package/dist/src/types/{Requester.d.ts.map → requester.d.ts.map} +1 -1
- package/dist/src/types/{Transporter.d.ts → transporter.d.ts} +4 -4
- package/dist/src/types/{Transporter.d.ts.map → transporter.d.ts.map} +1 -1
- package/index.ts +1 -1
- package/package.json +2 -2
- package/src/__tests__/create-iterable-promise.test.ts +238 -0
- package/src/createIterablePromise.ts +47 -0
- package/src/types/{Cache.ts → cache.ts} +0 -0
- package/src/types/{CreateClient.ts → createClient.ts} +1 -1
- package/src/types/createIterablePromise.ts +40 -0
- package/src/types/{Host.ts → host.ts} +0 -0
- package/src/types/index.ts +6 -6
- package/src/types/{Requester.ts → requester.ts} +1 -1
- package/src/types/{Transporter.ts → transporter.ts} +3 -3
- package/dist/src/createRetryablePromise.d.ts +0 -14
- package/dist/src/createRetryablePromise.d.ts.map +0 -1
- package/dist/src/types/CreateRetryablePromise.d.ts +0 -19
- package/dist/src/types/CreateRetryablePromise.d.ts.map +0 -1
- package/src/__tests__/create-retryable-promise.test.ts +0 -86
- package/src/createRetryablePromise.ts +0 -52
- package/src/types/CreateRetryablePromise.ts +0 -21
|
@@ -73,43 +73,43 @@ function createEchoRequester({
|
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
const DEFAULT_MAX_RETRIES = 50;
|
|
77
|
-
const DEFAULT_TIMEOUT = retryCount => Math.min(retryCount * 200, 5000);
|
|
78
76
|
/**
|
|
79
|
-
*
|
|
77
|
+
* Helper: Returns the promise of a given `func` to iterate on, based on a given `validate` condition.
|
|
80
78
|
*
|
|
81
|
-
* @param
|
|
82
|
-
* @param
|
|
83
|
-
* @param
|
|
84
|
-
* @param
|
|
85
|
-
* @param
|
|
79
|
+
* @param createIterator - The createIterator options.
|
|
80
|
+
* @param createIterator.func - The function to run, which returns a promise.
|
|
81
|
+
* @param createIterator.validate - The validator function. It receives the resolved return of `func`.
|
|
82
|
+
* @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`.
|
|
83
|
+
* @param createIterator.error - The `validate` condition to throw an error, and its message.
|
|
84
|
+
* @param createIterator.timeout - The function to decide how long to wait between iterations.
|
|
86
85
|
*/
|
|
87
|
-
|
|
88
|
-
function createRetryablePromise({
|
|
86
|
+
function createIterablePromise({
|
|
89
87
|
func,
|
|
90
88
|
validate,
|
|
91
|
-
|
|
92
|
-
|
|
89
|
+
aggregator,
|
|
90
|
+
error,
|
|
91
|
+
timeout = () => 0
|
|
93
92
|
}) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const retry = () => {
|
|
93
|
+
const retry = previousResponse => {
|
|
97
94
|
return new Promise((resolve, reject) => {
|
|
98
|
-
func().then(response => {
|
|
99
|
-
|
|
95
|
+
func(previousResponse).then(response => {
|
|
96
|
+
if (aggregator) {
|
|
97
|
+
aggregator(response);
|
|
98
|
+
}
|
|
100
99
|
|
|
101
|
-
if (
|
|
102
|
-
resolve(response);
|
|
103
|
-
} else if (retryCount + 1 >= maxRetries) {
|
|
104
|
-
reject(new Error(`The maximum number of retries exceeded. (${retryCount + 1}/${maxRetries})`));
|
|
105
|
-
} else {
|
|
106
|
-
retryCount += 1;
|
|
107
|
-
setTimeout(() => {
|
|
108
|
-
retry().then(resolve).catch(reject);
|
|
109
|
-
}, timeout(retryCount));
|
|
100
|
+
if (validate(response)) {
|
|
101
|
+
return resolve(response);
|
|
110
102
|
}
|
|
111
|
-
|
|
112
|
-
|
|
103
|
+
|
|
104
|
+
if (error && error.validate(response)) {
|
|
105
|
+
return reject(new Error(error.message(response)));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return setTimeout(() => {
|
|
109
|
+
retry(response).then(resolve).catch(reject);
|
|
110
|
+
}, timeout());
|
|
111
|
+
}).catch(err => {
|
|
112
|
+
reject(err);
|
|
113
113
|
});
|
|
114
114
|
});
|
|
115
115
|
};
|
|
@@ -779,10 +779,8 @@ exports.AlgoliaError = AlgoliaError;
|
|
|
779
779
|
exports.ApiError = ApiError;
|
|
780
780
|
exports.DEFAULT_CONNECT_TIMEOUT_BROWSER = DEFAULT_CONNECT_TIMEOUT_BROWSER;
|
|
781
781
|
exports.DEFAULT_CONNECT_TIMEOUT_NODE = DEFAULT_CONNECT_TIMEOUT_NODE;
|
|
782
|
-
exports.DEFAULT_MAX_RETRIES = DEFAULT_MAX_RETRIES;
|
|
783
782
|
exports.DEFAULT_READ_TIMEOUT_BROWSER = DEFAULT_READ_TIMEOUT_BROWSER;
|
|
784
783
|
exports.DEFAULT_READ_TIMEOUT_NODE = DEFAULT_READ_TIMEOUT_NODE;
|
|
785
|
-
exports.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
|
|
786
784
|
exports.DEFAULT_WRITE_TIMEOUT_BROWSER = DEFAULT_WRITE_TIMEOUT_BROWSER;
|
|
787
785
|
exports.DEFAULT_WRITE_TIMEOUT_NODE = DEFAULT_WRITE_TIMEOUT_NODE;
|
|
788
786
|
exports.DeserializationError = DeserializationError;
|
|
@@ -793,9 +791,9 @@ exports.createAuth = createAuth;
|
|
|
793
791
|
exports.createBrowserLocalStorageCache = createBrowserLocalStorageCache;
|
|
794
792
|
exports.createEchoRequester = createEchoRequester;
|
|
795
793
|
exports.createFallbackableCache = createFallbackableCache;
|
|
794
|
+
exports.createIterablePromise = createIterablePromise;
|
|
796
795
|
exports.createMemoryCache = createMemoryCache;
|
|
797
796
|
exports.createNullCache = createNullCache;
|
|
798
|
-
exports.createRetryablePromise = createRetryablePromise;
|
|
799
797
|
exports.createStatefulHost = createStatefulHost;
|
|
800
798
|
exports.createTransporter = createTransporter;
|
|
801
799
|
exports.deserializeFailure = deserializeFailure;
|
|
@@ -69,43 +69,43 @@ function createEchoRequester({
|
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
const DEFAULT_MAX_RETRIES = 50;
|
|
73
|
-
const DEFAULT_TIMEOUT = retryCount => Math.min(retryCount * 200, 5000);
|
|
74
72
|
/**
|
|
75
|
-
*
|
|
73
|
+
* Helper: Returns the promise of a given `func` to iterate on, based on a given `validate` condition.
|
|
76
74
|
*
|
|
77
|
-
* @param
|
|
78
|
-
* @param
|
|
79
|
-
* @param
|
|
80
|
-
* @param
|
|
81
|
-
* @param
|
|
75
|
+
* @param createIterator - The createIterator options.
|
|
76
|
+
* @param createIterator.func - The function to run, which returns a promise.
|
|
77
|
+
* @param createIterator.validate - The validator function. It receives the resolved return of `func`.
|
|
78
|
+
* @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`.
|
|
79
|
+
* @param createIterator.error - The `validate` condition to throw an error, and its message.
|
|
80
|
+
* @param createIterator.timeout - The function to decide how long to wait between iterations.
|
|
82
81
|
*/
|
|
83
|
-
|
|
84
|
-
function createRetryablePromise({
|
|
82
|
+
function createIterablePromise({
|
|
85
83
|
func,
|
|
86
84
|
validate,
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
aggregator,
|
|
86
|
+
error,
|
|
87
|
+
timeout = () => 0
|
|
89
88
|
}) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const retry = () => {
|
|
89
|
+
const retry = previousResponse => {
|
|
93
90
|
return new Promise((resolve, reject) => {
|
|
94
|
-
func().then(response => {
|
|
95
|
-
|
|
91
|
+
func(previousResponse).then(response => {
|
|
92
|
+
if (aggregator) {
|
|
93
|
+
aggregator(response);
|
|
94
|
+
}
|
|
96
95
|
|
|
97
|
-
if (
|
|
98
|
-
resolve(response);
|
|
99
|
-
} else if (retryCount + 1 >= maxRetries) {
|
|
100
|
-
reject(new Error(`The maximum number of retries exceeded. (${retryCount + 1}/${maxRetries})`));
|
|
101
|
-
} else {
|
|
102
|
-
retryCount += 1;
|
|
103
|
-
setTimeout(() => {
|
|
104
|
-
retry().then(resolve).catch(reject);
|
|
105
|
-
}, timeout(retryCount));
|
|
96
|
+
if (validate(response)) {
|
|
97
|
+
return resolve(response);
|
|
106
98
|
}
|
|
107
|
-
|
|
108
|
-
|
|
99
|
+
|
|
100
|
+
if (error && error.validate(response)) {
|
|
101
|
+
return reject(new Error(error.message(response)));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return setTimeout(() => {
|
|
105
|
+
retry(response).then(resolve).catch(reject);
|
|
106
|
+
}, timeout());
|
|
107
|
+
}).catch(err => {
|
|
108
|
+
reject(err);
|
|
109
109
|
});
|
|
110
110
|
});
|
|
111
111
|
};
|
|
@@ -771,4 +771,4 @@ const DEFAULT_CONNECT_TIMEOUT_NODE = 2000;
|
|
|
771
771
|
const DEFAULT_READ_TIMEOUT_NODE = 5000;
|
|
772
772
|
const DEFAULT_WRITE_TIMEOUT_NODE = 30000;
|
|
773
773
|
|
|
774
|
-
export { AlgoliaError, ApiError, DEFAULT_CONNECT_TIMEOUT_BROWSER, DEFAULT_CONNECT_TIMEOUT_NODE,
|
|
774
|
+
export { AlgoliaError, ApiError, 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, ErrorWithStackTrace, RetryError, createAlgoliaAgent, createAuth, createBrowserLocalStorageCache, createEchoRequester, createFallbackableCache, createIterablePromise, createMemoryCache, createNullCache, createStatefulHost, createTransporter, deserializeFailure, deserializeSuccess, getAlgoliaAgent, isNetworkError, isRetryable, isSuccess, serializeData, serializeHeaders, serializeQueryParameters, serializeUrl, shuffle, stackFrameWithoutCredentials, stackTraceWithoutCredentials };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './src/createAuth';
|
|
2
2
|
export * from './src/createEchoRequester';
|
|
3
|
-
export * from './src/
|
|
3
|
+
export * from './src/createIterablePromise';
|
|
4
4
|
export * from './src/cache';
|
|
5
5
|
export * from './src/transporter';
|
|
6
6
|
export * from './src/createAlgoliaAgent';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CreateIterablePromise } from './types/createIterablePromise';
|
|
2
|
+
/**
|
|
3
|
+
* Helper: Returns the promise of a given `func` to iterate on, based on a given `validate` condition.
|
|
4
|
+
*
|
|
5
|
+
* @param createIterator - The createIterator options.
|
|
6
|
+
* @param createIterator.func - The function to run, which returns a promise.
|
|
7
|
+
* @param createIterator.validate - The validator function. It receives the resolved return of `func`.
|
|
8
|
+
* @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`.
|
|
9
|
+
* @param createIterator.error - The `validate` condition to throw an error, and its message.
|
|
10
|
+
* @param createIterator.timeout - The function to decide how long to wait between iterations.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createIterablePromise<TResponse>({ func, validate, aggregator, error, timeout, }: CreateIterablePromise<TResponse>): Promise<TResponse>;
|
|
13
|
+
//# sourceMappingURL=createIterablePromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createIterablePromise.d.ts","sourceRoot":"","sources":["../../../../packages/client-common/src/createIterablePromise.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAE3E;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,EAC/C,IAAI,EACJ,QAAQ,EACR,UAAU,EACV,KAAK,EACL,OAAyB,GAC1B,EAAE,qBAAqB,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CA4BvD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/cache.ts"],"names":[],"mappings":"AAAA,oBAAY,KAAK,GAAG;IAClB;;OAEG;IACH,GAAG,EAAE,CAAC,MAAM,EACV,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,EACjC,YAAY,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,EACnC,MAAM,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,KACzB,OAAO,CAAC,MAAM,CAAC,CAAC;IAErB;;OAEG;IACH,GAAG,EAAE,CAAC,MAAM,EACV,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,EACjC,KAAK,EAAE,MAAM,KACV,OAAO,CAAC,MAAM,CAAC,CAAC;IAErB;;OAEG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAE7D;;OAEG;IACH,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5B,CAAC;AAEF,oBAAY,WAAW,CAAC,MAAM,IAAI;IAChC;;OAEG;IACH,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CACvC,CAAC;AAEF,oBAAY,kBAAkB,GAAG;IAC/B;;OAEG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,oBAAY,0BAA0B,GAAG;IACvC;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,oBAAY,wBAAwB,GAAG;IACrC;;OAEG;IACH,MAAM,EAAE,KAAK,EAAE,CAAC;CACjB,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AlgoliaAgentOptions, TransporterOptions } from './
|
|
1
|
+
import type { AlgoliaAgentOptions, TransporterOptions } from './transporter';
|
|
2
2
|
export declare type AuthMode = 'WithinHeaders' | 'WithinQueryParameters';
|
|
3
3
|
declare type OverriddenTransporterOptions = 'baseHeaders' | 'baseQueryParameters' | 'hosts';
|
|
4
4
|
export declare type CreateClientOptions = Omit<TransporterOptions, OverriddenTransporterOptions | 'algoliaAgent'> & Partial<Pick<TransporterOptions, OverriddenTransporterOptions>> & {
|
|
@@ -9,4 +9,4 @@ export declare type CreateClientOptions = Omit<TransporterOptions, OverriddenTra
|
|
|
9
9
|
};
|
|
10
10
|
export declare type ClientOptions = Partial<Omit<CreateClientOptions, 'apiKey' | 'appId'>>;
|
|
11
11
|
export {};
|
|
12
|
-
//# sourceMappingURL=
|
|
12
|
+
//# sourceMappingURL=createClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"createClient.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/createClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAE7E,oBAAY,QAAQ,GAAG,eAAe,GAAG,uBAAuB,CAAC;AAEjE,aAAK,4BAA4B,GAC7B,aAAa,GACb,qBAAqB,GACrB,OAAO,CAAC;AAEZ,oBAAY,mBAAmB,GAAG,IAAI,CACpC,kBAAkB,EAClB,4BAA4B,GAAG,cAAc,CAC9C,GACC,OAAO,CAAC,IAAI,CAAC,kBAAkB,EAAE,4BAA4B,CAAC,CAAC,GAAG;IAChE,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,aAAa,EAAE,mBAAmB,EAAE,CAAC;CACtC,CAAC;AAEJ,oBAAY,aAAa,GAAG,OAAO,CACjC,IAAI,CAAC,mBAAmB,EAAE,QAAQ,GAAG,OAAO,CAAC,CAC9C,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare 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
|
+
* The `validate` condition to throw an error and its message.
|
|
8
|
+
*/
|
|
9
|
+
error: {
|
|
10
|
+
/**
|
|
11
|
+
* The function to validate the error condition.
|
|
12
|
+
*/
|
|
13
|
+
validate: (response: TResponse) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* The error message to throw.
|
|
16
|
+
*/
|
|
17
|
+
message: (response: TResponse) => string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The function to decide how long to wait between iterations.
|
|
21
|
+
*/
|
|
22
|
+
timeout: () => number;
|
|
23
|
+
}>;
|
|
24
|
+
export declare type CreateIterablePromise<TResponse> = IterableOptions<TResponse> & {
|
|
25
|
+
/**
|
|
26
|
+
* The function to run, which returns a promise.
|
|
27
|
+
*
|
|
28
|
+
* 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.
|
|
29
|
+
*/
|
|
30
|
+
func: (previousResponse?: TResponse) => Promise<TResponse>;
|
|
31
|
+
/**
|
|
32
|
+
* The validator function. It receive the resolved return of the API call.
|
|
33
|
+
*/
|
|
34
|
+
validate: (response: TResponse) => boolean;
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=createIterablePromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createIterablePromise.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/createIterablePromise.ts"],"names":[],"mappings":"AAAA,oBAAY,eAAe,CAAC,SAAS,IAAI,OAAO,CAAC;IAC/C;;OAEG;IACH,UAAU,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,IAAI,CAAC;IAE1C;;OAEG;IACH,KAAK,EAAE;QACL;;WAEG;QACH,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,OAAO,CAAC;QAE3C;;WAEG;QACH,OAAO,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,MAAM,CAAC;KAC1C,CAAC;IAEF;;OAEG;IACH,OAAO,EAAE,MAAM,MAAM,CAAC;CACvB,CAAC,CAAC;AAEH,oBAAY,qBAAqB,CAAC,SAAS,IAAI,eAAe,CAAC,SAAS,CAAC,GAAG;IAC1E;;;;OAIG;IACH,IAAI,EAAE,CAAC,gBAAgB,CAAC,EAAE,SAAS,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC;IAE3D;;OAEG;IACH,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,OAAO,CAAC;CAC5C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/host.ts"],"names":[],"mappings":"AAAA,oBAAY,IAAI,GAAG;IACjB;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,MAAM,EAAE,MAAM,GAAG,WAAW,GAAG,OAAO,CAAC;IAEvC;;OAEG;IACH,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC;CAC5B,CAAC;AAEF,oBAAY,YAAY,GAAG,IAAI,GAAG;IAChC;;OAEG;IACH,MAAM,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAAC;IAEpC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,IAAI,EAAE,MAAM,OAAO,CAAC;IAEpB;;OAEG;IACH,UAAU,EAAE,MAAM,OAAO,CAAC;CAC3B,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
1
|
+
export * from './cache';
|
|
2
|
+
export * from './createClient';
|
|
3
|
+
export * from './createIterablePromise';
|
|
4
|
+
export * from './host';
|
|
5
|
+
export * from './requester';
|
|
6
|
+
export * from './transporter';
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC;AACxC,cAAc,QAAQ,CAAC;AACvB,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Headers, QueryParameters } from './
|
|
1
|
+
import type { Headers, QueryParameters } from './transporter';
|
|
2
2
|
/**
|
|
3
3
|
* The method of the request.
|
|
4
4
|
*/
|
|
@@ -63,4 +63,4 @@ export declare type EchoResponse = Omit<EndRequest, 'data'> & Pick<Request, 'dat
|
|
|
63
63
|
algoliaAgent: string;
|
|
64
64
|
searchParams?: Record<string, string>;
|
|
65
65
|
};
|
|
66
|
-
//# sourceMappingURL=
|
|
66
|
+
//# sourceMappingURL=requester.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"requester.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/requester.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAE9D;;GAEG;AACH,oBAAY,MAAM,GAAG,QAAQ,GAAG,KAAK,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC;AAEjE,oBAAY,OAAO,GAAG;IACpB,MAAM,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,eAAe,EAAE,eAAe,CAAC;IACjC,IAAI,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACxD,OAAO,EAAE,OAAO,CAAC;IACjB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAEF,oBAAY,UAAU,GAAG,IAAI,CAAC,OAAO,EAAE,SAAS,GAAG,QAAQ,CAAC,GAAG;IAC7D;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,eAAe,EAAE,MAAM,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,oBAAY,QAAQ,GAAG;IACrB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,UAAU,EAAE,OAAO,CAAC;IACpB;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,oBAAY,SAAS,GAAG;IACtB;;OAEG;IACH,IAAI,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CAClD,CAAC;AAEF,oBAAY,YAAY,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GACjD,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACvC,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Cache } from './
|
|
2
|
-
import type { Host } from './
|
|
3
|
-
import type { Request, Requester, EndRequest, Response } from './
|
|
1
|
+
import type { Cache } from './cache';
|
|
2
|
+
import type { Host } from './host';
|
|
3
|
+
import type { Request, Requester, EndRequest, Response } from './requester';
|
|
4
4
|
export declare type Headers = Record<string, string>;
|
|
5
5
|
export declare type QueryParameters = Record<string, any>;
|
|
6
6
|
export declare type RequestOptions = Pick<Request, 'cacheable'> & {
|
|
@@ -125,4 +125,4 @@ export declare type Transporter = TransporterOptions & {
|
|
|
125
125
|
*/
|
|
126
126
|
request: <TResponse>(baseRequest: Request, baseRequestOptions?: RequestOptions) => Promise<TResponse>;
|
|
127
127
|
};
|
|
128
|
-
//# sourceMappingURL=
|
|
128
|
+
//# sourceMappingURL=transporter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"transporter.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/transporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5E,oBAAY,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAC7C,oBAAY,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAElD,oBAAY,cAAc,GAAG,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,GAAG;IACxD;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;OAGG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC;;;OAGG;IACH,IAAI,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACzD,CAAC;AAEF,oBAAY,UAAU,GAAG;IACvB,OAAO,EAAE,UAAU,CAAC;IACpB,QAAQ,EAAE,QAAQ,CAAC;IACnB,IAAI,EAAE,IAAI,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,oBAAY,mBAAmB,GAAG;IAChC;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,oBAAY,YAAY,GAAG;IACzB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,GAAG,EAAE,CAAC,OAAO,EAAE,mBAAmB,KAAK,YAAY,CAAC;CACrD,CAAC;AAEF,oBAAY,QAAQ,GAAG;IACrB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,oBAAY,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,UAAU,EAAE,KAAK,CAAC;IAElB;;;;OAIG;IACH,SAAS,EAAE,SAAS,CAAC;IAErB;;;;;OAKG;IACH,aAAa,EAAE,KAAK,CAAC;IAErB;;;;OAIG;IACH,cAAc,EAAE,KAAK,CAAC;IAEtB;;;;OAIG;IACH,QAAQ,EAAE,QAAQ,CAAC;IAEnB;;OAEG;IACH,KAAK,EAAE,IAAI,EAAE,CAAC;IAEd;;;;OAIG;IACH,WAAW,EAAE,OAAO,CAAC;IAErB;;;;OAIG;IACH,mBAAmB,EAAE,eAAe,CAAC;IAErC;;OAEG;IACH,YAAY,EAAE,YAAY,CAAC;CAC5B,CAAC;AAEF,oBAAY,WAAW,GAAG,kBAAkB,GAAG;IAC7C;;;OAGG;IACH,OAAO,EAAE,CAAC,SAAS,EACjB,WAAW,EAAE,OAAO,EACpB,kBAAkB,CAAC,EAAE,cAAc,KAChC,OAAO,CAAC,SAAS,CAAC,CAAC;CACzB,CAAC"}
|
package/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './src/createAuth';
|
|
2
2
|
export * from './src/createEchoRequester';
|
|
3
|
-
export * from './src/
|
|
3
|
+
export * from './src/createIterablePromise';
|
|
4
4
|
export * from './src/cache';
|
|
5
5
|
export * from './src/transporter';
|
|
6
6
|
export * from './src/createAlgoliaAgent';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@algolia/client-common",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.6",
|
|
4
4
|
"description": "Common package for the Algolia JavaScript API client.",
|
|
5
5
|
"repository": "algolia/algoliasearch-client-javascript",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/jest": "28.1.6",
|
|
22
|
-
"@types/node": "16.11.
|
|
22
|
+
"@types/node": "16.11.47",
|
|
23
23
|
"jest": "28.1.3",
|
|
24
24
|
"jest-environment-jsdom": "28.1.3",
|
|
25
25
|
"ts-jest": "28.0.7",
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { createIterablePromise } from '../createIterablePromise';
|
|
2
|
+
|
|
3
|
+
describe('createIterablePromise', () => {
|
|
4
|
+
describe('func', () => {
|
|
5
|
+
it('provides the `previousResponse` parameter', async () => {
|
|
6
|
+
const responses: Array<string | undefined> = [];
|
|
7
|
+
const promise = createIterablePromise<string | undefined>({
|
|
8
|
+
func: (previousResponse) => {
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
resolve(previousResponse === undefined ? 'yes' : 'no');
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
validate: () => responses.length === 3,
|
|
14
|
+
aggregator: (response) => responses.push(response),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
await expect(promise).resolves.toEqual('no');
|
|
18
|
+
expect(responses).toEqual(['yes', 'no', 'no']);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('validate', () => {
|
|
23
|
+
it('iterates on a `func` until `validate` is met', async () => {
|
|
24
|
+
let calls = 0;
|
|
25
|
+
const promise = createIterablePromise({
|
|
26
|
+
func: () => {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
calls += 1;
|
|
29
|
+
resolve(`success #${calls}`);
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
validate: () => calls >= 3,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await expect(promise).resolves.toEqual('success #3');
|
|
36
|
+
expect(calls).toBe(3);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('forward the response of the `func`', async () => {
|
|
40
|
+
let calls = 0;
|
|
41
|
+
const promise = createIterablePromise<number>({
|
|
42
|
+
func: () => {
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
calls += 1;
|
|
45
|
+
resolve(calls);
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
validate: (response) => response >= 3,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
await expect(promise).resolves.toEqual(3);
|
|
52
|
+
expect(calls).toBe(3);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('aggregator', () => {
|
|
57
|
+
it('is called before iterating', async () => {
|
|
58
|
+
let calls = 0;
|
|
59
|
+
let count = 0;
|
|
60
|
+
const promise = createIterablePromise({
|
|
61
|
+
func: () => {
|
|
62
|
+
return new Promise((resolve) => {
|
|
63
|
+
calls += 1;
|
|
64
|
+
resolve(`success #${calls}`);
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
validate: () => calls >= 3,
|
|
68
|
+
aggregator: () => (count += 3),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
await expect(promise).resolves.toEqual('success #3');
|
|
72
|
+
expect(calls).toBe(3);
|
|
73
|
+
expect(count).toBe(3 * 3);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('forward the response of the `func`', async () => {
|
|
77
|
+
let calls = 0;
|
|
78
|
+
const responses: string[] = [];
|
|
79
|
+
const promise = createIterablePromise<string>({
|
|
80
|
+
func: () => {
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
calls += 1;
|
|
83
|
+
resolve(`success #${calls}`);
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
validate: () => calls >= 3,
|
|
87
|
+
aggregator: (response) => {
|
|
88
|
+
responses.push(response);
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
await expect(promise).resolves.toEqual('success #3');
|
|
93
|
+
expect(calls).toBe(3);
|
|
94
|
+
expect(responses).toEqual(['success #1', 'success #2', 'success #3']);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('timeout', () => {
|
|
99
|
+
it('defaults to no timeout (0)', async () => {
|
|
100
|
+
let calls = 0;
|
|
101
|
+
const before = Date.now();
|
|
102
|
+
const promise = createIterablePromise({
|
|
103
|
+
func: () => {
|
|
104
|
+
return new Promise((resolve) => {
|
|
105
|
+
calls += 1;
|
|
106
|
+
resolve(`success #${calls}`);
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
validate: () => calls >= 2,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
await expect(promise).resolves.toEqual('success #2');
|
|
113
|
+
|
|
114
|
+
expect(Date.now() - before).toBeGreaterThanOrEqual(0);
|
|
115
|
+
expect(Date.now() - before).toBeLessThanOrEqual(10);
|
|
116
|
+
expect(calls).toBe(2);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('waits before calling the `func` again', async () => {
|
|
120
|
+
let calls = 0;
|
|
121
|
+
const before = Date.now();
|
|
122
|
+
const promise = createIterablePromise({
|
|
123
|
+
func: () => {
|
|
124
|
+
return new Promise((resolve) => {
|
|
125
|
+
calls += 1;
|
|
126
|
+
resolve(`success #${calls}`);
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
validate: () => calls >= 2,
|
|
130
|
+
timeout: () => 2000,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
await expect(promise).resolves.toEqual('success #2');
|
|
134
|
+
|
|
135
|
+
expect(Date.now() - before).toBeGreaterThanOrEqual(2000);
|
|
136
|
+
expect(Date.now() - before).toBeLessThanOrEqual(2010);
|
|
137
|
+
expect(calls).toBe(2);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('error', () => {
|
|
142
|
+
it('gets the rejection of the given promise via reject', async () => {
|
|
143
|
+
let calls = 0;
|
|
144
|
+
|
|
145
|
+
const promise = createIterablePromise({
|
|
146
|
+
func: () => {
|
|
147
|
+
return new Promise((resolve, reject) => {
|
|
148
|
+
calls += 1;
|
|
149
|
+
if (calls <= 3) {
|
|
150
|
+
resolve('okay');
|
|
151
|
+
} else {
|
|
152
|
+
reject(new Error('nope'));
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
validate: () => false,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
await expect(promise).rejects.toEqual(
|
|
160
|
+
expect.objectContaining({ message: 'nope' })
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('gets the rejection of the given promise via throw', async () => {
|
|
165
|
+
let calls = 0;
|
|
166
|
+
|
|
167
|
+
const promise = createIterablePromise({
|
|
168
|
+
func: () => {
|
|
169
|
+
return new Promise((resolve) => {
|
|
170
|
+
calls += 1;
|
|
171
|
+
if (calls <= 3) {
|
|
172
|
+
resolve('okay');
|
|
173
|
+
} else {
|
|
174
|
+
throw new Error('nope');
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
validate: () => false,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
await expect(promise).rejects.toEqual(
|
|
182
|
+
expect.objectContaining({ message: 'nope' })
|
|
183
|
+
);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('rejects with the given `message` when `validate` hits', async () => {
|
|
187
|
+
const MAX_RETRIES = 3;
|
|
188
|
+
let calls = 0;
|
|
189
|
+
|
|
190
|
+
const promise = createIterablePromise({
|
|
191
|
+
func: () => {
|
|
192
|
+
return new Promise((resolve) => {
|
|
193
|
+
calls += 1;
|
|
194
|
+
resolve('okay');
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
validate: () => false,
|
|
198
|
+
error: {
|
|
199
|
+
validate: () => calls >= MAX_RETRIES,
|
|
200
|
+
message: () => `Error is thrown: ${calls}/${MAX_RETRIES}`,
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
await expect(promise).rejects.toEqual(
|
|
205
|
+
expect.objectContaining({
|
|
206
|
+
message: 'Error is thrown: 3/3',
|
|
207
|
+
})
|
|
208
|
+
);
|
|
209
|
+
expect(calls).toBe(MAX_RETRIES);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('forward the response of the `func`', async () => {
|
|
213
|
+
const MAX_RETRIES = 3;
|
|
214
|
+
let calls = 0;
|
|
215
|
+
|
|
216
|
+
const promise = createIterablePromise<number>({
|
|
217
|
+
func: () => {
|
|
218
|
+
return new Promise((resolve) => {
|
|
219
|
+
calls += 1;
|
|
220
|
+
resolve(calls);
|
|
221
|
+
});
|
|
222
|
+
},
|
|
223
|
+
validate: () => false,
|
|
224
|
+
error: {
|
|
225
|
+
validate: (response) => response >= MAX_RETRIES,
|
|
226
|
+
message: (response) => `Error is thrown: ${response}/${MAX_RETRIES}`,
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
await expect(promise).rejects.toEqual(
|
|
231
|
+
expect.objectContaining({
|
|
232
|
+
message: 'Error is thrown: 3/3',
|
|
233
|
+
})
|
|
234
|
+
);
|
|
235
|
+
expect(calls).toBe(MAX_RETRIES);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { CreateIterablePromise } from './types/createIterablePromise';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Helper: Returns the promise of a given `func` to iterate on, based on a given `validate` condition.
|
|
5
|
+
*
|
|
6
|
+
* @param createIterator - The createIterator options.
|
|
7
|
+
* @param createIterator.func - The function to run, which returns a promise.
|
|
8
|
+
* @param createIterator.validate - The validator function. It receives the resolved return of `func`.
|
|
9
|
+
* @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`.
|
|
10
|
+
* @param createIterator.error - The `validate` condition to throw an error, and its message.
|
|
11
|
+
* @param createIterator.timeout - The function to decide how long to wait between iterations.
|
|
12
|
+
*/
|
|
13
|
+
export function createIterablePromise<TResponse>({
|
|
14
|
+
func,
|
|
15
|
+
validate,
|
|
16
|
+
aggregator,
|
|
17
|
+
error,
|
|
18
|
+
timeout = (): number => 0,
|
|
19
|
+
}: CreateIterablePromise<TResponse>): Promise<TResponse> {
|
|
20
|
+
const retry = (previousResponse?: TResponse): Promise<TResponse> => {
|
|
21
|
+
return new Promise<TResponse>((resolve, reject) => {
|
|
22
|
+
func(previousResponse)
|
|
23
|
+
.then((response) => {
|
|
24
|
+
if (aggregator) {
|
|
25
|
+
aggregator(response);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (validate(response)) {
|
|
29
|
+
return resolve(response);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (error && error.validate(response)) {
|
|
33
|
+
return reject(new Error(error.message(response)));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return setTimeout(() => {
|
|
37
|
+
retry(response).then(resolve).catch(reject);
|
|
38
|
+
}, timeout());
|
|
39
|
+
})
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
reject(err);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
return retry();
|
|
47
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
};
|
|
File without changes
|
package/src/types/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
1
|
+
export * from './cache';
|
|
2
|
+
export * from './createClient';
|
|
3
|
+
export * from './createIterablePromise';
|
|
4
|
+
export * from './host';
|
|
5
|
+
export * from './requester';
|
|
6
|
+
export * from './transporter';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Cache } from './
|
|
2
|
-
import type { Host } from './
|
|
3
|
-
import type { Request, Requester, EndRequest, Response } from './
|
|
1
|
+
import type { Cache } from './cache';
|
|
2
|
+
import type { Host } from './host';
|
|
3
|
+
import type { Request, Requester, EndRequest, Response } from './requester';
|
|
4
4
|
|
|
5
5
|
export type Headers = Record<string, string>;
|
|
6
6
|
export type QueryParameters = Record<string, any>;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { CreateRetryablePromiseOptions } from './types/CreateRetryablePromise';
|
|
2
|
-
export declare const DEFAULT_MAX_RETRIES = 50;
|
|
3
|
-
export declare const DEFAULT_TIMEOUT: (retryCount: number) => number;
|
|
4
|
-
/**
|
|
5
|
-
* Return a promise that retry a task until it meets the condition.
|
|
6
|
-
*
|
|
7
|
-
* @param createRetryablePromiseOptions - The createRetryablePromise options.
|
|
8
|
-
* @param createRetryablePromiseOptions.func - The function to run, which returns a promise.
|
|
9
|
-
* @param createRetryablePromiseOptions.validate - The validator function. It receives the resolved return of `func`.
|
|
10
|
-
* @param createRetryablePromiseOptions.maxRetries - The maximum number of retries. 50 by default.
|
|
11
|
-
* @param createRetryablePromiseOptions.timeout - The function to decide how long to wait between retries.
|
|
12
|
-
*/
|
|
13
|
-
export declare function createRetryablePromise<TResponse>({ func, validate, maxRetries, timeout, }: CreateRetryablePromiseOptions<TResponse>): Promise<TResponse>;
|
|
14
|
-
//# sourceMappingURL=createRetryablePromise.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createRetryablePromise.d.ts","sourceRoot":"","sources":["../../../../packages/client-common/src/createRetryablePromise.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAEpF,eAAO,MAAM,mBAAmB,KAAK,CAAC;AACtC,eAAO,MAAM,eAAe,eAAgB,MAAM,KAAG,MACnB,CAAC;AAEnC;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,EAChD,IAAI,EACJ,QAAQ,EACR,UAAgC,EAChC,OAAyB,GAC1B,EAAE,6BAA6B,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CA+B/D"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export declare type CreateRetryablePromiseOptions<TResponse> = {
|
|
2
|
-
/**
|
|
3
|
-
* The function to run, which returns a promise.
|
|
4
|
-
*/
|
|
5
|
-
func: () => Promise<TResponse>;
|
|
6
|
-
/**
|
|
7
|
-
* The validator function. It receives the resolved return of `func`.
|
|
8
|
-
*/
|
|
9
|
-
validate: (response: TResponse) => boolean;
|
|
10
|
-
/**
|
|
11
|
-
* The maximum number of retry. 50 by default.
|
|
12
|
-
*/
|
|
13
|
-
maxRetries?: number;
|
|
14
|
-
/**
|
|
15
|
-
* The function to decide how long to wait between retries.
|
|
16
|
-
*/
|
|
17
|
-
timeout?: (retryCount: number) => number;
|
|
18
|
-
};
|
|
19
|
-
//# sourceMappingURL=CreateRetryablePromise.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CreateRetryablePromise.d.ts","sourceRoot":"","sources":["../../../../../packages/client-common/src/types/CreateRetryablePromise.ts"],"names":[],"mappings":"AAAA,oBAAY,6BAA6B,CAAC,SAAS,IAAI;IACrD;;OAEG;IACH,IAAI,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,CAAC;IAE/B;;OAEG;IACH,QAAQ,EAAE,CAAC,QAAQ,EAAE,SAAS,KAAK,OAAO,CAAC;IAE3C;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,OAAO,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,MAAM,CAAC;CAC1C,CAAC"}
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { createRetryablePromise } from '../createRetryablePromise';
|
|
2
|
-
|
|
3
|
-
describe('createRetryablePromise', () => {
|
|
4
|
-
it('resolves promise after some retries', async () => {
|
|
5
|
-
let calls = 0;
|
|
6
|
-
const promise = createRetryablePromise({
|
|
7
|
-
func: () => {
|
|
8
|
-
return new Promise((resolve) => {
|
|
9
|
-
calls += 1;
|
|
10
|
-
resolve(`success #${calls}`);
|
|
11
|
-
});
|
|
12
|
-
},
|
|
13
|
-
validate: () => calls >= 3,
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
await expect(promise).resolves.toEqual('success #3');
|
|
17
|
-
expect(calls).toBe(3);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('gets the rejection of the given promise via reject', async () => {
|
|
21
|
-
let calls = 0;
|
|
22
|
-
|
|
23
|
-
const promise = createRetryablePromise({
|
|
24
|
-
func: () => {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
calls += 1;
|
|
27
|
-
if (calls <= 3) {
|
|
28
|
-
resolve('okay');
|
|
29
|
-
} else {
|
|
30
|
-
reject(new Error('nope'));
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
},
|
|
34
|
-
validate: () => false,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
await expect(promise).rejects.toEqual(
|
|
38
|
-
expect.objectContaining({ message: 'nope' })
|
|
39
|
-
);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('gets the rejection of the given promise via throw', async () => {
|
|
43
|
-
let calls = 0;
|
|
44
|
-
|
|
45
|
-
const promise = createRetryablePromise({
|
|
46
|
-
func: () => {
|
|
47
|
-
return new Promise((resolve) => {
|
|
48
|
-
calls += 1;
|
|
49
|
-
if (calls <= 3) {
|
|
50
|
-
resolve('okay');
|
|
51
|
-
} else {
|
|
52
|
-
throw new Error('nope');
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
},
|
|
56
|
-
validate: () => false,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
await expect(promise).rejects.toEqual(
|
|
60
|
-
expect.objectContaining({ message: 'nope' })
|
|
61
|
-
);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('gets the rejection when it exceeds the max retries number', async () => {
|
|
65
|
-
const MAX_RETRIES = 3;
|
|
66
|
-
let calls = 0;
|
|
67
|
-
|
|
68
|
-
const promise = createRetryablePromise({
|
|
69
|
-
func: () => {
|
|
70
|
-
return new Promise((resolve) => {
|
|
71
|
-
calls += 1;
|
|
72
|
-
resolve('okay');
|
|
73
|
-
});
|
|
74
|
-
},
|
|
75
|
-
validate: () => false,
|
|
76
|
-
maxRetries: MAX_RETRIES,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
await expect(promise).rejects.toEqual(
|
|
80
|
-
expect.objectContaining({
|
|
81
|
-
message: 'The maximum number of retries exceeded. (3/3)',
|
|
82
|
-
})
|
|
83
|
-
);
|
|
84
|
-
expect(calls).toBe(MAX_RETRIES);
|
|
85
|
-
});
|
|
86
|
-
});
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import type { CreateRetryablePromiseOptions } from './types/CreateRetryablePromise';
|
|
2
|
-
|
|
3
|
-
export const DEFAULT_MAX_RETRIES = 50;
|
|
4
|
-
export const DEFAULT_TIMEOUT = (retryCount: number): number =>
|
|
5
|
-
Math.min(retryCount * 200, 5000);
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Return a promise that retry a task until it meets the condition.
|
|
9
|
-
*
|
|
10
|
-
* @param createRetryablePromiseOptions - The createRetryablePromise options.
|
|
11
|
-
* @param createRetryablePromiseOptions.func - The function to run, which returns a promise.
|
|
12
|
-
* @param createRetryablePromiseOptions.validate - The validator function. It receives the resolved return of `func`.
|
|
13
|
-
* @param createRetryablePromiseOptions.maxRetries - The maximum number of retries. 50 by default.
|
|
14
|
-
* @param createRetryablePromiseOptions.timeout - The function to decide how long to wait between retries.
|
|
15
|
-
*/
|
|
16
|
-
export function createRetryablePromise<TResponse>({
|
|
17
|
-
func,
|
|
18
|
-
validate,
|
|
19
|
-
maxRetries = DEFAULT_MAX_RETRIES,
|
|
20
|
-
timeout = DEFAULT_TIMEOUT,
|
|
21
|
-
}: CreateRetryablePromiseOptions<TResponse>): Promise<TResponse> {
|
|
22
|
-
let retryCount = 0;
|
|
23
|
-
const retry = (): Promise<TResponse> => {
|
|
24
|
-
return new Promise<TResponse>((resolve, reject) => {
|
|
25
|
-
func()
|
|
26
|
-
.then((response) => {
|
|
27
|
-
const isValid = validate(response);
|
|
28
|
-
if (isValid) {
|
|
29
|
-
resolve(response);
|
|
30
|
-
} else if (retryCount + 1 >= maxRetries) {
|
|
31
|
-
reject(
|
|
32
|
-
new Error(
|
|
33
|
-
`The maximum number of retries exceeded. (${
|
|
34
|
-
retryCount + 1
|
|
35
|
-
}/${maxRetries})`
|
|
36
|
-
)
|
|
37
|
-
);
|
|
38
|
-
} else {
|
|
39
|
-
retryCount += 1;
|
|
40
|
-
setTimeout(() => {
|
|
41
|
-
retry().then(resolve).catch(reject);
|
|
42
|
-
}, timeout(retryCount));
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
.catch((error) => {
|
|
46
|
-
reject(error);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
return retry();
|
|
52
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export type CreateRetryablePromiseOptions<TResponse> = {
|
|
2
|
-
/**
|
|
3
|
-
* The function to run, which returns a promise.
|
|
4
|
-
*/
|
|
5
|
-
func: () => Promise<TResponse>;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The validator function. It receives the resolved return of `func`.
|
|
9
|
-
*/
|
|
10
|
-
validate: (response: TResponse) => boolean;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* The maximum number of retry. 50 by default.
|
|
14
|
-
*/
|
|
15
|
-
maxRetries?: number;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The function to decide how long to wait between retries.
|
|
19
|
-
*/
|
|
20
|
-
timeout?: (retryCount: number) => number;
|
|
21
|
-
};
|