@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/src/main.test.js CHANGED
@@ -1,60 +1,60 @@
1
- import {fetch, fetchJson} from '@alwatr/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
- cacheStrategy: 'network_only',
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 json', () => {
27
- it('should make a GET request to the specified URL with json body and parse valid json', async () => {
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
- a: 2,
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(responseJson.ok).toBe(undefined);
43
- expect(responseJson.json.a).toBe(2);
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
- const responseJson = await fetchJson(options);
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
- expect(responseJson.ok).toBe(false);
58
- expect(responseJson.statusCode).toBe(404);
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
@@ -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"}