@dereekb/util 9.12.0 → 9.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/fetch/CHANGELOG.md +4 -0
- package/fetch/package.json +2 -2
- package/fetch/src/lib/error.d.ts +13 -0
- package/fetch/src/lib/error.js +31 -0
- package/fetch/src/lib/error.js.map +1 -0
- package/fetch/src/lib/fetch.d.ts +32 -6
- package/fetch/src/lib/fetch.js +30 -12
- package/fetch/src/lib/fetch.js.map +1 -1
- package/fetch/src/lib/fetch.type.d.ts +13 -0
- package/fetch/src/lib/fetch.type.js +3 -0
- package/fetch/src/lib/fetch.type.js.map +1 -0
- package/fetch/src/lib/index.d.ts +2 -0
- package/fetch/src/lib/index.js +2 -0
- package/fetch/src/lib/index.js.map +1 -1
- package/fetch/src/lib/json.d.ts +24 -0
- package/fetch/src/lib/json.js +40 -0
- package/fetch/src/lib/json.js.map +1 -0
- package/fetch/src/lib/timeout.d.ts +7 -0
- package/fetch/src/lib/timeout.js +37 -0
- package/fetch/src/lib/timeout.js.map +1 -0
- package/package.json +1 -1
- package/src/lib/error/error.js.map +1 -1
- package/test/CHANGELOG.md +4 -0
- package/test/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
## [9.12.1](https://github.com/dereekb/dbx-components/compare/v9.12.0-dev...v9.12.1) (2022-11-07)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
5
9
|
# [9.12.0](https://github.com/dereekb/dbx-components/compare/v9.11.13-dev...v9.12.0) (2022-11-07)
|
|
6
10
|
|
|
7
11
|
|
package/fetch/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
## [9.12.1](https://github.com/dereekb/dbx-components/compare/v9.12.0-dev...v9.12.1) (2022-11-07)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
5
9
|
# [9.12.0](https://github.com/dereekb/dbx-components/compare/v9.11.13-dev...v9.12.0) (2022-11-07)
|
|
6
10
|
|
|
7
11
|
|
package/fetch/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dereekb/util/fetch",
|
|
3
|
-
"version": "9.12.
|
|
3
|
+
"version": "9.12.1",
|
|
4
4
|
"main": "./src/index.js",
|
|
5
5
|
"typings": "./src/index.d.ts",
|
|
6
6
|
"dependencies": {},
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"@dereekb/util": "9.12.
|
|
8
|
+
"@dereekb/util": "9.12.1",
|
|
9
9
|
"lodash.isequal": "^4.5.0",
|
|
10
10
|
"make-error": "^1.3.0",
|
|
11
11
|
"class-validator": "^0.13.2",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseError } from 'make-error';
|
|
2
|
+
/**
|
|
3
|
+
* Wraps the input fetch function to always pass the fetch response promise to requireOkResponse().
|
|
4
|
+
*
|
|
5
|
+
* @param inputFetch
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export declare function fetchOk(inputFetch: typeof fetch): typeof fetch;
|
|
9
|
+
export declare class FetchResponseError extends BaseError {
|
|
10
|
+
readonly response: Response;
|
|
11
|
+
constructor(response: Response);
|
|
12
|
+
}
|
|
13
|
+
export declare function requireOkResponse(responsePromise: Promise<Response>): Promise<Response>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireOkResponse = exports.FetchResponseError = exports.fetchOk = void 0;
|
|
4
|
+
const make_error_1 = require("make-error");
|
|
5
|
+
/**
|
|
6
|
+
* Wraps the input fetch function to always pass the fetch response promise to requireOkResponse().
|
|
7
|
+
*
|
|
8
|
+
* @param inputFetch
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
function fetchOk(inputFetch) {
|
|
12
|
+
return (input, init) => requireOkResponse(inputFetch(input, init));
|
|
13
|
+
}
|
|
14
|
+
exports.fetchOk = fetchOk;
|
|
15
|
+
class FetchResponseError extends make_error_1.BaseError {
|
|
16
|
+
constructor(response) {
|
|
17
|
+
super(`Fetch response was a non-ok status code (${response.status}): ${response.statusText}`);
|
|
18
|
+
this.response = response;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.FetchResponseError = FetchResponseError;
|
|
22
|
+
function requireOkResponse(responsePromise) {
|
|
23
|
+
return responsePromise.then((response) => {
|
|
24
|
+
if (!response.ok) {
|
|
25
|
+
throw new FetchResponseError(response);
|
|
26
|
+
}
|
|
27
|
+
return response;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
exports.requireOkResponse = requireOkResponse;
|
|
31
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../../../../../packages/util/fetch/src/lib/error.ts"],"names":[],"mappings":";;;AAAA,2CAAuC;AAEvC;;;;;GAKG;AACH,SAAgB,OAAO,CAAC,UAAwB;IAC9C,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,iBAAiB,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;AACrE,CAAC;AAFD,0BAEC;AAED,MAAa,kBAAmB,SAAQ,sBAAS;IAC/C,YAAqB,QAAkB;QACrC,KAAK,CAAC,4CAA4C,QAAQ,CAAC,MAAM,MAAM,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;QAD3E,aAAQ,GAAR,QAAQ,CAAU;IAEvC,CAAC;CACF;AAJD,gDAIC;AAED,SAAgB,iBAAiB,CAAC,eAAkC;IAClE,OAAO,eAAe,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;QACvC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,MAAM,IAAI,kBAAkB,CAAC,QAAQ,CAAC,CAAC;SACxC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC;AARD,8CAQC"}
|
package/fetch/src/lib/fetch.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { WebsiteUrl } from '@dereekb/util';
|
|
1
|
+
import { Factory, MapFunction, WebsiteUrl } from '@dereekb/util';
|
|
2
|
+
import { ConfiguredFetchWithTimeout, RequestInitWithTimeout } from './fetch.type';
|
|
2
3
|
/**
|
|
3
4
|
* Interface used for creating fetch related resource factories.
|
|
4
5
|
*/
|
|
5
6
|
export interface FetchService {
|
|
6
|
-
makeFetch:
|
|
7
|
+
makeFetch: (config?: ConfigureFetchInput) => ConfiguredFetchWithTimeout;
|
|
7
8
|
makeRequest: FetchRequestFactory;
|
|
8
9
|
fetchRequestFactory: typeof fetchRequestFactory;
|
|
9
10
|
}
|
|
10
11
|
/**
|
|
11
12
|
* fetchService() configuration.
|
|
12
13
|
*/
|
|
13
|
-
export declare type FetchServiceConfig = Required<Pick<ConfigureFetchInput, 'makeFetch' | 'makeRequest'
|
|
14
|
+
export declare type FetchServiceConfig = Required<Pick<ConfigureFetchInput, 'makeFetch' | 'makeRequest'>> & Pick<ConfigureFetchInput, 'baseRequest'>;
|
|
14
15
|
/**
|
|
15
16
|
* Used to create a FetchService.
|
|
16
17
|
*
|
|
@@ -18,17 +19,35 @@ export declare type FetchServiceConfig = Required<Pick<ConfigureFetchInput, 'mak
|
|
|
18
19
|
* @returns
|
|
19
20
|
*/
|
|
20
21
|
export declare function fetchService(config: FetchServiceConfig): FetchService;
|
|
22
|
+
export declare type MapFetchResponseFunction = MapFunction<Promise<Response>, Promise<Response>>;
|
|
21
23
|
export interface ConfigureFetchInput extends FetchRequestFactoryInput {
|
|
22
24
|
makeFetch?: typeof fetch;
|
|
25
|
+
/**
|
|
26
|
+
* Whether or not to add timeout handling using timeoutFetch().
|
|
27
|
+
*
|
|
28
|
+
* Default: false
|
|
29
|
+
*/
|
|
30
|
+
useTimeout?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Whether or not to map the fetch response using requireOkResponse().
|
|
33
|
+
*
|
|
34
|
+
* Default: false
|
|
35
|
+
*/
|
|
36
|
+
requireOkResponse?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* (Optional) MapFetchResponseFunction
|
|
39
|
+
*
|
|
40
|
+
* If requireOkResponse is true, this mapping occurs afterwards.
|
|
41
|
+
*/
|
|
42
|
+
mapResponse?: MapFetchResponseFunction;
|
|
23
43
|
}
|
|
24
|
-
export declare type ConfiguredFetch = typeof fetch;
|
|
25
44
|
/**
|
|
26
45
|
* Creates a function that wraps fetch and uses a FetchRequestFactory to generate a Request before invoking Fetch.
|
|
27
46
|
*
|
|
28
47
|
* @param config
|
|
29
48
|
* @returns
|
|
30
49
|
*/
|
|
31
|
-
export declare function configureFetch(config: ConfigureFetchInput):
|
|
50
|
+
export declare function configureFetch(config: ConfigureFetchInput): ConfiguredFetchWithTimeout;
|
|
32
51
|
export interface FetchRequestFactoryInput {
|
|
33
52
|
/**
|
|
34
53
|
* Request factory to use. If not defined, will default to window/global.
|
|
@@ -46,6 +65,12 @@ export interface FetchRequestFactoryInput {
|
|
|
46
65
|
* Base request info to add to each value.
|
|
47
66
|
*/
|
|
48
67
|
baseRequest?: RequestInit;
|
|
68
|
+
/**
|
|
69
|
+
* Default timeout to add to requestInit values.
|
|
70
|
+
*
|
|
71
|
+
* NOTE: This timeout is not used by this fetchRequest directly, but instead
|
|
72
|
+
*/
|
|
73
|
+
timeout?: number;
|
|
49
74
|
/**
|
|
50
75
|
* Maps the input RequestInit value to another.
|
|
51
76
|
*
|
|
@@ -53,7 +78,8 @@ export interface FetchRequestFactoryInput {
|
|
|
53
78
|
*/
|
|
54
79
|
requestInitFactory?: FetchRequestInitFactory;
|
|
55
80
|
}
|
|
56
|
-
export declare type FetchRequestInitFactory = (currRequest: Request, init?: RequestInit) =>
|
|
81
|
+
export declare type FetchRequestInitFactory = (currRequest: Request, init?: RequestInit) => RequestInitWithTimeout | undefined;
|
|
57
82
|
export declare type FetchRequestFactory = (input: RequestInfo | URL, init?: RequestInit | undefined) => Request;
|
|
83
|
+
export declare type AbortControllerFactory = Factory<AbortController>;
|
|
58
84
|
export declare function fetchRequestFactory(config: FetchRequestFactoryInput): FetchRequestFactory;
|
|
59
85
|
export declare function isFetchRequest(input: unknown): input is Request;
|
package/fetch/src/lib/fetch.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isFetchRequest = exports.fetchRequestFactory = exports.configureFetch = exports.fetchService = void 0;
|
|
4
4
|
const util_1 = require("@dereekb/util");
|
|
5
|
+
const error_1 = require("./error");
|
|
6
|
+
const timeout_1 = require("./timeout");
|
|
5
7
|
/**
|
|
6
8
|
* Used to create a FetchService.
|
|
7
9
|
*
|
|
@@ -9,10 +11,10 @@ const util_1 = require("@dereekb/util");
|
|
|
9
11
|
* @returns
|
|
10
12
|
*/
|
|
11
13
|
function fetchService(config) {
|
|
12
|
-
const { makeFetch: inputMakeFetch, makeRequest } = config;
|
|
14
|
+
const { makeFetch: inputMakeFetch, makeRequest, baseRequest } = config;
|
|
13
15
|
const factory = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
fetchRequestFactory: (config) => fetchRequestFactory(Object.assign({ makeRequest, baseRequest }, config)),
|
|
17
|
+
makeFetch: (config = {}) => configureFetch(Object.assign({ makeRequest, makeFetch: inputMakeFetch, baseRequest }, config)),
|
|
16
18
|
makeRequest: config.makeRequest
|
|
17
19
|
};
|
|
18
20
|
return factory;
|
|
@@ -25,16 +27,30 @@ exports.fetchService = fetchService;
|
|
|
25
27
|
* @returns
|
|
26
28
|
*/
|
|
27
29
|
function configureFetch(config) {
|
|
28
|
-
const { makeFetch = fetch } = config;
|
|
30
|
+
const { makeFetch: inputMakeFetch = fetch, useTimeout, requireOkResponse: inputRequireOkResponse, mapResponse } = config;
|
|
31
|
+
let makeFetch = inputMakeFetch;
|
|
32
|
+
if (useTimeout) {
|
|
33
|
+
// add fetchTimeout
|
|
34
|
+
makeFetch = (0, timeout_1.fetchTimeout)(makeFetch);
|
|
35
|
+
}
|
|
36
|
+
if (inputRequireOkResponse) {
|
|
37
|
+
// Add fetchOk
|
|
38
|
+
makeFetch = (0, error_1.fetchOk)(makeFetch);
|
|
39
|
+
}
|
|
29
40
|
const makeFetchRequest = fetchRequestFactory(config);
|
|
30
41
|
return (input, init) => {
|
|
31
|
-
|
|
42
|
+
let response = makeFetch(makeFetchRequest(input, init));
|
|
43
|
+
if (mapResponse) {
|
|
44
|
+
response = mapResponse(response);
|
|
45
|
+
}
|
|
46
|
+
return response;
|
|
32
47
|
};
|
|
33
48
|
}
|
|
34
49
|
exports.configureFetch = configureFetch;
|
|
35
50
|
function fetchRequestFactory(config) {
|
|
36
|
-
const { makeRequest
|
|
51
|
+
const { makeRequest = (input, init) => new Request(input, init), baseUrl: inputBaseUrl, baseRequest: inputBaseRequest, timeout, requestInitFactory, useBaseUrlForConfiguredFetchRequests = false } = config;
|
|
37
52
|
const baseUrl = inputBaseUrl ? new URL((0, util_1.removeTrailingFileTypeSeparators)(inputBaseUrl)) : undefined;
|
|
53
|
+
const baseRequest = timeout ? Object.assign(Object.assign({}, inputBaseRequest), { timeout }) : inputBaseRequest;
|
|
38
54
|
const buildUrl = baseUrl
|
|
39
55
|
? (url) => {
|
|
40
56
|
return new URL(url, baseUrl);
|
|
@@ -45,7 +61,7 @@ function fetchRequestFactory(config) {
|
|
|
45
61
|
return input;
|
|
46
62
|
}
|
|
47
63
|
else {
|
|
48
|
-
return
|
|
64
|
+
return makeRequest(input);
|
|
49
65
|
}
|
|
50
66
|
}
|
|
51
67
|
const buildRequestWithFixedUrl = buildUrl
|
|
@@ -66,21 +82,21 @@ function fetchRequestFactory(config) {
|
|
|
66
82
|
}
|
|
67
83
|
}
|
|
68
84
|
else {
|
|
69
|
-
request =
|
|
85
|
+
request = makeRequest(input);
|
|
70
86
|
}
|
|
71
87
|
if (!request) {
|
|
72
88
|
const url = buildUrl(relativeUrl);
|
|
73
|
-
request =
|
|
89
|
+
request = makeRequest(url.href, baseRequest);
|
|
74
90
|
}
|
|
75
91
|
return request;
|
|
76
92
|
}
|
|
77
93
|
: asFetchRequest;
|
|
78
94
|
let buildRequestInit;
|
|
79
95
|
if (baseRequest && requestInitFactory) {
|
|
80
|
-
buildRequestInit = (req, x) => requestInitFactory(req, Object.assign(Object.assign({}, baseRequest), x));
|
|
96
|
+
buildRequestInit = (req, x) => requestInitFactory(req, Object.assign(Object.assign(Object.assign({}, baseRequest), { timeout: req.timeout }), x));
|
|
81
97
|
}
|
|
82
98
|
else if (baseRequest) {
|
|
83
|
-
buildRequestInit = (
|
|
99
|
+
buildRequestInit = (req, x) => (Object.assign(Object.assign(Object.assign({}, baseRequest), { timeout: req.timeout }), x));
|
|
84
100
|
}
|
|
85
101
|
else if (requestInitFactory) {
|
|
86
102
|
buildRequestInit = requestInitFactory;
|
|
@@ -91,7 +107,9 @@ function fetchRequestFactory(config) {
|
|
|
91
107
|
return (input, init) => {
|
|
92
108
|
const fixedRequest = buildRequestWithFixedUrl(input);
|
|
93
109
|
init = buildRequestInit(fixedRequest, init);
|
|
94
|
-
|
|
110
|
+
const request = makeRequest(fixedRequest, init);
|
|
111
|
+
request.timeout = timeout; // copy/set timeout on the request directly
|
|
112
|
+
return request;
|
|
95
113
|
};
|
|
96
114
|
}
|
|
97
115
|
exports.fetchRequestFactory = fetchRequestFactory;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../../../../../packages/util/fetch/src/lib/fetch.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,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AAC,CAAC;QAE5C,MAAM,OAAO,GAAG,WAAW,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;QAC/C,OAA8B,CAAC,OAAO,GAAG,OAAO,CAAC,CAAC,2CAA2C;QAC9F,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;AACJ,CAAC;AAnED,kDAmEC;AAED,SAAgB,cAAc,CAAC,KAAc;IAC3C,OAAO,OAAO,CAAE,KAAiB,CAAC,GAAG,CAAC,CAAC;AACzC,CAAC;AAFD,wCAEC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare type FetchMethod = Request['method'];
|
|
2
|
+
export interface RequestTimeoutRef {
|
|
3
|
+
timeout?: number | null;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* RequestInit with timeout provided.
|
|
7
|
+
*/
|
|
8
|
+
export interface RequestInitWithTimeout extends RequestInit, RequestTimeoutRef {
|
|
9
|
+
}
|
|
10
|
+
export interface RequestWithTimeout extends Request, RequestTimeoutRef {
|
|
11
|
+
}
|
|
12
|
+
export declare type ConfiguredFetch = typeof fetch;
|
|
13
|
+
export declare type ConfiguredFetchWithTimeout = (input: Parameters<typeof fetch>[0], init?: RequestInitWithTimeout | undefined) => ReturnType<typeof fetch>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.type.js","sourceRoot":"","sources":["../../../../../../packages/util/fetch/src/lib/fetch.type.ts"],"names":[],"mappings":""}
|
package/fetch/src/lib/index.d.ts
CHANGED
package/fetch/src/lib/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./error"), exports);
|
|
4
5
|
tslib_1.__exportStar(require("./fetch"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./json"), exports);
|
|
5
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/util/fetch/src/lib/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/util/fetch/src/lib/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB;AACxB,kDAAwB;AACxB,iDAAuB"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { FetchMethod, ConfiguredFetch } from './fetch.type';
|
|
2
|
+
export declare type FetchJsonBody = string | object;
|
|
3
|
+
/**
|
|
4
|
+
* Converts the input to a JSON string, or undefined if not provided.
|
|
5
|
+
*
|
|
6
|
+
* @param body
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare function fetchJsonBodyString(body: FetchJsonBody | undefined): string | undefined;
|
|
10
|
+
export interface FetchJsonInput extends Omit<RequestInit, 'body'> {
|
|
11
|
+
method: FetchMethod;
|
|
12
|
+
body?: FetchJsonBody;
|
|
13
|
+
}
|
|
14
|
+
export declare type FetchJsonMethodAndBodyFunction = <R>(url: URL | string, method: string, body?: FetchJsonBody) => Promise<R>;
|
|
15
|
+
export declare type FetchJsonWithInputFunction = <R>(url: URL | string, input: FetchJsonInput) => Promise<R>;
|
|
16
|
+
/**
|
|
17
|
+
* Used to fetch from the input url and retrieve a JSON response.
|
|
18
|
+
*/
|
|
19
|
+
export declare type FetchJsonFunction = FetchJsonMethodAndBodyFunction & FetchJsonWithInputFunction;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a FetchJsonFunction from the input ConfiguredFetch.
|
|
22
|
+
*/
|
|
23
|
+
export declare function fetchJsonFunction(fetch: ConfiguredFetch): FetchJsonFunction;
|
|
24
|
+
export declare function fetchJsonRequestInit(methodOrInput: string | FetchJsonInput, body?: FetchJsonBody): RequestInit;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchJsonRequestInit = exports.fetchJsonFunction = exports.fetchJsonBodyString = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Converts the input to a JSON string, or undefined if not provided.
|
|
6
|
+
*
|
|
7
|
+
* @param body
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
function fetchJsonBodyString(body) {
|
|
11
|
+
return body != null ? (typeof body === 'string' ? body : JSON.stringify(body)) : undefined;
|
|
12
|
+
}
|
|
13
|
+
exports.fetchJsonBodyString = fetchJsonBodyString;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a FetchJsonFunction from the input ConfiguredFetch.
|
|
16
|
+
*/
|
|
17
|
+
function fetchJsonFunction(fetch) {
|
|
18
|
+
return (url, methodOrInput, body) => {
|
|
19
|
+
const requestInit = fetchJsonRequestInit(methodOrInput, body);
|
|
20
|
+
const response = fetch(url, requestInit);
|
|
21
|
+
return response.then((x) => x.json());
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
exports.fetchJsonFunction = fetchJsonFunction;
|
|
25
|
+
function fetchJsonRequestInit(methodOrInput, body) {
|
|
26
|
+
let config;
|
|
27
|
+
if (typeof methodOrInput === 'string') {
|
|
28
|
+
config = {
|
|
29
|
+
method: methodOrInput,
|
|
30
|
+
body
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
config = methodOrInput;
|
|
35
|
+
}
|
|
36
|
+
const requestInit = Object.assign(Object.assign({}, config), { body: fetchJsonBodyString(config.body) });
|
|
37
|
+
return requestInit;
|
|
38
|
+
}
|
|
39
|
+
exports.fetchJsonRequestInit = fetchJsonRequestInit;
|
|
40
|
+
//# sourceMappingURL=json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../../../../../packages/util/fetch/src/lib/json.ts"],"names":[],"mappings":";;;AAIA;;;;;GAKG;AACH,SAAgB,mBAAmB,CAAC,IAA+B;IACjE,OAAO,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC7F,CAAC;AAFD,kDAEC;AAeD;;GAEG;AACH,SAAgB,iBAAiB,CAAC,KAAsB;IACtD,OAAO,CAAC,GAAiB,EAAE,aAAsC,EAAE,IAAoB,EAAE,EAAE;QACzF,MAAM,WAAW,GAAG,oBAAoB,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;QAC9D,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;QACzC,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;IACxC,CAAC,CAAC;AACJ,CAAC;AAND,8CAMC;AAED,SAAgB,oBAAoB,CAAC,aAAsC,EAAE,IAAoB;IAC/F,IAAI,MAAsB,CAAC;IAE3B,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;QACrC,MAAM,GAAG;YACP,MAAM,EAAE,aAAa;YACrB,IAAI;SACL,CAAC;KACH;SAAM;QACL,MAAM,GAAG,aAAa,CAAC;KACxB;IAED,MAAM,WAAW,mCACZ,MAAM,KACT,IAAI,EAAE,mBAAmB,CAAC,MAAM,CAAC,IAAI,CAAC,GACvC,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC;AAlBD,oDAkBC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseError } from 'make-error';
|
|
2
|
+
export declare class FetchTimeoutError extends BaseError {
|
|
3
|
+
readonly response: Response;
|
|
4
|
+
readonly timeout: number;
|
|
5
|
+
constructor(response: Response, timeout: number);
|
|
6
|
+
}
|
|
7
|
+
export declare function fetchTimeout(inputFetch: typeof fetch): typeof fetch;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchTimeout = exports.FetchTimeoutError = void 0;
|
|
4
|
+
const make_error_1 = require("make-error");
|
|
5
|
+
class FetchTimeoutError extends make_error_1.BaseError {
|
|
6
|
+
constructor(response, timeout) {
|
|
7
|
+
super(`Fetch response was timed out (${timeout})`);
|
|
8
|
+
this.response = response;
|
|
9
|
+
this.timeout = timeout;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.FetchTimeoutError = FetchTimeoutError;
|
|
13
|
+
function fetchTimeout(inputFetch) {
|
|
14
|
+
return (input, init) => {
|
|
15
|
+
var _a;
|
|
16
|
+
let controller;
|
|
17
|
+
let timeout = (_a = init === null || init === void 0 ? void 0 : init.timeout) !== null && _a !== void 0 ? _a : input.timeout;
|
|
18
|
+
// if signal is not provided, and a timeout is specified, configure the timeout
|
|
19
|
+
if (!(init === null || init === void 0 ? void 0 : init.signal) && timeout) {
|
|
20
|
+
controller = new AbortController();
|
|
21
|
+
init = Object.assign(Object.assign({}, init), { signal: controller.signal // pass the abort signal
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
let responsePromise = inputFetch(input, init);
|
|
25
|
+
if (timeout) {
|
|
26
|
+
const timeoutId = setTimeout(() => {
|
|
27
|
+
controller === null || controller === void 0 ? void 0 : controller.abort();
|
|
28
|
+
}, timeout);
|
|
29
|
+
responsePromise = responsePromise.finally(() => {
|
|
30
|
+
clearTimeout(timeoutId);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return responsePromise;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.fetchTimeout = fetchTimeout;
|
|
37
|
+
//# sourceMappingURL=timeout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.js","sourceRoot":"","sources":["../../../../../../packages/util/fetch/src/lib/timeout.ts"],"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packages/util/src/lib/error/error.ts"],"names":[],"mappings":";;;AAAA,2CAAuC;AACvC,+CAAyD;
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../../../../../packages/util/src/lib/error/error.ts"],"names":[],"mappings":";;;AAAA,2CAAuC;AACvC,+CAAyD;AA0BzD,SAAgB,aAAa,CAAC,IAAqB,EAAE,OAAgB;IACnE,OAAO;QACL,IAAI;QACJ,OAAO;KACR,CAAC;AACJ,CAAC;AALD,sCAKC;AAYD;;;;;GAKG;AACH,SAAgB,eAAe,CAAC,UAA6B;IAC3D,IAAI,KAAwC,CAAC;IAE7C,IAAI,UAAU,EAAE;QACd,IAAK,UAAyB,CAAC,IAAI,EAAE;YACnC,KAAK,GAAG,UAA2B,CAAC;SACrC;aAAM,IAAK,UAA2B,CAAC,IAAI,EAAE;YAC5C,KAAK,GAAI,UAA2B,CAAC,IAAqB,CAAC;SAC5D;aAAM,IAAI,UAAU,YAAY,sBAAS,EAAE;YAC1C,KAAK,GAAG;gBACN,IAAI,EAAE,UAAU,CAAC,IAAI;gBACrB,OAAO,EAAE,UAAU,CAAC,OAAO;gBAC3B,MAAM,EAAE,UAAU;aACnB,CAAC;SACH;aAAM;YACL,KAAK,GAAG;gBACN,IAAI,EAAE,OAAO;gBACb,OAAO,EAAG,UAA4B,CAAC,OAAO,IAAI,EAAE;gBACpD,MAAM,EAAE,UAAU;aACnB,CAAC;SACH;KACF;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAxBD,0CAwBC;AAED,SAAgB,0BAA0B,CAAC,KAAiC,EAAE,MAAc;IAC1F,OAAO,KAAK,CAAC,CAAC,CAAC,kCAAkC,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAC3E,CAAC;AAFD,gEAEC;AAID,SAAgB,kCAAkC,CAAC,MAAc;IAC/D,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,IAAA,8BAAoB,EAAC,MAAM,CAAC,CAAC,CAAC;IAEvD,OAAO,CAAC,KAAiC,EAAE,EAAE;QAC3C,MAAM,OAAO,GAAkB,gBAAgB,CAAC,KAAK,CAAC,CAAC;QACvD,OAAO,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAC/C,CAAC,CAAC;AACJ,CAAC;AAPD,gFAOC;AAED,SAAgB,gBAAgB,CAAC,KAAiC;IAChE,OAAO,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAE,KAAuB,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,IAAK,KAAmC,CAAC;AACxH,CAAC;AAFD,4CAEC"}
|
package/test/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
## [9.12.1](https://github.com/dereekb/dbx-components/compare/v9.12.0-dev...v9.12.1) (2022-11-07)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
5
9
|
# [9.12.0](https://github.com/dereekb/dbx-components/compare/v9.11.13-dev...v9.12.0) (2022-11-07)
|
|
6
10
|
|
|
7
11
|
|
package/test/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dereekb/util/test",
|
|
3
|
-
"version": "9.12.
|
|
3
|
+
"version": "9.12.1",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"typings": "./src/index.d.ts",
|
|
7
7
|
"dependencies": {},
|
|
8
8
|
"peerDependencies": {
|
|
9
|
-
"@dereekb/util": "9.12.
|
|
9
|
+
"@dereekb/util": "9.12.1",
|
|
10
10
|
"lodash.isequal": "^4.5.0",
|
|
11
11
|
"make-error": "^1.3.0",
|
|
12
12
|
"class-validator": "^0.13.2",
|