@alwatr/fetch 5.6.6 → 6.0.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 +28 -0
- package/README.md +183 -43
- package/dist/main.cjs +54 -80
- package/dist/main.cjs.map +4 -4
- package/dist/main.d.ts +43 -39
- package/dist/main.d.ts.map +1 -1
- package/dist/main.mjs +53 -78
- package/dist/main.mjs.map +4 -4
- package/dist/type.d.ts +48 -74
- package/dist/type.d.ts.map +1 -1
- package/package.json +11 -11
- package/src/main.test.js +41 -41
- package/dist/core.d.ts +0 -28
- package/dist/core.d.ts.map +0 -1
package/src/main.test.js
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import {fetch
|
|
1
|
+
import {fetch} from '@alwatr/fetch';
|
|
2
2
|
|
|
3
3
|
describe('fetch with search params', () => {
|
|
4
4
|
it('should make a GET request to the specified URL', async () => {
|
|
5
|
-
|
|
6
|
-
* @type {import('@alwatr/fetch').FetchOptions}
|
|
7
|
-
*/
|
|
8
|
-
const options = {
|
|
9
|
-
url: 'http://httpbin.org/get',
|
|
5
|
+
const response = await fetch('http://httpbin.org/get', {
|
|
10
6
|
queryParams: {
|
|
11
|
-
a: 2,
|
|
7
|
+
a: 'b&c=2',
|
|
12
8
|
},
|
|
13
|
-
|
|
14
|
-
removeDuplicate: 'auto',
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
fetch(options);
|
|
18
|
-
const response = await fetch(options);
|
|
9
|
+
});
|
|
19
10
|
const responseJson = await response.json();
|
|
20
11
|
|
|
21
12
|
expect(response.status).toBe(200);
|
|
22
|
-
expect(responseJson.args.a).toBe('2');
|
|
13
|
+
expect(responseJson.args.a).toBe('b&c=2');
|
|
23
14
|
});
|
|
24
15
|
});
|
|
25
16
|
|
|
26
|
-
describe('fetch
|
|
27
|
-
it('should
|
|
28
|
-
|
|
29
|
-
* @type {import('@alwatr/fetch').FetchOptions}}
|
|
30
|
-
*/
|
|
31
|
-
const options = {
|
|
32
|
-
url: 'http://httpbin.org/post',
|
|
17
|
+
describe('fetch with bodyJson', () => {
|
|
18
|
+
it('should send JSON body and receive it back', async () => {
|
|
19
|
+
const response = await fetch('http://httpbin.org/post', {
|
|
33
20
|
method: 'POST',
|
|
34
|
-
bodyJson: {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
cacheStrategy: 'network_only',
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const responseJson = await fetchJson(options);
|
|
21
|
+
bodyJson: {foo: 'bar', baz: 42},
|
|
22
|
+
});
|
|
23
|
+
const responseJson = await response.json();
|
|
41
24
|
|
|
42
|
-
expect(
|
|
43
|
-
expect(responseJson.json
|
|
25
|
+
expect(response.status).toBe(200);
|
|
26
|
+
expect(responseJson.json).toEqual({foo: 'bar', baz: 42});
|
|
27
|
+
expect(responseJson.headers['Content-Type']).toMatch(/application\/json/);
|
|
44
28
|
});
|
|
29
|
+
});
|
|
45
30
|
|
|
46
|
-
it('should make a GET request to the specified URL and parse json and handle 404 status code', async () => {
|
|
47
|
-
/**
|
|
48
|
-
* @type {import('@alwatr/fetch').FetchOptions}}
|
|
49
|
-
*/
|
|
50
|
-
const options = {
|
|
51
|
-
url: 'https://httpbin.org/status/404',
|
|
52
|
-
cacheStrategy: 'network_only',
|
|
53
|
-
};
|
|
54
31
|
|
|
55
|
-
|
|
32
|
+
describe('fetch with timeout', () => {
|
|
33
|
+
it('should throw on timeout', async () => {
|
|
34
|
+
await expect(fetch('http://httpbin.org/delay/1', {timeout: 500, retry: 0})).rejects.toThrow('fetch_timeout');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
56
37
|
|
|
57
|
-
|
|
58
|
-
|
|
38
|
+
describe('fetch with removeDuplicate', () => {
|
|
39
|
+
it('should deduplicate parallel requests', async () => {
|
|
40
|
+
const url = 'http://httpbin.org/get?dedup=1';
|
|
41
|
+
const [res1, res2] = await Promise.all([
|
|
42
|
+
fetch(url, {
|
|
43
|
+
removeDuplicate: 'always',
|
|
44
|
+
headers: {'Req': '1'},
|
|
45
|
+
}),
|
|
46
|
+
fetch(url, {
|
|
47
|
+
removeDuplicate: 'always',
|
|
48
|
+
headers: {'Req': '2'},
|
|
49
|
+
}),
|
|
50
|
+
]);
|
|
51
|
+
expect(res1.status).toBe(200);
|
|
52
|
+
expect(res2.status).toBe(200);
|
|
53
|
+
const json1 = await res1.json();
|
|
54
|
+
const json2 = await res2.json();
|
|
55
|
+
expect(json1.args.dedup).toBe('1');
|
|
56
|
+
expect(json2.args.dedup).toBe('1');
|
|
57
|
+
expect(json1.headers['Req']).toBe('1');
|
|
58
|
+
expect(json2.headers['Req']).toBe('1'); // both should be '1' due to deduplication
|
|
59
59
|
});
|
|
60
60
|
});
|
package/dist/core.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { FetchOptions } from './type.js';
|
|
2
|
-
export declare const logger_: import("@alwatr/logger").AlwatrLogger;
|
|
3
|
-
export declare const cacheSupported: boolean;
|
|
4
|
-
/**
|
|
5
|
-
* Process fetch options and set defaults, etc.
|
|
6
|
-
*
|
|
7
|
-
* @param options Fetch options.
|
|
8
|
-
*
|
|
9
|
-
* @returns Required fetch options.
|
|
10
|
-
*/
|
|
11
|
-
export declare function processOptions_(options: FetchOptions): Required<FetchOptions>;
|
|
12
|
-
/**
|
|
13
|
-
* Handle Cache Strategy over `handleRemoveDuplicate_`.
|
|
14
|
-
*/
|
|
15
|
-
export declare function handleCacheStrategy_(options: Required<FetchOptions>): Promise<Response>;
|
|
16
|
-
/**
|
|
17
|
-
* Handle Remove Duplicates over `_handleRetryPattern`.
|
|
18
|
-
*/
|
|
19
|
-
export declare function handleRemoveDuplicate_(options: Required<FetchOptions>): Promise<Response>;
|
|
20
|
-
/**
|
|
21
|
-
* Handle retry pattern over `handleTimeout_`.
|
|
22
|
-
*/
|
|
23
|
-
export declare function handleRetryPattern_(options: Required<FetchOptions>): Promise<Response>;
|
|
24
|
-
/**
|
|
25
|
-
* It's a wrapper around the browser's `fetch` with timeout.
|
|
26
|
-
*/
|
|
27
|
-
export declare function handleTimeout_(options: FetchOptions): Promise<Response>;
|
|
28
|
-
//# sourceMappingURL=core.d.ts.map
|
package/dist/core.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,WAAW,CAAC;AAI5C,eAAO,MAAM,OAAO,uCAAgD,CAAC;AAKrE,eAAO,MAAM,cAAc,SAAuD,CAAC;AAInF;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,YAAY,GAAG,QAAQ,CAAC,YAAY,CAAC,CA+C7E;AAED;;GAEG;AACH,wBAAsB,oBAAoB,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAoF7F;AAED;;GAEG;AACH,wBAAsB,sBAAsB,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA2B/F;AAED;;GAEG;AACH,wBAAsB,mBAAmB,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8B5F;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAoCvE"}
|