@vpmedia/simplify 1.5.3 → 1.6.0
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/README.md +1 -1
- package/package.json +1 -1
- package/src/const/http_status.js +64 -0
- package/src/index.js +2 -1
- package/src/util/fetchRetry.js +27 -4
- package/types/const/http_status.d.ts +65 -0
- package/types/const/http_status.d.ts.map +1 -0
- package/types/index.d.ts +2 -1
- package/types/util/fetchRetry.d.ts +10 -0
- package/types/util/fetchRetry.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @vpmedia/simplify
|
|
2
2
|
|
|
3
|
-
[](https://badge.fury.io/js/@vpmedia%2Fsimplify)
|
|
4
4
|
[](https://github.com/vpmedia/simplify/actions/workflows/ci.yml)
|
|
5
5
|
|
|
6
6
|
@vpmedia/simplify TBD
|
package/package.json
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export const HTTP_100_CONTINUE = 100;
|
|
2
|
+
export const HTTP_101_SWITCHING_PROTOCOLS = 101;
|
|
3
|
+
export const HTTP_102_PROCESSING = 102;
|
|
4
|
+
export const HTTP_103_EARLY_HINTS = 103;
|
|
5
|
+
export const HTTP_200_OK = 200;
|
|
6
|
+
export const HTTP_201_CREATED = 201;
|
|
7
|
+
export const HTTP_202_ACCEPTED = 202;
|
|
8
|
+
export const HTTP_203_NON_AUTHORITATIVE_INFORMATION = 203;
|
|
9
|
+
export const HTTP_204_NO_CONTENT = 204;
|
|
10
|
+
export const HTTP_205_RESET_CONTENT = 205;
|
|
11
|
+
export const HTTP_206_PARTIAL_CONTENT = 206;
|
|
12
|
+
export const HTTP_207_MULTI_STATUS = 207;
|
|
13
|
+
export const HTTP_208_ALREADY_REPORTED = 208;
|
|
14
|
+
export const HTTP_226_IM_USED = 226;
|
|
15
|
+
export const HTTP_300_MULTIPLE_CHOICES = 300;
|
|
16
|
+
export const HTTP_301_MOVED_PERMANENTLY = 301;
|
|
17
|
+
export const HTTP_302_FOUND = 302;
|
|
18
|
+
export const HTTP_303_SEE_OTHER = 303;
|
|
19
|
+
export const HTTP_304_NOT_MODIFIED = 304;
|
|
20
|
+
export const HTTP_305_USE_PROXY = 305;
|
|
21
|
+
export const HTTP_306_RESERVED = 306;
|
|
22
|
+
export const HTTP_307_TEMPORARY_REDIRECT = 307;
|
|
23
|
+
export const HTTP_308_PERMANENT_REDIRECT = 308;
|
|
24
|
+
export const HTTP_400_BAD_REQUEST = 400;
|
|
25
|
+
export const HTTP_401_UNAUTHORIZED = 401;
|
|
26
|
+
export const HTTP_402_PAYMENT_REQUIRED = 402;
|
|
27
|
+
export const HTTP_403_FORBIDDEN = 403;
|
|
28
|
+
export const HTTP_404_NOT_FOUND = 404;
|
|
29
|
+
export const HTTP_405_METHOD_NOT_ALLOWED = 405;
|
|
30
|
+
export const HTTP_406_NOT_ACCEPTABLE = 406;
|
|
31
|
+
export const HTTP_407_PROXY_AUTHENTICATION_REQUIRED = 407;
|
|
32
|
+
export const HTTP_408_REQUEST_TIMEOUT = 408;
|
|
33
|
+
export const HTTP_409_CONFLICT = 409;
|
|
34
|
+
export const HTTP_410_GONE = 410;
|
|
35
|
+
export const HTTP_411_LENGTH_REQUIRED = 411;
|
|
36
|
+
export const HTTP_412_PRECONDITION_FAILED = 412;
|
|
37
|
+
export const HTTP_413_REQUEST_ENTITY_TOO_LARGE = 413;
|
|
38
|
+
export const HTTP_414_REQUEST_URI_TOO_LONG = 414;
|
|
39
|
+
export const HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415;
|
|
40
|
+
export const HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
|
|
41
|
+
export const HTTP_417_EXPECTATION_FAILED = 417;
|
|
42
|
+
export const HTTP_418_IM_A_TEAPOT = 418;
|
|
43
|
+
export const HTTP_421_MISDIRECTED_REQUEST = 421;
|
|
44
|
+
export const HTTP_422_UNPROCESSABLE_ENTITY = 422;
|
|
45
|
+
export const HTTP_423_LOCKED = 423;
|
|
46
|
+
export const HTTP_424_FAILED_DEPENDENCY = 424;
|
|
47
|
+
export const HTTP_425_TOO_EARLY = 425;
|
|
48
|
+
export const HTTP_426_UPGRADE_REQUIRED = 426;
|
|
49
|
+
export const HTTP_428_PRECONDITION_REQUIRED = 428;
|
|
50
|
+
export const HTTP_429_TOO_MANY_REQUESTS = 429;
|
|
51
|
+
export const HTTP_499_CLIENT_CLOSED_CONNECTION = 499;
|
|
52
|
+
export const HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
|
|
53
|
+
export const HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
|
|
54
|
+
export const HTTP_500_INTERNAL_SERVER_ERROR = 500;
|
|
55
|
+
export const HTTP_501_NOT_IMPLEMENTED = 501;
|
|
56
|
+
export const HTTP_502_BAD_GATEWAY = 502;
|
|
57
|
+
export const HTTP_503_SERVICE_UNAVAILABLE = 503;
|
|
58
|
+
export const HTTP_504_GATEWAY_TIMEOUT = 504;
|
|
59
|
+
export const HTTP_505_HTTP_VERSION_NOT_SUPPORTED = 505;
|
|
60
|
+
export const HTTP_506_VARIANT_ALSO_NEGOTIATES = 506;
|
|
61
|
+
export const HTTP_507_INSUFFICIENT_STORAGE = 507;
|
|
62
|
+
export const HTTP_508_LOOP_DETECTED = 508;
|
|
63
|
+
export const HTTP_510_NOT_EXTENDED = 510;
|
|
64
|
+
export const HTTP_511_NETWORK_AUTHENTICATION_REQUIRED = 511;
|
package/src/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// core
|
|
2
|
+
export * from './const/http_status.js';
|
|
2
3
|
export { Logger } from './logging/Logger.js';
|
|
3
4
|
export { addLeadingZero } from './util/addLeadingZero.js';
|
|
4
5
|
export { capitalize } from './util/capitalize.js';
|
|
5
6
|
export { delayPromise } from './util/delayPromise.js';
|
|
6
|
-
export { fetchRetry } from './util/fetchRetry.js';
|
|
7
|
+
export { FetchError, fetchRetry } from './util/fetchRetry.js';
|
|
7
8
|
export { getObjValueByPath } from './util/getObjValueByPath.js';
|
|
8
9
|
export { getRandomInt } from './util/getRandomInt.js';
|
|
9
10
|
export { getURLParam } from './util/getURLParam.js';
|
package/src/util/fetchRetry.js
CHANGED
|
@@ -1,33 +1,56 @@
|
|
|
1
|
+
import { HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN, HTTP_405_METHOD_NOT_ALLOWED } from '../const/http_status.js';
|
|
1
2
|
import { Logger } from '../logging/Logger.js';
|
|
2
3
|
import { delayPromise } from './delayPromise.js';
|
|
3
4
|
|
|
4
5
|
const logger = new Logger('fetch');
|
|
5
6
|
|
|
7
|
+
export class FetchError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new FetchError instance.
|
|
10
|
+
* @param {string} message - Error message.
|
|
11
|
+
* @param {Response} response - Fetch response object.
|
|
12
|
+
*/
|
|
13
|
+
constructor(message, response) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.response = response;
|
|
16
|
+
// Error.captureStackTrace(this, FetchError);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
6
20
|
/**
|
|
7
21
|
* Fetch with retry.
|
|
8
22
|
* @param {string | URL | Request} resource - Fetch URL.
|
|
9
23
|
* @param {RequestInit} [fetchOptions] - Fetch options.
|
|
10
|
-
* @param {{delay?: number, numTries?: number}} [retryOptions] - Retry options.
|
|
24
|
+
* @param {{delay?: number, numTries?: number, statusExcludes?: number[]}} [retryOptions] - Retry options.
|
|
11
25
|
* @returns {Promise<Response>} Fetch result.
|
|
12
26
|
*/
|
|
13
27
|
export const fetchRetry = async (resource, fetchOptions, retryOptions) => {
|
|
14
28
|
retryOptions = retryOptions ?? {};
|
|
15
29
|
retryOptions.delay = Math.max(retryOptions.delay ?? 500, 1);
|
|
16
30
|
retryOptions.numTries = Math.max(retryOptions.numTries ?? 3, 1);
|
|
31
|
+
retryOptions.statusExcludes = retryOptions.statusExcludes ?? [
|
|
32
|
+
HTTP_401_UNAUTHORIZED,
|
|
33
|
+
HTTP_403_FORBIDDEN,
|
|
34
|
+
HTTP_405_METHOD_NOT_ALLOWED,
|
|
35
|
+
];
|
|
17
36
|
while (retryOptions.numTries > 0) {
|
|
18
37
|
logger.info('request', { resource, fetchOptions, retryOptions });
|
|
19
38
|
try {
|
|
20
39
|
const response = await fetch(resource, fetchOptions);
|
|
21
40
|
if (!response.ok) {
|
|
22
41
|
logger.warn('failure', response);
|
|
23
|
-
throw new
|
|
42
|
+
throw new FetchError(`fetch ${response.url} returned status ${response.status}`, response);
|
|
24
43
|
}
|
|
25
44
|
logger.info('success', response);
|
|
26
45
|
return response;
|
|
27
46
|
} catch (error) {
|
|
28
|
-
|
|
47
|
+
const typedError = error instanceof Error ? error : new Error(String(error));
|
|
48
|
+
logger.error('error', typedError);
|
|
29
49
|
retryOptions.numTries -= 1;
|
|
30
|
-
if (
|
|
50
|
+
if (
|
|
51
|
+
retryOptions.numTries === 0 ||
|
|
52
|
+
(typedError instanceof FetchError && retryOptions.statusExcludes.includes(typedError.response.status))
|
|
53
|
+
) {
|
|
31
54
|
throw error;
|
|
32
55
|
}
|
|
33
56
|
await delayPromise(retryOptions.delay);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export const HTTP_100_CONTINUE: 100;
|
|
2
|
+
export const HTTP_101_SWITCHING_PROTOCOLS: 101;
|
|
3
|
+
export const HTTP_102_PROCESSING: 102;
|
|
4
|
+
export const HTTP_103_EARLY_HINTS: 103;
|
|
5
|
+
export const HTTP_200_OK: 200;
|
|
6
|
+
export const HTTP_201_CREATED: 201;
|
|
7
|
+
export const HTTP_202_ACCEPTED: 202;
|
|
8
|
+
export const HTTP_203_NON_AUTHORITATIVE_INFORMATION: 203;
|
|
9
|
+
export const HTTP_204_NO_CONTENT: 204;
|
|
10
|
+
export const HTTP_205_RESET_CONTENT: 205;
|
|
11
|
+
export const HTTP_206_PARTIAL_CONTENT: 206;
|
|
12
|
+
export const HTTP_207_MULTI_STATUS: 207;
|
|
13
|
+
export const HTTP_208_ALREADY_REPORTED: 208;
|
|
14
|
+
export const HTTP_226_IM_USED: 226;
|
|
15
|
+
export const HTTP_300_MULTIPLE_CHOICES: 300;
|
|
16
|
+
export const HTTP_301_MOVED_PERMANENTLY: 301;
|
|
17
|
+
export const HTTP_302_FOUND: 302;
|
|
18
|
+
export const HTTP_303_SEE_OTHER: 303;
|
|
19
|
+
export const HTTP_304_NOT_MODIFIED: 304;
|
|
20
|
+
export const HTTP_305_USE_PROXY: 305;
|
|
21
|
+
export const HTTP_306_RESERVED: 306;
|
|
22
|
+
export const HTTP_307_TEMPORARY_REDIRECT: 307;
|
|
23
|
+
export const HTTP_308_PERMANENT_REDIRECT: 308;
|
|
24
|
+
export const HTTP_400_BAD_REQUEST: 400;
|
|
25
|
+
export const HTTP_401_UNAUTHORIZED: 401;
|
|
26
|
+
export const HTTP_402_PAYMENT_REQUIRED: 402;
|
|
27
|
+
export const HTTP_403_FORBIDDEN: 403;
|
|
28
|
+
export const HTTP_404_NOT_FOUND: 404;
|
|
29
|
+
export const HTTP_405_METHOD_NOT_ALLOWED: 405;
|
|
30
|
+
export const HTTP_406_NOT_ACCEPTABLE: 406;
|
|
31
|
+
export const HTTP_407_PROXY_AUTHENTICATION_REQUIRED: 407;
|
|
32
|
+
export const HTTP_408_REQUEST_TIMEOUT: 408;
|
|
33
|
+
export const HTTP_409_CONFLICT: 409;
|
|
34
|
+
export const HTTP_410_GONE: 410;
|
|
35
|
+
export const HTTP_411_LENGTH_REQUIRED: 411;
|
|
36
|
+
export const HTTP_412_PRECONDITION_FAILED: 412;
|
|
37
|
+
export const HTTP_413_REQUEST_ENTITY_TOO_LARGE: 413;
|
|
38
|
+
export const HTTP_414_REQUEST_URI_TOO_LONG: 414;
|
|
39
|
+
export const HTTP_415_UNSUPPORTED_MEDIA_TYPE: 415;
|
|
40
|
+
export const HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE: 416;
|
|
41
|
+
export const HTTP_417_EXPECTATION_FAILED: 417;
|
|
42
|
+
export const HTTP_418_IM_A_TEAPOT: 418;
|
|
43
|
+
export const HTTP_421_MISDIRECTED_REQUEST: 421;
|
|
44
|
+
export const HTTP_422_UNPROCESSABLE_ENTITY: 422;
|
|
45
|
+
export const HTTP_423_LOCKED: 423;
|
|
46
|
+
export const HTTP_424_FAILED_DEPENDENCY: 424;
|
|
47
|
+
export const HTTP_425_TOO_EARLY: 425;
|
|
48
|
+
export const HTTP_426_UPGRADE_REQUIRED: 426;
|
|
49
|
+
export const HTTP_428_PRECONDITION_REQUIRED: 428;
|
|
50
|
+
export const HTTP_429_TOO_MANY_REQUESTS: 429;
|
|
51
|
+
export const HTTP_499_CLIENT_CLOSED_CONNECTION: 499;
|
|
52
|
+
export const HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE: 431;
|
|
53
|
+
export const HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS: 451;
|
|
54
|
+
export const HTTP_500_INTERNAL_SERVER_ERROR: 500;
|
|
55
|
+
export const HTTP_501_NOT_IMPLEMENTED: 501;
|
|
56
|
+
export const HTTP_502_BAD_GATEWAY: 502;
|
|
57
|
+
export const HTTP_503_SERVICE_UNAVAILABLE: 503;
|
|
58
|
+
export const HTTP_504_GATEWAY_TIMEOUT: 504;
|
|
59
|
+
export const HTTP_505_HTTP_VERSION_NOT_SUPPORTED: 505;
|
|
60
|
+
export const HTTP_506_VARIANT_ALSO_NEGOTIATES: 506;
|
|
61
|
+
export const HTTP_507_INSUFFICIENT_STORAGE: 507;
|
|
62
|
+
export const HTTP_508_LOOP_DETECTED: 508;
|
|
63
|
+
export const HTTP_510_NOT_EXTENDED: 510;
|
|
64
|
+
export const HTTP_511_NETWORK_AUTHENTICATION_REQUIRED: 511;
|
|
65
|
+
//# sourceMappingURL=http_status.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http_status.d.ts","sourceRoot":"","sources":["../../src/const/http_status.js"],"names":[],"mappings":"AAAA,oCAAqC;AACrC,+CAAgD;AAChD,sCAAuC;AACvC,uCAAwC;AACxC,8BAA+B;AAC/B,mCAAoC;AACpC,oCAAqC;AACrC,yDAA0D;AAC1D,sCAAuC;AACvC,yCAA0C;AAC1C,2CAA4C;AAC5C,wCAAyC;AACzC,4CAA6C;AAC7C,mCAAoC;AACpC,4CAA6C;AAC7C,6CAA8C;AAC9C,iCAAkC;AAClC,qCAAsC;AACtC,wCAAyC;AACzC,qCAAsC;AACtC,oCAAqC;AACrC,8CAA+C;AAC/C,8CAA+C;AAC/C,uCAAwC;AACxC,wCAAyC;AACzC,4CAA6C;AAC7C,qCAAsC;AACtC,qCAAsC;AACtC,8CAA+C;AAC/C,0CAA2C;AAC3C,yDAA0D;AAC1D,2CAA4C;AAC5C,oCAAqC;AACrC,gCAAiC;AACjC,2CAA4C;AAC5C,+CAAgD;AAChD,oDAAqD;AACrD,gDAAiD;AACjD,kDAAmD;AACnD,2DAA4D;AAC5D,8CAA+C;AAC/C,uCAAwC;AACxC,+CAAgD;AAChD,gDAAiD;AACjD,kCAAmC;AACnC,6CAA8C;AAC9C,qCAAsC;AACtC,4CAA6C;AAC7C,iDAAkD;AAClD,6CAA8C;AAC9C,oDAAqD;AACrD,2DAA4D;AAC5D,yDAA0D;AAC1D,iDAAkD;AAClD,2CAA4C;AAC5C,uCAAwC;AACxC,+CAAgD;AAChD,2CAA4C;AAC5C,sDAAuD;AACvD,mDAAoD;AACpD,gDAAiD;AACjD,yCAA0C;AAC1C,wCAAyC;AACzC,2DAA4D"}
|
package/types/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
export * from "./const/http_status.js";
|
|
1
2
|
export { Logger } from "./logging/Logger.js";
|
|
2
3
|
export { addLeadingZero } from "./util/addLeadingZero.js";
|
|
3
4
|
export { capitalize } from "./util/capitalize.js";
|
|
4
5
|
export { delayPromise } from "./util/delayPromise.js";
|
|
5
|
-
export { fetchRetry } from "./util/fetchRetry.js";
|
|
6
6
|
export { getObjValueByPath } from "./util/getObjValueByPath.js";
|
|
7
7
|
export { getRandomInt } from "./util/getRandomInt.js";
|
|
8
8
|
export { getURLParam } from "./util/getURLParam.js";
|
|
9
9
|
export { sanitizeURLParam } from "./util/sanitizeURLParam.js";
|
|
10
10
|
export { setObjValueByPath } from "./util/setObjValueByPath.js";
|
|
11
11
|
export { underscoreToCamelCase } from "./util/underscoreToCamelCase.js";
|
|
12
|
+
export { FetchError, fetchRetry } from "./util/fetchRetry.js";
|
|
12
13
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,5 +1,15 @@
|
|
|
1
|
+
export class FetchError extends Error {
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new FetchError instance.
|
|
4
|
+
* @param {string} message - Error message.
|
|
5
|
+
* @param {Response} response - Fetch response object.
|
|
6
|
+
*/
|
|
7
|
+
constructor(message: string, response: Response);
|
|
8
|
+
response: Response;
|
|
9
|
+
}
|
|
1
10
|
export function fetchRetry(resource: string | URL | Request, fetchOptions?: RequestInit, retryOptions?: {
|
|
2
11
|
delay?: number;
|
|
3
12
|
numTries?: number;
|
|
13
|
+
statusExludes?: number[];
|
|
4
14
|
}): Promise<Response>;
|
|
5
15
|
//# sourceMappingURL=fetchRetry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchRetry.d.ts","sourceRoot":"","sources":["../../src/util/fetchRetry.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetchRetry.d.ts","sourceRoot":"","sources":["../../src/util/fetchRetry.js"],"names":[],"mappings":"AAMA;IACE;;;;OAIG;IACH,qBAHW,MAAM,YACN,QAAQ,EAMlB;IAFC,mBAAwB;CAG3B;AASM,qCALI,MAAM,GAAG,GAAG,GAAG,OAAO,iBACtB,WAAW,iBACX;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;CAAC,GAC3D,OAAO,CAAC,QAAQ,CAAC,CA+B7B"}
|