@dereekb/util 10.1.29 → 10.2.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/fetch/LICENSE +21 -0
- package/fetch/index.cjs.d.ts +1 -0
- package/fetch/index.cjs.js +5480 -0
- package/fetch/index.esm.js +5448 -0
- package/fetch/package.json +15 -3
- package/fetch/src/lib/error.d.ts +7 -0
- package/fetch/src/lib/fetch.d.ts +5 -5
- package/fetch/src/lib/fetch.page.d.ts +116 -0
- package/fetch/src/lib/index.d.ts +1 -0
- package/fetch/src/lib/json.d.ts +17 -1
- package/index.cjs.js +1440 -1319
- package/index.esm.js +1577 -1431
- package/package.json +10 -5
- package/src/lib/array/array.unique.d.ts +5 -0
- package/src/lib/date/date.d.ts +14 -0
- package/src/lib/model/model.d.ts +1 -0
- package/src/lib/number/round.d.ts +2 -1
- package/src/lib/object/object.equal.d.ts +2 -0
- package/src/lib/page/page.calculator.d.ts +6 -1
- package/src/lib/page/page.d.ts +0 -2
- package/src/lib/string/char.d.ts +4 -0
- package/src/lib/string/replace.d.ts +29 -1
- package/test/CHANGELOG.md +13 -0
- package/test/package.json +1 -1
- package/test/src/lib/jest.fail.d.ts +22 -3
- package/test/src/lib/jest.fail.js +29 -2
- package/test/src/lib/jest.fail.js.map +1 -1
- package/fetch/CHANGELOG.md +0 -927
- package/fetch/src/index.js +0 -5
- package/fetch/src/index.js.map +0 -1
- package/fetch/src/lib/error.js +0 -31
- package/fetch/src/lib/error.js.map +0 -1
- package/fetch/src/lib/fetch.js +0 -177
- package/fetch/src/lib/fetch.js.map +0 -1
- package/fetch/src/lib/fetch.type.js +0 -3
- package/fetch/src/lib/fetch.type.js.map +0 -1
- package/fetch/src/lib/index.js +0 -11
- package/fetch/src/lib/index.js.map +0 -1
- package/fetch/src/lib/json.js +0 -80
- package/fetch/src/lib/json.js.map +0 -1
- package/fetch/src/lib/provider.js +0 -9
- package/fetch/src/lib/provider.js.map +0 -1
- package/fetch/src/lib/timeout.js +0 -38
- package/fetch/src/lib/timeout.js.map +0 -1
- package/fetch/src/lib/url.js +0 -102
- package/fetch/src/lib/url.js.map +0 -1
package/fetch/package.json
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dereekb/util/fetch",
|
|
3
|
-
"version": "10.
|
|
4
|
-
"
|
|
3
|
+
"version": "10.2.0",
|
|
4
|
+
".": {
|
|
5
|
+
"types": "./src/index.d.ts",
|
|
6
|
+
"node": {
|
|
7
|
+
"require": "./index.cjs.js",
|
|
8
|
+
"import": "./index.mjs"
|
|
9
|
+
},
|
|
10
|
+
"browser": {
|
|
11
|
+
"require": "./index.cjs.js",
|
|
12
|
+
"import": "./index.esm.js"
|
|
13
|
+
},
|
|
14
|
+
"default": "./index.cjs.js"
|
|
15
|
+
},
|
|
5
16
|
"peerDependencies": {
|
|
6
17
|
"@dereekb/util": "*"
|
|
7
18
|
},
|
|
8
|
-
"
|
|
19
|
+
"module": "./index.esm.js",
|
|
20
|
+
"main": "./index.cjs.js"
|
|
9
21
|
}
|
package/fetch/src/lib/error.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { BaseError } from 'make-error';
|
|
2
|
+
/**
|
|
3
|
+
* Thrown by a FetchRequestFactory if one of the async request initialization steps fails.
|
|
4
|
+
*/
|
|
5
|
+
export declare class FetchRequestFactoryError extends BaseError {
|
|
6
|
+
readonly error: Error | string | unknown;
|
|
7
|
+
constructor(error: Error | string | unknown);
|
|
8
|
+
}
|
|
2
9
|
/**
|
|
3
10
|
* Wraps the input fetch function to always pass the fetch response promise to requireOkResponse().
|
|
4
11
|
*
|
package/fetch/src/lib/fetch.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Factory, type MapFunction, type Maybe, type WebsiteUrl } from '@dereekb/util';
|
|
1
|
+
import { type Factory, type MapFunction, type Maybe, type WebsiteUrl, type PromiseOrValue, type GetterOrValue } from '@dereekb/util';
|
|
2
2
|
import { type ConfiguredFetchWithTimeout, type RequestInitWithTimeout } from './fetch.type';
|
|
3
3
|
/**
|
|
4
4
|
* Interface used for creating fetch related resource factories.
|
|
@@ -64,11 +64,11 @@ export interface FetchRequestFactoryInput {
|
|
|
64
64
|
/**
|
|
65
65
|
* Base request info to add to each value.
|
|
66
66
|
*/
|
|
67
|
-
baseRequest?: RequestInit
|
|
67
|
+
baseRequest?: GetterOrValue<PromiseOrValue<RequestInit>>;
|
|
68
68
|
/**
|
|
69
69
|
* Default timeout to add to requestInit values.
|
|
70
70
|
*
|
|
71
|
-
* NOTE: This timeout is not used by this fetchRequest directly, but
|
|
71
|
+
* NOTE: This timeout is not used by this fetchRequest directly, but is added to the baseRequest.
|
|
72
72
|
*/
|
|
73
73
|
timeout?: number;
|
|
74
74
|
/**
|
|
@@ -78,8 +78,8 @@ export interface FetchRequestFactoryInput {
|
|
|
78
78
|
*/
|
|
79
79
|
requestInitFactory?: FetchRequestInitFactory;
|
|
80
80
|
}
|
|
81
|
-
export type FetchRequestInitFactory = (currRequest: Request
|
|
82
|
-
export type FetchRequestFactory = (input: RequestInfo | URL, init?: RequestInit | undefined) => Request
|
|
81
|
+
export type FetchRequestInitFactory = (currRequest: PromiseOrValue<Request>, init?: PromiseOrValue<RequestInit>) => PromiseOrValue<RequestInitWithTimeout | undefined>;
|
|
82
|
+
export type FetchRequestFactory = (input: RequestInfo | URL, init?: RequestInit | undefined) => PromiseOrValue<Request>;
|
|
83
83
|
export type AbortControllerFactory = Factory<AbortController>;
|
|
84
84
|
export declare function fetchRequestFactory(config: FetchRequestFactoryInput): FetchRequestFactory;
|
|
85
85
|
export declare function mergeRequestInits<T extends RequestInit>(base: T, requestInit?: T | undefined): T;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { type CachedGetter, type Maybe, type Page, type PromiseOrValue } from '@dereekb/util';
|
|
2
|
+
import { FetchRequestFactoryError } from './error';
|
|
3
|
+
export declare class FetchPageNoNextPageError extends FetchRequestFactoryError {
|
|
4
|
+
readonly page: FetchPage<unknown, unknown>;
|
|
5
|
+
constructor(page: FetchPage<unknown, unknown>);
|
|
6
|
+
}
|
|
7
|
+
export declare class FetchPageLimitReachedError extends FetchRequestFactoryError {
|
|
8
|
+
readonly page: FetchPage<unknown, unknown>;
|
|
9
|
+
readonly limit: number;
|
|
10
|
+
constructor(page: FetchPage<unknown, unknown>, limit: number);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Query/Search cursor string.
|
|
14
|
+
*
|
|
15
|
+
* Used by some APIs for pagination.
|
|
16
|
+
*/
|
|
17
|
+
export type FetchPageCursor = string;
|
|
18
|
+
export interface FetchPage<I, O> {
|
|
19
|
+
/**
|
|
20
|
+
* Input for this page.
|
|
21
|
+
*/
|
|
22
|
+
readonly input: I;
|
|
23
|
+
/**
|
|
24
|
+
* Fetches the next page of results.
|
|
25
|
+
*
|
|
26
|
+
* If the page of results has been returned, returns the same reference and does not perform the fetch request again.
|
|
27
|
+
*
|
|
28
|
+
* If there are no pages to return, calling this will throw a FetchPageNoNextPageError error.
|
|
29
|
+
*/
|
|
30
|
+
readonly fetchNext: CachedGetter<Promise<FetchNextPage<I, O>>>;
|
|
31
|
+
}
|
|
32
|
+
export interface FetchPageResultInfo extends Page {
|
|
33
|
+
/**
|
|
34
|
+
* Cursor for this page, if applicable.
|
|
35
|
+
*/
|
|
36
|
+
readonly cursor?: Maybe<FetchPageCursor>;
|
|
37
|
+
/**
|
|
38
|
+
* Whether or not there are more results to fetch.
|
|
39
|
+
*
|
|
40
|
+
* If not defined, is assumed this info is not available and will default to true.
|
|
41
|
+
*
|
|
42
|
+
* Defaults to true.
|
|
43
|
+
*/
|
|
44
|
+
readonly hasNext?: Maybe<boolean>;
|
|
45
|
+
}
|
|
46
|
+
export interface FetchPageResults<O> extends FetchPageResultInfo {
|
|
47
|
+
/**
|
|
48
|
+
* Result for this page.
|
|
49
|
+
*/
|
|
50
|
+
readonly result: O;
|
|
51
|
+
}
|
|
52
|
+
export interface FetchNextPage<I, O> extends FetchPageResults<O>, FetchPage<I, O> {
|
|
53
|
+
/**
|
|
54
|
+
* Previous page.
|
|
55
|
+
*
|
|
56
|
+
* Undefined if this is the first page.
|
|
57
|
+
*/
|
|
58
|
+
readonly previous?: Maybe<FetchNextPage<I, O>>;
|
|
59
|
+
/**
|
|
60
|
+
* Whether or not the max page has been reached.
|
|
61
|
+
*/
|
|
62
|
+
readonly isAtMaxPage?: boolean;
|
|
63
|
+
}
|
|
64
|
+
export interface FetchPageFactoryConfig<I, O> {
|
|
65
|
+
/**
|
|
66
|
+
* The configured Fetch function that takes in the input and returns a results.
|
|
67
|
+
*/
|
|
68
|
+
readonly fetch: (input: I) => Promise<O>;
|
|
69
|
+
/**
|
|
70
|
+
* Returns the page results from the result.
|
|
71
|
+
*
|
|
72
|
+
* The page number is ignored as it is inferred from the previous page.
|
|
73
|
+
*
|
|
74
|
+
* @param result
|
|
75
|
+
* @returns
|
|
76
|
+
*/
|
|
77
|
+
readonly readFetchPageResultInfo: (result: O) => PromiseOrValue<Omit<FetchPageResultInfo, 'page'>>;
|
|
78
|
+
/**
|
|
79
|
+
* Creates new input for the next page that is merged with the previous input.
|
|
80
|
+
*
|
|
81
|
+
* Returns undefined if the next page should not be loaded.
|
|
82
|
+
*
|
|
83
|
+
* @param result
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
readonly buildInputForNextPage: (pageResult: Partial<FetchPageResults<O>>, input: I) => PromiseOrValue<Maybe<Partial<I>>>;
|
|
87
|
+
/**
|
|
88
|
+
* The default max page to load up to.
|
|
89
|
+
*
|
|
90
|
+
* Defaults to 100. Pass null to disable the max page amount.
|
|
91
|
+
*/
|
|
92
|
+
readonly defaultMaxPage?: Maybe<number>;
|
|
93
|
+
}
|
|
94
|
+
export interface FetchPageFactoryOptions<I, O> {
|
|
95
|
+
/**
|
|
96
|
+
* The max number of pages to load.
|
|
97
|
+
*
|
|
98
|
+
* Pass null to disable the max page amount.
|
|
99
|
+
*/
|
|
100
|
+
readonly maxPage?: Maybe<number>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Creates a new FetchPage instance.
|
|
104
|
+
*/
|
|
105
|
+
export type FetchPageFactory<I, O> = (input: I, options?: FetchPageFactoryOptions<I, O>) => FetchPage<I, O>;
|
|
106
|
+
/**
|
|
107
|
+
* Default max page for a FetchPageFactory.
|
|
108
|
+
*/
|
|
109
|
+
export declare const FETCH_PAGE_FACTORY_DEFAULT_MAX_PAGE = 100;
|
|
110
|
+
/**
|
|
111
|
+
* Creates a new FetchPageFactory from the input.
|
|
112
|
+
*
|
|
113
|
+
* @param config
|
|
114
|
+
* @returns
|
|
115
|
+
*/
|
|
116
|
+
export declare function fetchPageFactory<I, O>(config: FetchPageFactoryConfig<I, O>): FetchPageFactory<I, O>;
|
package/fetch/src/lib/index.d.ts
CHANGED
package/fetch/src/lib/json.d.ts
CHANGED
|
@@ -16,6 +16,12 @@ export declare function fetchJsonBodyString(body: FetchJsonBody | undefined): st
|
|
|
16
16
|
export interface FetchJsonInput extends Omit<RequestInit, 'body'> {
|
|
17
17
|
method: FetchMethod;
|
|
18
18
|
body?: FetchJsonBody | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Optional intercept function to intercept/transform the response.
|
|
21
|
+
*
|
|
22
|
+
* Does not override any other configured interceptor and occurs after those configured interceptors.
|
|
23
|
+
*/
|
|
24
|
+
interceptResponse?: FetchJsonInterceptJsonResponseFunction;
|
|
19
25
|
}
|
|
20
26
|
export type FetchJsonInputMapFunction = MapSameFunction<FetchJsonInput>;
|
|
21
27
|
export type FetchJsonGetFunction = <R>(url: FetchURLInput) => Promise<R>;
|
|
@@ -25,11 +31,21 @@ export type FetchJsonWithInputFunction = <R>(url: FetchURLInput, input: FetchJso
|
|
|
25
31
|
* Used to fetch from the input url and retrieve a JSON response.
|
|
26
32
|
*/
|
|
27
33
|
export type FetchJsonFunction = FetchJsonGetFunction & FetchJsonMethodAndBodyFunction & FetchJsonWithInputFunction;
|
|
34
|
+
export type FetchJsonInterceptJsonResponseFunction = (json: any, response: Response) => any;
|
|
28
35
|
export type HandleFetchJsonParseErrorFunction = (response: Response) => string | null | never;
|
|
29
36
|
export declare const throwJsonResponseParseErrorFunction: HandleFetchJsonParseErrorFunction;
|
|
30
37
|
export declare const returnNullHandleFetchJsonParseErrorFunction: HandleFetchJsonParseErrorFunction;
|
|
31
38
|
export interface FetchJsonFunctionConfig extends FetchJsonRequestInitFunctionConfig {
|
|
32
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Optional intercept function to transform all response JSON before returning the result.
|
|
41
|
+
*
|
|
42
|
+
* Useful for cases where an API returns errors with a 200 response.
|
|
43
|
+
*/
|
|
44
|
+
readonly interceptJsonResponse?: FetchJsonInterceptJsonResponseFunction;
|
|
45
|
+
/**
|
|
46
|
+
* Optional function to handle JSON parsing errors.
|
|
47
|
+
*/
|
|
48
|
+
readonly handleFetchJsonParseErrorFunction?: HandleFetchJsonParseErrorFunction;
|
|
33
49
|
}
|
|
34
50
|
/**
|
|
35
51
|
* Creates a FetchJsonFunction from the input ConfiguredFetch.
|