@alessiofrittoli/fetcher 1.0.0 → 2.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/README.md +83 -48
- package/dist/error/index.cjs +1 -0
- package/dist/error/index.d.cts +2 -0
- package/dist/error/index.d.mts +2 -0
- package/dist/error/index.mjs +1 -0
- package/dist/getErrorFromResponse-CIppVMTN.mjs +1 -0
- package/dist/getErrorFromResponse-D3OARgUA.cjs +1 -0
- package/dist/globals/index.d.cts +15 -0
- package/dist/globals/index.d.mts +15 -0
- package/dist/globals/index.mjs +1 -0
- package/dist/index-B8rmZCit.d.mts +398 -0
- package/dist/index-CQSGH5s-.d.cts +25 -0
- package/dist/index-CQSGH5s-.d.mts +25 -0
- package/dist/index-D-vZzzH1.d.cts +398 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +17 -0
- package/dist/index.d.mts +16 -11
- package/dist/index.mjs +1 -0
- package/dist/ping/index.cjs +1 -0
- package/dist/ping/index.d.cts +9 -0
- package/dist/ping/index.d.mts +9 -0
- package/dist/ping/index.mjs +1 -0
- package/dist/types/index.cjs +0 -0
- package/dist/types/index.d.cts +2 -0
- package/dist/types/index.d.mts +2 -0
- package/dist/types/index.mjs +1 -0
- package/dist/xhr/index.cjs +1 -0
- package/dist/xhr/index.d.cts +2 -0
- package/dist/xhr/index.d.mts +2 -8
- package/dist/xhr/index.mjs +1 -1
- package/package.json +127 -155
- package/dist/chunk-HNNMLJIA.js +0 -1
- package/dist/chunk-LOWOZ6LK.mjs +0 -1
- package/dist/error.d.mts +0 -13
- package/dist/error.d.ts +0 -13
- package/dist/error.js +0 -1
- package/dist/error.mjs +0 -1
- package/dist/fetch.d.mts +0 -23
- package/dist/fetch.d.ts +0 -23
- package/dist/fetch.js +0 -1
- package/dist/fetch.mjs +0 -1
- package/dist/getErrorFromResponse-7V2YFS4F.js +0 -1
- package/dist/getErrorFromResponse-IJTUOFPH.mjs +0 -1
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -1
- package/dist/logReadyStateChange-2QWX5XIU.mjs +0 -1
- package/dist/logReadyStateChange-MSYI3OBQ.js +0 -1
- package/dist/ping.d.mts +0 -8
- package/dist/ping.d.ts +0 -8
- package/dist/ping.js +0 -1
- package/dist/ping.mjs +0 -1
- package/dist/types-CVc-rxXe.d.ts +0 -227
- package/dist/types-DuGUXaTD.d.mts +0 -227
- package/dist/types.d.mts +0 -8
- package/dist/types.d.ts +0 -8
- package/dist/types.js +0 -1
- package/dist/xhr/index.d.ts +0 -8
- package/dist/xhr/index.js +0 -1
- package/scripts/postinstall/index.js +0 -16
- package/scripts/postinstall/ts-setup.js +0 -15
- /package/dist/{types.mjs → globals/index.cjs} +0 -0
package/README.md
CHANGED
|
@@ -1,34 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
<h1 align="center">Fetcher 🔭</h1>
|
|
2
|
+
<h2 align="center">
|
|
3
|
+
Typescript fetch/xhr utilities
|
|
4
|
+
</h2>
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://npmjs.org/package/@alessiofrittoli/fetcher">
|
|
7
|
+
<img src="https://img.shields.io/npm/v/@alessiofrittoli/fetcher" alt="Latest version"/>
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://coveralls.io/github/alessiofrittoli/fetcher">
|
|
10
|
+
<img src="https://coveralls.io/repos/github/alessiofrittoli/fetcher/badge.svg" alt="Test coverage"/>
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://socket.dev/npm/package/@alessiofrittoli/fetcher/overview">
|
|
13
|
+
<img src="https://socket.dev/api/badge/npm/package/@alessiofrittoli/fetcher" alt="Socket Security score"/>
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://npmjs.org/package/@alessiofrittoli/fetcher">
|
|
16
|
+
<img src="https://img.shields.io/npm/dm/@alessiofrittoli/fetcher.svg" alt="npm downloads"/>
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://bundlephobia.com/package/@alessiofrittoli/fetcher">
|
|
19
|
+
<img src="https://badgen.net/bundlephobia/dependency-count/@alessiofrittoli/fetcher" alt="Dependencies"/>
|
|
20
|
+
</a>
|
|
21
|
+
<a href="https://libraries.io/npm/%40alessiofrittoli%2Ffetcher">
|
|
22
|
+
<img src="https://img.shields.io/librariesio/release/npm/@alessiofrittoli/fetcher" alt="Dependencies status"/>
|
|
23
|
+
</a>
|
|
24
|
+
</p>
|
|
25
|
+
<p align="center">
|
|
26
|
+
<a href="https://bundlephobia.com/package/@alessiofrittoli/fetcher">
|
|
27
|
+
<img src="https://badgen.net/bundlephobia/min/@alessiofrittoli/fetcher" alt="minified"/>
|
|
28
|
+
</a>
|
|
29
|
+
<a href="https://bundlephobia.com/package/@alessiofrittoli/fetcher">
|
|
30
|
+
<img src="https://badgen.net/bundlephobia/minzip/@alessiofrittoli/fetcher" alt="minizipped"/>
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://bundlephobia.com/package/@alessiofrittoli/fetcher">
|
|
33
|
+
<img src="https://badgen.net/bundlephobia/tree-shaking/@alessiofrittoli/fetcher" alt="Tree shakable"/>
|
|
34
|
+
</a>
|
|
35
|
+
</p>
|
|
36
|
+
<p align="center">
|
|
37
|
+
<a href="https://github.com/sponsors/alessiofrittoli">
|
|
38
|
+
<img src="https://img.shields.io/static/v1?label=Fund%20this%20package&message=%E2%9D%A4&logo=GitHub&color=%23DB61A2" alt="Fund this package"/>
|
|
39
|
+
</a>
|
|
40
|
+
</p>
|
|
16
41
|
|
|
17
42
|
[sponsor-badge]: https://img.shields.io/static/v1?label=Fund%20this%20package&message=%E2%9D%A4&logo=GitHub&color=%23DB61A2
|
|
18
43
|
[sponsor-url]: https://github.com/sponsors/alessiofrittoli
|
|
19
44
|
|
|
20
|
-
|
|
45
|
+
### Table of Content
|
|
21
46
|
|
|
22
|
-
### Table of Contents
|
|
23
|
-
|
|
24
|
-
- [Getting started](#getting-started)
|
|
25
47
|
- [Development](#development)
|
|
26
|
-
- [
|
|
27
|
-
- [
|
|
28
|
-
- [
|
|
29
|
-
|
|
30
|
-
- [Contributing](#contributing)
|
|
31
|
-
- [Security](#security)
|
|
48
|
+
- [Local development](#local-development)
|
|
49
|
+
- [Production build](#production-build)
|
|
50
|
+
- [Unit tests](#unit-tests)
|
|
51
|
+
- [Run tests with coverage](#run-tests-with-coverage)
|
|
52
|
+
- [Contributing](#contributing)
|
|
53
|
+
- [Security](#security)
|
|
54
|
+
- [API Reference](#api-reference)
|
|
32
55
|
- [Credits](#made-with-)
|
|
33
56
|
|
|
34
57
|
---
|
|
@@ -51,39 +74,43 @@ pnpm i @alessiofrittoli/fetcher
|
|
|
51
74
|
|
|
52
75
|
### Development
|
|
53
76
|
|
|
54
|
-
|
|
77
|
+
Run the following to start development
|
|
55
78
|
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
```
|
|
79
|
+
```shell
|
|
80
|
+
nvm use
|
|
59
81
|
|
|
60
|
-
or using `pnpm`
|
|
61
|
-
|
|
62
|
-
```bash
|
|
63
82
|
pnpm i
|
|
64
83
|
```
|
|
65
84
|
|
|
66
|
-
####
|
|
85
|
+
#### Local development
|
|
67
86
|
|
|
68
|
-
Run the following
|
|
87
|
+
Run the following
|
|
69
88
|
|
|
70
|
-
```
|
|
71
|
-
pnpm
|
|
89
|
+
```shell
|
|
90
|
+
pnpm dev
|
|
72
91
|
```
|
|
73
92
|
|
|
74
|
-
|
|
93
|
+
This will generate unminified output code, sourcemaps and will enable source file watcher.
|
|
75
94
|
|
|
76
|
-
|
|
95
|
+
---
|
|
77
96
|
|
|
78
|
-
|
|
79
|
-
|
|
97
|
+
#### Production build
|
|
98
|
+
|
|
99
|
+
Run the following to create a local production build
|
|
100
|
+
|
|
101
|
+
```shell
|
|
102
|
+
pnpm build
|
|
80
103
|
```
|
|
81
104
|
|
|
82
|
-
|
|
105
|
+
This will generate minified output code without sourcemaps.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
#### Unit tests
|
|
83
110
|
|
|
84
111
|
Run all the defined test suites by running the following:
|
|
85
112
|
|
|
86
|
-
```
|
|
113
|
+
```shell
|
|
87
114
|
# Run tests and watch file changes.
|
|
88
115
|
pnpm test:watch
|
|
89
116
|
|
|
@@ -91,21 +118,21 @@ pnpm test:watch
|
|
|
91
118
|
pnpm test:ci
|
|
92
119
|
```
|
|
93
120
|
|
|
94
|
-
|
|
121
|
+
---
|
|
95
122
|
|
|
96
|
-
Run tests with coverage
|
|
123
|
+
##### Run tests with coverage
|
|
97
124
|
|
|
98
125
|
An HTTP server is then started to serve coverage files from `./coverage` folder.
|
|
99
126
|
|
|
100
127
|
⚠️ You may see a blank page the first time you run this command. Simply refresh the browser to see the updates.
|
|
101
128
|
|
|
102
|
-
```
|
|
103
|
-
test:coverage:serve
|
|
129
|
+
```shell
|
|
130
|
+
pnpm test:coverage:serve
|
|
104
131
|
```
|
|
105
132
|
|
|
106
133
|
---
|
|
107
134
|
|
|
108
|
-
|
|
135
|
+
#### Contributing
|
|
109
136
|
|
|
110
137
|
Contributions are truly welcome!
|
|
111
138
|
|
|
@@ -117,10 +144,18 @@ Help keep this project up to date with [GitHub Sponsor][sponsor-url].
|
|
|
117
144
|
|
|
118
145
|
---
|
|
119
146
|
|
|
120
|
-
|
|
147
|
+
#### Security
|
|
121
148
|
|
|
122
149
|
If you believe you have found a security vulnerability, we encourage you to **_responsibly disclose this and NOT open a public issue_**. We will investigate all legitimate reports. Email `security@alessiofrittoli.it` to disclose any security vulnerabilities.
|
|
123
150
|
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
### API Reference
|
|
154
|
+
|
|
155
|
+
⚠️ Docs coming soon ⚠️
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
124
159
|
### Made with ☕
|
|
125
160
|
|
|
126
161
|
<table style='display:flex;gap:20px;'>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@alessiofrittoli/exception/code");const t={XHR_NOT_AVAILABLE:`ERR:XHRNOTAVAILABLE`,XHR_INVALID_STATE:`ERR:XHRINVALIDSTATE`,XHR_DESTROYED:`ERR:XHRDESTROYED`},n={...e.ErrorCode,...t};exports.ErrorCode=n,exports.Request=t;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ErrorCode as e}from"@alessiofrittoli/exception/code";const t={XHR_NOT_AVAILABLE:`ERR:XHRNOTAVAILABLE`,XHR_INVALID_STATE:`ERR:XHRINVALIDSTATE`,XHR_DESTROYED:`ERR:XHRDESTROYED`},n={...e,...t};export{n as ErrorCode,t as Request};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Exception as e}from"@alessiofrittoli/exception";import{binaryToString as t}from"@alessiofrittoli/crypto-buffer";import{isJson as n}from"@alessiofrittoli/web-utils";const r=async r=>{let{response:i,responseType:a}=r;switch(a){case`document`:return null;case`json`:{let t=i;return t&&e.isException(t)?new e(t):null}default:{let r=a===`arraybuffer`?t(i):a===`blob`?await i.text():i;if(n(r)){let t=JSON.parse(r);if(e.isException(t))return new e(t)}return null}}};export{r as getErrorFromResponse};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let e=require("@alessiofrittoli/exception"),t=require("@alessiofrittoli/crypto-buffer"),n=require("@alessiofrittoli/web-utils");const r=async r=>{let{response:i,responseType:a}=r;switch(a){case`document`:return null;case`json`:{let t=i;return t&&e.Exception.isException(t)?new e.Exception(t):null}default:{let r=a===`arraybuffer`?(0,t.binaryToString)(i):a===`blob`?await i.text():i;if((0,n.isJson)(r)){let t=JSON.parse(r);if(e.Exception.isException(t))return new e.Exception(t)}return null}}};exports.getErrorFromResponse=r;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/globals/index.d.ts
|
|
2
|
+
declare global {
|
|
3
|
+
interface RequestInit {
|
|
4
|
+
/**
|
|
5
|
+
* The expected ResponseType.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
responseType?: XMLHttpRequestResponseType | 'formdata';
|
|
9
|
+
}
|
|
10
|
+
interface ProgressEvent {
|
|
11
|
+
readonly type: keyof XMLHttpRequestEventTargetEventMap;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/globals/index.d.ts
|
|
2
|
+
declare global {
|
|
3
|
+
interface RequestInit {
|
|
4
|
+
/**
|
|
5
|
+
* The expected ResponseType.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
responseType?: XMLHttpRequestResponseType | 'formdata';
|
|
9
|
+
}
|
|
10
|
+
interface ProgressEvent {
|
|
11
|
+
readonly type: keyof XMLHttpRequestEventTargetEventMap;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{};
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import { t as ErrorCode } from "./index-CQSGH5s-.mjs";
|
|
2
|
+
import { Exception } from "@alessiofrittoli/exception";
|
|
3
|
+
import { UrlInput } from "@alessiofrittoli/url-utils";
|
|
4
|
+
import { AbortError, AbortErrorOptions } from "@alessiofrittoli/exception/abort";
|
|
5
|
+
import { AbortController } from "@alessiofrittoli/abort-controller";
|
|
6
|
+
import { UUID } from "@alessiofrittoli/math-utils";
|
|
7
|
+
import { EventEmitter, Listener } from "@alessiofrittoli/event-emitter";
|
|
8
|
+
//#region src/fetch/types.d.ts
|
|
9
|
+
declare namespace Fetch {
|
|
10
|
+
type Method = RequestMethod;
|
|
11
|
+
type Result<T> = {
|
|
12
|
+
/**
|
|
13
|
+
* The received Response.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
response: Response | null;
|
|
17
|
+
/**
|
|
18
|
+
* The received Response Headers.
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
headers: Headers | null;
|
|
22
|
+
} & ({
|
|
23
|
+
/** The parsed data. */
|
|
24
|
+
data: T;
|
|
25
|
+
error: null;
|
|
26
|
+
} | {
|
|
27
|
+
/** The parsed error. */
|
|
28
|
+
error: Exception;
|
|
29
|
+
data: null;
|
|
30
|
+
});
|
|
31
|
+
type ReturnDataType<T, U extends RequestInit = RequestInit> = T extends undefined ? U['responseType'] extends 'blob' ? Blob : U['responseType'] extends 'formdata' ? FormData : U['responseType'] extends 'arraybuffer' ? ArrayBuffer : string : T;
|
|
32
|
+
type OnFulfilledCallback<V> = (response: Response) => Fetch.Result<V> | PromiseLike<Fetch.Result<V>>;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/xhr/index.d.ts
|
|
36
|
+
declare class Xhr<TReturn = unknown> extends EventEmitter<XHR.Event.Map<TReturn>> {
|
|
37
|
+
/**
|
|
38
|
+
* The XMLHttpRequest instance.
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
readonly request: XMLHttpRequest;
|
|
42
|
+
/**
|
|
43
|
+
* The current request UUID.
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
protected _requestId: UUID;
|
|
47
|
+
/**
|
|
48
|
+
* The current request UUID.
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
get requestId(): typeof this._requestId;
|
|
52
|
+
protected set requestId(value: typeof this._requestId);
|
|
53
|
+
/**
|
|
54
|
+
* Enables debug logging.
|
|
55
|
+
*
|
|
56
|
+
* @default false
|
|
57
|
+
*/
|
|
58
|
+
protected _debug: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Enables debug logging.
|
|
61
|
+
*
|
|
62
|
+
* @default false
|
|
63
|
+
*/
|
|
64
|
+
get debug(): typeof this._debug;
|
|
65
|
+
protected set debug(value: typeof this._debug);
|
|
66
|
+
/**
|
|
67
|
+
* The AbortController used to cancel network activities.
|
|
68
|
+
*
|
|
69
|
+
*/
|
|
70
|
+
protected _controller?: AbortController<ErrorCode>;
|
|
71
|
+
/**
|
|
72
|
+
* The AbortController used to cancel network activities.
|
|
73
|
+
*
|
|
74
|
+
*/
|
|
75
|
+
get controller(): typeof this._controller;
|
|
76
|
+
protected set controller(value: typeof this._controller);
|
|
77
|
+
/**
|
|
78
|
+
* The Xhr.timeout property is an unsigned long representing the number of milliseconds a request can take before
|
|
79
|
+
* automatically being terminated. The default value is 0, which means there is no timeout.
|
|
80
|
+
* When a timeout happens, a timeout event is fired.
|
|
81
|
+
*
|
|
82
|
+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/timeout)
|
|
83
|
+
*
|
|
84
|
+
* @default 0
|
|
85
|
+
*/
|
|
86
|
+
protected _timeout: number | null;
|
|
87
|
+
/**
|
|
88
|
+
* The Xhr.timeout property is an unsigned long representing the number of milliseconds a request can take before
|
|
89
|
+
* automatically being terminated. The default value is 0, which means there is no timeout.
|
|
90
|
+
* When a timeout happens, a timeout event is fired.
|
|
91
|
+
*
|
|
92
|
+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/timeout)
|
|
93
|
+
*
|
|
94
|
+
* @default 0
|
|
95
|
+
*/
|
|
96
|
+
get timeout(): typeof this._timeout;
|
|
97
|
+
protected set timeout(value: typeof this._timeout);
|
|
98
|
+
/**
|
|
99
|
+
* An object with `username` and `password` to send along with the request.
|
|
100
|
+
*
|
|
101
|
+
*/
|
|
102
|
+
protected _credentials?: XHR.Credentials | null;
|
|
103
|
+
/**
|
|
104
|
+
* An object with `username` and `password` to send along with the request.
|
|
105
|
+
*
|
|
106
|
+
*/
|
|
107
|
+
get credentials(): typeof this._credentials;
|
|
108
|
+
protected set credentials(value: typeof this._credentials);
|
|
109
|
+
/**
|
|
110
|
+
* The Xhr.withCredentials property is a boolean value that indicates whether or not cross-site Access-Control
|
|
111
|
+
* requests should be made using credentials such as cookies, authentication headers
|
|
112
|
+
* or TLS client certificates. Setting `withCredentials` has no effect on same-origin requests.
|
|
113
|
+
*
|
|
114
|
+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/withCredentials)
|
|
115
|
+
*
|
|
116
|
+
* @default false
|
|
117
|
+
*/
|
|
118
|
+
protected _withCredentials: boolean | null;
|
|
119
|
+
/**
|
|
120
|
+
* The Xhr.withCredentials property is a boolean value that indicates whether or not cross-site Access-Control
|
|
121
|
+
* requests should be made using credentials such as cookies, authentication headers
|
|
122
|
+
* or TLS client certificates. Setting `withCredentials` has no effect on same-origin requests.
|
|
123
|
+
*
|
|
124
|
+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/withCredentials)
|
|
125
|
+
*
|
|
126
|
+
* @default false
|
|
127
|
+
*/
|
|
128
|
+
get withCredentials(): typeof this._withCredentials;
|
|
129
|
+
protected set withCredentials(value: typeof this._withCredentials);
|
|
130
|
+
/**
|
|
131
|
+
* Indicates whether to automatically set the XHR response type based on the Response Content-Type Header.
|
|
132
|
+
*
|
|
133
|
+
*/
|
|
134
|
+
private autoResponseType;
|
|
135
|
+
/**
|
|
136
|
+
* Indicates whther event listeners have been registered.
|
|
137
|
+
*
|
|
138
|
+
*/
|
|
139
|
+
private listenersRegistered;
|
|
140
|
+
/** XHR progress events. */
|
|
141
|
+
static readonly ProgressEvents: XHR.Event.Progress[];
|
|
142
|
+
private static MapReadyStateToXhrEvent;
|
|
143
|
+
/**
|
|
144
|
+
* Construct a new instance of Xhr.
|
|
145
|
+
*
|
|
146
|
+
* @param options Required options for a proper initialization.
|
|
147
|
+
*/
|
|
148
|
+
constructor(options?: XHR.ConstructOptions);
|
|
149
|
+
/**
|
|
150
|
+
* Initialise XMLHttpRequest.
|
|
151
|
+
*
|
|
152
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
153
|
+
*/
|
|
154
|
+
private init;
|
|
155
|
+
/**
|
|
156
|
+
* Send XMLHttpRequest.
|
|
157
|
+
*
|
|
158
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
159
|
+
*/
|
|
160
|
+
send<T = TReturn>(options: XHR.Options): Xhr<T>;
|
|
161
|
+
/**
|
|
162
|
+
* Upload files via XMLHttpRequest.
|
|
163
|
+
*
|
|
164
|
+
* Use `Xhr.request.upload` to listen upload events.
|
|
165
|
+
*
|
|
166
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
167
|
+
*/
|
|
168
|
+
upload<T = TReturn>(options: XHR.UploadOptions): Xhr<T>;
|
|
169
|
+
/**
|
|
170
|
+
* Get formatted response.
|
|
171
|
+
*
|
|
172
|
+
* @returns The response text or an object from a json response.
|
|
173
|
+
*/
|
|
174
|
+
getResponse<T = TReturn>(): T | null | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* Cancels any network activity.
|
|
177
|
+
*
|
|
178
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
179
|
+
*/
|
|
180
|
+
abort<T = ErrorCode, U = TReturn>(reason?: string, options?: AbortErrorOptions<T>): Xhr<U>;
|
|
181
|
+
/**
|
|
182
|
+
* Set the request headers.
|
|
183
|
+
*
|
|
184
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
185
|
+
*/
|
|
186
|
+
setRequestHeaders<T = TReturn>(headers: Headers | HeadersInit): Xhr<T>;
|
|
187
|
+
/**
|
|
188
|
+
* Get Response Header.
|
|
189
|
+
*
|
|
190
|
+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/getResponseHeader)
|
|
191
|
+
*
|
|
192
|
+
* @param name The Header name.
|
|
193
|
+
* @returns The Response Header value, `null` if none has been found.
|
|
194
|
+
*/
|
|
195
|
+
getResponseHeader<T = TReturn>(name: string): string | null;
|
|
196
|
+
/**
|
|
197
|
+
* Cancel any network activity an unregister all listeners.
|
|
198
|
+
*
|
|
199
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
200
|
+
*/
|
|
201
|
+
destroy<T = TReturn>(): Xhr<T>;
|
|
202
|
+
/**
|
|
203
|
+
* Prints to `stdout` with newline.
|
|
204
|
+
*
|
|
205
|
+
* @param data The data print out.
|
|
206
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
207
|
+
*/
|
|
208
|
+
log<T = TReturn>(...data: any[]): Xhr<T>;
|
|
209
|
+
/**
|
|
210
|
+
* On success functions.
|
|
211
|
+
*
|
|
212
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
213
|
+
*/
|
|
214
|
+
private success;
|
|
215
|
+
/**
|
|
216
|
+
* On error functions.
|
|
217
|
+
*
|
|
218
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
219
|
+
*/
|
|
220
|
+
private error;
|
|
221
|
+
/**
|
|
222
|
+
* Get XMLHttpRequest not available error.
|
|
223
|
+
*
|
|
224
|
+
* @returns The newly created error with `ErrorCode.XHR_NOT_AVAILABLE` code.
|
|
225
|
+
*/
|
|
226
|
+
private getXhrNotAvailableError;
|
|
227
|
+
private setupAbortController;
|
|
228
|
+
/**
|
|
229
|
+
* Automatically set `XMLHttpRequestResponseType` based on the received Response `Content-Type` Header.
|
|
230
|
+
*
|
|
231
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
232
|
+
*/
|
|
233
|
+
private setResponseTypeFromResponseHeaders;
|
|
234
|
+
/**
|
|
235
|
+
* Convert MIME `Content-Type` to `XMLHttpRequestResponseType`.
|
|
236
|
+
*
|
|
237
|
+
* @param contentType (Optional) The MIME `Content-Type`. Default: `Content-Type` Response Header.
|
|
238
|
+
* @returns The `XMLHttpRequestResponseType` based on the given MIME `Content-Type`.
|
|
239
|
+
*/
|
|
240
|
+
private contentTypeToResponseType;
|
|
241
|
+
/**
|
|
242
|
+
* Get `Content-Type` Response Header.
|
|
243
|
+
*
|
|
244
|
+
* @returns The Response Header value, `null` if none has been found.
|
|
245
|
+
*/
|
|
246
|
+
getResponseContentType(): string | null;
|
|
247
|
+
/**
|
|
248
|
+
* Handle `readystatechange` events.
|
|
249
|
+
*
|
|
250
|
+
* @param event The Event interface.
|
|
251
|
+
*/
|
|
252
|
+
private onReadyStateChange;
|
|
253
|
+
/**
|
|
254
|
+
* Handle and emit progress events.
|
|
255
|
+
*
|
|
256
|
+
* @param event The ProgressEvent interface.
|
|
257
|
+
*/
|
|
258
|
+
private onProgressChange;
|
|
259
|
+
/**
|
|
260
|
+
* Executed when `Xhr.controller` get aborted through `Xhr.abort()` or `Xhr.controller.abort()` methods.
|
|
261
|
+
*
|
|
262
|
+
*/
|
|
263
|
+
private onControllerSignalAbort;
|
|
264
|
+
/**
|
|
265
|
+
* Emit ReadyStateChange events.
|
|
266
|
+
*
|
|
267
|
+
*/
|
|
268
|
+
private emitReadyStateEvent;
|
|
269
|
+
/**
|
|
270
|
+
* Add listeners to the XMLHttpRequest instance.
|
|
271
|
+
*
|
|
272
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
273
|
+
*/
|
|
274
|
+
private registerListeners;
|
|
275
|
+
/**
|
|
276
|
+
* Remove listeners attached to the XMLHttpRequest instance.
|
|
277
|
+
*
|
|
278
|
+
* @returns The current `Xhr` instance for chaining purposes.
|
|
279
|
+
*/
|
|
280
|
+
private unregisterListeners;
|
|
281
|
+
}
|
|
282
|
+
//#endregion
|
|
283
|
+
//#region src/xhr/types.d.ts
|
|
284
|
+
declare namespace XHR {
|
|
285
|
+
type Method = RequestMethod;
|
|
286
|
+
type ReadyState = typeof XMLHttpRequest.UNSENT | typeof XMLHttpRequest.OPENED | typeof XMLHttpRequest.HEADERS_RECEIVED | typeof XMLHttpRequest.LOADING | typeof XMLHttpRequest.DONE;
|
|
287
|
+
type Credentials = {
|
|
288
|
+
/**
|
|
289
|
+
* The optional user name to use for authentication purposes.
|
|
290
|
+
*
|
|
291
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open#user)
|
|
292
|
+
*/
|
|
293
|
+
username?: string | null;
|
|
294
|
+
/**
|
|
295
|
+
* The optional user password to use for authentication purposes.
|
|
296
|
+
*
|
|
297
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open#password)
|
|
298
|
+
*/
|
|
299
|
+
password?: string | null;
|
|
300
|
+
};
|
|
301
|
+
namespace Event {
|
|
302
|
+
type Progress = keyof XMLHttpRequestEventTargetEventMap;
|
|
303
|
+
type ReadyStateChange = 'readystatechange' | 'unsent' | 'opened' | 'headers' | 'loading' | 'done';
|
|
304
|
+
type Map<TReturn = unknown> = {
|
|
305
|
+
init: [xhr: Xhr<TReturn>];
|
|
306
|
+
send: [xhr: Xhr<TReturn>];
|
|
307
|
+
success: [response: TReturn | null | undefined, xhr: Xhr<TReturn>];
|
|
308
|
+
error: [error: Exception<ErrorCode>, xhr: Xhr<TReturn>];
|
|
309
|
+
abort: [reason: AbortError<ErrorCode>, event: ProgressEvent<XMLHttpRequestEventTarget>, xhr: Xhr<TReturn>];
|
|
310
|
+
} & { [event in ReadyStateChange]: [event: Event, xhr: Xhr<TReturn>]; } & { [event in Exclude<XHR.Event.Progress, 'abort' | 'error'>]: [event: ProgressEvent<XMLHttpRequestEventTarget>, xhr: Xhr<TReturn>]; };
|
|
311
|
+
type Listener<TEvent extends keyof XHR.Event.Map<TReturn>, TReturn = unknown> = Listener<XHR.Event.Map<TReturn>, TEvent>;
|
|
312
|
+
}
|
|
313
|
+
interface ConstructOptions {
|
|
314
|
+
/**
|
|
315
|
+
* Enables debug logging.
|
|
316
|
+
*
|
|
317
|
+
* @default false
|
|
318
|
+
*/
|
|
319
|
+
debug?: boolean;
|
|
320
|
+
/**
|
|
321
|
+
* Automatically set the `responseType` from Response Content-Type Header if set to `true` and `responseType` hasn't been set.
|
|
322
|
+
*
|
|
323
|
+
* @default true
|
|
324
|
+
*/
|
|
325
|
+
autoResponseType?: boolean;
|
|
326
|
+
/**
|
|
327
|
+
* The Xhr.withCredentials property is a boolean value that indicates whether or not cross-site Access-Control
|
|
328
|
+
* requests should be made using credentials such as cookies, authentication headers
|
|
329
|
+
* or TLS client certificates. Setting `withCredentials` has no effect on same-origin requests.
|
|
330
|
+
*
|
|
331
|
+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/withCredentials)
|
|
332
|
+
*
|
|
333
|
+
* @default false
|
|
334
|
+
*/
|
|
335
|
+
withCredentials?: XMLHttpRequest['withCredentials'] | null;
|
|
336
|
+
/**
|
|
337
|
+
* An object with `username` and `password` to send along with the request.
|
|
338
|
+
*
|
|
339
|
+
*/
|
|
340
|
+
credentials?: XHR.Credentials | null;
|
|
341
|
+
/**
|
|
342
|
+
* Can be set to a time in milliseconds. When set to a non-zero value will cause fetching to terminate after the given time has passed. When the time has passed, the request has not yet completed, and this's synchronous flag is unset, a timeout event will then be dispatched, or a "TimeoutError" DOMException will be thrown otherwise (for the send() method).
|
|
343
|
+
*
|
|
344
|
+
* When set: throws an "InvalidAccessError" DOMException if the synchronous flag is set and current global object is a Window object.
|
|
345
|
+
*
|
|
346
|
+
* @default 0
|
|
347
|
+
*/
|
|
348
|
+
timeout?: XMLHttpRequest['timeout'] | null;
|
|
349
|
+
}
|
|
350
|
+
interface InitOptions extends XHR.ConstructOptions {
|
|
351
|
+
/**
|
|
352
|
+
* The URL string or URL object the request is made to.
|
|
353
|
+
*
|
|
354
|
+
*/
|
|
355
|
+
url: UrlInput;
|
|
356
|
+
/**
|
|
357
|
+
* The request method.
|
|
358
|
+
*
|
|
359
|
+
* @default 'GET'
|
|
360
|
+
*/
|
|
361
|
+
method?: RequestMethod;
|
|
362
|
+
/**
|
|
363
|
+
* The request HeadersInit object.
|
|
364
|
+
*
|
|
365
|
+
*/
|
|
366
|
+
headers?: Headers | HeadersInit;
|
|
367
|
+
/**
|
|
368
|
+
* A custom controller object that allows you to abort one or more DOM requests as and when desired.
|
|
369
|
+
*
|
|
370
|
+
*/
|
|
371
|
+
controller?: AbortController<ErrorCode>;
|
|
372
|
+
/**
|
|
373
|
+
* The expected Response Type.
|
|
374
|
+
*
|
|
375
|
+
*/
|
|
376
|
+
responseType?: XMLHttpRequestResponseType;
|
|
377
|
+
}
|
|
378
|
+
interface Options extends Omit<XHR.InitOptions, 'eventsTarget'> {
|
|
379
|
+
/**
|
|
380
|
+
* The body object to send.
|
|
381
|
+
*
|
|
382
|
+
*/
|
|
383
|
+
body?: Document | XMLHttpRequestBodyInit;
|
|
384
|
+
}
|
|
385
|
+
interface UploadOptions extends Omit<XHR.Options, 'body'> {
|
|
386
|
+
/**
|
|
387
|
+
* The HTMLInputElement `FileList` or an array of `File` to upload.
|
|
388
|
+
*
|
|
389
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/files)
|
|
390
|
+
*/
|
|
391
|
+
files: File[] | FileList;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
//#endregion
|
|
395
|
+
//#region src/types/index.d.ts
|
|
396
|
+
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
397
|
+
//#endregion
|
|
398
|
+
export { Fetch as i, XHR as n, Xhr as r, RequestMethod as t };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ValueOf } from "@alessiofrittoli/type-utils";
|
|
2
|
+
//#region src/error/index.d.ts
|
|
3
|
+
declare const Request: {
|
|
4
|
+
readonly XHR_NOT_AVAILABLE: "ERR:XHRNOTAVAILABLE";
|
|
5
|
+
readonly XHR_INVALID_STATE: "ERR:XHRINVALIDSTATE";
|
|
6
|
+
readonly XHR_DESTROYED: "ERR:XHRDESTROYED";
|
|
7
|
+
};
|
|
8
|
+
declare const ErrorCode: {
|
|
9
|
+
readonly XHR_NOT_AVAILABLE: "ERR:XHRNOTAVAILABLE";
|
|
10
|
+
readonly XHR_INVALID_STATE: "ERR:XHRINVALIDSTATE";
|
|
11
|
+
readonly XHR_DESTROYED: "ERR:XHRDESTROYED";
|
|
12
|
+
readonly UNKNOWN: "ERR:UNKNOWN";
|
|
13
|
+
readonly ABORT: "ERR:ABORT";
|
|
14
|
+
readonly EMPTY_VALUE: "ERR:EMPTYVALUE";
|
|
15
|
+
readonly WRONG_VALUE: "ERR:WRONGVALUE";
|
|
16
|
+
readonly EXPIRED: "ERR:EXPIRED";
|
|
17
|
+
readonly TOO_EARLY: "ERR:TOOEARLY";
|
|
18
|
+
readonly TOO_MANY: "ERR:TOOMANY";
|
|
19
|
+
readonly QUOTA_REACHED: "ERR:QUOTAREACHED";
|
|
20
|
+
readonly NOT_FOUND: "ERR:NOTFOUND";
|
|
21
|
+
readonly OFFLINE: "ERR:INTERNETDISCONNECTED";
|
|
22
|
+
};
|
|
23
|
+
type ErrorCode = ValueOf<typeof ErrorCode>;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { Request as n, ErrorCode as t };
|