@alwatr/fetch 0.17.0 → 0.18.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/CHANGELOG.md +12 -0
- package/README.md +41 -27
- package/fetch.d.ts +47 -19
- package/fetch.d.ts.map +1 -1
- package/fetch.js +118 -57
- package/fetch.js.map +1 -1
- package/package.json +6 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.18.0](https://github.com/AliMD/alwatr/compare/v0.17.0...v0.18.0) (2022-10-22)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **fetch:** update cache on network_first ([54b59cd](https://github.com/AliMD/alwatr/commit/54b59cdc64a543a0afcd09d2ec78d20661f3a06d))
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
- **fetch:** cache strategy ([106eabd](https://github.com/AliMD/alwatr/commit/106eabdd10574b24f9919049d0b509ae40168a9a))
|
|
15
|
+
- **fetch:** optional cacheStorageName ([f2bd8f2](https://github.com/AliMD/alwatr/commit/f2bd8f2a899466628417294cb269fd569b87e17a))
|
|
16
|
+
- **fetch:** stale_while_revalidate cache strategy ([f758f49](https://github.com/AliMD/alwatr/commit/f758f495efb42ce7441e8f2058c1b67de0615a8c))
|
|
17
|
+
|
|
6
18
|
# [0.17.0](https://github.com/AliMD/alwatr/compare/v0.16.1...v0.17.0) (2022-10-21)
|
|
7
19
|
|
|
8
20
|
### Bug Fixes
|
package/README.md
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
1
|
# @alwatr/fetch
|
|
2
2
|
|
|
3
|
-
Enhanced fetch API with
|
|
4
|
-
|
|
5
|
-
## Options
|
|
6
|
-
|
|
7
|
-
`Options` inherited from the `RequestInit`. you can watch all documents of the parameters RequestInit in [`fetch init parameters`](https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters)
|
|
8
|
-
|
|
9
|
-
Options have some other parameters:
|
|
10
|
-
|
|
11
|
-
- `bodyJson`: a JSON object that converts to string and put on the body.
|
|
12
|
-
- `queryParameters`: a JSON object that converts to URL query params.
|
|
13
|
-
- `timeout`: A timeout for the fetch request.
|
|
14
|
-
- `retry` If fetch response not acceptable or timed out, it will retry the request
|
|
3
|
+
Enhanced fetch API with cache strategy, retry pattern, timeout, helper methods and enhanced types written in tiny TypeScript, ES module.
|
|
15
4
|
|
|
16
5
|
## Example usage
|
|
17
6
|
|
|
@@ -25,35 +14,60 @@ interface ProductInterface {
|
|
|
25
14
|
image: string;
|
|
26
15
|
}
|
|
27
16
|
|
|
28
|
-
const productList = await getJson<Record<string, ProductInterface>>(
|
|
17
|
+
const productList = await getJson<Record<string, ProductInterface>>({
|
|
18
|
+
url: '/api/products',
|
|
29
19
|
queryParameters: {limit: 10},
|
|
30
|
-
timeout:
|
|
31
|
-
retry:
|
|
20
|
+
timeout: 5_000,
|
|
21
|
+
retry: 3,
|
|
22
|
+
cacheStrategy: 'stale_while_revalidate',
|
|
32
23
|
});
|
|
33
24
|
```
|
|
34
25
|
|
|
35
|
-
##
|
|
26
|
+
## Fetch Options
|
|
36
27
|
|
|
37
|
-
|
|
28
|
+
`FetchOptions` inherited from the [fetch standard parameters](https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters) and some other...
|
|
38
29
|
|
|
39
|
-
|
|
30
|
+
- `url`: Request URL.
|
|
31
|
+
- `bodyJson`: Body as JS Object.
|
|
32
|
+
- `queryParameters`: URL Query Parameters as JS Object.
|
|
33
|
+
- `timeout`: A timeout in ms for the fetch request (default `5000`ms).
|
|
34
|
+
- `retry`: If fetch response not acceptable or timed out, it will retry the request (default `3`).
|
|
35
|
+
- `cacheStorageName`: Cache storage name (default `alwatr_fetch_cache`).
|
|
36
|
+
- `cacheStrategy`: Strategies for caching, (default `network_only`).
|
|
37
|
+
- `network_only`: Only network request without any cache.
|
|
38
|
+
- `network_first`: Network first, falling back to cache.
|
|
39
|
+
- `cache_only`: Cache only without any network request.
|
|
40
|
+
- `cache_first`: Cache first, falling back to network.
|
|
41
|
+
- `stale_while_revalidate`: Fastest strategy, Use cached first but always request network to update the cache.
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
await fetch(url, {timeout: 5_000, bodyJson: {a: 1, b: 2}});
|
|
43
|
-
```
|
|
43
|
+
[Read more about standard cache strategies](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#caching-strategies)
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### `fetch(options: FetchOptions): Promise<Response>`
|
|
46
48
|
|
|
47
|
-
It
|
|
49
|
+
It's a wrapper around the browser's `fetch` function that adds retry pattern with timeout and cacheStrategy.
|
|
48
50
|
|
|
49
51
|
```ts
|
|
50
|
-
await
|
|
52
|
+
const response = await fetch({
|
|
53
|
+
url: '/api/products',
|
|
54
|
+
queryParameters: {limit: 10},
|
|
55
|
+
timeout: 5_000,
|
|
56
|
+
retry: 3,
|
|
57
|
+
cacheStrategy: 'stale_while_revalidate',
|
|
58
|
+
});
|
|
51
59
|
```
|
|
52
60
|
|
|
53
|
-
### `
|
|
61
|
+
### `getJson<T>(options: FetchOptions): Promise<T>`
|
|
54
62
|
|
|
55
|
-
It
|
|
63
|
+
It fetches a JSON file from a URL, and returns the parsed data.
|
|
56
64
|
|
|
57
65
|
```ts
|
|
58
|
-
await
|
|
66
|
+
const productList = await getJson<ProductResponse>({
|
|
67
|
+
url: '/api/products',
|
|
68
|
+
queryParameters: {limit: 10},
|
|
69
|
+
timeout: 5_000,
|
|
70
|
+
retry: 3,
|
|
71
|
+
cacheStrategy: 'stale_while_revalidate',
|
|
72
|
+
});
|
|
59
73
|
```
|
package/fetch.d.ts
CHANGED
|
@@ -3,51 +3,79 @@ declare global {
|
|
|
3
3
|
abort(reason?: string): void;
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
|
+
export declare type CacheStrategy = 'network_only' | 'network_first' | 'cache_only' | 'cache_first' | 'stale_while_revalidate';
|
|
6
7
|
export interface FetchOptions extends RequestInit {
|
|
8
|
+
/**
|
|
9
|
+
* Request URL.
|
|
10
|
+
*/
|
|
11
|
+
url: string;
|
|
7
12
|
/**
|
|
8
13
|
* A timeout for the fetch request.
|
|
9
14
|
*
|
|
10
15
|
* @default 5000 ms
|
|
11
16
|
*/
|
|
12
|
-
timeout
|
|
17
|
+
timeout: number;
|
|
13
18
|
/**
|
|
14
19
|
* If fetch response not acceptable or timed out, it will retry the request.
|
|
15
20
|
*
|
|
16
21
|
* @default 3
|
|
17
22
|
*/
|
|
18
|
-
retry
|
|
23
|
+
retry: number;
|
|
24
|
+
/**
|
|
25
|
+
* Strategies for caching.
|
|
26
|
+
*
|
|
27
|
+
* @default 'network_only'
|
|
28
|
+
*/
|
|
29
|
+
cacheStrategy: CacheStrategy;
|
|
30
|
+
/**
|
|
31
|
+
* Cache storage name.
|
|
32
|
+
*
|
|
33
|
+
* @default 'alwatr_fetch_cache'
|
|
34
|
+
*/
|
|
35
|
+
cacheStorageName: string;
|
|
36
|
+
/**
|
|
37
|
+
* Body as JS Object.
|
|
38
|
+
*/
|
|
19
39
|
bodyJson?: Record<string | number, unknown>;
|
|
40
|
+
/**
|
|
41
|
+
* URL Query Parameters as JS Object.
|
|
42
|
+
*/
|
|
20
43
|
queryParameters?: Record<string, string | number | boolean>;
|
|
21
44
|
}
|
|
22
45
|
/**
|
|
23
|
-
* It's a wrapper around the browser's `fetch` function that adds retry pattern with timeout
|
|
24
|
-
*
|
|
25
|
-
* Example:
|
|
26
|
-
*
|
|
27
|
-
* ```ts
|
|
28
|
-
* const response = await fetch(url, {timeout: 5_000, bodyJson: {a: 1, b: 2}});
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
export declare function fetch(url: string, options?: FetchOptions): Promise<Response>;
|
|
32
|
-
/**
|
|
33
|
-
* It fetches a JSON file from a URL, and returns the JSON data
|
|
46
|
+
* It's a wrapper around the browser's `fetch` function that adds retry pattern with timeout and cacheStrategy.
|
|
34
47
|
*
|
|
35
48
|
* Example:
|
|
36
49
|
*
|
|
37
50
|
* ```ts
|
|
38
|
-
* const
|
|
51
|
+
* const response = await fetch({
|
|
52
|
+
* url: '/api/products',
|
|
53
|
+
* queryParameters: {limit: 10},
|
|
54
|
+
* timeout: 5_000,
|
|
55
|
+
* retry: 3,
|
|
56
|
+
* cacheStrategy: 'stale_while_revalidate',
|
|
57
|
+
* });
|
|
39
58
|
* ```
|
|
40
59
|
*/
|
|
41
|
-
export declare function
|
|
60
|
+
export declare function fetch(_options: Partial<FetchOptions> & {
|
|
61
|
+
url: string;
|
|
62
|
+
}): Promise<Response>;
|
|
42
63
|
/**
|
|
43
|
-
* It
|
|
44
|
-
* Response object
|
|
64
|
+
* It fetches a JSON file from a URL, and returns the parsed data.
|
|
45
65
|
*
|
|
46
66
|
* Example:
|
|
47
67
|
*
|
|
48
68
|
* ```ts
|
|
49
|
-
* const
|
|
69
|
+
* const productList = await getJson<ProductResponse>({
|
|
70
|
+
* url: '/api/products',
|
|
71
|
+
* queryParameters: {limit: 10},
|
|
72
|
+
* timeout: 5_000,
|
|
73
|
+
* retry: 3,
|
|
74
|
+
* cacheStrategy: 'stale_while_revalidate',
|
|
75
|
+
* });
|
|
50
76
|
* ```
|
|
51
77
|
*/
|
|
52
|
-
export declare function
|
|
78
|
+
export declare function getJson<ResponseType extends Record<string | number, unknown>>(options: Partial<FetchOptions> & {
|
|
79
|
+
url: string;
|
|
80
|
+
}): Promise<ResponseType>;
|
|
53
81
|
//# sourceMappingURL=fetch.d.ts.map
|
package/fetch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"AASA,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,eAA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BvB"}
|
package/fetch.js
CHANGED
|
@@ -4,33 +4,102 @@ alwatrRegisteredList.push({
|
|
|
4
4
|
name: '@alwatr/fetch',
|
|
5
5
|
version: '{{ALWATR_VERSION}}',
|
|
6
6
|
});
|
|
7
|
+
let cacheStorage;
|
|
8
|
+
const cacheSupported = 'caches' in self;
|
|
7
9
|
/**
|
|
8
|
-
* It's a wrapper around the browser's `fetch` function that adds retry pattern with timeout
|
|
10
|
+
* It's a wrapper around the browser's `fetch` function that adds retry pattern with timeout and cacheStrategy.
|
|
9
11
|
*
|
|
10
12
|
* Example:
|
|
11
13
|
*
|
|
12
14
|
* ```ts
|
|
13
|
-
* const response = await fetch(
|
|
15
|
+
* const response = await fetch({
|
|
16
|
+
* url: '/api/products',
|
|
17
|
+
* queryParameters: {limit: 10},
|
|
18
|
+
* timeout: 5_000,
|
|
19
|
+
* retry: 3,
|
|
20
|
+
* cacheStrategy: 'stale_while_revalidate',
|
|
21
|
+
* });
|
|
14
22
|
* ```
|
|
15
23
|
*/
|
|
16
|
-
export function fetch(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
export async function fetch(_options) {
|
|
25
|
+
const options = _processOptions(_options);
|
|
26
|
+
logger.logMethodArgs('fetch', { options });
|
|
27
|
+
if (options.cacheStrategy === 'network_only') {
|
|
28
|
+
return _fetch(options);
|
|
29
|
+
}
|
|
30
|
+
// else handle cache strategies!
|
|
31
|
+
if (cacheStorage == null) {
|
|
32
|
+
cacheStorage = await caches.open(options.cacheStorageName);
|
|
33
|
+
}
|
|
34
|
+
const request = new Request(options.url, options);
|
|
35
|
+
switch (options.cacheStrategy) {
|
|
36
|
+
case 'cache_first': {
|
|
37
|
+
const cachedResponse = await cacheStorage.match(request);
|
|
38
|
+
if (cachedResponse != null)
|
|
39
|
+
return cachedResponse;
|
|
40
|
+
const response = await _fetch(options);
|
|
41
|
+
if (response.ok) {
|
|
42
|
+
cacheStorage.put(request, response.clone());
|
|
43
|
+
}
|
|
44
|
+
return response;
|
|
45
|
+
}
|
|
46
|
+
case 'cache_only': {
|
|
47
|
+
const cachedResponse = await cacheStorage.match(request);
|
|
48
|
+
if (cachedResponse == null)
|
|
49
|
+
throw new Error('fetch_cache_not_found');
|
|
50
|
+
return cachedResponse;
|
|
51
|
+
}
|
|
52
|
+
case 'network_first': {
|
|
53
|
+
try {
|
|
54
|
+
const networkResponse = await _fetch(options);
|
|
55
|
+
if (networkResponse.ok) {
|
|
56
|
+
cacheStorage.put(request, networkResponse.clone());
|
|
57
|
+
}
|
|
58
|
+
return networkResponse;
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
const cachedResponse = await cacheStorage.match(request);
|
|
62
|
+
if (cachedResponse == null)
|
|
63
|
+
throw err;
|
|
64
|
+
return cachedResponse;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
case 'stale_while_revalidate': {
|
|
68
|
+
const cachedResponse = await cacheStorage.match(request);
|
|
69
|
+
const fetchedResponsePromise = _fetch(options).then((networkResponse) => {
|
|
70
|
+
if (networkResponse.ok) {
|
|
71
|
+
cacheStorage.put(request, networkResponse.clone());
|
|
72
|
+
}
|
|
73
|
+
return networkResponse;
|
|
74
|
+
});
|
|
75
|
+
return cachedResponse || fetchedResponsePromise;
|
|
76
|
+
}
|
|
77
|
+
default: {
|
|
78
|
+
return _fetch(options);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function _processOptions(options) {
|
|
22
83
|
options.method ?? (options.method = 'GET');
|
|
84
|
+
options.window ?? (options.window = null);
|
|
23
85
|
options.timeout ?? (options.timeout = 5000);
|
|
24
86
|
options.retry ?? (options.retry = 3);
|
|
25
|
-
options.
|
|
26
|
-
|
|
87
|
+
options.cacheStrategy ?? (options.cacheStrategy = 'network_only');
|
|
88
|
+
options.cacheStorageName ?? (options.cacheStorageName = 'alwatr_fetch_cache');
|
|
89
|
+
if (options.cacheStrategy !== 'network_only' && cacheSupported !== true) {
|
|
90
|
+
logger.accident('fetch', 'fetch_cache_strategy_ignore', 'Cache storage not support in this browser', {
|
|
91
|
+
cacheSupported,
|
|
92
|
+
});
|
|
93
|
+
options.cacheStrategy = 'network_only';
|
|
94
|
+
}
|
|
95
|
+
if (options.url.lastIndexOf('?') === -1 && options.queryParameters != null) {
|
|
27
96
|
// prettier-ignore
|
|
28
97
|
const queryArray = Object
|
|
29
98
|
.keys(options.queryParameters)
|
|
30
99
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
31
100
|
.map((key) => `${key}=${String(options.queryParameters[key])}`);
|
|
32
101
|
if (queryArray.length > 0) {
|
|
33
|
-
url += '?' + queryArray.join('&');
|
|
102
|
+
options.url += '?' + queryArray.join('&');
|
|
34
103
|
}
|
|
35
104
|
}
|
|
36
105
|
if (options.body != null && options.bodyJson != null) {
|
|
@@ -40,6 +109,13 @@ export function fetch(url, options = {}) {
|
|
|
40
109
|
'Content-Type': 'application/json',
|
|
41
110
|
};
|
|
42
111
|
}
|
|
112
|
+
return options;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* It's a wrapper around the browser's `fetch` function that adds retry pattern with timeout.
|
|
116
|
+
*/
|
|
117
|
+
async function _fetch(options) {
|
|
118
|
+
logger.logMethodArgs('_fetch', { options });
|
|
43
119
|
// @TODO: AbortController polyfill
|
|
44
120
|
const abortController = new AbortController();
|
|
45
121
|
const externalAbortSignal = options.signal;
|
|
@@ -57,58 +133,61 @@ export function fetch(url, options = {}) {
|
|
|
57
133
|
});
|
|
58
134
|
}
|
|
59
135
|
abortController.signal.addEventListener('abort', () => {
|
|
60
|
-
logger.incident('fetch', '
|
|
61
|
-
url,
|
|
136
|
+
logger.incident('fetch', 'fetch_abort_signal', 'fetch abort signal received', {
|
|
62
137
|
reason: abortController.signal.reason,
|
|
63
138
|
});
|
|
64
139
|
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.
|
|
140
|
+
const retryFetch = () => {
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
142
|
+
options.retry--;
|
|
143
|
+
options.signal = externalAbortSignal;
|
|
144
|
+
return fetch(options);
|
|
145
|
+
};
|
|
146
|
+
try {
|
|
147
|
+
// @TODO: browser fetch polyfill
|
|
148
|
+
const response = await window.fetch(options.url, options);
|
|
69
149
|
clearTimeout(timeoutId);
|
|
70
150
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
71
|
-
if (response.status >= 502 && response.status <= 504
|
|
72
|
-
|
|
73
|
-
options.retry--;
|
|
74
|
-
options.signal = externalAbortSignal;
|
|
75
|
-
logger.accident('fetch', 'fetch_nok', 'fetch not ok and retry', {
|
|
76
|
-
retry: options.retry,
|
|
151
|
+
if (options.retry > 1 && response.status >= 502 && response.status <= 504) {
|
|
152
|
+
logger.accident('fetch', 'fetch_not_valid', 'fetch not valid and retry', {
|
|
77
153
|
response,
|
|
78
154
|
});
|
|
79
|
-
return
|
|
155
|
+
return retryFetch();
|
|
80
156
|
}
|
|
81
157
|
return response;
|
|
82
|
-
}
|
|
83
|
-
|
|
158
|
+
}
|
|
159
|
+
catch (reason) {
|
|
84
160
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
85
161
|
if (timedOut && options.retry > 1) {
|
|
86
|
-
|
|
87
|
-
options.retry--;
|
|
88
|
-
options.signal = externalAbortSignal;
|
|
89
|
-
logger.accident('fetch', 'fetch_catch', 'fetch catch and retry', {
|
|
90
|
-
retry: options.retry,
|
|
162
|
+
logger.incident('fetch', 'fetch_timeout', 'fetch timeout and retry', {
|
|
91
163
|
reason,
|
|
92
164
|
});
|
|
93
|
-
return
|
|
165
|
+
return retryFetch();
|
|
94
166
|
}
|
|
95
167
|
else {
|
|
168
|
+
clearTimeout(timeoutId);
|
|
96
169
|
throw reason;
|
|
97
170
|
}
|
|
98
|
-
}
|
|
171
|
+
}
|
|
99
172
|
}
|
|
100
173
|
/**
|
|
101
|
-
* It fetches a JSON file from a URL, and returns the
|
|
174
|
+
* It fetches a JSON file from a URL, and returns the parsed data.
|
|
102
175
|
*
|
|
103
176
|
* Example:
|
|
104
177
|
*
|
|
105
178
|
* ```ts
|
|
106
|
-
* const productList = await getJson<ProductResponse>(
|
|
179
|
+
* const productList = await getJson<ProductResponse>({
|
|
180
|
+
* url: '/api/products',
|
|
181
|
+
* queryParameters: {limit: 10},
|
|
182
|
+
* timeout: 5_000,
|
|
183
|
+
* retry: 3,
|
|
184
|
+
* cacheStrategy: 'stale_while_revalidate',
|
|
185
|
+
* });
|
|
107
186
|
* ```
|
|
108
187
|
*/
|
|
109
|
-
export async function getJson(
|
|
110
|
-
logger.logMethodArgs('getJson', {
|
|
111
|
-
const response = await fetch(
|
|
188
|
+
export async function getJson(options) {
|
|
189
|
+
logger.logMethodArgs('getJson', { options });
|
|
190
|
+
const response = await fetch(options);
|
|
112
191
|
let data;
|
|
113
192
|
try {
|
|
114
193
|
if (!response.ok) {
|
|
@@ -123,7 +202,7 @@ export async function getJson(url, options = {}) {
|
|
|
123
202
|
});
|
|
124
203
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
125
204
|
if (options.retry > 1) {
|
|
126
|
-
data = await getJson(
|
|
205
|
+
data = await getJson(options);
|
|
127
206
|
}
|
|
128
207
|
else {
|
|
129
208
|
throw err;
|
|
@@ -131,22 +210,4 @@ export async function getJson(url, options = {}) {
|
|
|
131
210
|
}
|
|
132
211
|
return data;
|
|
133
212
|
}
|
|
134
|
-
/**
|
|
135
|
-
* It takes a URL, a JSON object, and an optional FetchOptions object, and returns a Promise of a
|
|
136
|
-
* Response object
|
|
137
|
-
*
|
|
138
|
-
* Example:
|
|
139
|
-
*
|
|
140
|
-
* ```ts
|
|
141
|
-
* const response = await postJson('/api/product/new', {name: 'foo', ...});
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
export function postJson(url, bodyJson, options) {
|
|
145
|
-
logger.logMethod('postJson');
|
|
146
|
-
return fetch(url, {
|
|
147
|
-
method: 'POST',
|
|
148
|
-
bodyJson,
|
|
149
|
-
...options,
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
213
|
//# sourceMappingURL=fetch.js.map
|
package/fetch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;AAElE,MAAM,MAAM,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC;AAE5C,oBAAoB,CAAC,IAAI,CAAC;IACxB,IAAI,EAAE,eAAe;IACrB,OAAO,EAAE,oBAAoB;CAC9B,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;AAElE,MAAM,MAAM,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC;AAE5C,oBAAoB,CAAC,IAAI,CAAC;IACxB,IAAI,EAAE,eAAe;IACrB,OAAO,EAAE,oBAAoB;CAC9B,CAAC,CAAC;AAwDH,IAAI,YAAmB,CAAC;AACxB,MAAM,cAAc,GAAG,QAAQ,IAAI,IAAI,CAAC;AAExC;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,QAA+C;IACzE,MAAM,OAAO,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;IAE1C,MAAM,CAAC,aAAa,CAAC,OAAO,EAAE,EAAC,OAAO,EAAC,CAAC,CAAC;IAEzC,IAAI,OAAO,CAAC,aAAa,KAAK,cAAc,EAAE;QAC5C,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC;KACxB;IACD,gCAAgC;IAEhC,IAAI,YAAY,IAAI,IAAI,EAAE;QACxB,YAAY,GAAG,MAAM,M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C,EAAE;YAClC,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,eAAe,EAAE,yBAAyB,EAAE;gBACnE,MAAM;aACP,CAAC,CAAC;YACH,OAAO,UAAU,EAAE,CAAC;SACrB;aACI;YACH,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,MAAM,MAAM,CAAC;SACd;KACF;AACH,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CACzB,OAA8C;IAEhD,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,EAAC,OAAO,EAAC,CAAC,CAAC;IAE3C,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,CAAC;IAEtC,IAAI,IAAkB,CAAC;IAEvB,IAAI;QACF,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,MAAM,IAAI,KAAK,CAAC,WAAW,CAAC,CAAC;SAC9B;QACD,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAiB,CAAC;KAChD;IACD,OAAO,GAAG,EAAE;QACV,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,eAAe,EAAE,qBAAqB,EAAE;YACjE,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,GAAG;SACJ,CAAC,CAAC;QAEH,oEAAoE;QACpE,IAAI,OAAO,CAAC,KAAM,GAAG,CAAC,EAAE;YACtB,IAAI,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;SAC/B;aACI;YACH,MAAM,GAAG,CAAC;SACX;KACF;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/fetch",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Enhanced fetch
|
|
3
|
+
"version": "0.18.0",
|
|
4
|
+
"description": "Enhanced fetch API with cache strategy, retry pattern, timeout, helper methods and enhanced types written in tiny TypeScript, ES module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
7
7
|
"request",
|
|
8
8
|
"api",
|
|
9
|
+
"retry",
|
|
10
|
+
"cache",
|
|
9
11
|
"timeout",
|
|
10
12
|
"typescript",
|
|
11
13
|
"esm",
|
|
@@ -32,8 +34,8 @@
|
|
|
32
34
|
"url": "https://github.com/AliMD/alwatr/issues"
|
|
33
35
|
},
|
|
34
36
|
"dependencies": {
|
|
35
|
-
"@alwatr/logger": "^0.
|
|
37
|
+
"@alwatr/logger": "^0.18.0",
|
|
36
38
|
"tslib": "^2.3.1"
|
|
37
39
|
},
|
|
38
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "a6f75b381a758849b514ea0d59eec6a3b3444008"
|
|
39
41
|
}
|