Comparing version 1.1.1 to 1.2.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 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(p=>{throw new h("NETWORK_ERROR",p.message,e)});return clearTimeout(f),e.rawResponse=b,e.response=await m(b,e.responseType),e.data=e.response.data,e.headers=e.response.headers,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 h{constructor(s,t){super(t.statusText,s),this.response=t,this.name="FexiosResponseError"}}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"}})}); | ||
*/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"),e=await this.emit("beforeRequest",e);const c=e.abortController||new AbortController,u=new Request(e.url,{...n,signal:c.signal});e.rawRequest=u;const f=setTimeout(()=>{c.abort()},e.timeout||this.baseConfigs.timeout||60*1e3),b=await fetch(e.rawRequest).catch(p=>{throw new h("NETWORK_ERROR",p.message,e)});return clearTimeout(f),e.rawResponse=b,e.response=await m(b,e.responseType),e.data=e.response.data,e.headers=e.response.headers,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 h{constructor(s,t){super(t.statusText,s),this.response=t,this.name="FexiosResponseError"}}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 |
@@ -97,2 +97,3 @@ "use strict"; | ||
} | ||
ctx = await this.emit('beforeRequest', ctx); | ||
const abortController = ctx.abortController || new AbortController(); | ||
@@ -104,3 +105,2 @@ const rawRequest = new Request(ctx.url, { | ||
ctx.rawRequest = rawRequest; | ||
ctx = await this.emit('beforeRequest', ctx); | ||
const timer = setTimeout(() => { | ||
@@ -107,0 +107,0 @@ abortController.abort(); |
@@ -72,5 +72,6 @@ /** | ||
} | ||
export type FexiosFinalContext<T = any> = Omit<FexiosContext<T>, 'rawResponse' | 'response' | 'data'> & { | ||
export type FexiosFinalContext<T = any> = Omit<FexiosContext<T>, 'rawResponse' | 'response' | 'data' | 'headers'> & { | ||
rawResponse: Response; | ||
response: FexiosResponse<T>; | ||
headers: Headers; | ||
data: T; | ||
@@ -77,0 +78,0 @@ }; |
@@ -7,3 +7,3 @@ var Fexios=function(n){"use strict";/** | ||
* @author dragon-fish <dragon-fish@qq.com> | ||
*/class h{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 c("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 c("NETWORK_ERROR",T.message,e)});return clearTimeout(f),e.rawResponse=m,e.response=await b(m,e.responseType),e.data=e.response.data,e.headers=e.response.headers,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 c("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 c("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 h({...this.baseConfigs,...s});return t.hooks={...this.hooks},t}static create(s){return new h(s)}}class c extends Error{constructor(s,t,e){super(t),this.code=s,this.context=e,this.name="FexiosError"}}class l extends c{constructor(s,t){super(t.statusText,s),this.response=t,this.name="FexiosResponseError"}}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 h.create(r)}return typeof window<"u"&&(window.fexios=d()),n.Fexios=h,n.FexiosError=c,n.FexiosResponseError=l,n.createFexios=d,n.createFexiosResponse=b,n.default=g,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),n}({}); | ||
*/class h{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 c("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"),e=await this.emit("beforeRequest",e);const i=e.abortController||new AbortController,u=new Request(e.url,{...a,signal:i.signal});e.rawRequest=u;const f=setTimeout(()=>{i.abort()},e.timeout||this.baseConfigs.timeout||60*1e3),m=await fetch(e.rawRequest).catch(T=>{throw new c("NETWORK_ERROR",T.message,e)});return clearTimeout(f),e.rawResponse=m,e.response=await b(m,e.responseType),e.data=e.response.data,e.headers=e.response.headers,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 c("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 c("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 h({...this.baseConfigs,...s});return t.hooks={...this.hooks},t}static create(s){return new h(s)}}class c extends Error{constructor(s,t,e){super(t),this.code=s,this.context=e,this.name="FexiosError"}}class l extends c{constructor(s,t){super(t.statusText,s),this.response=t,this.name="FexiosResponseError"}}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 h.create(r)}return typeof window<"u"&&(window.fexios=d()),n.Fexios=h,n.FexiosError=c,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 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(p=>{throw new h("NETWORK_ERROR",p.message,e)});return clearTimeout(f),e.rawResponse=b,e.response=await m(b,e.responseType),e.data=e.response.data,e.headers=e.response.headers,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 h{constructor(s,t){super(t.statusText,s),this.response=t,this.name="FexiosResponseError"}}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"}})}); | ||
*/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"),e=await this.emit("beforeRequest",e);const c=e.abortController||new AbortController,u=new Request(e.url,{...n,signal:c.signal});e.rawRequest=u;const f=setTimeout(()=>{c.abort()},e.timeout||this.baseConfigs.timeout||60*1e3),b=await fetch(e.rawRequest).catch(p=>{throw new h("NETWORK_ERROR",p.message,e)});return clearTimeout(f),e.rawResponse=b,e.response=await m(b,e.responseType),e.data=e.response.data,e.headers=e.response.headers,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 h{constructor(s,t){super(t.statusText,s),this.response=t,this.name="FexiosResponseError"}}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 |
@@ -72,5 +72,6 @@ /** | ||
} | ||
export type FexiosFinalContext<T = any> = Omit<FexiosContext<T>, 'rawResponse' | 'response' | 'data'> & { | ||
export type FexiosFinalContext<T = any> = Omit<FexiosContext<T>, 'rawResponse' | 'response' | 'data' | 'headers'> & { | ||
rawResponse: Response; | ||
response: FexiosResponse<T>; | ||
headers: Headers; | ||
data: T; | ||
@@ -77,0 +78,0 @@ }; |
@@ -97,2 +97,3 @@ "use strict"; | ||
} | ||
ctx = await this.emit('beforeRequest', ctx); | ||
const abortController = ctx.abortController || new AbortController(); | ||
@@ -104,3 +105,2 @@ const rawRequest = new Request(ctx.url, { | ||
ctx.rawRequest = rawRequest; | ||
ctx = await this.emit('beforeRequest', ctx); | ||
const timer = setTimeout(() => { | ||
@@ -107,0 +107,0 @@ abortController.abort(); |
{ | ||
"name": "fexios", | ||
"version": "1.1.1", | ||
"version": "1.2.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
113678
965