@alessiofrittoli/fetcher 2.0.0 → 2.1.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/dist/error/index.cjs +1 -1
- package/dist/error/index.d.cts +2 -2
- package/dist/error/index.d.mts +2 -2
- package/dist/error/index.mjs +1 -1
- package/dist/getErrorFromResponse-8dAcd3AT.mjs +1 -0
- package/dist/getErrorFromResponse-CVhPWMuU.cjs +1 -0
- package/dist/globals/index.d.cts +0 -2
- package/dist/globals/index.d.mts +0 -2
- package/dist/{index-CQSGH5s-.d.mts → index-Cv9D-wd5.d.cts} +1 -8
- package/dist/{index-CQSGH5s-.d.cts → index-Cv9D-wd5.d.mts} +1 -8
- package/dist/{index-D-vZzzH1.d.cts → index-D42xj8eP.d.cts} +1 -9
- package/dist/{index-B8rmZCit.d.mts → index-DwlQQKx3.d.mts} +1 -9
- package/dist/index.d.cts +1 -3
- package/dist/index.d.mts +1 -3
- package/dist/ping/index.d.cts +1 -3
- package/dist/ping/index.d.mts +1 -3
- package/dist/types/index.d.cts +1 -1
- package/dist/types/index.d.mts +1 -1
- package/dist/xhr/index.cjs +1 -1
- package/dist/xhr/index.d.cts +1 -1
- package/dist/xhr/index.d.mts +1 -1
- package/dist/xhr/index.mjs +1 -1
- package/license.md +1 -1
- package/package.json +44 -25
- package/scripts/postinstall/index.cjs +17 -0
- package/scripts/postinstall/ts-setup.cjs +14 -0
- package/dist/getErrorFromResponse-CIppVMTN.mjs +0 -1
- package/dist/getErrorFromResponse-D3OARgUA.cjs +0 -1
package/dist/error/index.cjs
CHANGED
|
@@ -1 +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
|
|
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;
|
package/dist/error/index.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { ErrorCode
|
|
1
|
+
import { t as ErrorCode } from "../index-Cv9D-wd5.cjs";
|
|
2
|
+
export { ErrorCode };
|
package/dist/error/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { ErrorCode
|
|
1
|
+
import { t as ErrorCode } from "../index-Cv9D-wd5.mjs";
|
|
2
|
+
export { ErrorCode };
|
package/dist/error/index.mjs
CHANGED
|
@@ -1 +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
|
|
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};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Exception as e}from"@alessiofrittoli/exception";import{binaryToString as t}from"@alessiofrittoli/crypto-buffer";const n=e=>{try{return JSON.parse(e),!0}catch{return!1}},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");const n=e=>{try{return JSON.parse(e),!0}catch{return!1}},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(n(r)){let t=JSON.parse(r);if(e.Exception.isException(t))return new e.Exception(t)}return null}}};exports.getErrorFromResponse=r;
|
package/dist/globals/index.d.cts
CHANGED
package/dist/globals/index.d.mts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
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
2
|
declare const ErrorCode: {
|
|
9
3
|
readonly XHR_NOT_AVAILABLE: "ERR:XHRNOTAVAILABLE";
|
|
10
4
|
readonly XHR_INVALID_STATE: "ERR:XHRINVALIDSTATE";
|
|
@@ -21,5 +15,4 @@ declare const ErrorCode: {
|
|
|
21
15
|
readonly OFFLINE: "ERR:INTERNETDISCONNECTED";
|
|
22
16
|
};
|
|
23
17
|
type ErrorCode = ValueOf<typeof ErrorCode>;
|
|
24
|
-
|
|
25
|
-
export { Request as n, ErrorCode as t };
|
|
18
|
+
export { ErrorCode as t };
|
|
@@ -1,10 +1,4 @@
|
|
|
1
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
2
|
declare const ErrorCode: {
|
|
9
3
|
readonly XHR_NOT_AVAILABLE: "ERR:XHRNOTAVAILABLE";
|
|
10
4
|
readonly XHR_INVALID_STATE: "ERR:XHRINVALIDSTATE";
|
|
@@ -21,5 +15,4 @@ declare const ErrorCode: {
|
|
|
21
15
|
readonly OFFLINE: "ERR:INTERNETDISCONNECTED";
|
|
22
16
|
};
|
|
23
17
|
type ErrorCode = ValueOf<typeof ErrorCode>;
|
|
24
|
-
|
|
25
|
-
export { Request as n, ErrorCode as t };
|
|
18
|
+
export { ErrorCode as t };
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { t as ErrorCode } from "./index-
|
|
1
|
+
import { t as ErrorCode } from "./index-Cv9D-wd5.cjs";
|
|
2
2
|
import { AbortController } from "@alessiofrittoli/abort-controller";
|
|
3
3
|
import { Exception } from "@alessiofrittoli/exception";
|
|
4
4
|
import { EventEmitter, Listener } from "@alessiofrittoli/event-emitter";
|
|
5
5
|
import { AbortError, AbortErrorOptions } from "@alessiofrittoli/exception/abort";
|
|
6
6
|
import { UrlInput } from "@alessiofrittoli/url-utils";
|
|
7
7
|
import { UUID } from "@alessiofrittoli/math-utils";
|
|
8
|
-
//#region src/fetch/types.d.ts
|
|
9
8
|
declare namespace Fetch {
|
|
10
9
|
type Method = RequestMethod;
|
|
11
10
|
type Result<T> = {
|
|
@@ -31,8 +30,6 @@ declare namespace Fetch {
|
|
|
31
30
|
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
31
|
type OnFulfilledCallback<V> = (response: Response) => Fetch.Result<V> | PromiseLike<Fetch.Result<V>>;
|
|
33
32
|
}
|
|
34
|
-
//#endregion
|
|
35
|
-
//#region src/xhr/index.d.ts
|
|
36
33
|
declare class Xhr<TReturn = unknown> extends EventEmitter<XHR.Event.Map<TReturn>> {
|
|
37
34
|
/**
|
|
38
35
|
* The XMLHttpRequest instance.
|
|
@@ -279,8 +276,6 @@ declare class Xhr<TReturn = unknown> extends EventEmitter<XHR.Event.Map<TReturn>
|
|
|
279
276
|
*/
|
|
280
277
|
private unregisterListeners;
|
|
281
278
|
}
|
|
282
|
-
//#endregion
|
|
283
|
-
//#region src/xhr/types.d.ts
|
|
284
279
|
declare namespace XHR {
|
|
285
280
|
type Method = RequestMethod;
|
|
286
281
|
type ReadyState = typeof XMLHttpRequest.UNSENT | typeof XMLHttpRequest.OPENED | typeof XMLHttpRequest.HEADERS_RECEIVED | typeof XMLHttpRequest.LOADING | typeof XMLHttpRequest.DONE;
|
|
@@ -391,8 +386,5 @@ declare namespace XHR {
|
|
|
391
386
|
files: File[] | FileList;
|
|
392
387
|
}
|
|
393
388
|
}
|
|
394
|
-
//#endregion
|
|
395
|
-
//#region src/types/index.d.ts
|
|
396
389
|
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
397
|
-
//#endregion
|
|
398
390
|
export { Fetch as i, XHR as n, Xhr as r, RequestMethod as t };
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { t as ErrorCode } from "./index-
|
|
1
|
+
import { t as ErrorCode } from "./index-Cv9D-wd5.mjs";
|
|
2
2
|
import { Exception } from "@alessiofrittoli/exception";
|
|
3
3
|
import { UrlInput } from "@alessiofrittoli/url-utils";
|
|
4
4
|
import { AbortError, AbortErrorOptions } from "@alessiofrittoli/exception/abort";
|
|
5
5
|
import { AbortController } from "@alessiofrittoli/abort-controller";
|
|
6
6
|
import { UUID } from "@alessiofrittoli/math-utils";
|
|
7
7
|
import { EventEmitter, Listener } from "@alessiofrittoli/event-emitter";
|
|
8
|
-
//#region src/fetch/types.d.ts
|
|
9
8
|
declare namespace Fetch {
|
|
10
9
|
type Method = RequestMethod;
|
|
11
10
|
type Result<T> = {
|
|
@@ -31,8 +30,6 @@ declare namespace Fetch {
|
|
|
31
30
|
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
31
|
type OnFulfilledCallback<V> = (response: Response) => Fetch.Result<V> | PromiseLike<Fetch.Result<V>>;
|
|
33
32
|
}
|
|
34
|
-
//#endregion
|
|
35
|
-
//#region src/xhr/index.d.ts
|
|
36
33
|
declare class Xhr<TReturn = unknown> extends EventEmitter<XHR.Event.Map<TReturn>> {
|
|
37
34
|
/**
|
|
38
35
|
* The XMLHttpRequest instance.
|
|
@@ -279,8 +276,6 @@ declare class Xhr<TReturn = unknown> extends EventEmitter<XHR.Event.Map<TReturn>
|
|
|
279
276
|
*/
|
|
280
277
|
private unregisterListeners;
|
|
281
278
|
}
|
|
282
|
-
//#endregion
|
|
283
|
-
//#region src/xhr/types.d.ts
|
|
284
279
|
declare namespace XHR {
|
|
285
280
|
type Method = RequestMethod;
|
|
286
281
|
type ReadyState = typeof XMLHttpRequest.UNSENT | typeof XMLHttpRequest.OPENED | typeof XMLHttpRequest.HEADERS_RECEIVED | typeof XMLHttpRequest.LOADING | typeof XMLHttpRequest.DONE;
|
|
@@ -391,8 +386,5 @@ declare namespace XHR {
|
|
|
391
386
|
files: File[] | FileList;
|
|
392
387
|
}
|
|
393
388
|
}
|
|
394
|
-
//#endregion
|
|
395
|
-
//#region src/types/index.d.ts
|
|
396
389
|
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
397
|
-
//#endregion
|
|
398
390
|
export { Fetch as i, XHR as n, Xhr as r, RequestMethod as t };
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { i as Fetch } from "./index-
|
|
1
|
+
import { i as Fetch } from "./index-D42xj8eP.cjs";
|
|
2
2
|
import { AbortSignal } from "@alessiofrittoli/abort-controller";
|
|
3
|
-
//#region src/index.d.ts
|
|
4
3
|
interface RequestInit extends globalThis.RequestInit {
|
|
5
4
|
signal?: AbortSignal;
|
|
6
5
|
}
|
|
@@ -13,5 +12,4 @@ interface RequestInit extends globalThis.RequestInit {
|
|
|
13
12
|
* @returns An object with the parsed Request data, Request error, Request Response instance and Request Headers.
|
|
14
13
|
*/
|
|
15
14
|
export declare const fetch: <T, U extends RequestInit = RequestInit, V = Fetch.ReturnDataType<T, U>>(input: RequestInfo | URL, init?: U, onfulfilled?: Fetch.OnFulfilledCallback<V>) => Promise<Fetch.Result<V>>;
|
|
16
|
-
//#endregion
|
|
17
15
|
export {};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { i as Fetch } from "./index-
|
|
1
|
+
import { i as Fetch } from "./index-DwlQQKx3.mjs";
|
|
2
2
|
import { AbortSignal } from "@alessiofrittoli/abort-controller";
|
|
3
|
-
//#region src/index.d.ts
|
|
4
3
|
interface RequestInit extends globalThis.RequestInit {
|
|
5
4
|
signal?: AbortSignal;
|
|
6
5
|
}
|
|
@@ -13,5 +12,4 @@ interface RequestInit extends globalThis.RequestInit {
|
|
|
13
12
|
* @returns An object with the parsed Request data, Request error, Request Response instance and Request Headers.
|
|
14
13
|
*/
|
|
15
14
|
export declare const fetch: <T, U extends RequestInit = RequestInit, V = Fetch.ReturnDataType<T, U>>(input: RequestInfo | URL, init?: U, onfulfilled?: Fetch.OnFulfilledCallback<V>) => Promise<Fetch.Result<V>>;
|
|
16
|
-
//#endregion
|
|
17
15
|
export {};
|
package/dist/ping/index.d.cts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { UrlInput } from "@alessiofrittoli/url-utils";
|
|
2
|
-
//#region src/ping/index.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Asynchronously sends an HTTP POST request containing a small amount of data to a web server.
|
|
5
4
|
*
|
|
6
5
|
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon)
|
|
7
6
|
*/
|
|
8
|
-
export declare const ping: (url: UrlInput, data?: BodyInit | null) => boolean;
|
|
9
|
-
//#endregion
|
|
7
|
+
export declare const ping: (url: UrlInput, data?: BodyInit | null) => boolean;
|
package/dist/ping/index.d.mts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { UrlInput } from "@alessiofrittoli/url-utils";
|
|
2
|
-
//#region src/ping/index.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Asynchronously sends an HTTP POST request containing a small amount of data to a web server.
|
|
5
4
|
*
|
|
6
5
|
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon)
|
|
7
6
|
*/
|
|
8
|
-
export declare const ping: (url: UrlInput, data?: BodyInit | null) => boolean;
|
|
9
|
-
//#endregion
|
|
7
|
+
export declare const ping: (url: UrlInput, data?: BodyInit | null) => boolean;
|
package/dist/types/index.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as Fetch, n as XHR, t as RequestMethod } from "../index-
|
|
1
|
+
import { i as Fetch, n as XHR, t as RequestMethod } from "../index-D42xj8eP.cjs";
|
|
2
2
|
export { Fetch, RequestMethod, XHR };
|
package/dist/types/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as Fetch, n as XHR, t as RequestMethod } from "../index-
|
|
1
|
+
import { i as Fetch, n as XHR, t as RequestMethod } from "../index-DwlQQKx3.mjs";
|
|
2
2
|
export { Fetch, RequestMethod, XHR };
|
package/dist/xhr/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../error/index.cjs");let t=require("@alessiofrittoli/exception"),n=require("@alessiofrittoli/url-utils"),r=require("@alessiofrittoli/exception/abort"),i=require("@alessiofrittoli/abort-controller"),a=require("@alessiofrittoli/math-utils"),o=require("@alessiofrittoli/event-emitter");var s=class s extends o.EventEmitter{request;_requestId=(0,a.randomUUID)();get requestId(){return this._requestId}set requestId(e){this._requestId=e}_debug;get debug(){return this._debug}set debug(e){this._debug=e}_controller;get controller(){return this._controller}set controller(e){this._controller=e}_timeout;get timeout(){return this._timeout}set timeout(e){this._timeout=e}_credentials;get credentials(){return this._credentials}set credentials(e){this._credentials=e}_withCredentials;get withCredentials(){return this._withCredentials}set withCredentials(e){this._withCredentials=e}autoResponseType=!1;listenersRegistered=!1;static ProgressEvents=[`abort`,`timeout`,`error`,`loadstart`,`progress`,`load`,`loadend`];static MapReadyStateToXhrEvent={0:`unsent`,1:`opened`,2:`headers`,3:`loading`,4:`done`};constructor(e={}){super({captureRejections:!0}),this.request=typeof XMLHttpRequest<`u`?new XMLHttpRequest:null;let{credentials:t,withCredentials:n=!1,timeout:r=0,autoResponseType:i=!0,debug:a=!1}=e;this._debug=a,this._timeout=r,this._withCredentials=n,this._credentials=t,this.autoResponseType=i}init(e){if(!this.request)return this.error(this.getXhrNotAvailableError());let{url:t,method:r=`GET`,headers:i,controller:o,debug:s=this.debug,timeout:c=this.timeout,withCredentials:l=this.withCredentials,credentials:u=this.credentials,responseType:d,autoResponseType:f=this.autoResponseType}=e;return this.autoResponseType=f,this.requestId=(0,a.randomUUID)(),this.debug=s,this.request.open(r,n.Url.format(t),!0,u?.username,u?.password),this.request.timeout=c===null?0:c,this.request.withCredentials=l!==null&&l,d!==void 0&&(this.autoResponseType=!1,this.request.responseType=d),i&&this.setRequestHeaders(i),this.setupAbortController(o).registerListeners().emit(`init`,this),this.log(`Xhr initialized.`),this}send(n){if(!this.request)return this.error(this.getXhrNotAvailableError());this.request.readyState!==XMLHttpRequest.OPENED&&this.init(n);let{body:r}=n;try{return this.emit(`send`,this),this.request.send(r),this}catch(n){let r=n,i=r.name===t.ExceptionName.InvalidStateError?e.ErrorCode.XHR_INVALID_STATE:e.ErrorCode.UNKNOWN;return this.error(new t.Exception({...r,name:r.name,message:r.message,code:i,cause:r,isCritical:!0}))}}upload(e){let{files:t}=e,n=new FormData;return Array.from(t).forEach(e=>{n.append(e.name,e,e.name)}),this.send({...e,body:n})}getResponse(){if(!this.request)this.error(this.getXhrNotAvailableError());else return this.request.response}abort(e,t){return this.request?(this.controller?.abort(e,t),this):this.error(this.getXhrNotAvailableError())}setRequestHeaders(e){if(!this.request)return this.error(this.getXhrNotAvailableError());let t=new Headers(e);return Array.from(t.entries()).forEach(([e,t])=>{this.request.setRequestHeader(e,t)}),this}getResponseHeader(e){return this.request?this.request.getResponseHeader(e):(this.error(this.getXhrNotAvailableError()),null)}destroy(){return this.log({message:`Xhr destroyed.`}).abort(`Xhr has been destroyed.`,{code:e.ErrorCode.XHR_DESTROYED}).unregisterListeners().removeAllListeners()}log(...e){return this.debug&&console.log({class:`Xhr`,requestId:this.requestId},...e),this}success(e){return this.emit(`success`,e,this),this}error(e){if(this.listenerCount(`error`)===0)throw e;return this.emit(`error`,e,this),this}getXhrNotAvailableError(){return new t.Exception({message:`XMLHttpRequest not available.`,code:e.ErrorCode.XHR_NOT_AVAILABLE,isCritical:!0})}setupAbortController(e){let t=this.controller;return e&&(this.controller=e),(!this.controller||this.controller.signal.aborted)&&(this.controller=new i.AbortController,this.log({method:`.init()`},`new AbortController created.`)),t!==this.controller&&(this.log({method:`.init()`},`Register AbortSignal "abort" event listener.`),t?.signal.removeEventListener(`abort`,this.onControllerSignalAbort),this.controller.signal.addEventListener(`abort`,this.onControllerSignalAbort)),this}setResponseTypeFromResponseHeaders(){let e=this.getResponseContentType();return this.request.responseType=this.contentTypeToResponseType(e),this.log({method:`.setResponseTypeFromResponseHeaders()`,contentType:e,XhrResponseType:this.request.responseType},`Set XMLHttpRequest.responseType from Content-Type Response Headers.`),this}contentTypeToResponseType(e=this.getResponseContentType()){let t=e?.toLowerCase()||!1;return!t||t.includes(`text/plain`)?`text`:t.includes(`text/html`)||t.includes(`application/xml`)?`document`:t.includes(`application/json`)?`json`:t.startsWith(`image/`)||t.startsWith(`video/`)?`blob`:`arraybuffer`}getResponseContentType(){return this.getResponseHeader(`Content-Type`)}onReadyStateChange=async n=>{let{readyState:r,status:i}=this.request;if(this.log({event:`readystatechange`,readyState:r,status:this.request.status,data:n}),this.autoResponseType&&r===XMLHttpRequest.HEADERS_RECEIVED&&this.setResponseTypeFromResponseHeaders(),this.emit(`readystatechange`,n,this),r!==XMLHttpRequest.DONE||i<=0)return this.emitReadyStateEvent({readyState:r,event:n});let a=this.getResponse();if(i<100||i>=400){let{responseType:o}=this.request,{getErrorFromResponse:s}=await Promise.resolve().then(()=>require("../getErrorFromResponse-
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../error/index.cjs");let t=require("@alessiofrittoli/exception"),n=require("@alessiofrittoli/url-utils"),r=require("@alessiofrittoli/exception/abort"),i=require("@alessiofrittoli/abort-controller"),a=require("@alessiofrittoli/math-utils"),o=require("@alessiofrittoli/event-emitter");var s=class s extends o.EventEmitter{request;_requestId=(0,a.randomUUID)();get requestId(){return this._requestId}set requestId(e){this._requestId=e}_debug;get debug(){return this._debug}set debug(e){this._debug=e}_controller;get controller(){return this._controller}set controller(e){this._controller=e}_timeout;get timeout(){return this._timeout}set timeout(e){this._timeout=e}_credentials;get credentials(){return this._credentials}set credentials(e){this._credentials=e}_withCredentials;get withCredentials(){return this._withCredentials}set withCredentials(e){this._withCredentials=e}autoResponseType=!1;listenersRegistered=!1;static ProgressEvents=[`abort`,`timeout`,`error`,`loadstart`,`progress`,`load`,`loadend`];static MapReadyStateToXhrEvent={0:`unsent`,1:`opened`,2:`headers`,3:`loading`,4:`done`};constructor(e={}){super({captureRejections:!0}),this.request=typeof XMLHttpRequest<`u`?new XMLHttpRequest:null;let{credentials:t,withCredentials:n=!1,timeout:r=0,autoResponseType:i=!0,debug:a=!1}=e;this._debug=a,this._timeout=r,this._withCredentials=n,this._credentials=t,this.autoResponseType=i}init(e){if(!this.request)return this.error(this.getXhrNotAvailableError());let{url:t,method:r=`GET`,headers:i,controller:o,debug:s=this.debug,timeout:c=this.timeout,withCredentials:l=this.withCredentials,credentials:u=this.credentials,responseType:d,autoResponseType:f=this.autoResponseType}=e;return this.autoResponseType=f,this.requestId=(0,a.randomUUID)(),this.debug=s,this.request.open(r,n.Url.format(t),!0,u?.username,u?.password),this.request.timeout=c===null?0:c,this.request.withCredentials=l!==null&&l,d!==void 0&&(this.autoResponseType=!1,this.request.responseType=d),i&&this.setRequestHeaders(i),this.setupAbortController(o).registerListeners().emit(`init`,this),this.log(`Xhr initialized.`),this}send(n){if(!this.request)return this.error(this.getXhrNotAvailableError());this.request.readyState!==XMLHttpRequest.OPENED&&this.init(n);let{body:r}=n;try{return this.emit(`send`,this),this.request.send(r),this}catch(n){let r=n,i=r.name===t.ExceptionName.InvalidStateError?e.ErrorCode.XHR_INVALID_STATE:e.ErrorCode.UNKNOWN;return this.error(new t.Exception({...r,name:r.name,message:r.message,code:i,cause:r,isCritical:!0}))}}upload(e){let{files:t}=e,n=new FormData;return Array.from(t).forEach(e=>{n.append(e.name,e,e.name)}),this.send({...e,body:n})}getResponse(){if(!this.request)this.error(this.getXhrNotAvailableError());else return this.request.response}abort(e,t){return this.request?(this.controller?.abort(e,t),this):this.error(this.getXhrNotAvailableError())}setRequestHeaders(e){if(!this.request)return this.error(this.getXhrNotAvailableError());let t=new Headers(e);return Array.from(t.entries()).forEach(([e,t])=>{this.request.setRequestHeader(e,t)}),this}getResponseHeader(e){return this.request?this.request.getResponseHeader(e):(this.error(this.getXhrNotAvailableError()),null)}destroy(){return this.log({message:`Xhr destroyed.`}).abort(`Xhr has been destroyed.`,{code:e.ErrorCode.XHR_DESTROYED}).unregisterListeners().removeAllListeners()}log(...e){return this.debug&&console.log({class:`Xhr`,requestId:this.requestId},...e),this}success(e){return this.emit(`success`,e,this),this}error(e){if(this.listenerCount(`error`)===0)throw e;return this.emit(`error`,e,this),this}getXhrNotAvailableError(){return new t.Exception({message:`XMLHttpRequest not available.`,code:e.ErrorCode.XHR_NOT_AVAILABLE,isCritical:!0})}setupAbortController(e){let t=this.controller;return e&&(this.controller=e),(!this.controller||this.controller.signal.aborted)&&(this.controller=new i.AbortController,this.log({method:`.init()`},`new AbortController created.`)),t!==this.controller&&(this.log({method:`.init()`},`Register AbortSignal "abort" event listener.`),t?.signal.removeEventListener(`abort`,this.onControllerSignalAbort),this.controller.signal.addEventListener(`abort`,this.onControllerSignalAbort)),this}setResponseTypeFromResponseHeaders(){let e=this.getResponseContentType();return this.request.responseType=this.contentTypeToResponseType(e),this.log({method:`.setResponseTypeFromResponseHeaders()`,contentType:e,XhrResponseType:this.request.responseType},`Set XMLHttpRequest.responseType from Content-Type Response Headers.`),this}contentTypeToResponseType(e=this.getResponseContentType()){let t=e?.toLowerCase()||!1;return!t||t.includes(`text/plain`)?`text`:t.includes(`text/html`)||t.includes(`application/xml`)?`document`:t.includes(`application/json`)?`json`:t.startsWith(`image/`)||t.startsWith(`video/`)?`blob`:`arraybuffer`}getResponseContentType(){return this.getResponseHeader(`Content-Type`)}onReadyStateChange=async n=>{let{readyState:r,status:i}=this.request;if(this.log({event:`readystatechange`,readyState:r,status:this.request.status,data:n}),this.autoResponseType&&r===XMLHttpRequest.HEADERS_RECEIVED&&this.setResponseTypeFromResponseHeaders(),this.emit(`readystatechange`,n,this),r!==XMLHttpRequest.DONE||i<=0)return this.emitReadyStateEvent({readyState:r,event:n});let a=this.getResponse();if(i<100||i>=400){let{responseType:o}=this.request,{getErrorFromResponse:s}=await Promise.resolve().then(()=>require("../getErrorFromResponse-CVhPWMuU.cjs")),c=await s({response:a,responseType:o});return this.emitReadyStateEvent({readyState:r,event:n}),c?this.error(c):this.error(new t.Exception({message:`Error from the server.`,code:e.ErrorCode.UNKNOWN,status:i,cause:a,isCritical:!0}))}return this.emitReadyStateEvent({readyState:r,event:n}).success(a)};onProgressChange=n=>{let{type:i}=n;if(this.log({event:i,data:n,progress:n.loaded/n.total*100},`${n.loaded} bytes transferred.`),i===`abort`){let t=this.controller?.signal.reason||new r.AbortError({message:`signal is aborted without reason`,code:e.ErrorCode.ABORT});return this.emit(`abort`,t,n,this)}if(i===`error`)return this.error(new t.Exception({message:`ProgressEvent Error`,code:e.ErrorCode.UNKNOWN,cause:n,isCritical:!0}));this.emit(i,n,this)};onControllerSignalAbort=()=>{this.log({method:`.onControllerSignalAbort()`,reason:this.controller?.signal.reason},`Abort XHR request.`),this.controller?.signal.removeEventListener(`abort`,this.onControllerSignalAbort),this.request.abort()};emitReadyStateEvent({readyState:e,event:t}){let n=s.MapReadyStateToXhrEvent[e];return this.emit(n,t,this),this.log({event:`readystatechange:${n}`,readyState:e,status:this.request.status,data:t}),this}registerListeners(){return this.request?(this.listenersRegistered||=(this.log({method:`.registerListeners()`},`Register "readystatechange" event listener.`),this.request.addEventListener(`readystatechange`,this.onReadyStateChange),s.ProgressEvents.map(e=>{this.log({method:`.registerListeners()`},`Register "${e}" event listener.`),this.request.addEventListener(e,this.onProgressChange)}),!0),this):this.error(this.getXhrNotAvailableError())}unregisterListeners(){return this.request?(this.listenersRegistered&&(this.listenersRegistered=!1,this.log({method:`.unregisterListeners()`},`Unregister "readystatechange" event listener.`),this.request.removeEventListener(`readystatechange`,this.onReadyStateChange),s.ProgressEvents.map(e=>{this.log({method:`.unregisterListeners()`},`Unregister "${e}" event listener.`),this.request.removeEventListener(e,this.onProgressChange)})),this):this.error(this.getXhrNotAvailableError())}};exports.Xhr=s;
|
package/dist/xhr/index.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { r as Xhr } from "../index-
|
|
1
|
+
import { r as Xhr } from "../index-D42xj8eP.cjs";
|
|
2
2
|
export { Xhr };
|
package/dist/xhr/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { r as Xhr } from "../index-
|
|
1
|
+
import { r as Xhr } from "../index-DwlQQKx3.mjs";
|
|
2
2
|
export { Xhr };
|
package/dist/xhr/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ErrorCode as e}from"../error/index.mjs";import{Exception as t,ExceptionName as n}from"@alessiofrittoli/exception";import{Url as r}from"@alessiofrittoli/url-utils";import{AbortError as i}from"@alessiofrittoli/exception/abort";import{AbortController as a}from"@alessiofrittoli/abort-controller";import{randomUUID as o}from"@alessiofrittoli/math-utils";import{EventEmitter as s}from"@alessiofrittoli/event-emitter";var c=class c extends s{request;_requestId=o();get requestId(){return this._requestId}set requestId(e){this._requestId=e}_debug;get debug(){return this._debug}set debug(e){this._debug=e}_controller;get controller(){return this._controller}set controller(e){this._controller=e}_timeout;get timeout(){return this._timeout}set timeout(e){this._timeout=e}_credentials;get credentials(){return this._credentials}set credentials(e){this._credentials=e}_withCredentials;get withCredentials(){return this._withCredentials}set withCredentials(e){this._withCredentials=e}autoResponseType=!1;listenersRegistered=!1;static ProgressEvents=[`abort`,`timeout`,`error`,`loadstart`,`progress`,`load`,`loadend`];static MapReadyStateToXhrEvent={0:`unsent`,1:`opened`,2:`headers`,3:`loading`,4:`done`};constructor(e={}){super({captureRejections:!0}),this.request=typeof XMLHttpRequest<`u`?new XMLHttpRequest:null;let{credentials:t,withCredentials:n=!1,timeout:r=0,autoResponseType:i=!0,debug:a=!1}=e;this._debug=a,this._timeout=r,this._withCredentials=n,this._credentials=t,this.autoResponseType=i}init(e){if(!this.request)return this.error(this.getXhrNotAvailableError());let{url:t,method:n=`GET`,headers:i,controller:a,debug:s=this.debug,timeout:c=this.timeout,withCredentials:l=this.withCredentials,credentials:u=this.credentials,responseType:d,autoResponseType:f=this.autoResponseType}=e;return this.autoResponseType=f,this.requestId=o(),this.debug=s,this.request.open(n,r.format(t),!0,u?.username,u?.password),this.request.timeout=c===null?0:c,this.request.withCredentials=l!==null&&l,d!==void 0&&(this.autoResponseType=!1,this.request.responseType=d),i&&this.setRequestHeaders(i),this.setupAbortController(a).registerListeners().emit(`init`,this),this.log(`Xhr initialized.`),this}send(r){if(!this.request)return this.error(this.getXhrNotAvailableError());this.request.readyState!==XMLHttpRequest.OPENED&&this.init(r);let{body:i}=r;try{return this.emit(`send`,this),this.request.send(i),this}catch(r){let i=r,a=i.name===n.InvalidStateError?e.XHR_INVALID_STATE:e.UNKNOWN;return this.error(new t({...i,name:i.name,message:i.message,code:a,cause:i,isCritical:!0}))}}upload(e){let{files:t}=e,n=new FormData;return Array.from(t).forEach(e=>{n.append(e.name,e,e.name)}),this.send({...e,body:n})}getResponse(){if(!this.request)this.error(this.getXhrNotAvailableError());else return this.request.response}abort(e,t){return this.request?(this.controller?.abort(e,t),this):this.error(this.getXhrNotAvailableError())}setRequestHeaders(e){if(!this.request)return this.error(this.getXhrNotAvailableError());let t=new Headers(e);return Array.from(t.entries()).forEach(([e,t])=>{this.request.setRequestHeader(e,t)}),this}getResponseHeader(e){return this.request?this.request.getResponseHeader(e):(this.error(this.getXhrNotAvailableError()),null)}destroy(){return this.log({message:`Xhr destroyed.`}).abort(`Xhr has been destroyed.`,{code:e.XHR_DESTROYED}).unregisterListeners().removeAllListeners()}log(...e){return this.debug&&console.log({class:`Xhr`,requestId:this.requestId},...e),this}success(e){return this.emit(`success`,e,this),this}error(e){if(this.listenerCount(`error`)===0)throw e;return this.emit(`error`,e,this),this}getXhrNotAvailableError(){return new t({message:`XMLHttpRequest not available.`,code:e.XHR_NOT_AVAILABLE,isCritical:!0})}setupAbortController(e){let t=this.controller;return e&&(this.controller=e),(!this.controller||this.controller.signal.aborted)&&(this.controller=new a,this.log({method:`.init()`},`new AbortController created.`)),t!==this.controller&&(this.log({method:`.init()`},`Register AbortSignal "abort" event listener.`),t?.signal.removeEventListener(`abort`,this.onControllerSignalAbort),this.controller.signal.addEventListener(`abort`,this.onControllerSignalAbort)),this}setResponseTypeFromResponseHeaders(){let e=this.getResponseContentType();return this.request.responseType=this.contentTypeToResponseType(e),this.log({method:`.setResponseTypeFromResponseHeaders()`,contentType:e,XhrResponseType:this.request.responseType},`Set XMLHttpRequest.responseType from Content-Type Response Headers.`),this}contentTypeToResponseType(e=this.getResponseContentType()){let t=e?.toLowerCase()||!1;return!t||t.includes(`text/plain`)?`text`:t.includes(`text/html`)||t.includes(`application/xml`)?`document`:t.includes(`application/json`)?`json`:t.startsWith(`image/`)||t.startsWith(`video/`)?`blob`:`arraybuffer`}getResponseContentType(){return this.getResponseHeader(`Content-Type`)}onReadyStateChange=async n=>{let{readyState:r,status:i}=this.request;if(this.log({event:`readystatechange`,readyState:r,status:this.request.status,data:n}),this.autoResponseType&&r===XMLHttpRequest.HEADERS_RECEIVED&&this.setResponseTypeFromResponseHeaders(),this.emit(`readystatechange`,n,this),r!==XMLHttpRequest.DONE||i<=0)return this.emitReadyStateEvent({readyState:r,event:n});let a=this.getResponse();if(i<100||i>=400){let{responseType:o}=this.request,{getErrorFromResponse:s}=await import(`../getErrorFromResponse-
|
|
1
|
+
import{ErrorCode as e}from"../error/index.mjs";import{Exception as t,ExceptionName as n}from"@alessiofrittoli/exception";import{Url as r}from"@alessiofrittoli/url-utils";import{AbortError as i}from"@alessiofrittoli/exception/abort";import{AbortController as a}from"@alessiofrittoli/abort-controller";import{randomUUID as o}from"@alessiofrittoli/math-utils";import{EventEmitter as s}from"@alessiofrittoli/event-emitter";var c=class c extends s{request;_requestId=o();get requestId(){return this._requestId}set requestId(e){this._requestId=e}_debug;get debug(){return this._debug}set debug(e){this._debug=e}_controller;get controller(){return this._controller}set controller(e){this._controller=e}_timeout;get timeout(){return this._timeout}set timeout(e){this._timeout=e}_credentials;get credentials(){return this._credentials}set credentials(e){this._credentials=e}_withCredentials;get withCredentials(){return this._withCredentials}set withCredentials(e){this._withCredentials=e}autoResponseType=!1;listenersRegistered=!1;static ProgressEvents=[`abort`,`timeout`,`error`,`loadstart`,`progress`,`load`,`loadend`];static MapReadyStateToXhrEvent={0:`unsent`,1:`opened`,2:`headers`,3:`loading`,4:`done`};constructor(e={}){super({captureRejections:!0}),this.request=typeof XMLHttpRequest<`u`?new XMLHttpRequest:null;let{credentials:t,withCredentials:n=!1,timeout:r=0,autoResponseType:i=!0,debug:a=!1}=e;this._debug=a,this._timeout=r,this._withCredentials=n,this._credentials=t,this.autoResponseType=i}init(e){if(!this.request)return this.error(this.getXhrNotAvailableError());let{url:t,method:n=`GET`,headers:i,controller:a,debug:s=this.debug,timeout:c=this.timeout,withCredentials:l=this.withCredentials,credentials:u=this.credentials,responseType:d,autoResponseType:f=this.autoResponseType}=e;return this.autoResponseType=f,this.requestId=o(),this.debug=s,this.request.open(n,r.format(t),!0,u?.username,u?.password),this.request.timeout=c===null?0:c,this.request.withCredentials=l!==null&&l,d!==void 0&&(this.autoResponseType=!1,this.request.responseType=d),i&&this.setRequestHeaders(i),this.setupAbortController(a).registerListeners().emit(`init`,this),this.log(`Xhr initialized.`),this}send(r){if(!this.request)return this.error(this.getXhrNotAvailableError());this.request.readyState!==XMLHttpRequest.OPENED&&this.init(r);let{body:i}=r;try{return this.emit(`send`,this),this.request.send(i),this}catch(r){let i=r,a=i.name===n.InvalidStateError?e.XHR_INVALID_STATE:e.UNKNOWN;return this.error(new t({...i,name:i.name,message:i.message,code:a,cause:i,isCritical:!0}))}}upload(e){let{files:t}=e,n=new FormData;return Array.from(t).forEach(e=>{n.append(e.name,e,e.name)}),this.send({...e,body:n})}getResponse(){if(!this.request)this.error(this.getXhrNotAvailableError());else return this.request.response}abort(e,t){return this.request?(this.controller?.abort(e,t),this):this.error(this.getXhrNotAvailableError())}setRequestHeaders(e){if(!this.request)return this.error(this.getXhrNotAvailableError());let t=new Headers(e);return Array.from(t.entries()).forEach(([e,t])=>{this.request.setRequestHeader(e,t)}),this}getResponseHeader(e){return this.request?this.request.getResponseHeader(e):(this.error(this.getXhrNotAvailableError()),null)}destroy(){return this.log({message:`Xhr destroyed.`}).abort(`Xhr has been destroyed.`,{code:e.XHR_DESTROYED}).unregisterListeners().removeAllListeners()}log(...e){return this.debug&&console.log({class:`Xhr`,requestId:this.requestId},...e),this}success(e){return this.emit(`success`,e,this),this}error(e){if(this.listenerCount(`error`)===0)throw e;return this.emit(`error`,e,this),this}getXhrNotAvailableError(){return new t({message:`XMLHttpRequest not available.`,code:e.XHR_NOT_AVAILABLE,isCritical:!0})}setupAbortController(e){let t=this.controller;return e&&(this.controller=e),(!this.controller||this.controller.signal.aborted)&&(this.controller=new a,this.log({method:`.init()`},`new AbortController created.`)),t!==this.controller&&(this.log({method:`.init()`},`Register AbortSignal "abort" event listener.`),t?.signal.removeEventListener(`abort`,this.onControllerSignalAbort),this.controller.signal.addEventListener(`abort`,this.onControllerSignalAbort)),this}setResponseTypeFromResponseHeaders(){let e=this.getResponseContentType();return this.request.responseType=this.contentTypeToResponseType(e),this.log({method:`.setResponseTypeFromResponseHeaders()`,contentType:e,XhrResponseType:this.request.responseType},`Set XMLHttpRequest.responseType from Content-Type Response Headers.`),this}contentTypeToResponseType(e=this.getResponseContentType()){let t=e?.toLowerCase()||!1;return!t||t.includes(`text/plain`)?`text`:t.includes(`text/html`)||t.includes(`application/xml`)?`document`:t.includes(`application/json`)?`json`:t.startsWith(`image/`)||t.startsWith(`video/`)?`blob`:`arraybuffer`}getResponseContentType(){return this.getResponseHeader(`Content-Type`)}onReadyStateChange=async n=>{let{readyState:r,status:i}=this.request;if(this.log({event:`readystatechange`,readyState:r,status:this.request.status,data:n}),this.autoResponseType&&r===XMLHttpRequest.HEADERS_RECEIVED&&this.setResponseTypeFromResponseHeaders(),this.emit(`readystatechange`,n,this),r!==XMLHttpRequest.DONE||i<=0)return this.emitReadyStateEvent({readyState:r,event:n});let a=this.getResponse();if(i<100||i>=400){let{responseType:o}=this.request,{getErrorFromResponse:s}=await import(`../getErrorFromResponse-8dAcd3AT.mjs`),c=await s({response:a,responseType:o});return this.emitReadyStateEvent({readyState:r,event:n}),c?this.error(c):this.error(new t({message:`Error from the server.`,code:e.UNKNOWN,status:i,cause:a,isCritical:!0}))}return this.emitReadyStateEvent({readyState:r,event:n}).success(a)};onProgressChange=n=>{let{type:r}=n;if(this.log({event:r,data:n,progress:n.loaded/n.total*100},`${n.loaded} bytes transferred.`),r===`abort`){let t=this.controller?.signal.reason||new i({message:`signal is aborted without reason`,code:e.ABORT});return this.emit(`abort`,t,n,this)}if(r===`error`)return this.error(new t({message:`ProgressEvent Error`,code:e.UNKNOWN,cause:n,isCritical:!0}));this.emit(r,n,this)};onControllerSignalAbort=()=>{this.log({method:`.onControllerSignalAbort()`,reason:this.controller?.signal.reason},`Abort XHR request.`),this.controller?.signal.removeEventListener(`abort`,this.onControllerSignalAbort),this.request.abort()};emitReadyStateEvent({readyState:e,event:t}){let n=c.MapReadyStateToXhrEvent[e];return this.emit(n,t,this),this.log({event:`readystatechange:${n}`,readyState:e,status:this.request.status,data:t}),this}registerListeners(){return this.request?(this.listenersRegistered||=(this.log({method:`.registerListeners()`},`Register "readystatechange" event listener.`),this.request.addEventListener(`readystatechange`,this.onReadyStateChange),c.ProgressEvents.map(e=>{this.log({method:`.registerListeners()`},`Register "${e}" event listener.`),this.request.addEventListener(e,this.onProgressChange)}),!0),this):this.error(this.getXhrNotAvailableError())}unregisterListeners(){return this.request?(this.listenersRegistered&&(this.listenersRegistered=!1,this.log({method:`.unregisterListeners()`},`Unregister "readystatechange" event listener.`),this.request.removeEventListener(`readystatechange`,this.onReadyStateChange),c.ProgressEvents.map(e=>{this.log({method:`.unregisterListeners()`},`Unregister "${e}" event listener.`),this.request.removeEventListener(e,this.onProgressChange)})),this):this.error(this.getXhrNotAvailableError())}};export{c as Xhr};
|
package/license.md
CHANGED
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alessiofrittoli/fetcher",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Typescript fetch/xhr utilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"module": "./dist/index.mjs",
|
|
39
39
|
"types": "./dist/index.d.mts",
|
|
40
40
|
"files": [
|
|
41
|
-
"dist"
|
|
41
|
+
"dist",
|
|
42
|
+
"scripts/postinstall"
|
|
42
43
|
],
|
|
43
44
|
"exports": {
|
|
44
45
|
".": {
|
|
@@ -64,39 +65,57 @@
|
|
|
64
65
|
},
|
|
65
66
|
"sideEffects": false,
|
|
66
67
|
"peerDependencies": {
|
|
67
|
-
"@alessiofrittoli/abort-controller": "^1.
|
|
68
|
-
"@alessiofrittoli/chain-functions": "^2.
|
|
69
|
-
"@alessiofrittoli/crypto-buffer": "^3.
|
|
70
|
-
"@alessiofrittoli/event-emitter": "^1.
|
|
71
|
-
"@alessiofrittoli/exception": "^4.
|
|
72
|
-
"@alessiofrittoli/math-utils": "^2.
|
|
73
|
-
"@alessiofrittoli/node-scripts": "^
|
|
74
|
-
"@alessiofrittoli/type-utils": "^2.
|
|
75
|
-
"@alessiofrittoli/url-utils": "^4.
|
|
76
|
-
|
|
68
|
+
"@alessiofrittoli/abort-controller": "^1.1.0",
|
|
69
|
+
"@alessiofrittoli/chain-functions": "^2.8.0",
|
|
70
|
+
"@alessiofrittoli/crypto-buffer": "^3.12.0",
|
|
71
|
+
"@alessiofrittoli/event-emitter": "^1.8.0",
|
|
72
|
+
"@alessiofrittoli/exception": "^4.2.0",
|
|
73
|
+
"@alessiofrittoli/math-utils": "^2.2.0",
|
|
74
|
+
"@alessiofrittoli/node-scripts": "^4.2.0",
|
|
75
|
+
"@alessiofrittoli/type-utils": "^2.1.0",
|
|
76
|
+
"@alessiofrittoli/url-utils": "^4.2.0"
|
|
77
|
+
},
|
|
78
|
+
"peerDependenciesMeta": {
|
|
79
|
+
"@alessiofrittoli/abort-controller": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"@alessiofrittoli/crypto-buffer": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"@alessiofrittoli/event-emitter": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"@alessiofrittoli/exception": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"@alessiofrittoli/math-utils": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"@alessiofrittoli/type-utils": {
|
|
95
|
+
"optional": true
|
|
96
|
+
}
|
|
77
97
|
},
|
|
78
98
|
"devDependencies": {
|
|
79
|
-
"@alessiofrittoli/abort-controller": "^1.
|
|
80
|
-
"@alessiofrittoli/chain-functions": "^2.
|
|
81
|
-
"@alessiofrittoli/crypto-buffer": "^3.
|
|
82
|
-
"@alessiofrittoli/event-emitter": "^1.
|
|
83
|
-
"@alessiofrittoli/exception": "^4.
|
|
84
|
-
"@alessiofrittoli/math-utils": "^2.
|
|
85
|
-
"@alessiofrittoli/node-scripts": "^4.
|
|
86
|
-
"@alessiofrittoli/package-configs": "^1.
|
|
87
|
-
"@alessiofrittoli/type-utils": "^2.
|
|
88
|
-
"@alessiofrittoli/url-utils": "^4.
|
|
89
|
-
"@alessiofrittoli/web-utils": "^2.4.0",
|
|
99
|
+
"@alessiofrittoli/abort-controller": "^1.1.0",
|
|
100
|
+
"@alessiofrittoli/chain-functions": "^2.8.0",
|
|
101
|
+
"@alessiofrittoli/crypto-buffer": "^3.12.0",
|
|
102
|
+
"@alessiofrittoli/event-emitter": "^1.8.0",
|
|
103
|
+
"@alessiofrittoli/exception": "^4.2.0",
|
|
104
|
+
"@alessiofrittoli/math-utils": "^2.2.0",
|
|
105
|
+
"@alessiofrittoli/node-scripts": "^4.2.0",
|
|
106
|
+
"@alessiofrittoli/package-configs": "^1.9.0",
|
|
107
|
+
"@alessiofrittoli/type-utils": "^2.1.0",
|
|
108
|
+
"@alessiofrittoli/url-utils": "^4.2.0",
|
|
90
109
|
"@types/node": "^24.19.1",
|
|
91
110
|
"@vitest/coverage-v8": "^5.0.5",
|
|
92
111
|
"concurrently": "^10.0.6",
|
|
93
112
|
"http-server": "^14.1.1",
|
|
94
113
|
"oxlint": "^1.89.0",
|
|
114
|
+
"oxlint-tsgolint": "^7.0.2005",
|
|
95
115
|
"prettier": "^3.9.10",
|
|
96
116
|
"prettier-plugin-sort-imports": "^1.8.11",
|
|
97
|
-
"tsdown": "^0.23.
|
|
117
|
+
"tsdown": "^0.23.2",
|
|
98
118
|
"typescript": "^6.0.3",
|
|
99
|
-
"vite": "^8.3.5",
|
|
100
119
|
"vitest": "^5.0.5"
|
|
101
120
|
},
|
|
102
121
|
"scripts": {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const { Chain } = require('@alessiofrittoli/chain-functions')
|
|
2
|
+
const project = require('../../package.json')
|
|
3
|
+
const tsSetup = require('./ts-setup.cjs')
|
|
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. Thank you for installing ${project.name}!`,
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @type {import( '@alessiofrittoli/chain-functions/types' ).ChainFactory<() => void | Promise<void>>} */
|
|
14
|
+
const chain = [tsSetup, lastChainLink]
|
|
15
|
+
|
|
16
|
+
// oxlint-disable-next-line typescript/no-floating-promises
|
|
17
|
+
Chain.functions(chain)()
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { addTypesReference } = require('@alessiofrittoli/node-scripts/postinstall')
|
|
2
|
+
const project = require('../../package.json')
|
|
3
|
+
|
|
4
|
+
/** @type {import( '@alessiofrittoli/chain-functions/types' ).ChainLink<() => void | Promise<void>>} */
|
|
5
|
+
const tsSetup = next => () => {
|
|
6
|
+
addTypesReference({
|
|
7
|
+
name: project.name,
|
|
8
|
+
scopes: [`${project.name}/globals`],
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
return next()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = tsSetup
|
|
@@ -1 +0,0 @@
|
|
|
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};
|
|
@@ -1 +0,0 @@
|
|
|
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;
|