@alwatr/fetch 0.6.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 ADDED
@@ -0,0 +1,48 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [0.6.0](https://github.com/AliMD/alwatr/compare/v0.5.0...v0.6.0) (2022-03-11)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * alalwatr ([898aa6e](https://github.com/AliMD/alwatr/commit/898aa6ed0888eab9265c83b96a50f1b8c216d143))
12
+ * **packages:** duplicate alwatr keyword ([77d4aa2](https://github.com/AliMD/alwatr/commit/77d4aa2105ad47515c3eee251fd6b8c281d0d1fc))
13
+
14
+
15
+
16
+
17
+
18
+ # [0.5.0](https://github.com/AliMD/alwatr/compare/v0.4.0...v0.5.0) (2022-03-11)
19
+
20
+ **Note:** Version bump only for package @alwatr/fetch
21
+
22
+ # [0.4.0](https://github.com/AliMD/alwatr/compare/v0.3.0...v0.4.0) (2022-03-11)
23
+
24
+ ### Bug Fixes
25
+
26
+ * **fetch:** build issue [#73](https://github.com/AliMD/alwatr/issues/73) ([fb74463](https://github.com/AliMD/alwatr/commit/fb74463d367393706d16e482488a565bdfef70a1))
27
+
28
+ # [0.3.0](https://github.com/AliMD/alwatr/compare/v0.2.1...v0.3.0) (2022-03-06)
29
+
30
+ **Note:** Version bump only for package @alwatr/fetch
31
+
32
+ ## [0.2.1](https://github.com/AliMD/alwatr/compare/v0.2.0...v0.2.1) (2022-03-05)
33
+
34
+ **Note:** Version bump only for package @alwatr/fetch
35
+
36
+ # [0.2.0](https://github.com/AliMD/alwatr/compare/v0.1.2...v0.2.0) (2022-03-05)
37
+
38
+ ### Bug Fixes
39
+
40
+ * **fetch:** error codes ([4e0be80](https://github.com/AliMD/alwatr/commit/4e0be80786b6667035ae82750f2351bd2da4f341))
41
+
42
+ ### Features
43
+
44
+ * **fetch:** add getData ([4a627c4](https://github.com/AliMD/alwatr/commit/4a627c43d2ad07acb340f058f02d41ce57288405))
45
+ * **fetch:** add postData and fetchJson ([b0262b8](https://github.com/AliMD/alwatr/commit/b0262b83b5b7d8b7c376dce68aae3e16bd8e2610))
46
+ * **fetch:** base fetch api ([925cdab](https://github.com/AliMD/alwatr/commit/925cdab8700ffb801e1d836ffc5d3245b66815cf))
47
+ * **fetch:** FetchOptions type ([dd2d141](https://github.com/AliMD/alwatr/commit/dd2d1418d090387e1afa8277f78ae88ace17fd17))
48
+ * **fetch:** new enhanced fetch package ([7e90472](https://github.com/AliMD/alwatr/commit/7e904720c983372317358ce107f6ad0f79c65647))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 S. Ali Mihandoost
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # @alwatr/fetch
2
+
3
+ Enhanced fetch API with the timeout, helper methods, and types written in tiny TypeScript, ES module.
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 two other parameters:
10
+
11
+ - `bodyObject`: a JSON object that converts to string and put on the body.
12
+ - `queryParameters`: a JSON object that converts to URL query params
13
+
14
+ ## Example usage
15
+
16
+ ```js
17
+ import {getJson} from 'https://esm.run/@alwatr/fetch';
18
+
19
+ const productList = await getJson('/api/products', {limit: 10}, {timeout: 5_000});
20
+ ```
package/fetch.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ declare global {
2
+ interface AbortController {
3
+ abort(reason?: string): void;
4
+ }
5
+ interface AbortSignal {
6
+ reason?: string;
7
+ }
8
+ }
9
+ export interface FetchOptions extends RequestInit {
10
+ /**
11
+ * @default 10_000 ms
12
+ */
13
+ timeout?: number;
14
+ bodyObject?: Record<string | number, unknown>;
15
+ queryParameters?: Record<string, string | number | boolean>;
16
+ }
17
+ /**
18
+ * Enhanced base fetch API.
19
+ * @example const response = await fetch(url, {jsonResponse: false});
20
+ */
21
+ export declare function fetch(url: string, options?: FetchOptions): Promise<Response>;
22
+ /**
23
+ * Enhanced get data.
24
+ * @example
25
+ * const response = await postData('/api/products', {limit: 10}, {timeout: 5_000});
26
+ */
27
+ export declare function getData(url: string, queryParameters?: Record<string | number, string | number | boolean>, options?: FetchOptions): Promise<Response>;
28
+ /**
29
+ * Enhanced fetch JSON.
30
+ * @example
31
+ * const productList = await getJson('/api/products', {limit: 10}, {timeout: 5_000});
32
+ */
33
+ export declare function getJson<ResponseType extends Record<string | number, unknown>>(url: string, queryParameters?: Record<string | number, string | number | boolean>, options?: FetchOptions): Promise<ResponseType>;
34
+ /**
35
+ * Enhanced post json data.
36
+ * @example
37
+ * const response = await postData('/api/product/new', {name: 'foo', ...});
38
+ */
39
+ export declare function postData(url: string, body: Record<string | number, unknown>, options?: FetchOptions): Promise<Response>;
40
+ //# sourceMappingURL=fetch.d.ts.map
package/fetch.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["src/fetch.ts"],"names":[],"mappings":"AASA,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,eAAe;QACvB,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KAC9B;IACD,UAAU,WAAW;QACnB,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB;CACF;AAGD,MAAM,WAAW,YAAa,SAAQ,WAAW;IAE/C;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9C,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;CAC7D;AAED;;;GAGG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsD5E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CACnB,GAAG,EAAE,MAAM,EACX,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EACpE,OAAO,CAAC,EAAE,YAAY,GACvB,OAAO,CAAC,QAAQ,CAAC,CAMnB;AAED;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,YAAY,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAC/E,GAAG,EAAE,MAAM,EACX,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EACpE,OAAO,CAAC,EAAE,YAAY,GACvB,OAAO,CAAC,YAAY,CAAC,CASvB;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CACpB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EACtC,OAAO,CAAC,EAAE,YAAY,GACvB,OAAO,CAAC,QAAQ,CAAC,CAOnB"}
package/fetch.js ADDED
@@ -0,0 +1,96 @@
1
+ import { createLogger, alwatrRegisteredList } from '@alwatr/logger';
2
+ const logger = createLogger('alwatr/fetch');
3
+ alwatrRegisteredList.push({
4
+ name: '@alwatr/fetch',
5
+ version: '{{ALWATR_VERSION}}',
6
+ });
7
+ /**
8
+ * Enhanced base fetch API.
9
+ * @example const response = await fetch(url, {jsonResponse: false});
10
+ */
11
+ export function fetch(url, options) {
12
+ logger.logMethodArgs('fetch', { url, options });
13
+ if (!navigator.onLine) {
14
+ logger.accident('fetch', 'abort_signal', 'abort signal received', { url });
15
+ throw new Error('fetch_offline');
16
+ }
17
+ options = {
18
+ method: 'GET',
19
+ timeout: 15000,
20
+ window: null,
21
+ ...options,
22
+ };
23
+ if (options.queryParameters != null) {
24
+ const queryArray = Object
25
+ .keys(options.queryParameters)
26
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
27
+ .map((key) => `${key}=${String(options.queryParameters[key])}`);
28
+ if (queryArray.length > 0) {
29
+ url += '?' + queryArray.join('&');
30
+ }
31
+ }
32
+ if (options.bodyObject != null) {
33
+ options.body = JSON.stringify(options.bodyObject);
34
+ options.headers = {
35
+ ...options.headers,
36
+ 'Content-Type': 'application/json',
37
+ };
38
+ }
39
+ // @TODO: AbortController polyfill
40
+ const abortController = new AbortController();
41
+ const externalAbortSignal = options.signal;
42
+ if (externalAbortSignal != null) {
43
+ // Respect external abort signal
44
+ externalAbortSignal.addEventListener('abort', () => {
45
+ abortController.abort(`external abort signal: ${externalAbortSignal.reason}`);
46
+ });
47
+ }
48
+ abortController.signal.addEventListener('abort', () => {
49
+ logger.incident('fetch', 'abort_signal', 'abort signal received', { url, reason: abortController.signal.reason });
50
+ });
51
+ options.signal = abortController.signal;
52
+ const timeoutId = setTimeout(() => abortController.abort('fetch_timeout'), options.timeout);
53
+ // @TODO: browser fetch polyfill
54
+ const response = window.fetch(url, options);
55
+ response.then(() => clearTimeout(timeoutId));
56
+ return response;
57
+ }
58
+ /**
59
+ * Enhanced get data.
60
+ * @example
61
+ * const response = await postData('/api/products', {limit: 10}, {timeout: 5_000});
62
+ */
63
+ export function getData(url, queryParameters, options) {
64
+ logger.logMethodArgs('getData', { url, queryParameters, options });
65
+ return fetch(url, {
66
+ queryParameters,
67
+ ...options,
68
+ });
69
+ }
70
+ /**
71
+ * Enhanced fetch JSON.
72
+ * @example
73
+ * const productList = await getJson('/api/products', {limit: 10}, {timeout: 5_000});
74
+ */
75
+ export async function getJson(url, queryParameters, options) {
76
+ logger.logMethodArgs('getJson', { url, queryParameters, options });
77
+ const response = await getData(url, queryParameters, options);
78
+ if (!response.ok) {
79
+ throw new Error('fetch_nok');
80
+ }
81
+ return response.json();
82
+ }
83
+ /**
84
+ * Enhanced post json data.
85
+ * @example
86
+ * const response = await postData('/api/product/new', {name: 'foo', ...});
87
+ */
88
+ export function postData(url, body, options) {
89
+ logger.logMethodArgs('postData', { url, body, options });
90
+ return fetch(url, {
91
+ method: 'POST',
92
+ bodyObject: body,
93
+ ...options,
94
+ });
95
+ }
96
+ //# sourceMappingURL=fetch.js.map
package/fetch.js.map ADDED
@@ -0,0 +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;AAuBH;;;GAGG;AACH,MA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AAe,EAAE,OAAO,EAAC,CAAC,CAAC;IACjE,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE,eAAe,EAAE,OAAO,CAAC,CAAC;IAE9D,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,WAAW,CAAC,CAAC;KAC9B;IAED,OAAO,QAAQ,CAAC,IAAI,EAA2B,CAAC;AAClD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CACpB,GAAW,EACX,IAAsC,EACtC,OAAsB;IAExB,MAAM,CAAC,aAAa,CAAC,UAAU,EAAE,EAAC,GAAG,EAAE,IAAI,EAAE,OAAO,EAAC,CAAC,CAAC;IACvD,OAAO,KAAK,CAAC,GAAG,EAAE;QAChB,MAAM,EAAE,MAAM;QACd,UAAU,EAAE,IAAI;QAChB,GAAG,OAAO;KACX,CAAC,CAAC;AACL,CAAC"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@alwatr/fetch",
3
+ "version": "0.6.0",
4
+ "description": "Enhanced fetch api with timeout, helper methods and types written in tiny TypeScript module.",
5
+ "keywords": [
6
+ "fetch",
7
+ "request",
8
+ "api",
9
+ "timeout",
10
+ "typescript",
11
+ "esm",
12
+ "alwatr"
13
+ ],
14
+ "main": "fetch.js",
15
+ "type": "module",
16
+ "types": "fetch.d.ts",
17
+ "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
18
+ "license": "MIT",
19
+ "files": [
20
+ "**/*.js",
21
+ "**/*.d.ts",
22
+ "**/*.map",
23
+ "**/*.html",
24
+ "**/*.md"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/AliMD/alwatr",
32
+ "directory": "package/fetch"
33
+ },
34
+ "homepage": "https://github.com/AliMD/alwatr/tree/main/package/fetch#readme",
35
+ "bugs": {
36
+ "url": "https://github.com/AliMD/alwatr/issues"
37
+ },
38
+ "dependencies": {
39
+ "@alwatr/logger": "^0.6.0",
40
+ "tslib": "^2.3.1"
41
+ },
42
+ "gitHead": "8ed13adce22d7bb0cc24eb17c9f8ecfb6bbe8553"
43
+ }