diff --git a/dist/retrieve.d.ts b/dist/retrieve.d.ts index cdbe94a..4d0e40e 100644 --- a/dist/retrieve.d.ts +++ b/dist/retrieve.d.ts @@ -247,4 +247,4 @@ type ResponseSuccessHandler = (responseObj: RetrieveResponse) => RetrieveRespons type ResponseErrorHandler = (responseError: ResponseError, responseObj: RetrieveResponse | undefined, ...fetchParams: RetrieveFetchParams) => ErrorHandlerResult | Promise>; declare function retrieve(config: RetrieveConfig): Promise; -export { BeforeRequestHandler, ErrorCorrectedResult, ErrorMaintainedResult, RequestErrorHandler, ResponseError, ResponseErrorHandler, ResponseSuccessHandler, RetrieveConfig, RetrieveFetchParams, RetrieveResponse, retrieve }; +export { type BeforeRequestHandler, type ErrorCorrectedResult, type ErrorMaintainedResult, type RequestErrorHandler, ResponseError, type ResponseErrorHandler, type ResponseSuccessHandler, type RetrieveConfig, type RetrieveFetchParams, type RetrieveResponse, retrieve }; diff --git a/dist/retrieve.js b/dist/retrieve.js index f0a0a6d..c28d596 100644 --- a/dist/retrieve.js +++ b/dist/retrieve.js @@ -1 +1 @@ -var p=class extends Error{name="ResponseError";response;constructor(t,r,s){super(r||`${t.status} ${t.statusText}`.trim(),s),this.response=t}toJSON(){return{name:this.name,message:this.message}}};var u="content-type",f="multipart/form-data",R="application/json",y="application/problem+json",c="application/octet-stream",m="plain/text",T={arrayBuffer:c,blob:c,formData:f,json:R,text:m};async function v(e){let t=H(e),r=P(e),s=[t,r];for(let n of e.beforeRequestHandlers??[])s=await n(...s);let a;try{a=await fetch(...s)}catch(n){let i=q(n,e.requestErrorMessage);for(let h of e.requestErrorHandlers??[]){let d=await h(i,...s);if(d.status==="corrected"){a=d.value;break}else i=d.value}if(!a)throw i}let o=await E(a);for(let n of e.responseSuccessHandlers??[])return o.response.ok&&(o=await n(o)),o;let l=new p(a,e.responseErrorMessage);for(let n of e.responseErrorHandlers??[]){let i=await n(l,o,...s);i.status==="corrected"?o=await E(i.value):l=i.value}if(o.response.ok)return o;throw l}function H(e){let t=new URL(e.url,e.baseUrl??window.location.origin);if(e.params){let r=e.params instanceof URLSearchParams?e.params:new URLSearchParams(e.params);for(let[s,a]of r)t.searchParams.set(s,a)}return t}function P(e){let t=e.init??{},r={...t};r.method=(t.method??"GET").toUpperCase(),r.headers=new Headers(t.headers),r.headers.has("x-request-with")||r.headers.set("x-requested-with","XMLHttpRequest");let s;if("data"in e){let a=r.headers.get(u);e.data instanceof ArrayBuffer?s="arrayBuffer":e.data instanceof Blob?s="blob":e.data instanceof FormData?s="formData":a?.startsWith(R)||a===null&&!["GET","HEAD"].includes(r.method)?s="json":typeof e.data=="string"&&(s="text")}return s==="formData"?r.headers.delete(u):s&&!r.headers.has(u)&&r.headers.set(u,T[s]),"data"in e?r.body=s==="json"?JSON.stringify(e.data):e.data:"body"in t&&(r.body=t.body),e.timeout&&!("signal"in r)&&(r.signal=AbortSignal.timeout(e.timeout)),r}function q(e,t){let r=e instanceof Error?e:new Error;return r.message&&(r.cause=r.message),t?r.message=t:typeof e=="string"&&e.length>0?r.message=e:r.message||(r.message="Unknown request error"),r}async function E(e){let t=e.headers.get(u)??"",r;t.startsWith(R)||t.startsWith(y)?r="json":t.startsWith(f)?r="formData":t.startsWith(m)&&(r="text");try{let s=r?await e[r]():null;return{response:e,data:s}}catch(s){let a=s,o={};throw"cause"in a&&(o.cause=a.cause),new p(e,a.message,o)}}export{p as ResponseError,v as retrieve}; +var p=class extends Error{name="ResponseError";response;constructor(t,r,s){super(r||`${t.status} ${t.statusText}`.trim(),s),this.response=t}toJSON(){return{name:this.name,message:this.message}}};var u="content-type",f="multipart/form-data",R="application/json",y="application/problem+json",c="application/octet-stream",m="plain/text",T={arrayBuffer:c,blob:c,formData:f,json:R,text:m};async function v(e){let t=H(e),r=P(e),s=[t,r];for(let n of e.beforeRequestHandlers??[])s=await n(...s);let a;try{a=await fetch(...s)}catch(n){let i=q(n,e.requestErrorMessage);for(let h of e.requestErrorHandlers??[]){let d=await h(i,...s);if(d.status==="corrected"){a=d.value;break}else i=d.value}if(!a)throw i}let o=await E(a);for(let n of e.responseSuccessHandlers??[])return o.response.ok&&(o=await n(o)),o;let l=new p(a,e.responseErrorMessage);for(let n of e.responseErrorHandlers??[]){let i=await n(l,o,...s);i.status==="corrected"?o=await E(i.value):l=i.value}if(o.response.ok)return o;throw l}function H(e){let t=new URL(e.url,e.baseUrl??window.location.origin);if(e.params){let r=e.params instanceof URLSearchParams?e.params:new URLSearchParams(e.params);for(let[s,a]of r)t.searchParams.set(s,a)}return t}function P(e){let t=e.init??{},r={...t};r.method=(t.method??"GET").toUpperCase(),r.headers=new Headers(t.headers),r.headers.has("x-request-with")||r.headers.set("x-requested-with","XMLHttpRequest");let s;if("data"in e){let a=r.headers.get(u);e.data instanceof ArrayBuffer?s="arrayBuffer":e.data instanceof Blob?s="blob":e.data instanceof FormData?s="formData":a?.startsWith(R)||a===null&&!["GET","HEAD"].includes(r.method)?s="json":typeof e.data=="string"&&(s="text")}return s==="formData"?r.headers.delete(u):s&&!r.headers.has(u)&&r.headers.set(u,T[s]),"data"in e?r.body=s==="json"?JSON.stringify(e.data):e.data:"body"in t&&(r.body=t.body),e.timeout&&!("signal"in r)&&(r.signal=AbortSignal.timeout(e.timeout)),r}function q(e,t){let r=e instanceof Error?e:new Error;return r.message&&(r.cause=r.message),t?r.message=t:typeof e=="string"&&e!==""?r.message=e:r.message||(r.message="Unknown request error"),r}async function E(e){let t=e.headers.get(u)??"",r;t.startsWith(R)||t.startsWith(y)?r="json":t.startsWith(f)?r="formData":t.startsWith(m)&&(r="text");try{let s=r?await e[r]():null;return{response:e,data:s}}catch(s){let a=s,o={};throw"cause"in a&&(o.cause=a.cause),new p(e,a.message,o)}}export{p as ResponseError,v as retrieve};