@andrewcaires/fetch 1.1.1 → 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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2015-2022 Andrew Caires (https://github.com/andrewcaires)
3
+ Copyright (c) 2015-2026 Andrew Caires (https://github.com/andrewcaires)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.cjs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.1.1
3
- * Plugin for fetch api
4
- * (c) 2025 Andrew Caires
2
+ * @andrewcaires/fetch v2.0.0
3
+ * Plugin for fetch api
4
+ * (c) 2026 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- "use strict";var t=require("@andrewcaires/utils.js");function e(t,e,r,n){return new(r||(r=Promise))((function(o,i){function s(t){try{u(n.next(t))}catch(t){i(t)}}function a(t){try{u(n.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,a)}u((n=n.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const r={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class n extends t.EventEmitter{constructor(t){super(),this._options=Object.assign({headers:{},timeout:1e4},null!=t?t:{})}delete(t,r){return e(this,void 0,void 0,(function*(){return this.request({method:"delete",path:t,query:r})}))}static delete(t,r,o){return e(this,void 0,void 0,(function*(){return new n(o).delete(t,r)}))}get(t,r){return e(this,void 0,void 0,(function*(){return this.request({method:"get",path:t,query:r})}))}static get(t,r,o){return e(this,void 0,void 0,(function*(){return new n(o).get(t,r)}))}head(t,r){return e(this,void 0,void 0,(function*(){return this.request({method:"head",path:t,query:r})}))}static head(t,r,o){return e(this,void 0,void 0,(function*(){return new n(o).head(t,r)}))}options(t,r){return e(this,void 0,void 0,(function*(){return this.request({method:"options",path:t,query:r})}))}static options(t,r,o){return e(this,void 0,void 0,(function*(){return new n(o).options(t,r)}))}patch(t,r,n){return e(this,void 0,void 0,(function*(){return this.request({method:"patch",path:t,body:r,query:n})}))}static patch(t,r,o,i){return e(this,void 0,void 0,(function*(){return new n(i).patch(t,r,o)}))}post(t,r,n){return e(this,void 0,void 0,(function*(){return this.request({method:"post",path:t,body:r,query:n})}))}static post(t,r,o,i){return e(this,void 0,void 0,(function*(){return new n(i).post(t,r,o)}))}put(t,r,n){return e(this,void 0,void 0,(function*(){return this.request({method:"put",path:t,body:r,query:n})}))}static put(t,r,o,i){return e(this,void 0,void 0,(function*(){return new n(i).put(t,r,o)}))}request(t){return e(this,void 0,void 0,(function*(){const e=this._request(t),r={data:{}};try{this.emit("before",e),r.raw=yield fetch(e.url,e),r.data=yield this._data(r.raw),r.raw.ok||(r.error=this._status(r.raw)),this.emit("complete",r)}catch(t){this.emit("error",t),r.error=t.message}return r}))}static request(t,r){return e(this,void 0,void 0,(function*(){return new n(r).request(t)}))}_body(e){const r=e instanceof Blob,n=e instanceof FormData,o=e instanceof URLSearchParams,i=!(r||n||o||t.isString(e));return{json:i,parse:i?e?JSON.stringify(e):null:e}}_data(t){const e=this._header(t,"Content-Type").toLowerCase(),r=e.indexOf("text/html")>=0,n=e.indexOf("text/plain")>=0,o=e.indexOf("application/json")>=0;return!e||r||n?t.text():o?t.json():t.blob()}_header(t,e){var r;return null!==(r=t.headers.get(e))&&void 0!==r?r:""}_headers(t){return Object.assign(Object.assign({},this._options.headers),null!=t?t:{})}_request({url:t,path:e,query:r,method:n,body:o,headers:i,timeout:s}){t=this._url(t,e,r),n=n.toUpperCase(),i=this._headers(i);const{json:a,parse:u}=this._body(o);o&&a&&!i["Content-Type"]&&(i["Content-Type"]="application/json");return{url:t,method:n,body:u,headers:i,signal:this._signal(s)}}_signal(e){const r=new AbortController;return t.isUndefined(e)&&(e=this._options.timeout),e&&setTimeout((()=>r.abort()),e),r.signal}_status(t){var e;return t.statusText||(null!==(e=r[t.status])&&void 0!==e?e:"ERROR")}_url(e,r,n){const o=new URL(null!=r?r:"",null!=e?e:this._options.url);return n&&t.forEachKey(n,((t,e)=>o.searchParams.set(e,t))),o.toString()}}exports.Fetch=n,exports.FetchStatusText=r;
7
+ "use strict";var e=require("@andrewcaires/utils.js");const t={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class r extends e.EventEmitter{constructor(t){super(),this.baseOptions=e.merge({headers:{},timeout:1e4},null!=t?t:{})}async get(e,t){return this.request(this._buildParams(e,"GET",t))}static async get(e,t){return(new r).get(e,t)}async post(e,t){return this.request(this._buildParams(e,"POST",t))}static async post(e,t){return(new r).post(e,t)}async put(e,t){return this.request(this._buildParams(e,"PUT",t))}static async put(e,t){return(new r).put(e,t)}async patch(e,t){return this.request(this._buildParams(e,"PATCH",t))}static async patch(e,t){return(new r).patch(e,t)}async delete(e,t){return this.request(this._buildParams(e,"DELETE",t))}static async delete(e,t){return(new r).delete(e,t)}async head(e,t){return this.request(this._buildParams(e,"HEAD",t))}static async head(e,t){return(new r).head(e,t)}async options(e,t){return this.request(this._buildParams(e,"OPTIONS",t))}static async options(e,t){return(new r).options(e,t)}async request(e){const t={...this.baseOptions,...e,headers:{...this.baseOptions.headers,...e.headers}},r=this._buildRequest(t),s={data:{}};try{this.emit("before",r),s.raw=await fetch(r.url,r),s.data=await this._parseResponseData(s.raw),s.raw.ok||(s.error=this._resolveStatusText(s.raw)),this.emit("complete",s)}catch(e){this.emit("error",e),s.error=e.message}return s}static request(e){return(new r).request(e)}_buildUrl(t,r,s){const a=new URL(null!=r?r:"",t);return s?(e.forEachKey(s,((t,r)=>{e.isArray(t)?t.forEach((t=>e.isDef(t)&&a.searchParams.append(r,String(t)))):e.isDef(t)&&a.searchParams.set(r,String(t))})),a.toString()):a.toString()}_buildParams(e,t,r){return{...null!=r?r:{},path:e,method:t}}_buildRequest(e){var t;const r=this._buildUrl(e.url,e.path,e.query),s=e.method.toUpperCase(),{body:a,isJson:n}=this._serializeBody(e.body),i={...null!==(t=e.headers)&&void 0!==t?t:{}};n&&void 0!==a&&!i["Content-Type"]&&(i["Content-Type"]="application/json");const{signal:o,cleanup:u}=this._createAbortSignal(e.timeout);return{url:r,method:s,body:a,headers:i,credentials:e.credentials,signal:o,cleanup:u}}_createAbortSignal(e){if(!e)return{signal:void 0,cleanup:void 0};const t=new AbortController,r=setTimeout((()=>t.abort()),e);return{signal:t.signal,cleanup:()=>clearTimeout(r)}}_getHeader(e,t){var r;return null!==(r=e.headers.get(t))&&void 0!==r?r:""}async _parseResponseData(e){if(204===e.status)return null;const t=this._getHeader(e,"Content-Type").toLowerCase();return!t||t.includes("text/plain")||t.includes("text/html")?e.text():t.includes("application/json")?e.json():e.blob()}_serializeBody(t){if(null==t)return{isJson:!1,body:void 0};const r=!(t instanceof Blob||t instanceof FormData||t instanceof URLSearchParams||e.isString(t));return{isJson:r,body:r?JSON.stringify(t):t}}_resolveStatusText(e){return e.statusText||t[e.status]||"ERROR"}}exports.Fetch=r,exports.FetchStatusText=t;
package/dist/index.d.ts CHANGED
@@ -1,42 +1,35 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.1.1
3
- * Plugin for fetch api
4
- * (c) 2025 Andrew Caires
2
+ * @andrewcaires/fetch v2.0.0
3
+ * Plugin for fetch api
4
+ * (c) 2026 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- import { TypeObjectString, TypeNullable, EventEmitter } from '@andrewcaires/utils.js';
7
+ import { TypeObjectString, TypeObject, TypeScalar, TypeObjectOptional, EventEmitter } from '@andrewcaires/utils.js';
8
8
 
9
- interface FetchBody {
10
- json: boolean;
11
- parse: any;
9
+ interface FetchAbort {
10
+ signal?: AbortSignal;
11
+ cleanup?: () => void;
12
12
  }
13
13
 
14
- type FetchHeaders = TypeObjectString;
15
-
16
- interface FetchOptions {
17
- url?: string;
18
- headers?: FetchHeaders;
19
- timeout?: number;
14
+ interface FetchBody {
15
+ body?: any;
16
+ isJson: boolean;
20
17
  }
21
18
 
22
- type FetchQuery = TypeNullable<TypeObjectString>;
19
+ type FetchCredentials = "include" | "omit" | "same-origin";
23
20
 
24
- interface FetchParams {
25
- url?: string;
26
- path: string;
27
- query?: FetchQuery;
28
- method: string;
29
- body?: any;
30
- headers?: FetchHeaders;
31
- timeout?: number;
32
- }
21
+ type FetchHeaders = TypeObjectString;
22
+
23
+ type FetchMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
33
24
 
34
25
  interface FetchRequest {
35
26
  url: string;
36
- method: string;
27
+ method: FetchMethod;
37
28
  body: any;
38
- signal: AbortSignal;
39
29
  headers: FetchHeaders;
30
+ credentials?: FetchCredentials;
31
+ signal?: AbortSignal;
32
+ cleanup?: () => void;
40
33
  }
41
34
 
42
35
  interface FetchResponse<T = any> {
@@ -45,36 +38,67 @@ interface FetchResponse<T = any> {
45
38
  raw?: Response;
46
39
  }
47
40
 
48
- declare class Fetch extends EventEmitter {
49
- private _options;
41
+ interface FetchEvents {
42
+ before: FetchRequest;
43
+ retry: FetchRequest;
44
+ interception: FetchResponse;
45
+ complete: FetchResponse;
46
+ error: any;
47
+ }
48
+
49
+ interface FetchOptionsBase {
50
+ url?: string;
51
+ headers?: FetchHeaders;
52
+ timeout?: number;
53
+ credentials?: FetchCredentials;
54
+ }
55
+
56
+ interface FetchOptions extends FetchOptionsBase {
57
+ }
58
+
59
+ type FetchQuery = TypeObject<TypeScalar | Array<TypeScalar>>;
60
+
61
+ interface FetchOptionsRequest extends TypeObjectOptional<FetchOptions> {
62
+ path: string;
63
+ query?: FetchQuery;
64
+ method: FetchMethod;
65
+ body?: any;
66
+ }
67
+
68
+ type FetchOptionsBody = Omit<FetchOptionsRequest, "method" | "path">;
69
+
70
+ type FetchOptionsGet = Omit<FetchOptionsBody, "body">;
71
+
72
+ declare class Fetch<E extends FetchEvents = FetchEvents> extends EventEmitter<E> {
73
+ private baseOptions;
50
74
  constructor(options?: FetchOptions);
51
- delete<T = any>(path: string, query?: FetchQuery): Promise<FetchResponse<T>>;
52
- static delete<T = any>(path: string, query?: FetchQuery, options?: FetchOptions): Promise<FetchResponse<T>>;
53
- get<T = any>(path: string, query?: FetchQuery): Promise<FetchResponse<T>>;
54
- static get<T = any>(path: string, query?: FetchQuery, options?: FetchOptions): Promise<FetchResponse<T>>;
55
- head<T = any>(path: string, query?: FetchQuery): Promise<FetchResponse<T>>;
56
- static head<T = any>(path: string, query?: FetchQuery, options?: FetchOptions): Promise<FetchResponse<T>>;
57
- options<T = any>(path: string, query?: FetchQuery): Promise<FetchResponse<T>>;
58
- static options<T = any>(path: string, query?: FetchQuery, options?: FetchOptions): Promise<FetchResponse<T>>;
59
- patch<T = any>(path: string, body?: any, query?: FetchQuery): Promise<FetchResponse<T>>;
60
- static patch<T = any>(path: string, body?: any, query?: FetchQuery, options?: FetchOptions): Promise<FetchResponse<T>>;
61
- post<T = any>(path: string, body?: any, query?: FetchQuery): Promise<FetchResponse<T>>;
62
- static post<T = any>(path: string, body?: any, query?: FetchQuery, options?: FetchOptions): Promise<FetchResponse<T>>;
63
- put<T = any>(path: string, body?: any, query?: FetchQuery): Promise<FetchResponse<T>>;
64
- static put<T = any>(path: string, body?: any, query?: FetchQuery, options?: FetchOptions): Promise<FetchResponse<T>>;
65
- request<T = any>(params: FetchParams): Promise<FetchResponse<T>>;
66
- static request<T = any>(params: FetchParams, options?: FetchOptions): Promise<FetchResponse<T>>;
67
- private _body;
68
- private _data;
69
- private _header;
70
- private _headers;
71
- private _request;
72
- private _signal;
73
- private _status;
74
- private _url;
75
+ get<T = any>(path: string, options?: FetchOptionsGet): Promise<FetchResponse<T>>;
76
+ static get<T = any>(path: string, options?: FetchOptionsGet): Promise<FetchResponse<T>>;
77
+ post<T = any>(path: string, options?: FetchOptionsBody): Promise<FetchResponse<T>>;
78
+ static post<T = any>(path: string, options?: FetchOptionsBody): Promise<FetchResponse<T>>;
79
+ put<T = any>(path: string, options?: FetchOptionsBody): Promise<FetchResponse<T>>;
80
+ static put<T = any>(path: string, options?: FetchOptionsBody): Promise<FetchResponse<T>>;
81
+ patch<T = any>(path: string, options?: FetchOptionsBody): Promise<FetchResponse<T>>;
82
+ static patch<T = any>(path: string, options?: FetchOptionsBody): Promise<FetchResponse<T>>;
83
+ delete<T = any>(path: string, options?: FetchOptionsGet): Promise<FetchResponse<T>>;
84
+ static delete<T = any>(path: string, options?: FetchOptionsGet): Promise<FetchResponse<T>>;
85
+ head<T = any>(path: string, options?: FetchOptionsGet): Promise<FetchResponse<T>>;
86
+ static head<T = any>(path: string, options?: FetchOptionsGet): Promise<FetchResponse<T>>;
87
+ options<T = any>(path: string, options?: FetchOptionsGet): Promise<FetchResponse<T>>;
88
+ static options<T = any>(path: string, options?: FetchOptionsGet): Promise<FetchResponse<T>>;
89
+ request<T = any>(options: FetchOptionsRequest): Promise<FetchResponse<T>>;
90
+ static request<T = any>(options: FetchOptionsRequest): Promise<FetchResponse<T>>;
91
+ private _buildUrl;
92
+ private _buildParams;
93
+ private _buildRequest;
94
+ private _createAbortSignal;
95
+ private _getHeader;
96
+ private _parseResponseData;
97
+ private _serializeBody;
98
+ private _resolveStatusText;
75
99
  }
76
100
 
77
101
  declare const FetchStatusText: TypeObjectString;
78
102
 
79
103
  export { Fetch, FetchStatusText };
80
- export type { FetchBody, FetchHeaders, FetchOptions, FetchParams, FetchQuery, FetchRequest, FetchResponse };
104
+ export type { FetchAbort, FetchBody, FetchCredentials, FetchEvents, FetchHeaders, FetchMethod, FetchOptions, FetchOptionsBase, FetchOptionsBody, FetchOptionsGet, FetchOptionsRequest, FetchQuery, FetchRequest, FetchResponse };
package/dist/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.1.1
3
- * Plugin for fetch api
4
- * (c) 2025 Andrew Caires
2
+ * @andrewcaires/fetch v2.0.0
3
+ * Plugin for fetch api
4
+ * (c) 2026 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- import{EventEmitter as t,isString as e,isUndefined as n,forEachKey as r}from"@andrewcaires/utils.js";function o(t,e,n,r){return new(n||(n=Promise))((function(o,i){function s(t){try{u(r.next(t))}catch(t){i(t)}}function a(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const i={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class s extends t{constructor(t){super(),this._options=Object.assign({headers:{},timeout:1e4},null!=t?t:{})}delete(t,e){return o(this,void 0,void 0,(function*(){return this.request({method:"delete",path:t,query:e})}))}static delete(t,e,n){return o(this,void 0,void 0,(function*(){return new s(n).delete(t,e)}))}get(t,e){return o(this,void 0,void 0,(function*(){return this.request({method:"get",path:t,query:e})}))}static get(t,e,n){return o(this,void 0,void 0,(function*(){return new s(n).get(t,e)}))}head(t,e){return o(this,void 0,void 0,(function*(){return this.request({method:"head",path:t,query:e})}))}static head(t,e,n){return o(this,void 0,void 0,(function*(){return new s(n).head(t,e)}))}options(t,e){return o(this,void 0,void 0,(function*(){return this.request({method:"options",path:t,query:e})}))}static options(t,e,n){return o(this,void 0,void 0,(function*(){return new s(n).options(t,e)}))}patch(t,e,n){return o(this,void 0,void 0,(function*(){return this.request({method:"patch",path:t,body:e,query:n})}))}static patch(t,e,n,r){return o(this,void 0,void 0,(function*(){return new s(r).patch(t,e,n)}))}post(t,e,n){return o(this,void 0,void 0,(function*(){return this.request({method:"post",path:t,body:e,query:n})}))}static post(t,e,n,r){return o(this,void 0,void 0,(function*(){return new s(r).post(t,e,n)}))}put(t,e,n){return o(this,void 0,void 0,(function*(){return this.request({method:"put",path:t,body:e,query:n})}))}static put(t,e,n,r){return o(this,void 0,void 0,(function*(){return new s(r).put(t,e,n)}))}request(t){return o(this,void 0,void 0,(function*(){const e=this._request(t),n={data:{}};try{this.emit("before",e),n.raw=yield fetch(e.url,e),n.data=yield this._data(n.raw),n.raw.ok||(n.error=this._status(n.raw)),this.emit("complete",n)}catch(t){this.emit("error",t),n.error=t.message}return n}))}static request(t,e){return o(this,void 0,void 0,(function*(){return new s(e).request(t)}))}_body(t){const n=t instanceof Blob,r=t instanceof FormData,o=t instanceof URLSearchParams,i=!(n||r||o||e(t));return{json:i,parse:i?t?JSON.stringify(t):null:t}}_data(t){const e=this._header(t,"Content-Type").toLowerCase(),n=e.indexOf("text/html")>=0,r=e.indexOf("text/plain")>=0,o=e.indexOf("application/json")>=0;return!e||n||r?t.text():o?t.json():t.blob()}_header(t,e){var n;return null!==(n=t.headers.get(e))&&void 0!==n?n:""}_headers(t){return Object.assign(Object.assign({},this._options.headers),null!=t?t:{})}_request({url:t,path:e,query:n,method:r,body:o,headers:i,timeout:s}){t=this._url(t,e,n),r=r.toUpperCase(),i=this._headers(i);const{json:a,parse:u}=this._body(o);o&&a&&!i["Content-Type"]&&(i["Content-Type"]="application/json");return{url:t,method:r,body:u,headers:i,signal:this._signal(s)}}_signal(t){const e=new AbortController;return n(t)&&(t=this._options.timeout),t&&setTimeout((()=>e.abort()),t),e.signal}_status(t){var e;return t.statusText||(null!==(e=i[t.status])&&void 0!==e?e:"ERROR")}_url(t,e,n){const o=new URL(null!=e?e:"",null!=t?t:this._options.url);return n&&r(n,((t,e)=>o.searchParams.set(e,t))),o.toString()}}export{s as Fetch,i as FetchStatusText};
7
+ import{EventEmitter as e,merge as t,forEachKey as r,isArray as a,isDef as s,isString as n}from"@andrewcaires/utils.js";const i={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class o extends e{constructor(e){super(),this.baseOptions=t({headers:{},timeout:1e4},null!=e?e:{})}async get(e,t){return this.request(this._buildParams(e,"GET",t))}static async get(e,t){return(new o).get(e,t)}async post(e,t){return this.request(this._buildParams(e,"POST",t))}static async post(e,t){return(new o).post(e,t)}async put(e,t){return this.request(this._buildParams(e,"PUT",t))}static async put(e,t){return(new o).put(e,t)}async patch(e,t){return this.request(this._buildParams(e,"PATCH",t))}static async patch(e,t){return(new o).patch(e,t)}async delete(e,t){return this.request(this._buildParams(e,"DELETE",t))}static async delete(e,t){return(new o).delete(e,t)}async head(e,t){return this.request(this._buildParams(e,"HEAD",t))}static async head(e,t){return(new o).head(e,t)}async options(e,t){return this.request(this._buildParams(e,"OPTIONS",t))}static async options(e,t){return(new o).options(e,t)}async request(e){const t={...this.baseOptions,...e,headers:{...this.baseOptions.headers,...e.headers}},r=this._buildRequest(t),a={data:{}};try{this.emit("before",r),a.raw=await fetch(r.url,r),a.data=await this._parseResponseData(a.raw),a.raw.ok||(a.error=this._resolveStatusText(a.raw)),this.emit("complete",a)}catch(e){this.emit("error",e),a.error=e.message}return a}static request(e){return(new o).request(e)}_buildUrl(e,t,n){const i=new URL(null!=t?t:"",e);return n?(r(n,((e,t)=>{a(e)?e.forEach((e=>s(e)&&i.searchParams.append(t,String(e)))):s(e)&&i.searchParams.set(t,String(e))})),i.toString()):i.toString()}_buildParams(e,t,r){return{...null!=r?r:{},path:e,method:t}}_buildRequest(e){var t;const r=this._buildUrl(e.url,e.path,e.query),a=e.method.toUpperCase(),{body:s,isJson:n}=this._serializeBody(e.body),i={...null!==(t=e.headers)&&void 0!==t?t:{}};n&&void 0!==s&&!i["Content-Type"]&&(i["Content-Type"]="application/json");const{signal:o,cleanup:u}=this._createAbortSignal(e.timeout);return{url:r,method:a,body:s,headers:i,credentials:e.credentials,signal:o,cleanup:u}}_createAbortSignal(e){if(!e)return{signal:void 0,cleanup:void 0};const t=new AbortController,r=setTimeout((()=>t.abort()),e);return{signal:t.signal,cleanup:()=>clearTimeout(r)}}_getHeader(e,t){var r;return null!==(r=e.headers.get(t))&&void 0!==r?r:""}async _parseResponseData(e){if(204===e.status)return null;const t=this._getHeader(e,"Content-Type").toLowerCase();return!t||t.includes("text/plain")||t.includes("text/html")?e.text():t.includes("application/json")?e.json():e.blob()}_serializeBody(e){if(null==e)return{isJson:!1,body:void 0};const t=!(e instanceof Blob||e instanceof FormData||e instanceof URLSearchParams||n(e));return{isJson:t,body:t?JSON.stringify(e):e}}_resolveStatusText(e){return e.statusText||i[e.status]||"ERROR"}}export{o as Fetch,i as FetchStatusText};
package/dist/index.min.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.1.1
3
- * Plugin for fetch api
4
- * (c) 2025 Andrew Caires
2
+ * @andrewcaires/fetch v2.0.0
3
+ * Plugin for fetch api
4
+ * (c) 2026 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- var Fetch=function(t,e){"use strict";function n(t,e,n,r){return new(n||(n=Promise))((function(o,i){function s(t){try{a(r.next(t))}catch(t){i(t)}}function u(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,u)}a((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const r={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class o extends e.EventEmitter{constructor(t){super(),this._options=Object.assign({headers:{},timeout:1e4},null!=t?t:{})}delete(t,e){return n(this,void 0,void 0,(function*(){return this.request({method:"delete",path:t,query:e})}))}static delete(t,e,r){return n(this,void 0,void 0,(function*(){return new o(r).delete(t,e)}))}get(t,e){return n(this,void 0,void 0,(function*(){return this.request({method:"get",path:t,query:e})}))}static get(t,e,r){return n(this,void 0,void 0,(function*(){return new o(r).get(t,e)}))}head(t,e){return n(this,void 0,void 0,(function*(){return this.request({method:"head",path:t,query:e})}))}static head(t,e,r){return n(this,void 0,void 0,(function*(){return new o(r).head(t,e)}))}options(t,e){return n(this,void 0,void 0,(function*(){return this.request({method:"options",path:t,query:e})}))}static options(t,e,r){return n(this,void 0,void 0,(function*(){return new o(r).options(t,e)}))}patch(t,e,r){return n(this,void 0,void 0,(function*(){return this.request({method:"patch",path:t,body:e,query:r})}))}static patch(t,e,r,i){return n(this,void 0,void 0,(function*(){return new o(i).patch(t,e,r)}))}post(t,e,r){return n(this,void 0,void 0,(function*(){return this.request({method:"post",path:t,body:e,query:r})}))}static post(t,e,r,i){return n(this,void 0,void 0,(function*(){return new o(i).post(t,e,r)}))}put(t,e,r){return n(this,void 0,void 0,(function*(){return this.request({method:"put",path:t,body:e,query:r})}))}static put(t,e,r,i){return n(this,void 0,void 0,(function*(){return new o(i).put(t,e,r)}))}request(t){return n(this,void 0,void 0,(function*(){const e=this._request(t),n={data:{}};try{this.emit("before",e),n.raw=yield fetch(e.url,e),n.data=yield this._data(n.raw),n.raw.ok||(n.error=this._status(n.raw)),this.emit("complete",n)}catch(t){this.emit("error",t),n.error=t.message}return n}))}static request(t,e){return n(this,void 0,void 0,(function*(){return new o(e).request(t)}))}_body(t){const n=t instanceof Blob,r=t instanceof FormData,o=t instanceof URLSearchParams,i=!(n||r||o||e.isString(t));return{json:i,parse:i?t?JSON.stringify(t):null:t}}_data(t){const e=this._header(t,"Content-Type").toLowerCase(),n=e.indexOf("text/html")>=0,r=e.indexOf("text/plain")>=0,o=e.indexOf("application/json")>=0;return!e||n||r?t.text():o?t.json():t.blob()}_header(t,e){var n;return null!==(n=t.headers.get(e))&&void 0!==n?n:""}_headers(t){return Object.assign(Object.assign({},this._options.headers),null!=t?t:{})}_request({url:t,path:e,query:n,method:r,body:o,headers:i,timeout:s}){t=this._url(t,e,n),r=r.toUpperCase(),i=this._headers(i);const{json:u,parse:a}=this._body(o);o&&u&&!i["Content-Type"]&&(i["Content-Type"]="application/json");return{url:t,method:r,body:a,headers:i,signal:this._signal(s)}}_signal(t){const n=new AbortController;return e.isUndefined(t)&&(t=this._options.timeout),t&&setTimeout((()=>n.abort()),t),n.signal}_status(t){var e;return t.statusText||(null!==(e=r[t.status])&&void 0!==e?e:"ERROR")}_url(t,n,r){const o=new URL(null!=n?n:"",null!=t?t:this._options.url);return r&&e.forEachKey(r,((t,e)=>o.searchParams.set(e,t))),o.toString()}}return t.Fetch=o,t.FetchStatusText=r,t}({},UtilsJS);
7
+ var Fetch=function(e,t){"use strict";const r={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class s extends t.EventEmitter{constructor(e){super(),this.baseOptions=t.merge({headers:{},timeout:1e4},null!=e?e:{})}async get(e,t){return this.request(this._buildParams(e,"GET",t))}static async get(e,t){return(new s).get(e,t)}async post(e,t){return this.request(this._buildParams(e,"POST",t))}static async post(e,t){return(new s).post(e,t)}async put(e,t){return this.request(this._buildParams(e,"PUT",t))}static async put(e,t){return(new s).put(e,t)}async patch(e,t){return this.request(this._buildParams(e,"PATCH",t))}static async patch(e,t){return(new s).patch(e,t)}async delete(e,t){return this.request(this._buildParams(e,"DELETE",t))}static async delete(e,t){return(new s).delete(e,t)}async head(e,t){return this.request(this._buildParams(e,"HEAD",t))}static async head(e,t){return(new s).head(e,t)}async options(e,t){return this.request(this._buildParams(e,"OPTIONS",t))}static async options(e,t){return(new s).options(e,t)}async request(e){const t={...this.baseOptions,...e,headers:{...this.baseOptions.headers,...e.headers}},r=this._buildRequest(t),s={data:{}};try{this.emit("before",r),s.raw=await fetch(r.url,r),s.data=await this._parseResponseData(s.raw),s.raw.ok||(s.error=this._resolveStatusText(s.raw)),this.emit("complete",s)}catch(e){this.emit("error",e),s.error=e.message}return s}static request(e){return(new s).request(e)}_buildUrl(e,r,s){const a=new URL(null!=r?r:"",e);return s?(t.forEachKey(s,((e,r)=>{t.isArray(e)?e.forEach((e=>t.isDef(e)&&a.searchParams.append(r,String(e)))):t.isDef(e)&&a.searchParams.set(r,String(e))})),a.toString()):a.toString()}_buildParams(e,t,r){return{...null!=r?r:{},path:e,method:t}}_buildRequest(e){var t;const r=this._buildUrl(e.url,e.path,e.query),s=e.method.toUpperCase(),{body:a,isJson:n}=this._serializeBody(e.body),i={...null!==(t=e.headers)&&void 0!==t?t:{}};n&&void 0!==a&&!i["Content-Type"]&&(i["Content-Type"]="application/json");const{signal:o,cleanup:u}=this._createAbortSignal(e.timeout);return{url:r,method:s,body:a,headers:i,credentials:e.credentials,signal:o,cleanup:u}}_createAbortSignal(e){if(!e)return{signal:void 0,cleanup:void 0};const t=new AbortController,r=setTimeout((()=>t.abort()),e);return{signal:t.signal,cleanup:()=>clearTimeout(r)}}_getHeader(e,t){var r;return null!==(r=e.headers.get(t))&&void 0!==r?r:""}async _parseResponseData(e){if(204===e.status)return null;const t=this._getHeader(e,"Content-Type").toLowerCase();return!t||t.includes("text/plain")||t.includes("text/html")?e.text():t.includes("application/json")?e.json():e.blob()}_serializeBody(e){if(null==e)return{isJson:!1,body:void 0};const r=!(e instanceof Blob||e instanceof FormData||e instanceof URLSearchParams||t.isString(e));return{isJson:r,body:r?JSON.stringify(e):e}}_resolveStatusText(e){return e.statusText||r[e.status]||"ERROR"}}return e.Fetch=s,e.FetchStatusText=r,e}({},UtilsJS);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrewcaires/fetch",
3
- "version": "1.1.1",
3
+ "version": "2.0.0",
4
4
  "description": "Plugin for fetch api",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs.js",
@@ -21,7 +21,9 @@
21
21
  "dist"
22
22
  ],
23
23
  "scripts": {
24
- "build": "rollup --bundleConfigAsCjs -c rollup.config.js"
24
+ "dev": "tsx watch ./src/index.ts",
25
+ "build": "npm run lint && rollup --bundleConfigAsCjs -c rollup.config.js",
26
+ "lint": "eslint \"src/**/*.{ts,tsx,js,jsx}\" --max-warnings=0"
25
27
  },
26
28
  "repository": {
27
29
  "type": "git",
@@ -37,16 +39,18 @@
37
39
  },
38
40
  "homepage": "https://github.com/andrewcaires/npm#readme",
39
41
  "dependencies": {
40
- "@andrewcaires/utils.js": "^0.4.14"
42
+ "@andrewcaires/utils.js": "^0.4.32"
41
43
  },
42
44
  "devDependencies": {
43
- "@rollup/plugin-commonjs": "^28.0.6",
44
- "@rollup/plugin-terser": "^0.4.4",
45
- "@rollup/plugin-typescript": "^12.1.4",
46
- "eslint": "^9.34.0",
47
- "rollup": "^4.50.0",
48
- "rollup-plugin-dts": "^6.2.3",
45
+ "@eslint/js": "^10.0.1",
46
+ "@rollup/plugin-commonjs": "^29.0.2",
47
+ "@rollup/plugin-terser": "^1.0.0",
48
+ "@rollup/plugin-typescript": "^12.3.0",
49
+ "eslint": "^10.0.3",
50
+ "rollup": "^4.59.0",
51
+ "rollup-plugin-dts": "^6.4.0",
49
52
  "tslib": "^2.8.1",
50
- "typescript-eslint": "^8.42.0"
53
+ "tsx": "^4.21.0",
54
+ "typescript-eslint": "^8.57.0"
51
55
  }
52
56
  }