Comparing version 1.0.1 to 1.1.0
@@ -7,3 +7,3 @@ (function(a,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(a=typeof globalThis<"u"?globalThis:a||self,i(a.Fexios={}))})(this,function(a){"use strict";/** | ||
* @author dragon-fish <dragon-fish@qq.com> | ||
*/class i{constructor(s={}){this.baseConfigs=s,this.hooks={beforeInit:[],beforeRequest:[],afterResponse:[]},this.DEFAULT_CONFIGS={baseURL:"",timeout:60*1e3,credentials:"same-origin",headers:{"content-type":"application/json; charset=UTF-8"},query:{},responseType:"json"},this.METHODS_WITHOUT_BODY=["get","head","options"],this.makeMethodShortcut("get").makeMethodShortcut("post").makeMethodShortcut("put").makeMethodShortcut("patch").makeMethodShortcut("delete").makeMethodShortcut("head").makeMethodShortcut("options")}makeMethodShortcut(s){return Object.defineProperty(this,s,{value:(t,e,o)=>(this.METHODS_WITHOUT_BODY.includes(s.toLocaleLowerCase())?o=e:(o=o||{},o.body=e),this.request(t,{...o,method:s}))}),this}async request(s,t){var m,y;let e=t=t||{};e.url=s.toString(),e=await this.emit("beforeInit",e);const o=new URL(s.toString(),t.baseURL||this.baseConfigs.baseURL||((m=globalThis.location)==null?void 0:m.href));if(e.url=o.toString(),e.headers=this.mergeHeaders(this.baseConfigs.headers,t.headers),e.query=this.mergeQuery(this.baseConfigs.query,o.searchParams,t.query),o.search=new URLSearchParams(e.query).toString(),e.url=o.toString(),this.METHODS_WITHOUT_BODY.includes((y=e.method)==null?void 0:y.toLocaleLowerCase())&&e.body)throw new h("BODY_NOT_ALLOWED",`Request method "${e.method}" does not allow body`);const n={method:e.method||"GET",credentials:e.credentials,headers:e.headers};e.body&&(n.body=e.body instanceof FormData||e.body instanceof URLSearchParams?e.body:JSON.stringify(e.body)),e.body instanceof FormData?e.headers["content-type"]="multipart/form-data":e.body instanceof URLSearchParams?e.headers["content-type"]="application/x-www-form-urlencoded; charset=UTF-8":typeof e.body=="object"&&(e.headers["content-type"]="application/json; charset=UTF-8");const c=e.abortController||new AbortController,u=new Request(e.url,{...n,signal:c.signal});e.rawRequest=u,e=await this.emit("beforeRequest",e),setTimeout(()=>{c.abort()},e.timeout||this.baseConfigs.timeout||60*1e3);const d=await fetch(e.rawRequest).catch(T=>{throw new h("NETWORK_ERROR",T.message,e)});return e.rawResponse=d,e.response=await b(d,e.responseType),e.data=e.response.data,this.emit("afterResponse",e)}mergeQuery(s,...t){const e=new URLSearchParams(s);for(const o of t)new URLSearchParams(o).forEach((c,u)=>{e.set(u,c)});return Object.fromEntries(e)}mergeHeaders(s,...t){const e={},o=new Headers(s);for(const n of t)new Headers(n).forEach((u,d)=>{o.set(d,u)});return o.forEach((n,c)=>{e[c]=n}),e}async emit(s,t){const e=this.hooks[s]||[];try{for(const o of e){const n=await o(t);if(n===!1)throw new h("ABORTED_BY_HOOK",`Request aborted by hook "${o.name}"`,t);typeof n=="object"?t=n:globalThis["con".concat("sole")].warn(`Hook "${o.name}" should return a context object or false to abort request`)}}catch(o){return Promise.reject(o)}return t}on(s,t,e=!1){var o;if(typeof t!="function")throw new h("INVALID_HOOK",`Hook "${t}" should be a function`);return(o=this.hooks)[s]??(o[s]=[]),this.hooks[s][e?"unshift":"push"](t),this}get interceptors(){return{request:{use:(s,t=!1)=>this.on("beforeRequest",s,t)},response:{use:(s,t=!1)=>this.on("afterResponse",s,t)}}}extends(s){const t=new i({...this.baseConfigs,...s});return t.hooks={...this.hooks},t}static create(s){return new i(s)}}class h extends Error{constructor(s,t,e){super(t),this.code=s,this.context=e,this.name="FexiosError"}}class l extends Error{constructor(s,t){super(s),this.response=t,this.name="FexiosResponseError",this.code=t.statusText}}async function b(r,s="json"){let t;s==="blob"&&(t=await r.clone().blob().catch(()=>{})),t||(t=await r.clone().json().catch(()=>r.clone().text()));const e=r.ok&&r.status>=200&&r.status<300,o={rawResponse:r,data:t,ok:r.ok,status:r.status,isGood:e,statusText:r.statusText,headers:r.headers};if(!e)throw new l(`Request failed with status code ${r.status}`,o);return o}const w=f();function f(r){return i.create(r)}typeof window<"u"&&(window.fexios=f()),a.Fexios=i,a.FexiosError=h,a.FexiosResponseError=l,a.createFexios=f,a.createFexiosResponse=b,a.default=w,Object.defineProperties(a,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
*/class i{constructor(s={}){this.baseConfigs=s,this.hooks={beforeInit:[],beforeRequest:[],afterResponse:[]},this.DEFAULT_CONFIGS={baseURL:"",timeout:60*1e3,credentials:"same-origin",headers:{"content-type":"application/json; charset=UTF-8"},query:{},responseType:"json"},this.METHODS_WITHOUT_BODY=["get","head","options"],this.makeMethodShortcut("get").makeMethodShortcut("post").makeMethodShortcut("put").makeMethodShortcut("patch").makeMethodShortcut("delete").makeMethodShortcut("head").makeMethodShortcut("options")}makeMethodShortcut(s){return Object.defineProperty(this,s,{value:(t,e,o)=>(this.METHODS_WITHOUT_BODY.includes(s.toLocaleLowerCase())?o=e:(o=o||{},o.body=e),this.request(t,{...o,method:s}))}),this}async request(s,t){var y,w;let e=t=t||{};e.url=s.toString(),e=await this.emit("beforeInit",e);const o=new URL(s.toString(),t.baseURL||this.baseConfigs.baseURL||((y=globalThis.location)==null?void 0:y.href));if(e.url=o.toString(),e.headers=this.mergeHeaders(this.baseConfigs.headers,t.headers),e.query=this.mergeQuery(this.baseConfigs.query,o.searchParams,t.query),o.search=new URLSearchParams(e.query).toString(),e.url=o.toString(),this.METHODS_WITHOUT_BODY.includes((w=e.method)==null?void 0:w.toLocaleLowerCase())&&e.body)throw new h("BODY_NOT_ALLOWED",`Request method "${e.method}" does not allow body`);const n={method:e.method||"GET",credentials:e.credentials,headers:e.headers};e.body&&(n.body=e.body instanceof FormData||e.body instanceof URLSearchParams?e.body:JSON.stringify(e.body)),e.body instanceof FormData?e.headers["content-type"]="multipart/form-data":e.body instanceof URLSearchParams?e.headers["content-type"]="application/x-www-form-urlencoded; charset=UTF-8":typeof e.body=="object"&&(e.headers["content-type"]="application/json; charset=UTF-8");const c=e.abortController||new AbortController,u=new Request(e.url,{...n,signal:c.signal});e.rawRequest=u,e=await this.emit("beforeRequest",e);const f=setTimeout(()=>{c.abort()},e.timeout||this.baseConfigs.timeout||60*1e3),b=await fetch(e.rawRequest).catch(g=>{throw new h("NETWORK_ERROR",g.message,e)});return clearTimeout(f),e.rawResponse=b,e.response=await m(b,e.responseType),e.data=e.response.data,this.emit("afterResponse",e)}mergeQuery(s,...t){const e=new URLSearchParams(s);for(const o of t)new URLSearchParams(o).forEach((c,u)=>{e.set(u,c)});return Object.fromEntries(e)}mergeHeaders(s,...t){const e={},o=new Headers(s);for(const n of t)new Headers(n).forEach((u,f)=>{o.set(f,u)});return o.forEach((n,c)=>{e[c]=n}),e}async emit(s,t){const e=this.hooks[s]||[];try{for(const o of e){const n=await o(t);if(n===!1)throw new h("ABORTED_BY_HOOK",`Request aborted by hook "${o.name}"`,t);typeof n=="object"?t=n:globalThis["con".concat("sole")].warn(`Hook "${o.name}" should return a context object or false to abort request`)}}catch(o){return Promise.reject(o)}return t}on(s,t,e=!1){var o;if(typeof t!="function")throw new h("INVALID_HOOK",`Hook "${t}" should be a function`);return(o=this.hooks)[s]??(o[s]=[]),this.hooks[s][e?"unshift":"push"](t),this}get interceptors(){return{request:{use:(s,t=!1)=>this.on("beforeRequest",s,t)},response:{use:(s,t=!1)=>this.on("afterResponse",s,t)}}}extends(s){const t=new i({...this.baseConfigs,...s});return t.hooks={...this.hooks},t}static create(s){return new i(s)}}class h extends Error{constructor(s,t,e){super(t),this.code=s,this.context=e,this.name="FexiosError"}}class l extends Error{constructor(s,t){super(s),this.response=t,this.name="FexiosResponseError",this.code=t.statusText}}async function m(r,s="json"){let t;s==="blob"&&(t=await r.clone().blob().catch(()=>{})),t||(t=await r.clone().json().catch(()=>r.clone().text()));const e=r.ok&&r.status>=200&&r.status<300,o={rawResponse:r,data:t,ok:r.ok,status:r.status,isGood:e,statusText:r.statusText,headers:r.headers};if(!e)throw new l(`Request failed with status code ${r.status}`,o);return o}const T=d();function d(r){return i.create(r)}typeof window<"u"&&(window.fexios=d()),a.Fexios=i,a.FexiosError=h,a.FexiosResponseError=l,a.createFexios=d,a.createFexiosResponse=m,a.default=T,Object.defineProperties(a,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -104,3 +104,3 @@ "use strict"; | ||
ctx = await this.emit('beforeRequest', ctx); | ||
setTimeout(() => { | ||
const timer = setTimeout(() => { | ||
abortController.abort(); | ||
@@ -111,2 +111,3 @@ }, ctx.timeout || this.baseConfigs.timeout || 60 * 1000); | ||
}); | ||
clearTimeout(timer); | ||
ctx.rawResponse = rawResponse; | ||
@@ -113,0 +114,0 @@ ctx.response = await createFexiosResponse(rawResponse, ctx.responseType); |
@@ -7,3 +7,3 @@ var Fexios=function(n){"use strict";/** | ||
* @author dragon-fish <dragon-fish@qq.com> | ||
*/class c{constructor(s={}){this.baseConfigs=s,this.hooks={beforeInit:[],beforeRequest:[],afterResponse:[]},this.DEFAULT_CONFIGS={baseURL:"",timeout:6e4,credentials:"same-origin",headers:{"content-type":"application/json; charset=UTF-8"},query:{},responseType:"json"},this.METHODS_WITHOUT_BODY=["get","head","options"],this.makeMethodShortcut("get").makeMethodShortcut("post").makeMethodShortcut("put").makeMethodShortcut("patch").makeMethodShortcut("delete").makeMethodShortcut("head").makeMethodShortcut("options")}makeMethodShortcut(s){return Object.defineProperty(this,s,{value:(t,e,o)=>(this.METHODS_WITHOUT_BODY.includes(s.toLocaleLowerCase())?o=e:(o=o||{},o.body=e),this.request(t,{...o,method:s}))}),this}async request(s,t){var m,y;let e=t=t||{};e.url=s.toString(),e=await this.emit("beforeInit",e);const o=new URL(s.toString(),t.baseURL||this.baseConfigs.baseURL||((m=globalThis.location)==null?void 0:m.href));if(e.url=o.toString(),e.headers=this.mergeHeaders(this.baseConfigs.headers,t.headers),e.query=this.mergeQuery(this.baseConfigs.query,o.searchParams,t.query),o.search=new URLSearchParams(e.query).toString(),e.url=o.toString(),this.METHODS_WITHOUT_BODY.includes((y=e.method)==null?void 0:y.toLocaleLowerCase())&&e.body)throw new h("BODY_NOT_ALLOWED",`Request method "${e.method}" does not allow body`);const a={method:e.method||"GET",credentials:e.credentials,headers:e.headers};e.body&&(a.body=e.body instanceof FormData||e.body instanceof URLSearchParams?e.body:JSON.stringify(e.body)),e.body instanceof FormData?e.headers["content-type"]="multipart/form-data":e.body instanceof URLSearchParams?e.headers["content-type"]="application/x-www-form-urlencoded; charset=UTF-8":typeof e.body=="object"&&(e.headers["content-type"]="application/json; charset=UTF-8");const i=e.abortController||new AbortController,u=new Request(e.url,{...a,signal:i.signal});e.rawRequest=u,e=await this.emit("beforeRequest",e),setTimeout(()=>{i.abort()},e.timeout||this.baseConfigs.timeout||6e4);const d=await fetch(e.rawRequest).catch(g=>{throw new h("NETWORK_ERROR",g.message,e)});return e.rawResponse=d,e.response=await b(d,e.responseType),e.data=e.response.data,this.emit("afterResponse",e)}mergeQuery(s,...t){const e=new URLSearchParams(s);for(const o of t)new URLSearchParams(o).forEach((i,u)=>{e.set(u,i)});return Object.fromEntries(e)}mergeHeaders(s,...t){const e={},o=new Headers(s);for(const a of t)new Headers(a).forEach((u,d)=>{o.set(d,u)});return o.forEach((a,i)=>{e[i]=a}),e}async emit(s,t){const e=this.hooks[s]||[];try{for(const o of e){const a=await o(t);if(a===!1)throw new h("ABORTED_BY_HOOK",`Request aborted by hook "${o.name}"`,t);typeof a=="object"?t=a:globalThis["con".concat("sole")].warn(`Hook "${o.name}" should return a context object or false to abort request`)}}catch(o){return Promise.reject(o)}return t}on(s,t,e=!1){var o;if(typeof t!="function")throw new h("INVALID_HOOK",`Hook "${t}" should be a function`);return(o=this.hooks)[s]??(o[s]=[]),this.hooks[s][e?"unshift":"push"](t),this}get interceptors(){return{request:{use:(s,t=!1)=>this.on("beforeRequest",s,t)},response:{use:(s,t=!1)=>this.on("afterResponse",s,t)}}}extends(s){const t=new c({...this.baseConfigs,...s});return t.hooks={...this.hooks},t}static create(s){return new c(s)}}class h extends Error{constructor(s,t,e){super(t),this.code=s,this.context=e,this.name="FexiosError"}}class l extends Error{constructor(s,t){super(s),this.response=t,this.name="FexiosResponseError",this.code=t.statusText}}async function b(r,s="json"){let t;s==="blob"&&(t=await r.clone().blob().catch(()=>{})),t||(t=await r.clone().json().catch(()=>r.clone().text()));const e=r.ok&&r.status>=200&&r.status<300,o={rawResponse:r,data:t,ok:r.ok,status:r.status,isGood:e,statusText:r.statusText,headers:r.headers};if(!e)throw new l(`Request failed with status code ${r.status}`,o);return o}const w=f();function f(r){return c.create(r)}return typeof window<"u"&&(window.fexios=f()),n.Fexios=c,n.FexiosError=h,n.FexiosResponseError=l,n.createFexios=f,n.createFexiosResponse=b,n.default=w,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),n}({}); | ||
*/class c{constructor(s={}){this.baseConfigs=s,this.hooks={beforeInit:[],beforeRequest:[],afterResponse:[]},this.DEFAULT_CONFIGS={baseURL:"",timeout:6e4,credentials:"same-origin",headers:{"content-type":"application/json; charset=UTF-8"},query:{},responseType:"json"},this.METHODS_WITHOUT_BODY=["get","head","options"],this.makeMethodShortcut("get").makeMethodShortcut("post").makeMethodShortcut("put").makeMethodShortcut("patch").makeMethodShortcut("delete").makeMethodShortcut("head").makeMethodShortcut("options")}makeMethodShortcut(s){return Object.defineProperty(this,s,{value:(t,e,o)=>(this.METHODS_WITHOUT_BODY.includes(s.toLocaleLowerCase())?o=e:(o=o||{},o.body=e),this.request(t,{...o,method:s}))}),this}async request(s,t){var y,w;let e=t=t||{};e.url=s.toString(),e=await this.emit("beforeInit",e);const o=new URL(s.toString(),t.baseURL||this.baseConfigs.baseURL||((y=globalThis.location)==null?void 0:y.href));if(e.url=o.toString(),e.headers=this.mergeHeaders(this.baseConfigs.headers,t.headers),e.query=this.mergeQuery(this.baseConfigs.query,o.searchParams,t.query),o.search=new URLSearchParams(e.query).toString(),e.url=o.toString(),this.METHODS_WITHOUT_BODY.includes((w=e.method)==null?void 0:w.toLocaleLowerCase())&&e.body)throw new h("BODY_NOT_ALLOWED",`Request method "${e.method}" does not allow body`);const a={method:e.method||"GET",credentials:e.credentials,headers:e.headers};e.body&&(a.body=e.body instanceof FormData||e.body instanceof URLSearchParams?e.body:JSON.stringify(e.body)),e.body instanceof FormData?e.headers["content-type"]="multipart/form-data":e.body instanceof URLSearchParams?e.headers["content-type"]="application/x-www-form-urlencoded; charset=UTF-8":typeof e.body=="object"&&(e.headers["content-type"]="application/json; charset=UTF-8");const i=e.abortController||new AbortController,u=new Request(e.url,{...a,signal:i.signal});e.rawRequest=u,e=await this.emit("beforeRequest",e);const f=setTimeout(()=>{i.abort()},e.timeout||this.baseConfigs.timeout||60*1e3),m=await fetch(e.rawRequest).catch(T=>{throw new h("NETWORK_ERROR",T.message,e)});return clearTimeout(f),e.rawResponse=m,e.response=await b(m,e.responseType),e.data=e.response.data,this.emit("afterResponse",e)}mergeQuery(s,...t){const e=new URLSearchParams(s);for(const o of t)new URLSearchParams(o).forEach((i,u)=>{e.set(u,i)});return Object.fromEntries(e)}mergeHeaders(s,...t){const e={},o=new Headers(s);for(const a of t)new Headers(a).forEach((u,f)=>{o.set(f,u)});return o.forEach((a,i)=>{e[i]=a}),e}async emit(s,t){const e=this.hooks[s]||[];try{for(const o of e){const a=await o(t);if(a===!1)throw new h("ABORTED_BY_HOOK",`Request aborted by hook "${o.name}"`,t);typeof a=="object"?t=a:globalThis["con".concat("sole")].warn(`Hook "${o.name}" should return a context object or false to abort request`)}}catch(o){return Promise.reject(o)}return t}on(s,t,e=!1){var o;if(typeof t!="function")throw new h("INVALID_HOOK",`Hook "${t}" should be a function`);return(o=this.hooks)[s]??(o[s]=[]),this.hooks[s][e?"unshift":"push"](t),this}get interceptors(){return{request:{use:(s,t=!1)=>this.on("beforeRequest",s,t)},response:{use:(s,t=!1)=>this.on("afterResponse",s,t)}}}extends(s){const t=new c({...this.baseConfigs,...s});return t.hooks={...this.hooks},t}static create(s){return new c(s)}}class h extends Error{constructor(s,t,e){super(t),this.code=s,this.context=e,this.name="FexiosError"}}class l extends Error{constructor(s,t){super(s),this.response=t,this.name="FexiosResponseError",this.code=t.statusText}}async function b(r,s="json"){let t;s==="blob"&&(t=await r.clone().blob().catch(()=>{})),t||(t=await r.clone().json().catch(()=>r.clone().text()));const e=r.ok&&r.status>=200&&r.status<300,o={rawResponse:r,data:t,ok:r.ok,status:r.status,isGood:e,statusText:r.statusText,headers:r.headers};if(!e)throw new l(`Request failed with status code ${r.status}`,o);return o}const g=d();function d(r){return c.create(r)}return typeof window<"u"&&(window.fexios=d()),n.Fexios=c,n.FexiosError=h,n.FexiosResponseError=l,n.createFexios=d,n.createFexiosResponse=b,n.default=g,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),n}({}); | ||
//# sourceMappingURL=index.iife.js.map |
@@ -7,3 +7,3 @@ (function(a,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(a=typeof globalThis<"u"?globalThis:a||self,i(a.Fexios={}))})(this,function(a){"use strict";/** | ||
* @author dragon-fish <dragon-fish@qq.com> | ||
*/class i{constructor(s={}){this.baseConfigs=s,this.hooks={beforeInit:[],beforeRequest:[],afterResponse:[]},this.DEFAULT_CONFIGS={baseURL:"",timeout:60*1e3,credentials:"same-origin",headers:{"content-type":"application/json; charset=UTF-8"},query:{},responseType:"json"},this.METHODS_WITHOUT_BODY=["get","head","options"],this.makeMethodShortcut("get").makeMethodShortcut("post").makeMethodShortcut("put").makeMethodShortcut("patch").makeMethodShortcut("delete").makeMethodShortcut("head").makeMethodShortcut("options")}makeMethodShortcut(s){return Object.defineProperty(this,s,{value:(t,e,o)=>(this.METHODS_WITHOUT_BODY.includes(s.toLocaleLowerCase())?o=e:(o=o||{},o.body=e),this.request(t,{...o,method:s}))}),this}async request(s,t){var m,y;let e=t=t||{};e.url=s.toString(),e=await this.emit("beforeInit",e);const o=new URL(s.toString(),t.baseURL||this.baseConfigs.baseURL||((m=globalThis.location)==null?void 0:m.href));if(e.url=o.toString(),e.headers=this.mergeHeaders(this.baseConfigs.headers,t.headers),e.query=this.mergeQuery(this.baseConfigs.query,o.searchParams,t.query),o.search=new URLSearchParams(e.query).toString(),e.url=o.toString(),this.METHODS_WITHOUT_BODY.includes((y=e.method)==null?void 0:y.toLocaleLowerCase())&&e.body)throw new h("BODY_NOT_ALLOWED",`Request method "${e.method}" does not allow body`);const n={method:e.method||"GET",credentials:e.credentials,headers:e.headers};e.body&&(n.body=e.body instanceof FormData||e.body instanceof URLSearchParams?e.body:JSON.stringify(e.body)),e.body instanceof FormData?e.headers["content-type"]="multipart/form-data":e.body instanceof URLSearchParams?e.headers["content-type"]="application/x-www-form-urlencoded; charset=UTF-8":typeof e.body=="object"&&(e.headers["content-type"]="application/json; charset=UTF-8");const c=e.abortController||new AbortController,u=new Request(e.url,{...n,signal:c.signal});e.rawRequest=u,e=await this.emit("beforeRequest",e),setTimeout(()=>{c.abort()},e.timeout||this.baseConfigs.timeout||60*1e3);const d=await fetch(e.rawRequest).catch(T=>{throw new h("NETWORK_ERROR",T.message,e)});return e.rawResponse=d,e.response=await b(d,e.responseType),e.data=e.response.data,this.emit("afterResponse",e)}mergeQuery(s,...t){const e=new URLSearchParams(s);for(const o of t)new URLSearchParams(o).forEach((c,u)=>{e.set(u,c)});return Object.fromEntries(e)}mergeHeaders(s,...t){const e={},o=new Headers(s);for(const n of t)new Headers(n).forEach((u,d)=>{o.set(d,u)});return o.forEach((n,c)=>{e[c]=n}),e}async emit(s,t){const e=this.hooks[s]||[];try{for(const o of e){const n=await o(t);if(n===!1)throw new h("ABORTED_BY_HOOK",`Request aborted by hook "${o.name}"`,t);typeof n=="object"?t=n:globalThis["con".concat("sole")].warn(`Hook "${o.name}" should return a context object or false to abort request`)}}catch(o){return Promise.reject(o)}return t}on(s,t,e=!1){var o;if(typeof t!="function")throw new h("INVALID_HOOK",`Hook "${t}" should be a function`);return(o=this.hooks)[s]??(o[s]=[]),this.hooks[s][e?"unshift":"push"](t),this}get interceptors(){return{request:{use:(s,t=!1)=>this.on("beforeRequest",s,t)},response:{use:(s,t=!1)=>this.on("afterResponse",s,t)}}}extends(s){const t=new i({...this.baseConfigs,...s});return t.hooks={...this.hooks},t}static create(s){return new i(s)}}class h extends Error{constructor(s,t,e){super(t),this.code=s,this.context=e,this.name="FexiosError"}}class l extends Error{constructor(s,t){super(s),this.response=t,this.name="FexiosResponseError",this.code=t.statusText}}async function b(r,s="json"){let t;s==="blob"&&(t=await r.clone().blob().catch(()=>{})),t||(t=await r.clone().json().catch(()=>r.clone().text()));const e=r.ok&&r.status>=200&&r.status<300,o={rawResponse:r,data:t,ok:r.ok,status:r.status,isGood:e,statusText:r.statusText,headers:r.headers};if(!e)throw new l(`Request failed with status code ${r.status}`,o);return o}const w=f();function f(r){return i.create(r)}typeof window<"u"&&(window.fexios=f()),a.Fexios=i,a.FexiosError=h,a.FexiosResponseError=l,a.createFexios=f,a.createFexiosResponse=b,a.default=w,Object.defineProperties(a,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
*/class i{constructor(s={}){this.baseConfigs=s,this.hooks={beforeInit:[],beforeRequest:[],afterResponse:[]},this.DEFAULT_CONFIGS={baseURL:"",timeout:60*1e3,credentials:"same-origin",headers:{"content-type":"application/json; charset=UTF-8"},query:{},responseType:"json"},this.METHODS_WITHOUT_BODY=["get","head","options"],this.makeMethodShortcut("get").makeMethodShortcut("post").makeMethodShortcut("put").makeMethodShortcut("patch").makeMethodShortcut("delete").makeMethodShortcut("head").makeMethodShortcut("options")}makeMethodShortcut(s){return Object.defineProperty(this,s,{value:(t,e,o)=>(this.METHODS_WITHOUT_BODY.includes(s.toLocaleLowerCase())?o=e:(o=o||{},o.body=e),this.request(t,{...o,method:s}))}),this}async request(s,t){var y,w;let e=t=t||{};e.url=s.toString(),e=await this.emit("beforeInit",e);const o=new URL(s.toString(),t.baseURL||this.baseConfigs.baseURL||((y=globalThis.location)==null?void 0:y.href));if(e.url=o.toString(),e.headers=this.mergeHeaders(this.baseConfigs.headers,t.headers),e.query=this.mergeQuery(this.baseConfigs.query,o.searchParams,t.query),o.search=new URLSearchParams(e.query).toString(),e.url=o.toString(),this.METHODS_WITHOUT_BODY.includes((w=e.method)==null?void 0:w.toLocaleLowerCase())&&e.body)throw new h("BODY_NOT_ALLOWED",`Request method "${e.method}" does not allow body`);const n={method:e.method||"GET",credentials:e.credentials,headers:e.headers};e.body&&(n.body=e.body instanceof FormData||e.body instanceof URLSearchParams?e.body:JSON.stringify(e.body)),e.body instanceof FormData?e.headers["content-type"]="multipart/form-data":e.body instanceof URLSearchParams?e.headers["content-type"]="application/x-www-form-urlencoded; charset=UTF-8":typeof e.body=="object"&&(e.headers["content-type"]="application/json; charset=UTF-8");const c=e.abortController||new AbortController,u=new Request(e.url,{...n,signal:c.signal});e.rawRequest=u,e=await this.emit("beforeRequest",e);const f=setTimeout(()=>{c.abort()},e.timeout||this.baseConfigs.timeout||60*1e3),b=await fetch(e.rawRequest).catch(g=>{throw new h("NETWORK_ERROR",g.message,e)});return clearTimeout(f),e.rawResponse=b,e.response=await m(b,e.responseType),e.data=e.response.data,this.emit("afterResponse",e)}mergeQuery(s,...t){const e=new URLSearchParams(s);for(const o of t)new URLSearchParams(o).forEach((c,u)=>{e.set(u,c)});return Object.fromEntries(e)}mergeHeaders(s,...t){const e={},o=new Headers(s);for(const n of t)new Headers(n).forEach((u,f)=>{o.set(f,u)});return o.forEach((n,c)=>{e[c]=n}),e}async emit(s,t){const e=this.hooks[s]||[];try{for(const o of e){const n=await o(t);if(n===!1)throw new h("ABORTED_BY_HOOK",`Request aborted by hook "${o.name}"`,t);typeof n=="object"?t=n:globalThis["con".concat("sole")].warn(`Hook "${o.name}" should return a context object or false to abort request`)}}catch(o){return Promise.reject(o)}return t}on(s,t,e=!1){var o;if(typeof t!="function")throw new h("INVALID_HOOK",`Hook "${t}" should be a function`);return(o=this.hooks)[s]??(o[s]=[]),this.hooks[s][e?"unshift":"push"](t),this}get interceptors(){return{request:{use:(s,t=!1)=>this.on("beforeRequest",s,t)},response:{use:(s,t=!1)=>this.on("afterResponse",s,t)}}}extends(s){const t=new i({...this.baseConfigs,...s});return t.hooks={...this.hooks},t}static create(s){return new i(s)}}class h extends Error{constructor(s,t,e){super(t),this.code=s,this.context=e,this.name="FexiosError"}}class l extends Error{constructor(s,t){super(s),this.response=t,this.name="FexiosResponseError",this.code=t.statusText}}async function m(r,s="json"){let t;s==="blob"&&(t=await r.clone().blob().catch(()=>{})),t||(t=await r.clone().json().catch(()=>r.clone().text()));const e=r.ok&&r.status>=200&&r.status<300,o={rawResponse:r,data:t,ok:r.ok,status:r.status,isGood:e,statusText:r.statusText,headers:r.headers};if(!e)throw new l(`Request failed with status code ${r.status}`,o);return o}const T=d();function d(r){return i.create(r)}typeof window<"u"&&(window.fexios=d()),a.Fexios=i,a.FexiosError=h,a.FexiosResponseError=l,a.createFexios=d,a.createFexiosResponse=m,a.default=T,Object.defineProperties(a,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -104,3 +104,3 @@ "use strict"; | ||
ctx = await this.emit('beforeRequest', ctx); | ||
setTimeout(() => { | ||
const timer = setTimeout(() => { | ||
abortController.abort(); | ||
@@ -111,2 +111,3 @@ }, ctx.timeout || this.baseConfigs.timeout || 60 * 1000); | ||
}); | ||
clearTimeout(timer); | ||
ctx.rawResponse = rawResponse; | ||
@@ -113,0 +114,0 @@ ctx.response = await createFexiosResponse(rawResponse, ctx.responseType); |
{ | ||
"name": "fexios", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Fetch based HTTP client with similar API to axios for browser and Node.js", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
112989
963