@alessiofrittoli/fetcher 1.1.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 -7
- package/dist/xhr/index.mjs +1 -1
- package/package.json +127 -156
- package/dist/chunk-HNNMLJIA.js +0 -1
- package/dist/chunk-LOWOZ6LK.mjs +0 -1
- package/dist/error.d.mts +0 -22
- package/dist/error.d.ts +0 -22
- package/dist/error.js +0 -1
- package/dist/error.mjs +0 -1
- package/dist/fetch.d.mts +0 -22
- package/dist/fetch.d.ts +0 -22
- 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-B7TKkr2l.d.ts +0 -227
- package/dist/types-DAKtp5Tp.d.mts +0 -227
- package/dist/types.d.mts +0 -7
- package/dist/types.d.ts +0 -7
- package/dist/types.js +0 -1
- package/dist/xhr/index.d.ts +0 -7
- 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/dist/types-B7TKkr2l.d.ts
DELETED
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import Emitter, { EventEmitter } from '@alessiofrittoli/event-emitter';
|
|
2
|
-
import { Exception } from '@alessiofrittoli/exception';
|
|
3
|
-
import { AbortErrorOptions, AbortError } from '@alessiofrittoli/exception/abort';
|
|
4
|
-
import * as _alessiofrittoli_url_utils from '@alessiofrittoli/url-utils';
|
|
5
|
-
import { UrlInput } from '@alessiofrittoli/url-utils';
|
|
6
|
-
import { AbortController } from '@alessiofrittoli/abort-controller';
|
|
7
|
-
import { ErrorCode } from './error.js';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Easly handle XML HTTP Requests.
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
declare class Xhr<TReturn = unknown> extends EventEmitter<XHR.Event.Map<TReturn>> implements XHR.Options {
|
|
14
|
-
/** The XMLHttpRequest instance. */
|
|
15
|
-
request: XMLHttpRequest;
|
|
16
|
-
url: _alessiofrittoli_url_utils.UrlInput;
|
|
17
|
-
method: RequestMethod;
|
|
18
|
-
body: Document | XMLHttpRequestBodyInit | undefined;
|
|
19
|
-
headers: Headers;
|
|
20
|
-
credentials: {
|
|
21
|
-
username?: string | null;
|
|
22
|
-
password?: string | null;
|
|
23
|
-
} | undefined;
|
|
24
|
-
response?: TReturn;
|
|
25
|
-
controller: AbortController<ErrorCode>;
|
|
26
|
-
debug: boolean;
|
|
27
|
-
/** Indicates whether to automatically set the XHR response type based on the Response Content-Type Header. */
|
|
28
|
-
private setResponseType;
|
|
29
|
-
private static NotAvailableErr;
|
|
30
|
-
/** XHR progress events. */
|
|
31
|
-
static readonly ProgressEvents: XHR.Event.Progress[];
|
|
32
|
-
/**
|
|
33
|
-
* Construct a new instance of Xhr.
|
|
34
|
-
*
|
|
35
|
-
* @param options Required options for a proper initialization.
|
|
36
|
-
*/
|
|
37
|
-
constructor(options: XHR.Options);
|
|
38
|
-
/**
|
|
39
|
-
* Initialise XMLHttpRequest.
|
|
40
|
-
*
|
|
41
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
42
|
-
*/
|
|
43
|
-
init(options?: XHR.InitOptions): this;
|
|
44
|
-
/**
|
|
45
|
-
* Send XMLHttpRequest.
|
|
46
|
-
*
|
|
47
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
48
|
-
*/
|
|
49
|
-
send(): Promise<this>;
|
|
50
|
-
/**
|
|
51
|
-
* Cancels any network activity.
|
|
52
|
-
*
|
|
53
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
54
|
-
*/
|
|
55
|
-
abort(reason?: string, options?: AbortErrorOptions): this;
|
|
56
|
-
/**
|
|
57
|
-
* Executed when `Xhr.controller` get aborted through `Xhr.abort()` or `Xhr.controller.abort()` methods.
|
|
58
|
-
*
|
|
59
|
-
*/
|
|
60
|
-
private controllerAbortListener;
|
|
61
|
-
/**
|
|
62
|
-
* Handle `readystatechange` events.
|
|
63
|
-
*
|
|
64
|
-
* @param event The Event interface.
|
|
65
|
-
*/
|
|
66
|
-
private readyStateChangeHandler;
|
|
67
|
-
/**
|
|
68
|
-
* Handle and emit progress events.
|
|
69
|
-
*
|
|
70
|
-
* See {@link Xhr.ProgressEvents} for a list of listened events.
|
|
71
|
-
*
|
|
72
|
-
* @param event The ProgressEvent interface.
|
|
73
|
-
*/
|
|
74
|
-
private progressEventHandler;
|
|
75
|
-
/**
|
|
76
|
-
* Emit success or error events based on the received Response on Request Load End.
|
|
77
|
-
*
|
|
78
|
-
*/
|
|
79
|
-
private loadEndHandler;
|
|
80
|
-
/**
|
|
81
|
-
* Add listeners to the XMLHttpRequest instance.
|
|
82
|
-
*
|
|
83
|
-
* See {@link Xhr.ProgressEvents} for a list of listened events.
|
|
84
|
-
*
|
|
85
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
86
|
-
*/
|
|
87
|
-
private addProgressListeners;
|
|
88
|
-
/**
|
|
89
|
-
* Set the request headers.
|
|
90
|
-
*
|
|
91
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
92
|
-
*/
|
|
93
|
-
private setHeaders;
|
|
94
|
-
/**
|
|
95
|
-
* Convert MIME `Content-Type` to `XMLHttpRequestResponseType`.
|
|
96
|
-
*
|
|
97
|
-
* @param contentType (Optional) The MIME `Content-Type`. Default: `Content-Type` Response Header.
|
|
98
|
-
* @returns The `XMLHttpRequestResponseType` based on the given MIME `Content-Type`.
|
|
99
|
-
*/
|
|
100
|
-
private contentTypeToResponseType;
|
|
101
|
-
/**
|
|
102
|
-
* Automatically set `XMLHttpRequestResponseType` based on the received Response `Content-Type` Header.
|
|
103
|
-
*
|
|
104
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
105
|
-
*/
|
|
106
|
-
private setResponseTypeFromResponseHeaders;
|
|
107
|
-
/**
|
|
108
|
-
* Get formatted response.
|
|
109
|
-
*
|
|
110
|
-
* @returns The response text or an object from a json response.
|
|
111
|
-
*/
|
|
112
|
-
getResponse(): TReturn | undefined;
|
|
113
|
-
/**
|
|
114
|
-
* Get `Content-Type` Response Header.
|
|
115
|
-
*
|
|
116
|
-
* @returns The Response Header value, `null` if none has been found.
|
|
117
|
-
*/
|
|
118
|
-
getResponseContentType(): string | null;
|
|
119
|
-
/**
|
|
120
|
-
* Get Response Header.
|
|
121
|
-
*
|
|
122
|
-
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/getResponseHeader)
|
|
123
|
-
*
|
|
124
|
-
* @param name The Header name.
|
|
125
|
-
* @returns The Response Header value, `null` if none has been found.
|
|
126
|
-
*/
|
|
127
|
-
getResponseHeader(name: string): string | null;
|
|
128
|
-
/**
|
|
129
|
-
* On success functions.
|
|
130
|
-
*
|
|
131
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
132
|
-
*/
|
|
133
|
-
private success;
|
|
134
|
-
/**
|
|
135
|
-
* On error functions.
|
|
136
|
-
*
|
|
137
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
138
|
-
*/
|
|
139
|
-
private error;
|
|
140
|
-
/**
|
|
141
|
-
* Prints to `stdout` with newline.
|
|
142
|
-
*
|
|
143
|
-
* @param data The data print out.
|
|
144
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
145
|
-
*/
|
|
146
|
-
log(data: Record<'message', string> & Record<string, any>): this;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
150
|
-
declare namespace Fetch {
|
|
151
|
-
type Result<T> = {
|
|
152
|
-
/** The received Response. */
|
|
153
|
-
response: Response | null;
|
|
154
|
-
/** The received Response Headers. */
|
|
155
|
-
headers: Headers | null;
|
|
156
|
-
} & ({
|
|
157
|
-
/** The parsed data. */
|
|
158
|
-
data: T;
|
|
159
|
-
error: null;
|
|
160
|
-
} | {
|
|
161
|
-
/** The parsed error. */
|
|
162
|
-
error: Exception;
|
|
163
|
-
data: null;
|
|
164
|
-
});
|
|
165
|
-
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);
|
|
166
|
-
type OnFulfilledCallback<V> = (response: Response) => Fetch.Result<V> | PromiseLike<Fetch.Result<V>>;
|
|
167
|
-
}
|
|
168
|
-
declare namespace XHR {
|
|
169
|
-
namespace Event {
|
|
170
|
-
type Progress = keyof XMLHttpRequestEventTargetEventMap;
|
|
171
|
-
type Map<TReturn = unknown> = {
|
|
172
|
-
init: [xhr: Xhr<TReturn>];
|
|
173
|
-
send: [xhr: Xhr<TReturn>];
|
|
174
|
-
success: [response: TReturn, xhr: Xhr<TReturn>];
|
|
175
|
-
error: [error: Exception<string, ErrorCode>, xhr: Xhr<TReturn>];
|
|
176
|
-
abort: [reason: AbortError<string, ErrorCode>, event: ProgressEvent<XMLHttpRequestEventTarget>, xhr: Xhr<TReturn>];
|
|
177
|
-
readystatechange: [event: Event, xhr: Xhr<TReturn>];
|
|
178
|
-
} & {
|
|
179
|
-
[event in Exclude<XHR.Event.Progress, 'abort' | 'error'>]: [event: ProgressEvent<XMLHttpRequestEventTarget>, xhr: Xhr<TReturn>];
|
|
180
|
-
};
|
|
181
|
-
type Listener<TEvent extends keyof XHR.Event.Map<TReturn>, TReturn = unknown> = (Emitter.Listener<XHR.Event.Map<TReturn>, TEvent>);
|
|
182
|
-
}
|
|
183
|
-
interface InitOptions {
|
|
184
|
-
/**
|
|
185
|
-
* A custom controller object that allows you to abort one or more DOM requests as and when desired.
|
|
186
|
-
*/
|
|
187
|
-
controller?: AbortController<ErrorCode>;
|
|
188
|
-
}
|
|
189
|
-
interface Options extends XHR.InitOptions {
|
|
190
|
-
/** The URL string or URL object. */
|
|
191
|
-
url: UrlInput;
|
|
192
|
-
/** The request method. Default: `GET`. */
|
|
193
|
-
method?: RequestMethod;
|
|
194
|
-
/** The expected Response Type. */
|
|
195
|
-
responseType?: XMLHttpRequestResponseType;
|
|
196
|
-
/** Automatically set the `responseType` from Response Content-Type Header if set to `true` and `responseType` hasn't been set. Default: `true`. */
|
|
197
|
-
autoResponseType?: boolean;
|
|
198
|
-
/** The body object to send. */
|
|
199
|
-
body?: Document | XMLHttpRequestBodyInit;
|
|
200
|
-
/** The request HeadersInit object. */
|
|
201
|
-
headers?: Headers | HeadersInit;
|
|
202
|
-
/**
|
|
203
|
-
* True when credentials are to be included in a cross-origin request. False when they are to be excluded in a cross-origin request and when cookies are to be ignored in its response. Initially false.
|
|
204
|
-
*
|
|
205
|
-
* When set: throws an "InvalidStateError" DOMException if state is not unsent or opened, or if the send() flag is set.
|
|
206
|
-
*/
|
|
207
|
-
withCredentials?: XMLHttpRequest['withCredentials'];
|
|
208
|
-
/**
|
|
209
|
-
* An object with `username` and `password` to send along with the request.
|
|
210
|
-
*
|
|
211
|
-
*/
|
|
212
|
-
credentials?: {
|
|
213
|
-
username?: string | null;
|
|
214
|
-
password?: string | null;
|
|
215
|
-
};
|
|
216
|
-
/**
|
|
217
|
-
* 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).
|
|
218
|
-
*
|
|
219
|
-
* When set: throws an "InvalidAccessError" DOMException if the synchronous flag is set and current global object is a Window object.
|
|
220
|
-
*/
|
|
221
|
-
timeout?: XMLHttpRequest['timeout'];
|
|
222
|
-
/** Whether to `console.log` data on different events. */
|
|
223
|
-
debug?: boolean;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
export { Fetch as F, type RequestMethod as R, Xhr as X, XHR as a };
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import Emitter, { EventEmitter } from '@alessiofrittoli/event-emitter';
|
|
2
|
-
import { Exception } from '@alessiofrittoli/exception';
|
|
3
|
-
import { AbortErrorOptions, AbortError } from '@alessiofrittoli/exception/abort';
|
|
4
|
-
import * as _alessiofrittoli_url_utils from '@alessiofrittoli/url-utils';
|
|
5
|
-
import { UrlInput } from '@alessiofrittoli/url-utils';
|
|
6
|
-
import { AbortController } from '@alessiofrittoli/abort-controller';
|
|
7
|
-
import { ErrorCode } from './error.mjs';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Easly handle XML HTTP Requests.
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
declare class Xhr<TReturn = unknown> extends EventEmitter<XHR.Event.Map<TReturn>> implements XHR.Options {
|
|
14
|
-
/** The XMLHttpRequest instance. */
|
|
15
|
-
request: XMLHttpRequest;
|
|
16
|
-
url: _alessiofrittoli_url_utils.UrlInput;
|
|
17
|
-
method: RequestMethod;
|
|
18
|
-
body: Document | XMLHttpRequestBodyInit | undefined;
|
|
19
|
-
headers: Headers;
|
|
20
|
-
credentials: {
|
|
21
|
-
username?: string | null;
|
|
22
|
-
password?: string | null;
|
|
23
|
-
} | undefined;
|
|
24
|
-
response?: TReturn;
|
|
25
|
-
controller: AbortController<ErrorCode>;
|
|
26
|
-
debug: boolean;
|
|
27
|
-
/** Indicates whether to automatically set the XHR response type based on the Response Content-Type Header. */
|
|
28
|
-
private setResponseType;
|
|
29
|
-
private static NotAvailableErr;
|
|
30
|
-
/** XHR progress events. */
|
|
31
|
-
static readonly ProgressEvents: XHR.Event.Progress[];
|
|
32
|
-
/**
|
|
33
|
-
* Construct a new instance of Xhr.
|
|
34
|
-
*
|
|
35
|
-
* @param options Required options for a proper initialization.
|
|
36
|
-
*/
|
|
37
|
-
constructor(options: XHR.Options);
|
|
38
|
-
/**
|
|
39
|
-
* Initialise XMLHttpRequest.
|
|
40
|
-
*
|
|
41
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
42
|
-
*/
|
|
43
|
-
init(options?: XHR.InitOptions): this;
|
|
44
|
-
/**
|
|
45
|
-
* Send XMLHttpRequest.
|
|
46
|
-
*
|
|
47
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
48
|
-
*/
|
|
49
|
-
send(): Promise<this>;
|
|
50
|
-
/**
|
|
51
|
-
* Cancels any network activity.
|
|
52
|
-
*
|
|
53
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
54
|
-
*/
|
|
55
|
-
abort(reason?: string, options?: AbortErrorOptions): this;
|
|
56
|
-
/**
|
|
57
|
-
* Executed when `Xhr.controller` get aborted through `Xhr.abort()` or `Xhr.controller.abort()` methods.
|
|
58
|
-
*
|
|
59
|
-
*/
|
|
60
|
-
private controllerAbortListener;
|
|
61
|
-
/**
|
|
62
|
-
* Handle `readystatechange` events.
|
|
63
|
-
*
|
|
64
|
-
* @param event The Event interface.
|
|
65
|
-
*/
|
|
66
|
-
private readyStateChangeHandler;
|
|
67
|
-
/**
|
|
68
|
-
* Handle and emit progress events.
|
|
69
|
-
*
|
|
70
|
-
* See {@link Xhr.ProgressEvents} for a list of listened events.
|
|
71
|
-
*
|
|
72
|
-
* @param event The ProgressEvent interface.
|
|
73
|
-
*/
|
|
74
|
-
private progressEventHandler;
|
|
75
|
-
/**
|
|
76
|
-
* Emit success or error events based on the received Response on Request Load End.
|
|
77
|
-
*
|
|
78
|
-
*/
|
|
79
|
-
private loadEndHandler;
|
|
80
|
-
/**
|
|
81
|
-
* Add listeners to the XMLHttpRequest instance.
|
|
82
|
-
*
|
|
83
|
-
* See {@link Xhr.ProgressEvents} for a list of listened events.
|
|
84
|
-
*
|
|
85
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
86
|
-
*/
|
|
87
|
-
private addProgressListeners;
|
|
88
|
-
/**
|
|
89
|
-
* Set the request headers.
|
|
90
|
-
*
|
|
91
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
92
|
-
*/
|
|
93
|
-
private setHeaders;
|
|
94
|
-
/**
|
|
95
|
-
* Convert MIME `Content-Type` to `XMLHttpRequestResponseType`.
|
|
96
|
-
*
|
|
97
|
-
* @param contentType (Optional) The MIME `Content-Type`. Default: `Content-Type` Response Header.
|
|
98
|
-
* @returns The `XMLHttpRequestResponseType` based on the given MIME `Content-Type`.
|
|
99
|
-
*/
|
|
100
|
-
private contentTypeToResponseType;
|
|
101
|
-
/**
|
|
102
|
-
* Automatically set `XMLHttpRequestResponseType` based on the received Response `Content-Type` Header.
|
|
103
|
-
*
|
|
104
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
105
|
-
*/
|
|
106
|
-
private setResponseTypeFromResponseHeaders;
|
|
107
|
-
/**
|
|
108
|
-
* Get formatted response.
|
|
109
|
-
*
|
|
110
|
-
* @returns The response text or an object from a json response.
|
|
111
|
-
*/
|
|
112
|
-
getResponse(): TReturn | undefined;
|
|
113
|
-
/**
|
|
114
|
-
* Get `Content-Type` Response Header.
|
|
115
|
-
*
|
|
116
|
-
* @returns The Response Header value, `null` if none has been found.
|
|
117
|
-
*/
|
|
118
|
-
getResponseContentType(): string | null;
|
|
119
|
-
/**
|
|
120
|
-
* Get Response Header.
|
|
121
|
-
*
|
|
122
|
-
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/getResponseHeader)
|
|
123
|
-
*
|
|
124
|
-
* @param name The Header name.
|
|
125
|
-
* @returns The Response Header value, `null` if none has been found.
|
|
126
|
-
*/
|
|
127
|
-
getResponseHeader(name: string): string | null;
|
|
128
|
-
/**
|
|
129
|
-
* On success functions.
|
|
130
|
-
*
|
|
131
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
132
|
-
*/
|
|
133
|
-
private success;
|
|
134
|
-
/**
|
|
135
|
-
* On error functions.
|
|
136
|
-
*
|
|
137
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
138
|
-
*/
|
|
139
|
-
private error;
|
|
140
|
-
/**
|
|
141
|
-
* Prints to `stdout` with newline.
|
|
142
|
-
*
|
|
143
|
-
* @param data The data print out.
|
|
144
|
-
* @returns The current `Xhr` instance for chaining purposes.
|
|
145
|
-
*/
|
|
146
|
-
log(data: Record<'message', string> & Record<string, any>): this;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
150
|
-
declare namespace Fetch {
|
|
151
|
-
type Result<T> = {
|
|
152
|
-
/** The received Response. */
|
|
153
|
-
response: Response | null;
|
|
154
|
-
/** The received Response Headers. */
|
|
155
|
-
headers: Headers | null;
|
|
156
|
-
} & ({
|
|
157
|
-
/** The parsed data. */
|
|
158
|
-
data: T;
|
|
159
|
-
error: null;
|
|
160
|
-
} | {
|
|
161
|
-
/** The parsed error. */
|
|
162
|
-
error: Exception;
|
|
163
|
-
data: null;
|
|
164
|
-
});
|
|
165
|
-
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);
|
|
166
|
-
type OnFulfilledCallback<V> = (response: Response) => Fetch.Result<V> | PromiseLike<Fetch.Result<V>>;
|
|
167
|
-
}
|
|
168
|
-
declare namespace XHR {
|
|
169
|
-
namespace Event {
|
|
170
|
-
type Progress = keyof XMLHttpRequestEventTargetEventMap;
|
|
171
|
-
type Map<TReturn = unknown> = {
|
|
172
|
-
init: [xhr: Xhr<TReturn>];
|
|
173
|
-
send: [xhr: Xhr<TReturn>];
|
|
174
|
-
success: [response: TReturn, xhr: Xhr<TReturn>];
|
|
175
|
-
error: [error: Exception<string, ErrorCode>, xhr: Xhr<TReturn>];
|
|
176
|
-
abort: [reason: AbortError<string, ErrorCode>, event: ProgressEvent<XMLHttpRequestEventTarget>, xhr: Xhr<TReturn>];
|
|
177
|
-
readystatechange: [event: Event, xhr: Xhr<TReturn>];
|
|
178
|
-
} & {
|
|
179
|
-
[event in Exclude<XHR.Event.Progress, 'abort' | 'error'>]: [event: ProgressEvent<XMLHttpRequestEventTarget>, xhr: Xhr<TReturn>];
|
|
180
|
-
};
|
|
181
|
-
type Listener<TEvent extends keyof XHR.Event.Map<TReturn>, TReturn = unknown> = (Emitter.Listener<XHR.Event.Map<TReturn>, TEvent>);
|
|
182
|
-
}
|
|
183
|
-
interface InitOptions {
|
|
184
|
-
/**
|
|
185
|
-
* A custom controller object that allows you to abort one or more DOM requests as and when desired.
|
|
186
|
-
*/
|
|
187
|
-
controller?: AbortController<ErrorCode>;
|
|
188
|
-
}
|
|
189
|
-
interface Options extends XHR.InitOptions {
|
|
190
|
-
/** The URL string or URL object. */
|
|
191
|
-
url: UrlInput;
|
|
192
|
-
/** The request method. Default: `GET`. */
|
|
193
|
-
method?: RequestMethod;
|
|
194
|
-
/** The expected Response Type. */
|
|
195
|
-
responseType?: XMLHttpRequestResponseType;
|
|
196
|
-
/** Automatically set the `responseType` from Response Content-Type Header if set to `true` and `responseType` hasn't been set. Default: `true`. */
|
|
197
|
-
autoResponseType?: boolean;
|
|
198
|
-
/** The body object to send. */
|
|
199
|
-
body?: Document | XMLHttpRequestBodyInit;
|
|
200
|
-
/** The request HeadersInit object. */
|
|
201
|
-
headers?: Headers | HeadersInit;
|
|
202
|
-
/**
|
|
203
|
-
* True when credentials are to be included in a cross-origin request. False when they are to be excluded in a cross-origin request and when cookies are to be ignored in its response. Initially false.
|
|
204
|
-
*
|
|
205
|
-
* When set: throws an "InvalidStateError" DOMException if state is not unsent or opened, or if the send() flag is set.
|
|
206
|
-
*/
|
|
207
|
-
withCredentials?: XMLHttpRequest['withCredentials'];
|
|
208
|
-
/**
|
|
209
|
-
* An object with `username` and `password` to send along with the request.
|
|
210
|
-
*
|
|
211
|
-
*/
|
|
212
|
-
credentials?: {
|
|
213
|
-
username?: string | null;
|
|
214
|
-
password?: string | null;
|
|
215
|
-
};
|
|
216
|
-
/**
|
|
217
|
-
* 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).
|
|
218
|
-
*
|
|
219
|
-
* When set: throws an "InvalidAccessError" DOMException if the synchronous flag is set and current global object is a Window object.
|
|
220
|
-
*/
|
|
221
|
-
timeout?: XMLHttpRequest['timeout'];
|
|
222
|
-
/** Whether to `console.log` data on different events. */
|
|
223
|
-
debug?: boolean;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
export { Fetch as F, type RequestMethod as R, Xhr as X, XHR as a };
|
package/dist/types.d.mts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import '@alessiofrittoli/event-emitter';
|
|
2
|
-
import '@alessiofrittoli/exception';
|
|
3
|
-
import '@alessiofrittoli/exception/abort';
|
|
4
|
-
import '@alessiofrittoli/url-utils';
|
|
5
|
-
import '@alessiofrittoli/abort-controller';
|
|
6
|
-
import './error.mjs';
|
|
7
|
-
export { F as Fetch, R as RequestMethod, a as XHR } from './types-DAKtp5Tp.mjs';
|
package/dist/types.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import '@alessiofrittoli/event-emitter';
|
|
2
|
-
import '@alessiofrittoli/exception';
|
|
3
|
-
import '@alessiofrittoli/exception/abort';
|
|
4
|
-
import '@alessiofrittoli/url-utils';
|
|
5
|
-
import '@alessiofrittoli/abort-controller';
|
|
6
|
-
import './error.js';
|
|
7
|
-
export { F as Fetch, R as RequestMethod, a as XHR } from './types-B7TKkr2l.js';
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/dist/xhr/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { X as Xhr } from '../types-B7TKkr2l.js';
|
|
2
|
-
import '@alessiofrittoli/url-utils';
|
|
3
|
-
import '@alessiofrittoli/event-emitter';
|
|
4
|
-
import '@alessiofrittoli/exception/abort';
|
|
5
|
-
import '@alessiofrittoli/abort-controller';
|
|
6
|
-
import '../error.js';
|
|
7
|
-
import '@alessiofrittoli/exception';
|
package/dist/xhr/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkHNNMLJIAjs = require('../chunk-HNNMLJIA.js');var _urlutils = require('@alessiofrittoli/url-utils');var _eventemitter = require('@alessiofrittoli/event-emitter');var _exception = require('@alessiofrittoli/exception');var _abortcontroller = require('@alessiofrittoli/abort-controller');var r=class r extends _eventemitter.EventEmitter{constructor(e){super({captureRejections:!0});this.setResponseType=!1;this.controllerAbortListener=()=>{this.controller.signal.removeEventListener("abort",this.controllerAbortListener),this.request.abort(),this.log({message:this.controller.signal.reason.message})};this.readyStateChangeHandler=async e=>{if(this.setResponseType&&this.setResponseTypeFromResponseHeaders(),this.debug){let{logReadyStateChange:t}=await Promise.resolve().then(() => _interopRequireWildcard(require("../logReadyStateChange-MSYI3OBQ.js")));t(this)}this.response=this.getResponse(),this.emit("readystatechange",e,this)};this.progressEventHandler=e=>{let t=e.type;return this.log({event:t,message:`${e.loaded} bytes transferred.`}),t==="abort"?(this.emit("abort",this.controller.signal.reason,e,this),this):t==="error"?this.error(new (0, _exception.Exception)("ProgressEvent Error",{code:_chunkHNNMLJIAjs.b.Exception.UNKNOWN,cause:e})):(this.emit(t,e,this),this)};this.loadEndHandler=async()=>{let{status:e}=this.request;if(e<=0)return this;try{if(e<100||e>=400){if(this.setResponseTypeFromResponseHeaders(),this.contentTypeToResponseType()==="json"&&_exception.Exception.isException(this.response))return this.error(new (0, _exception.Exception)(this.response.message,{...this.response}));let{response:t}=this,{responseType:n}=this.request,{getErrorFromResponse:u}=await Promise.resolve().then(() => _interopRequireWildcard(require("../getErrorFromResponse-7V2YFS4F.js"))),o=await u({responseType:n,response:t});return o?this.error(o):this.error(new (0, _exception.Exception)("Error from the server.",{code:_chunkHNNMLJIAjs.b.Exception.UNKNOWN,cause:this.response}))}return this.success()}catch(t){let n=t;return this.error(new (0, _exception.Exception)(n.message,{code:_chunkHNNMLJIAjs.b.Exception.UNKNOWN,...n}))}};this.request=typeof XMLHttpRequest<"u"?new XMLHttpRequest:null,this.setResponseType=_nullishCoalesce(e.autoResponseType, () => (!0)),this.request&&(typeof e.responseType<"u"&&(this.setResponseType=!1,this.request.responseType=e.responseType),typeof e.withCredentials<"u"&&(this.request.withCredentials=e.withCredentials),e.timeout&&(this.request.timeout=e.timeout)),this.url=e.url,this.method=e.method||"GET",this.body=e.body,this.controller=e.controller||new _abortcontroller.AbortController,this.headers=new Headers(e.headers),this.credentials=e.credentials,this.debug=e.debug||!1}init(e={}){return this.request?(e.controller&&(this.controller=e.controller),this.controller.signal.aborted&&(this.controller=e.controller||new _abortcontroller.AbortController),this.emit("init",this),this.addProgressListeners(),this.request.open(this.method,_urlutils.Url.format(this.url),!0,_optionalChain([this, 'access', _ => _.credentials, 'optionalAccess', _2 => _2.username]),_optionalChain([this, 'access', _3 => _3.credentials, 'optionalAccess', _4 => _4.password])),this.setHeaders(),this.request.removeEventListener("readystatechange",this.readyStateChangeHandler),this.request.addEventListener("readystatechange",this.readyStateChangeHandler),this.once("loadend",this.loadEndHandler),this.controller.signal.addEventListener("abort",this.controllerAbortListener),this):this.error(r.NotAvailableErr)}async send(){if(!this.request)return this.error(r.NotAvailableErr);try{return this.emit("send",this),await this.request.send(this.body),this}catch(e){let t=e,n=t.name==="InvalidStateError"?_chunkHNNMLJIAjs.b.Request.XHR_INVALID_STATE:_chunkHNNMLJIAjs.b.Exception.UNKNOWN;return this.error(new (0, _exception.Exception)(t.message,{code:n,name:t.name,cause:t}))}}abort(e,t){return this.request?(this.controller.abort(e,t),this):this.error(r.NotAvailableErr)}addProgressListeners(){return this.request?(r.ProgressEvents.map(e=>{this.request.removeEventListener(e,this.progressEventHandler),this.request.addEventListener(e,this.progressEventHandler)}),this):this.error(r.NotAvailableErr)}setHeaders(){return this.request?(Array.from(this.headers.entries()).forEach(([e,t])=>{this.request.setRequestHeader(e,t)}),this):this.error(r.NotAvailableErr)}contentTypeToResponseType(e){return typeof e>"u"&&(e=this.getResponseContentType()),e?(e=e.toLowerCase(),e.includes("text/html")||e.includes("application/xml")?"document":e.includes("application/json")?"json":e.includes("application/octet-stream")?"arraybuffer":e.startsWith("image/")||e.startsWith("video/")?"blob":"text"):""}setResponseTypeFromResponseHeaders(){if(this.request.readyState===XMLHttpRequest.LOADING||this.request.readyState===XMLHttpRequest.DONE)return this;let e=this.getResponseContentType();return this.request.responseType=this.contentTypeToResponseType(e),this.log({message:"Set XMLHttpRequest.responseType from Content-Type Response Headers.",contentType:e,XhrResponseType:this.request.responseType}),this}getResponse(){if(!this.request){this.error(r.NotAvailableErr);return}return this.response=this.request.response,this.response}getResponseContentType(){return this.getResponseHeader("Content-Type")}getResponseHeader(e){return this.request.getResponseHeader(e)}success(){return this.emit("success",this.response,this),this}error(e){return this.emit("error",e,this),this}log(e){return this.debug?(console.log({class:"Xhr",...e}),this):this}};r.NotAvailableErr=new (0, _exception.Exception)("XMLHttpRequest not available",{code:_chunkHNNMLJIAjs.b.Request.XHR_NOT_AVAILABLE}),r.ProgressEvents=["abort","timeout","error","loadstart","progress","load","loadend"];var h=r;exports.Xhr = h;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const { Chain } = require( '@alessiofrittoli/chain-functions' )
|
|
2
|
-
const tsSetup = require( './ts-setup.js' )
|
|
3
|
-
const project = require( '../../package.json' )
|
|
4
|
-
|
|
5
|
-
/** @type {import( '@alessiofrittoli/chain-functions/types' ).LastChainLink<() => void | Promise<void>>} */
|
|
6
|
-
const lastChainLink = () => () => {
|
|
7
|
-
console.log( {
|
|
8
|
-
package: project.name,
|
|
9
|
-
message: 'Post-Install done'
|
|
10
|
-
} )
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** @type {import( '@alessiofrittoli/chain-functions/types' ).ChainFactory<() => void | Promise<void>>} */
|
|
14
|
-
const chain = [ tsSetup, lastChainLink ]
|
|
15
|
-
|
|
16
|
-
Chain.functions( chain )()
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const project = require( '../../package.json' )
|
|
2
|
-
const { addTypesReference } = require( '@alessiofrittoli/node-scripts/postinstall' )
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/** @type {import( '@alessiofrittoli/chain-functions/types' ).ChainLink<() => void | Promise<void>>} */
|
|
6
|
-
const tsSetup = next => () => {
|
|
7
|
-
|
|
8
|
-
addTypesReference( {
|
|
9
|
-
name: project.name,
|
|
10
|
-
} )
|
|
11
|
-
|
|
12
|
-
return next()
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
module.exports = tsSetup
|
|
File without changes
|