app-fetch 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app-fetch.cjs +1 -1
- package/dist/app-fetch.min.js +1 -1
- package/dist/app-fetch.mjs +1 -1
- package/package.json +2 -2
package/dist/app-fetch.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class e extends Error{status;constructor(t,n){super(t),this.status=n,this.name=`HttpError`,Object.setPrototypeOf(this,e.prototype)}};const t=t=>{let n={status:500,message:`Internal Server Error`,data:null};return t instanceof e?{status:t.status,message:t.message,data:null}:t instanceof Error?{...n,message:t.message}:n},n=[`application/octet-stream`,`pdf`,`zip`,`tar`,`gzip`,`7z`,`rar`,`epub`,`excel`,`word`,`officedocument`,`vnd.ms-`],r=[`application/xml`,`text/xml`,`application/javascript`,`text/javascript`,`application/typescript`,`application/yaml`,`application/graphql`],i=e=>e.status===204||e.status===205||e.headers.get(`content-length`)===`0`,a=e=>e.includes(`json`),o=e=>e.includes(`multipart/`)||e.includes(`form-urlencoded`),s=e=>e.startsWith(`image/`)||e.startsWith(`audio/`)||e.startsWith(`video/`)||e.startsWith(`font/`)?!0:n.some(t=>e.includes(t)),c=e=>e.startsWith(`text/`)?!0:r.some(t=>e.includes(t)),l=Symbol(`app-fetch:not-matched`),u=async(e,t)=>a(t)?await e.json():s(t)?await e.blob():o(t)?await e.formData():c(t)?await e.text():l,d=async e=>{try{return await e.text()}catch{}try{return await e.blob()}catch{return null}},f=async e=>{if(i(e))return null;let t=(e.headers.get(`content-type`)||``).toLowerCase();try{let n=await u(e.clone(),t);if(n!==l)return n}catch(e){console.error(`Content parsing failed, executing fallback.`,e)}return d(e.clone())},p=e=>{let t=e?.maxRetries??3,n=e?.initialDelay??100,r=e?.factor??2,i=e?.statusCodes??[408,429,500,502,503,504];return e=>e.attempt>t||e.response&&!i.includes(e.response.status)?{shouldRetry:!1}:{shouldRetry:!0,delay:n*r**(e.attempt-1)}},m=async(e,t,n)=>{if(!t)return;let r=Array.isArray(t)?t:[t];for(let t of r){if(n?.aborted){let e=Error(`The operation was aborted`);throw e.name=`AbortError`,e}if(n){let r,i=new Promise((e,t)=>{r=()=>{let e=Error(`The operation was aborted`);e.name=`AbortError`,t(e)},n.addEventListener(`abort`,r,{once:!0})});try{await Promise.race([t(e),i])}finally{r&&n.removeEventListener(`abort`,r)}}else await t(e)}},h=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e.clone())}},g=e=>new Promise(t=>setTimeout(t,e)),_=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e)}},v=e=>e instanceof FormData||e instanceof Blob||e instanceof URLSearchParams||e instanceof ArrayBuffer||ArrayBuffer.isView(e)||typeof ReadableStream<`u`&&e instanceof ReadableStream||typeof e==`string`,y=(e,t)=>{let n=t?.method?.toLowerCase();if(!(t&&`body`in t&&t.body!==void 0&&t.body!==null)||n!==`post`&&n!==`put`&&n!==`patch`)return;let r=t.body;if(v(r))e.body=r;else{let t=e.headers;t.has(`Content-Type`)||t.set(`Content-Type`,`application/json`),e.body=JSON.stringify(r)}},b=(e,t)=>{if(!e)return t;if(!t)return e;if(`any`in AbortSignal&&typeof AbortSignal.any==`function`)return AbortSignal.any([e,t]);let n=new AbortController,r=()=>n.abort();return e.aborted||t.aborted?n.abort():(e.addEventListener(`abort`,r,{once:!0}),t.addEventListener(`abort`,r,{once:!0})),n.signal},x=e=>{let t=e?.body;return typeof ReadableStream<`u`&&t instanceof ReadableStream},S=async(e,t,n)=>{if(e.attempt>=10)return console.warn(`app-fetch: Retry limit hard cap reached (${e.attempt} attempts). Halting retries to prevent infinite loop.`),{shouldRetry:!1,delay:0};let r=await C(e,t,n);return r.shouldRetry&&x(n)?(console.warn(`app-fetch: Retry skipped because the request body is a ReadableStream, which can only be consumed once and cannot be safely re-sent. To enable retries for this request, provide a re-creatable body instead (e.g. a Blob, ArrayBuffer, string, or FormData).`),{shouldRetry:!1,delay:0}):r},C=async(e,t,n)=>{if(t){if(typeof t==`function`){let n=await t(e);return{shouldRetry:n.shouldRetry,delay:n.delay??0}}if(typeof t==`object`&&t)return{shouldRetry:await t.shouldRetry(e),delay:t.getDelay?await t.getDelay(e):n?.delay??0}}let r=n?.retry??0,i=e.response?.status;return{shouldRetry:(i?i===408||i===429||i>=500&&i<=599:!!e.error)&&e.attempt<=r,delay:n?.delay??0}},w=async(e,t,n,r)=>{let i={...e,baseURL:void 0,query:void 0,beforeRequest:void 0,afterResponse:void 0,onError:void 0,timeout:void 0,retry:void 0,delay:void 0,retryStrategy:void 0};return i.headers=n(e?.headers),y(i,e),i.signal=b(e?.signal,r.signal),await m(i,e?.beforeRequest,i.signal),i},T=async(e,t,n,r,i)=>{let a={response:e.clone(),attempt:r,maxRetries:n?.retry??0},o=await S(a,n?.retryStrategy,n);return o.shouldRetry?(o.delay>0&&await g(o.delay),await i(t,n,r+1)):(await h(e,n?.afterResponse),Object.assign(e,{getData:()=>f(e)}))},E=async(e,t,n,r,i,a)=>{let o=e;if(t&&e instanceof Error&&e.name===`AbortError`&&(o=Error(`Request Timeout. time : ${r?.timeout??3e3}ms`,{cause:e})),r?.signal?.aborted)throw await _(o,r?.onError),o;let s=!!r?.retryStrategy,c=(r?.retry??0)>0;if(s||c){let e={error:o,attempt:i,maxRetries:r?.retry??0},t=await S(e,r?.retryStrategy,r);if(t.shouldRetry)return t.delay>0&&await g(t.delay),await a(n,r,i+1)}throw await _(o,r?.onError),o},D=(e,t)=>{let n=[];e&&(Array.isArray(e)?n.push(...e):n.push(e));let r=[];t&&(Array.isArray(t)?r.push(...t):r.push(t));let i=[...n,...r];return i.length>0?i:void 0},O=(e,t)=>{t.beforeRequest&&(e.beforeRequest=Array.isArray(t.beforeRequest)?t.beforeRequest:[t.beforeRequest]),t.afterResponse&&(e.afterResponse=Array.isArray(t.afterResponse)?t.afterResponse:[t.afterResponse]),t.onError&&(e.onError=Array.isArray(t.onError)?t.onError:[t.onError])},k=(e,t)=>{let n={...t};return n.beforeRequest=D(e.beforeRequest,t?.beforeRequest),n.afterResponse=D(e.afterResponse,t?.afterResponse),n.onError=D(e.onError,t?.onError),n},A=e=>e instanceof Map||e instanceof Set?Array.from(e.values()):Object.values(e),
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class e extends Error{status;constructor(t,n){super(t),this.status=n,this.name=`HttpError`,Object.setPrototypeOf(this,e.prototype)}};const t=t=>{let n={status:500,message:`Internal Server Error`,data:null};return t instanceof e?{status:t.status,message:t.message,data:null}:t instanceof Error?{...n,message:t.message}:n},n=[`application/octet-stream`,`pdf`,`zip`,`tar`,`gzip`,`7z`,`rar`,`epub`,`excel`,`word`,`officedocument`,`vnd.ms-`],r=[`application/xml`,`text/xml`,`application/javascript`,`text/javascript`,`application/typescript`,`application/yaml`,`application/graphql`],i=e=>e.status===204||e.status===205||e.headers.get(`content-length`)===`0`,a=e=>e.includes(`json`),o=e=>e.includes(`multipart/`)||e.includes(`form-urlencoded`),s=e=>e.startsWith(`image/`)||e.startsWith(`audio/`)||e.startsWith(`video/`)||e.startsWith(`font/`)?!0:n.some(t=>e.includes(t)),c=e=>e.startsWith(`text/`)?!0:r.some(t=>e.includes(t)),l=Symbol(`app-fetch:not-matched`),u=async(e,t)=>a(t)?await e.json():s(t)?await e.blob():o(t)?await e.formData():c(t)?await e.text():l,d=async e=>{try{return await e.text()}catch{}try{return await e.blob()}catch{return null}},f=async e=>{if(i(e))return null;let t=(e.headers.get(`content-type`)||``).toLowerCase();try{let n=await u(e.clone(),t);if(n!==l)return n}catch(e){console.error(`Content parsing failed, executing fallback.`,e)}return d(e.clone())},p=e=>{let t=e?.maxRetries??3,n=e?.initialDelay??100,r=e?.factor??2,i=e?.statusCodes??[408,429,500,502,503,504];return e=>e.attempt>t||e.response&&!i.includes(e.response.status)?{shouldRetry:!1}:{shouldRetry:!0,delay:n*r**(e.attempt-1)}},m=async(e,t,n)=>{if(!t)return;let r=Array.isArray(t)?t:[t];for(let t of r){if(n?.aborted){let e=Error(`The operation was aborted`);throw e.name=`AbortError`,e}if(n){let r,i=new Promise((e,t)=>{r=()=>{let e=Error(`The operation was aborted`);e.name=`AbortError`,t(e)},n.addEventListener(`abort`,r,{once:!0})});try{await Promise.race([t(e),i])}finally{r&&n.removeEventListener(`abort`,r)}}else await t(e)}},h=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e.clone())}},g=e=>new Promise(t=>setTimeout(t,e)),_=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e)}},v=e=>e instanceof FormData||e instanceof Blob||e instanceof URLSearchParams||e instanceof ArrayBuffer||ArrayBuffer.isView(e)||typeof ReadableStream<`u`&&e instanceof ReadableStream||typeof e==`string`,y=(e,t)=>{let n=t?.method?.toLowerCase();if(!(t&&`body`in t&&t.body!==void 0&&t.body!==null)||n!==`post`&&n!==`put`&&n!==`patch`)return;let r=t.body;if(v(r))e.body=r;else{let t=e.headers;t.has(`Content-Type`)||t.set(`Content-Type`,`application/json`),e.body=JSON.stringify(r)}},b=(e,t)=>{if(!e)return t;if(!t)return e;if(`any`in AbortSignal&&typeof AbortSignal.any==`function`)return AbortSignal.any([e,t]);let n=new AbortController,r=()=>n.abort();return e.aborted||t.aborted?n.abort():(e.addEventListener(`abort`,r,{once:!0}),t.addEventListener(`abort`,r,{once:!0})),n.signal},x=e=>{let t=e?.body;return typeof ReadableStream<`u`&&t instanceof ReadableStream},S=async(e,t,n)=>{if(e.attempt>=10)return console.warn(`app-fetch: Retry limit hard cap reached (${e.attempt} attempts). Halting retries to prevent infinite loop.`),{shouldRetry:!1,delay:0};let r=await C(e,t,n);return r.shouldRetry&&x(n)?(console.warn(`app-fetch: Retry skipped because the request body is a ReadableStream, which can only be consumed once and cannot be safely re-sent. To enable retries for this request, provide a re-creatable body instead (e.g. a Blob, ArrayBuffer, string, or FormData).`),{shouldRetry:!1,delay:0}):r},C=async(e,t,n)=>{if(t){if(typeof t==`function`){let n=await t(e);return{shouldRetry:n.shouldRetry,delay:n.delay??0}}if(typeof t==`object`&&t)return{shouldRetry:await t.shouldRetry(e),delay:t.getDelay?await t.getDelay(e):n?.delay??0}}let r=n?.retry??0,i=e.response?.status;return{shouldRetry:(i?i===408||i===429||i>=500&&i<=599:!!e.error)&&e.attempt<=r,delay:n?.delay??0}},w=async(e,t,n,r)=>{let i={...e,baseURL:void 0,query:void 0,beforeRequest:void 0,afterResponse:void 0,onError:void 0,timeout:void 0,retry:void 0,delay:void 0,retryStrategy:void 0};return i.headers=n(e?.headers),y(i,e),i.signal=b(e?.signal,r.signal),await m(i,e?.beforeRequest,i.signal),i},T=async(e,t,n,r,i)=>{let a={response:e.clone(),attempt:r,maxRetries:n?.retry??0},o=await S(a,n?.retryStrategy,n);return o.shouldRetry?(o.delay>0&&await g(o.delay),await i(t,n,r+1)):(await h(e,n?.afterResponse),Object.assign(e,{getData:()=>f(e)}))},E=async(e,t,n,r,i,a)=>{let o=e;if(t&&e instanceof Error&&e.name===`AbortError`&&(o=Error(`Request Timeout. time : ${r?.timeout??3e3}ms`,{cause:e})),r?.signal?.aborted)throw await _(o,r?.onError),o;let s=!!r?.retryStrategy,c=(r?.retry??0)>0;if(s||c){let e={error:o,attempt:i,maxRetries:r?.retry??0},t=await S(e,r?.retryStrategy,r);if(t.shouldRetry)return t.delay>0&&await g(t.delay),await a(n,r,i+1)}throw await _(o,r?.onError),o},D=(e,t)=>{let n=[];e&&(Array.isArray(e)?n.push(...e):n.push(e));let r=[];t&&(Array.isArray(t)?r.push(...t):r.push(t));let i=[...n,...r];return i.length>0?i:void 0},O=(e,t)=>{t.beforeRequest&&(e.beforeRequest=Array.isArray(t.beforeRequest)?t.beforeRequest:[t.beforeRequest]),t.afterResponse&&(e.afterResponse=Array.isArray(t.afterResponse)?t.afterResponse:[t.afterResponse]),t.onError&&(e.onError=Array.isArray(t.onError)?t.onError:[t.onError])},k=(e,t)=>{let n={...t};return n.beforeRequest=D(e.beforeRequest,t?.beforeRequest),n.afterResponse=D(e.afterResponse,t?.afterResponse),n.onError=D(e.onError,t?.onError),n},A=`Circular reference detected in query parameters`,j=e=>e instanceof Map||e instanceof Set?Array.from(e.values()):Object.values(e),M=(e,t)=>{if(typeof e!=`object`||!e)return!1;if(t.has(e))return!0;t.add(e);let n=j(e).some(e=>M(e,t));return t.delete(e),n},N=(e,t)=>{if(M(e,t))throw Error(A);try{return JSON.stringify(e)}catch{return null}},P=(e,t)=>e instanceof Map?N(Array.from(e.entries()),t):e instanceof Set?N(Array.from(e),t):e instanceof RegExp?e.toString():N(e,t),F=(e,t)=>`${encodeURIComponent(e)}=${encodeURIComponent(t)}`,I=(e,t,n,r)=>{if(t==null)return[];if(Array.isArray(t)){if(n.has(t))throw Error(A);n.add(t);let i=[];for(let[a,o]of t.entries()){let t=`${e}[${a}]`;i.push(...I(t,o,n,r))}return n.delete(t),i}if(typeof t==`object`&&!(t instanceof Date)){if(Object.prototype.toString.call(t)===`[object Object]`)return r(t,e,n);let i=P(t,n);return i?[F(e,i)]:[]}return t instanceof Date?[F(e,t.toISOString())]:typeof t==`string`||typeof t==`number`||typeof t==`boolean`||typeof t==`bigint`?[F(e,String(t))]:[]},L=(e,t,n,r)=>{if(e instanceof Date)return[F(t,e.toISOString())];if(Object.prototype.toString.call(e)===`[object Object]`)return r(e,t,n);let i=P(e,n);return i?[F(t,i)]:[]},R=(e,t,n,r)=>{if(n.has(e))throw Error(A);n.add(e);let i=[];for(let[a,o]of e.entries()){let e=`${t}[${a}]`;i.push(...I(e,o,n,r))}return n.delete(e),i},z=(e,t,n=new WeakSet)=>{let r=[];if(n.has(e))throw Error(A);n.add(e);for(let[i,a]of Object.entries(e)){if(a==null)continue;let e=t?`${t}.${i}`:i;Array.isArray(a)?r.push(...R(a,e,n,z)):typeof a==`object`?r.push(...L(a,e,n,z)):(typeof a==`string`||typeof a==`number`||typeof a==`boolean`||typeof a==`bigint`)&&r.push(F(e,String(a)))}return n.delete(e),r},B=(e,t=new WeakSet)=>z(e,void 0,t).join(`&`),V=e=>{if(!e)return``;let t=e.length;for(;t>0&&e[t-1]===`/`;)t--;return e.slice(0,t)},H=(e,t)=>{if(/^(?:https?:)?\/\//i.test(e))return e;let n=V(t?.baseURL),r=e.startsWith(`/`)?e:`/${e}`;return n?`${n}${r}`:r},U=(e,t)=>{let n=H(e,t);if(!t?.query)return n;let r=B(t.query);return r?`${n}${n.includes(`?`)?`&`:`?`}${r}`:n},W=(e,t)=>{let n=new Headers(e);if(t)for(let[e,r]of new Headers(t).entries())n.set(e,r);return n},G=(e,t,n=1)=>{let r=(async()=>{let r=null,i=!1,a=new AbortController;try{let o=t?.timeout??3e3;r=o>0?setTimeout(()=>{i=!0,a.abort()},o):null;let s=await w(t,n,W,a),c=U(e,t),l=await fetch(c,s);return r&&clearTimeout(r),await T(l,e,t,n,G)}catch(a){return r&&clearTimeout(r),await E(a,i,e,t,n,G)}})();return Object.assign(r,{getData:()=>r.then(e=>e.getData())})},K=Object.assign(G,{create:e=>(t,n)=>{let r={...e,...n,headers:W(e.headers,n?.headers),beforeRequest:D(e.beforeRequest,n?.beforeRequest),afterResponse:D(e.afterResponse,n?.afterResponse),onError:D(e.onError,n?.onError)};return G(t,r)}});exports.HttpError=e,exports.appFetch=K,exports.composeInterceptors=D,exports.exponentialBackoffRetry=p,exports.getData=f,exports.mergeFetchOptions=k,exports.returnError=t,exports.setInterceptors=O;
|
package/dist/app-fetch.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var appFetch=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class e extends Error{status;constructor(t,n){super(t),this.status=n,this.name=`HttpError`,Object.setPrototypeOf(this,e.prototype)}};let n=e=>{let n={status:500,message:`Internal Server Error`,data:null};return e instanceof t?{status:e.status,message:e.message,data:null}:e instanceof Error?{...n,message:e.message}:n},r=[`application/octet-stream`,`pdf`,`zip`,`tar`,`gzip`,`7z`,`rar`,`epub`,`excel`,`word`,`officedocument`,`vnd.ms-`],i=[`application/xml`,`text/xml`,`application/javascript`,`text/javascript`,`application/typescript`,`application/yaml`,`application/graphql`],a=e=>e.status===204||e.status===205||e.headers.get(`content-length`)===`0`,o=e=>e.includes(`json`),s=e=>e.includes(`multipart/`)||e.includes(`form-urlencoded`),c=e=>e.startsWith(`image/`)||e.startsWith(`audio/`)||e.startsWith(`video/`)||e.startsWith(`font/`)?!0:r.some(t=>e.includes(t)),l=e=>e.startsWith(`text/`)?!0:i.some(t=>e.includes(t)),u=Symbol(`app-fetch:not-matched`),d=async(e,t)=>o(t)?await e.json():c(t)?await e.blob():s(t)?await e.formData():l(t)?await e.text():u,f=async e=>{try{return await e.text()}catch{}try{return await e.blob()}catch{return null}},p=async e=>{if(a(e))return null;let t=(e.headers.get(`content-type`)||``).toLowerCase();try{let n=await d(e.clone(),t);if(n!==u)return n}catch(e){console.error(`Content parsing failed, executing fallback.`,e)}return f(e.clone())},m=e=>{let t=e?.maxRetries??3,n=e?.initialDelay??100,r=e?.factor??2,i=e?.statusCodes??[408,429,500,502,503,504];return e=>e.attempt>t||e.response&&!i.includes(e.response.status)?{shouldRetry:!1}:{shouldRetry:!0,delay:n*r**(e.attempt-1)}},h=async(e,t,n)=>{if(!t)return;let r=Array.isArray(t)?t:[t];for(let t of r){if(n?.aborted){let e=Error(`The operation was aborted`);throw e.name=`AbortError`,e}if(n){let r,i=new Promise((e,t)=>{r=()=>{let e=Error(`The operation was aborted`);e.name=`AbortError`,t(e)},n.addEventListener(`abort`,r,{once:!0})});try{await Promise.race([t(e),i])}finally{r&&n.removeEventListener(`abort`,r)}}else await t(e)}},g=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e.clone())}},_=e=>new Promise(t=>setTimeout(t,e)),v=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e)}},y=e=>e instanceof FormData||e instanceof Blob||e instanceof URLSearchParams||e instanceof ArrayBuffer||ArrayBuffer.isView(e)||typeof ReadableStream<`u`&&e instanceof ReadableStream||typeof e==`string`,b=(e,t)=>{let n=t?.method?.toLowerCase();if(!(t&&`body`in t&&t.body!==void 0&&t.body!==null)||n!==`post`&&n!==`put`&&n!==`patch`)return;let r=t.body;if(y(r))e.body=r;else{let t=e.headers;t.has(`Content-Type`)||t.set(`Content-Type`,`application/json`),e.body=JSON.stringify(r)}},x=(e,t)=>{if(!e)return t;if(!t)return e;if(`any`in AbortSignal&&typeof AbortSignal.any==`function`)return AbortSignal.any([e,t]);let n=new AbortController,r=()=>n.abort();return e.aborted||t.aborted?n.abort():(e.addEventListener(`abort`,r,{once:!0}),t.addEventListener(`abort`,r,{once:!0})),n.signal},S=e=>{let t=e?.body;return typeof ReadableStream<`u`&&t instanceof ReadableStream},C=async(e,t,n)=>{if(e.attempt>=10)return console.warn(`app-fetch: Retry limit hard cap reached (${e.attempt} attempts). Halting retries to prevent infinite loop.`),{shouldRetry:!1,delay:0};let r=await w(e,t,n);return r.shouldRetry&&S(n)?(console.warn(`app-fetch: Retry skipped because the request body is a ReadableStream, which can only be consumed once and cannot be safely re-sent. To enable retries for this request, provide a re-creatable body instead (e.g. a Blob, ArrayBuffer, string, or FormData).`),{shouldRetry:!1,delay:0}):r},w=async(e,t,n)=>{if(t){if(typeof t==`function`){let n=await t(e);return{shouldRetry:n.shouldRetry,delay:n.delay??0}}if(typeof t==`object`&&t)return{shouldRetry:await t.shouldRetry(e),delay:t.getDelay?await t.getDelay(e):n?.delay??0}}let r=n?.retry??0,i=e.response?.status;return{shouldRetry:(i?i===408||i===429||i>=500&&i<=599:!!e.error)&&e.attempt<=r,delay:n?.delay??0}},T=async(e,t,n,r)=>{let i={...e,baseURL:void 0,query:void 0,beforeRequest:void 0,afterResponse:void 0,onError:void 0,timeout:void 0,retry:void 0,delay:void 0,retryStrategy:void 0};return i.headers=n(e?.headers),b(i,e),i.signal=x(e?.signal,r.signal),await h(i,e?.beforeRequest,i.signal),i},E=async(e,t,n,r,i)=>{let a={response:e.clone(),attempt:r,maxRetries:n?.retry??0},o=await C(a,n?.retryStrategy,n);return o.shouldRetry?(o.delay>0&&await _(o.delay),await i(t,n,r+1)):(await g(e,n?.afterResponse),Object.assign(e,{getData:()=>p(e)}))},D=async(e,t,n,r,i,a)=>{let o=e;if(t&&e instanceof Error&&e.name===`AbortError`&&(o=Error(`Request Timeout. time : ${r?.timeout??3e3}ms`,{cause:e})),r?.signal?.aborted)throw await v(o,r?.onError),o;let s=!!r?.retryStrategy,c=(r?.retry??0)>0;if(s||c){let e={error:o,attempt:i,maxRetries:r?.retry??0},t=await C(e,r?.retryStrategy,r);if(t.shouldRetry)return t.delay>0&&await _(t.delay),await a(n,r,i+1)}throw await v(o,r?.onError),o},O=(e,t)=>{let n=[];e&&(Array.isArray(e)?n.push(...e):n.push(e));let r=[];t&&(Array.isArray(t)?r.push(...t):r.push(t));let i=[...n,...r];return i.length>0?i:void 0},k=(e,t)=>{t.beforeRequest&&(e.beforeRequest=Array.isArray(t.beforeRequest)?t.beforeRequest:[t.beforeRequest]),t.afterResponse&&(e.afterResponse=Array.isArray(t.afterResponse)?t.afterResponse:[t.afterResponse]),t.onError&&(e.onError=Array.isArray(t.onError)?t.onError:[t.onError])},A=(e,t)=>{let n={...t};return n.beforeRequest=O(e.beforeRequest,t?.beforeRequest),n.afterResponse=O(e.afterResponse,t?.afterResponse),n.onError=O(e.onError,t?.onError),n},j=e=>e instanceof Map||e instanceof Set?Array.from(e.values()):Object.values(e),
|
|
1
|
+
var appFetch=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class e extends Error{status;constructor(t,n){super(t),this.status=n,this.name=`HttpError`,Object.setPrototypeOf(this,e.prototype)}};let n=e=>{let n={status:500,message:`Internal Server Error`,data:null};return e instanceof t?{status:e.status,message:e.message,data:null}:e instanceof Error?{...n,message:e.message}:n},r=[`application/octet-stream`,`pdf`,`zip`,`tar`,`gzip`,`7z`,`rar`,`epub`,`excel`,`word`,`officedocument`,`vnd.ms-`],i=[`application/xml`,`text/xml`,`application/javascript`,`text/javascript`,`application/typescript`,`application/yaml`,`application/graphql`],a=e=>e.status===204||e.status===205||e.headers.get(`content-length`)===`0`,o=e=>e.includes(`json`),s=e=>e.includes(`multipart/`)||e.includes(`form-urlencoded`),c=e=>e.startsWith(`image/`)||e.startsWith(`audio/`)||e.startsWith(`video/`)||e.startsWith(`font/`)?!0:r.some(t=>e.includes(t)),l=e=>e.startsWith(`text/`)?!0:i.some(t=>e.includes(t)),u=Symbol(`app-fetch:not-matched`),d=async(e,t)=>o(t)?await e.json():c(t)?await e.blob():s(t)?await e.formData():l(t)?await e.text():u,f=async e=>{try{return await e.text()}catch{}try{return await e.blob()}catch{return null}},p=async e=>{if(a(e))return null;let t=(e.headers.get(`content-type`)||``).toLowerCase();try{let n=await d(e.clone(),t);if(n!==u)return n}catch(e){console.error(`Content parsing failed, executing fallback.`,e)}return f(e.clone())},m=e=>{let t=e?.maxRetries??3,n=e?.initialDelay??100,r=e?.factor??2,i=e?.statusCodes??[408,429,500,502,503,504];return e=>e.attempt>t||e.response&&!i.includes(e.response.status)?{shouldRetry:!1}:{shouldRetry:!0,delay:n*r**(e.attempt-1)}},h=async(e,t,n)=>{if(!t)return;let r=Array.isArray(t)?t:[t];for(let t of r){if(n?.aborted){let e=Error(`The operation was aborted`);throw e.name=`AbortError`,e}if(n){let r,i=new Promise((e,t)=>{r=()=>{let e=Error(`The operation was aborted`);e.name=`AbortError`,t(e)},n.addEventListener(`abort`,r,{once:!0})});try{await Promise.race([t(e),i])}finally{r&&n.removeEventListener(`abort`,r)}}else await t(e)}},g=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e.clone())}},_=e=>new Promise(t=>setTimeout(t,e)),v=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e)}},y=e=>e instanceof FormData||e instanceof Blob||e instanceof URLSearchParams||e instanceof ArrayBuffer||ArrayBuffer.isView(e)||typeof ReadableStream<`u`&&e instanceof ReadableStream||typeof e==`string`,b=(e,t)=>{let n=t?.method?.toLowerCase();if(!(t&&`body`in t&&t.body!==void 0&&t.body!==null)||n!==`post`&&n!==`put`&&n!==`patch`)return;let r=t.body;if(y(r))e.body=r;else{let t=e.headers;t.has(`Content-Type`)||t.set(`Content-Type`,`application/json`),e.body=JSON.stringify(r)}},x=(e,t)=>{if(!e)return t;if(!t)return e;if(`any`in AbortSignal&&typeof AbortSignal.any==`function`)return AbortSignal.any([e,t]);let n=new AbortController,r=()=>n.abort();return e.aborted||t.aborted?n.abort():(e.addEventListener(`abort`,r,{once:!0}),t.addEventListener(`abort`,r,{once:!0})),n.signal},S=e=>{let t=e?.body;return typeof ReadableStream<`u`&&t instanceof ReadableStream},C=async(e,t,n)=>{if(e.attempt>=10)return console.warn(`app-fetch: Retry limit hard cap reached (${e.attempt} attempts). Halting retries to prevent infinite loop.`),{shouldRetry:!1,delay:0};let r=await w(e,t,n);return r.shouldRetry&&S(n)?(console.warn(`app-fetch: Retry skipped because the request body is a ReadableStream, which can only be consumed once and cannot be safely re-sent. To enable retries for this request, provide a re-creatable body instead (e.g. a Blob, ArrayBuffer, string, or FormData).`),{shouldRetry:!1,delay:0}):r},w=async(e,t,n)=>{if(t){if(typeof t==`function`){let n=await t(e);return{shouldRetry:n.shouldRetry,delay:n.delay??0}}if(typeof t==`object`&&t)return{shouldRetry:await t.shouldRetry(e),delay:t.getDelay?await t.getDelay(e):n?.delay??0}}let r=n?.retry??0,i=e.response?.status;return{shouldRetry:(i?i===408||i===429||i>=500&&i<=599:!!e.error)&&e.attempt<=r,delay:n?.delay??0}},T=async(e,t,n,r)=>{let i={...e,baseURL:void 0,query:void 0,beforeRequest:void 0,afterResponse:void 0,onError:void 0,timeout:void 0,retry:void 0,delay:void 0,retryStrategy:void 0};return i.headers=n(e?.headers),b(i,e),i.signal=x(e?.signal,r.signal),await h(i,e?.beforeRequest,i.signal),i},E=async(e,t,n,r,i)=>{let a={response:e.clone(),attempt:r,maxRetries:n?.retry??0},o=await C(a,n?.retryStrategy,n);return o.shouldRetry?(o.delay>0&&await _(o.delay),await i(t,n,r+1)):(await g(e,n?.afterResponse),Object.assign(e,{getData:()=>p(e)}))},D=async(e,t,n,r,i,a)=>{let o=e;if(t&&e instanceof Error&&e.name===`AbortError`&&(o=Error(`Request Timeout. time : ${r?.timeout??3e3}ms`,{cause:e})),r?.signal?.aborted)throw await v(o,r?.onError),o;let s=!!r?.retryStrategy,c=(r?.retry??0)>0;if(s||c){let e={error:o,attempt:i,maxRetries:r?.retry??0},t=await C(e,r?.retryStrategy,r);if(t.shouldRetry)return t.delay>0&&await _(t.delay),await a(n,r,i+1)}throw await v(o,r?.onError),o},O=(e,t)=>{let n=[];e&&(Array.isArray(e)?n.push(...e):n.push(e));let r=[];t&&(Array.isArray(t)?r.push(...t):r.push(t));let i=[...n,...r];return i.length>0?i:void 0},k=(e,t)=>{t.beforeRequest&&(e.beforeRequest=Array.isArray(t.beforeRequest)?t.beforeRequest:[t.beforeRequest]),t.afterResponse&&(e.afterResponse=Array.isArray(t.afterResponse)?t.afterResponse:[t.afterResponse]),t.onError&&(e.onError=Array.isArray(t.onError)?t.onError:[t.onError])},A=(e,t)=>{let n={...t};return n.beforeRequest=O(e.beforeRequest,t?.beforeRequest),n.afterResponse=O(e.afterResponse,t?.afterResponse),n.onError=O(e.onError,t?.onError),n},j=`Circular reference detected in query parameters`,M=e=>e instanceof Map||e instanceof Set?Array.from(e.values()):Object.values(e),N=(e,t)=>{if(typeof e!=`object`||!e)return!1;if(t.has(e))return!0;t.add(e);let n=M(e).some(e=>N(e,t));return t.delete(e),n},P=(e,t)=>{if(N(e,t))throw Error(j);try{return JSON.stringify(e)}catch{return null}},F=(e,t)=>e instanceof Map?P(Array.from(e.entries()),t):e instanceof Set?P(Array.from(e),t):e instanceof RegExp?e.toString():P(e,t),I=(e,t)=>`${encodeURIComponent(e)}=${encodeURIComponent(t)}`,L=(e,t,n,r)=>{if(t==null)return[];if(Array.isArray(t)){if(n.has(t))throw Error(j);n.add(t);let i=[];for(let[a,o]of t.entries()){let t=`${e}[${a}]`;i.push(...L(t,o,n,r))}return n.delete(t),i}if(typeof t==`object`&&!(t instanceof Date)){if(Object.prototype.toString.call(t)===`[object Object]`)return r(t,e,n);let i=F(t,n);return i?[I(e,i)]:[]}return t instanceof Date?[I(e,t.toISOString())]:typeof t==`string`||typeof t==`number`||typeof t==`boolean`||typeof t==`bigint`?[I(e,String(t))]:[]},R=(e,t,n,r)=>{if(e instanceof Date)return[I(t,e.toISOString())];if(Object.prototype.toString.call(e)===`[object Object]`)return r(e,t,n);let i=F(e,n);return i?[I(t,i)]:[]},z=(e,t,n,r)=>{if(n.has(e))throw Error(j);n.add(e);let i=[];for(let[a,o]of e.entries()){let e=`${t}[${a}]`;i.push(...L(e,o,n,r))}return n.delete(e),i},B=(e,t,n=new WeakSet)=>{let r=[];if(n.has(e))throw Error(j);n.add(e);for(let[i,a]of Object.entries(e)){if(a==null)continue;let e=t?`${t}.${i}`:i;Array.isArray(a)?r.push(...z(a,e,n,B)):typeof a==`object`?r.push(...R(a,e,n,B)):(typeof a==`string`||typeof a==`number`||typeof a==`boolean`||typeof a==`bigint`)&&r.push(I(e,String(a)))}return n.delete(e),r},V=(e,t=new WeakSet)=>B(e,void 0,t).join(`&`),H=e=>{if(!e)return``;let t=e.length;for(;t>0&&e[t-1]===`/`;)t--;return e.slice(0,t)},U=(e,t)=>{if(/^(?:https?:)?\/\//i.test(e))return e;let n=H(t?.baseURL),r=e.startsWith(`/`)?e:`/${e}`;return n?`${n}${r}`:r},W=(e,t)=>{let n=U(e,t);if(!t?.query)return n;let r=V(t.query);return r?`${n}${n.includes(`?`)?`&`:`?`}${r}`:n},G=(e,t)=>{let n=new Headers(e);if(t)for(let[e,r]of new Headers(t).entries())n.set(e,r);return n},K=(e,t,n=1)=>{let r=(async()=>{let r=null,i=!1,a=new AbortController;try{let o=t?.timeout??3e3;r=o>0?setTimeout(()=>{i=!0,a.abort()},o):null;let s=await T(t,n,G,a),c=W(e,t),l=await fetch(c,s);return r&&clearTimeout(r),await E(l,e,t,n,K)}catch(a){return r&&clearTimeout(r),await D(a,i,e,t,n,K)}})();return Object.assign(r,{getData:()=>r.then(e=>e.getData())})},q=Object.assign(K,{create:e=>(t,n)=>{let r={...e,...n,headers:G(e.headers,n?.headers),beforeRequest:O(e.beforeRequest,n?.beforeRequest),afterResponse:O(e.afterResponse,n?.afterResponse),onError:O(e.onError,n?.onError)};return K(t,r)}});return e.HttpError=t,e.appFetch=q,e.composeInterceptors=O,e.exponentialBackoffRetry=m,e.getData=p,e.mergeFetchOptions=A,e.returnError=n,e.setInterceptors=k,e})({});
|
package/dist/app-fetch.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=class e extends Error{status;constructor(t,n){super(t),this.status=n,this.name=`HttpError`,Object.setPrototypeOf(this,e.prototype)}};const t=t=>{let n={status:500,message:`Internal Server Error`,data:null};return t instanceof e?{status:t.status,message:t.message,data:null}:t instanceof Error?{...n,message:t.message}:n},n=[`application/octet-stream`,`pdf`,`zip`,`tar`,`gzip`,`7z`,`rar`,`epub`,`excel`,`word`,`officedocument`,`vnd.ms-`],r=[`application/xml`,`text/xml`,`application/javascript`,`text/javascript`,`application/typescript`,`application/yaml`,`application/graphql`],i=e=>e.status===204||e.status===205||e.headers.get(`content-length`)===`0`,a=e=>e.includes(`json`),o=e=>e.includes(`multipart/`)||e.includes(`form-urlencoded`),s=e=>e.startsWith(`image/`)||e.startsWith(`audio/`)||e.startsWith(`video/`)||e.startsWith(`font/`)?!0:n.some(t=>e.includes(t)),c=e=>e.startsWith(`text/`)?!0:r.some(t=>e.includes(t)),l=Symbol(`app-fetch:not-matched`),u=async(e,t)=>a(t)?await e.json():s(t)?await e.blob():o(t)?await e.formData():c(t)?await e.text():l,d=async e=>{try{return await e.text()}catch{}try{return await e.blob()}catch{return null}},f=async e=>{if(i(e))return null;let t=(e.headers.get(`content-type`)||``).toLowerCase();try{let n=await u(e.clone(),t);if(n!==l)return n}catch(e){console.error(`Content parsing failed, executing fallback.`,e)}return d(e.clone())},p=e=>{let t=e?.maxRetries??3,n=e?.initialDelay??100,r=e?.factor??2,i=e?.statusCodes??[408,429,500,502,503,504];return e=>e.attempt>t||e.response&&!i.includes(e.response.status)?{shouldRetry:!1}:{shouldRetry:!0,delay:n*r**(e.attempt-1)}},m=async(e,t,n)=>{if(!t)return;let r=Array.isArray(t)?t:[t];for(let t of r){if(n?.aborted){let e=Error(`The operation was aborted`);throw e.name=`AbortError`,e}if(n){let r,i=new Promise((e,t)=>{r=()=>{let e=Error(`The operation was aborted`);e.name=`AbortError`,t(e)},n.addEventListener(`abort`,r,{once:!0})});try{await Promise.race([t(e),i])}finally{r&&n.removeEventListener(`abort`,r)}}else await t(e)}},h=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e.clone())}},g=e=>new Promise(t=>setTimeout(t,e)),_=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e)}},v=e=>e instanceof FormData||e instanceof Blob||e instanceof URLSearchParams||e instanceof ArrayBuffer||ArrayBuffer.isView(e)||typeof ReadableStream<`u`&&e instanceof ReadableStream||typeof e==`string`,y=(e,t)=>{let n=t?.method?.toLowerCase();if(!(t&&`body`in t&&t.body!==void 0&&t.body!==null)||n!==`post`&&n!==`put`&&n!==`patch`)return;let r=t.body;if(v(r))e.body=r;else{let t=e.headers;t.has(`Content-Type`)||t.set(`Content-Type`,`application/json`),e.body=JSON.stringify(r)}},b=(e,t)=>{if(!e)return t;if(!t)return e;if(`any`in AbortSignal&&typeof AbortSignal.any==`function`)return AbortSignal.any([e,t]);let n=new AbortController,r=()=>n.abort();return e.aborted||t.aborted?n.abort():(e.addEventListener(`abort`,r,{once:!0}),t.addEventListener(`abort`,r,{once:!0})),n.signal},x=e=>{let t=e?.body;return typeof ReadableStream<`u`&&t instanceof ReadableStream},S=async(e,t,n)=>{if(e.attempt>=10)return console.warn(`app-fetch: Retry limit hard cap reached (${e.attempt} attempts). Halting retries to prevent infinite loop.`),{shouldRetry:!1,delay:0};let r=await C(e,t,n);return r.shouldRetry&&x(n)?(console.warn(`app-fetch: Retry skipped because the request body is a ReadableStream, which can only be consumed once and cannot be safely re-sent. To enable retries for this request, provide a re-creatable body instead (e.g. a Blob, ArrayBuffer, string, or FormData).`),{shouldRetry:!1,delay:0}):r},C=async(e,t,n)=>{if(t){if(typeof t==`function`){let n=await t(e);return{shouldRetry:n.shouldRetry,delay:n.delay??0}}if(typeof t==`object`&&t)return{shouldRetry:await t.shouldRetry(e),delay:t.getDelay?await t.getDelay(e):n?.delay??0}}let r=n?.retry??0,i=e.response?.status;return{shouldRetry:(i?i===408||i===429||i>=500&&i<=599:!!e.error)&&e.attempt<=r,delay:n?.delay??0}},w=async(e,t,n,r)=>{let i={...e,baseURL:void 0,query:void 0,beforeRequest:void 0,afterResponse:void 0,onError:void 0,timeout:void 0,retry:void 0,delay:void 0,retryStrategy:void 0};return i.headers=n(e?.headers),y(i,e),i.signal=b(e?.signal,r.signal),await m(i,e?.beforeRequest,i.signal),i},T=async(e,t,n,r,i)=>{let a={response:e.clone(),attempt:r,maxRetries:n?.retry??0},o=await S(a,n?.retryStrategy,n);return o.shouldRetry?(o.delay>0&&await g(o.delay),await i(t,n,r+1)):(await h(e,n?.afterResponse),Object.assign(e,{getData:()=>f(e)}))},E=async(e,t,n,r,i,a)=>{let o=e;if(t&&e instanceof Error&&e.name===`AbortError`&&(o=Error(`Request Timeout. time : ${r?.timeout??3e3}ms`,{cause:e})),r?.signal?.aborted)throw await _(o,r?.onError),o;let s=!!r?.retryStrategy,c=(r?.retry??0)>0;if(s||c){let e={error:o,attempt:i,maxRetries:r?.retry??0},t=await S(e,r?.retryStrategy,r);if(t.shouldRetry)return t.delay>0&&await g(t.delay),await a(n,r,i+1)}throw await _(o,r?.onError),o},D=(e,t)=>{let n=[];e&&(Array.isArray(e)?n.push(...e):n.push(e));let r=[];t&&(Array.isArray(t)?r.push(...t):r.push(t));let i=[...n,...r];return i.length>0?i:void 0},O=(e,t)=>{t.beforeRequest&&(e.beforeRequest=Array.isArray(t.beforeRequest)?t.beforeRequest:[t.beforeRequest]),t.afterResponse&&(e.afterResponse=Array.isArray(t.afterResponse)?t.afterResponse:[t.afterResponse]),t.onError&&(e.onError=Array.isArray(t.onError)?t.onError:[t.onError])},k=(e,t)=>{let n={...t};return n.beforeRequest=D(e.beforeRequest,t?.beforeRequest),n.afterResponse=D(e.afterResponse,t?.afterResponse),n.onError=D(e.onError,t?.onError),n},A=e=>e instanceof Map||e instanceof Set?Array.from(e.values()):Object.values(e),
|
|
1
|
+
var e=class e extends Error{status;constructor(t,n){super(t),this.status=n,this.name=`HttpError`,Object.setPrototypeOf(this,e.prototype)}};const t=t=>{let n={status:500,message:`Internal Server Error`,data:null};return t instanceof e?{status:t.status,message:t.message,data:null}:t instanceof Error?{...n,message:t.message}:n},n=[`application/octet-stream`,`pdf`,`zip`,`tar`,`gzip`,`7z`,`rar`,`epub`,`excel`,`word`,`officedocument`,`vnd.ms-`],r=[`application/xml`,`text/xml`,`application/javascript`,`text/javascript`,`application/typescript`,`application/yaml`,`application/graphql`],i=e=>e.status===204||e.status===205||e.headers.get(`content-length`)===`0`,a=e=>e.includes(`json`),o=e=>e.includes(`multipart/`)||e.includes(`form-urlencoded`),s=e=>e.startsWith(`image/`)||e.startsWith(`audio/`)||e.startsWith(`video/`)||e.startsWith(`font/`)?!0:n.some(t=>e.includes(t)),c=e=>e.startsWith(`text/`)?!0:r.some(t=>e.includes(t)),l=Symbol(`app-fetch:not-matched`),u=async(e,t)=>a(t)?await e.json():s(t)?await e.blob():o(t)?await e.formData():c(t)?await e.text():l,d=async e=>{try{return await e.text()}catch{}try{return await e.blob()}catch{return null}},f=async e=>{if(i(e))return null;let t=(e.headers.get(`content-type`)||``).toLowerCase();try{let n=await u(e.clone(),t);if(n!==l)return n}catch(e){console.error(`Content parsing failed, executing fallback.`,e)}return d(e.clone())},p=e=>{let t=e?.maxRetries??3,n=e?.initialDelay??100,r=e?.factor??2,i=e?.statusCodes??[408,429,500,502,503,504];return e=>e.attempt>t||e.response&&!i.includes(e.response.status)?{shouldRetry:!1}:{shouldRetry:!0,delay:n*r**(e.attempt-1)}},m=async(e,t,n)=>{if(!t)return;let r=Array.isArray(t)?t:[t];for(let t of r){if(n?.aborted){let e=Error(`The operation was aborted`);throw e.name=`AbortError`,e}if(n){let r,i=new Promise((e,t)=>{r=()=>{let e=Error(`The operation was aborted`);e.name=`AbortError`,t(e)},n.addEventListener(`abort`,r,{once:!0})});try{await Promise.race([t(e),i])}finally{r&&n.removeEventListener(`abort`,r)}}else await t(e)}},h=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e.clone())}},g=e=>new Promise(t=>setTimeout(t,e)),_=async(e,t)=>{if(t){let n=Array.isArray(t)?t:[t];for(let t of n)await t(e)}},v=e=>e instanceof FormData||e instanceof Blob||e instanceof URLSearchParams||e instanceof ArrayBuffer||ArrayBuffer.isView(e)||typeof ReadableStream<`u`&&e instanceof ReadableStream||typeof e==`string`,y=(e,t)=>{let n=t?.method?.toLowerCase();if(!(t&&`body`in t&&t.body!==void 0&&t.body!==null)||n!==`post`&&n!==`put`&&n!==`patch`)return;let r=t.body;if(v(r))e.body=r;else{let t=e.headers;t.has(`Content-Type`)||t.set(`Content-Type`,`application/json`),e.body=JSON.stringify(r)}},b=(e,t)=>{if(!e)return t;if(!t)return e;if(`any`in AbortSignal&&typeof AbortSignal.any==`function`)return AbortSignal.any([e,t]);let n=new AbortController,r=()=>n.abort();return e.aborted||t.aborted?n.abort():(e.addEventListener(`abort`,r,{once:!0}),t.addEventListener(`abort`,r,{once:!0})),n.signal},x=e=>{let t=e?.body;return typeof ReadableStream<`u`&&t instanceof ReadableStream},S=async(e,t,n)=>{if(e.attempt>=10)return console.warn(`app-fetch: Retry limit hard cap reached (${e.attempt} attempts). Halting retries to prevent infinite loop.`),{shouldRetry:!1,delay:0};let r=await C(e,t,n);return r.shouldRetry&&x(n)?(console.warn(`app-fetch: Retry skipped because the request body is a ReadableStream, which can only be consumed once and cannot be safely re-sent. To enable retries for this request, provide a re-creatable body instead (e.g. a Blob, ArrayBuffer, string, or FormData).`),{shouldRetry:!1,delay:0}):r},C=async(e,t,n)=>{if(t){if(typeof t==`function`){let n=await t(e);return{shouldRetry:n.shouldRetry,delay:n.delay??0}}if(typeof t==`object`&&t)return{shouldRetry:await t.shouldRetry(e),delay:t.getDelay?await t.getDelay(e):n?.delay??0}}let r=n?.retry??0,i=e.response?.status;return{shouldRetry:(i?i===408||i===429||i>=500&&i<=599:!!e.error)&&e.attempt<=r,delay:n?.delay??0}},w=async(e,t,n,r)=>{let i={...e,baseURL:void 0,query:void 0,beforeRequest:void 0,afterResponse:void 0,onError:void 0,timeout:void 0,retry:void 0,delay:void 0,retryStrategy:void 0};return i.headers=n(e?.headers),y(i,e),i.signal=b(e?.signal,r.signal),await m(i,e?.beforeRequest,i.signal),i},T=async(e,t,n,r,i)=>{let a={response:e.clone(),attempt:r,maxRetries:n?.retry??0},o=await S(a,n?.retryStrategy,n);return o.shouldRetry?(o.delay>0&&await g(o.delay),await i(t,n,r+1)):(await h(e,n?.afterResponse),Object.assign(e,{getData:()=>f(e)}))},E=async(e,t,n,r,i,a)=>{let o=e;if(t&&e instanceof Error&&e.name===`AbortError`&&(o=Error(`Request Timeout. time : ${r?.timeout??3e3}ms`,{cause:e})),r?.signal?.aborted)throw await _(o,r?.onError),o;let s=!!r?.retryStrategy,c=(r?.retry??0)>0;if(s||c){let e={error:o,attempt:i,maxRetries:r?.retry??0},t=await S(e,r?.retryStrategy,r);if(t.shouldRetry)return t.delay>0&&await g(t.delay),await a(n,r,i+1)}throw await _(o,r?.onError),o},D=(e,t)=>{let n=[];e&&(Array.isArray(e)?n.push(...e):n.push(e));let r=[];t&&(Array.isArray(t)?r.push(...t):r.push(t));let i=[...n,...r];return i.length>0?i:void 0},O=(e,t)=>{t.beforeRequest&&(e.beforeRequest=Array.isArray(t.beforeRequest)?t.beforeRequest:[t.beforeRequest]),t.afterResponse&&(e.afterResponse=Array.isArray(t.afterResponse)?t.afterResponse:[t.afterResponse]),t.onError&&(e.onError=Array.isArray(t.onError)?t.onError:[t.onError])},k=(e,t)=>{let n={...t};return n.beforeRequest=D(e.beforeRequest,t?.beforeRequest),n.afterResponse=D(e.afterResponse,t?.afterResponse),n.onError=D(e.onError,t?.onError),n},A=`Circular reference detected in query parameters`,j=e=>e instanceof Map||e instanceof Set?Array.from(e.values()):Object.values(e),M=(e,t)=>{if(typeof e!=`object`||!e)return!1;if(t.has(e))return!0;t.add(e);let n=j(e).some(e=>M(e,t));return t.delete(e),n},N=(e,t)=>{if(M(e,t))throw Error(A);try{return JSON.stringify(e)}catch{return null}},P=(e,t)=>e instanceof Map?N(Array.from(e.entries()),t):e instanceof Set?N(Array.from(e),t):e instanceof RegExp?e.toString():N(e,t),F=(e,t)=>`${encodeURIComponent(e)}=${encodeURIComponent(t)}`,I=(e,t,n,r)=>{if(t==null)return[];if(Array.isArray(t)){if(n.has(t))throw Error(A);n.add(t);let i=[];for(let[a,o]of t.entries()){let t=`${e}[${a}]`;i.push(...I(t,o,n,r))}return n.delete(t),i}if(typeof t==`object`&&!(t instanceof Date)){if(Object.prototype.toString.call(t)===`[object Object]`)return r(t,e,n);let i=P(t,n);return i?[F(e,i)]:[]}return t instanceof Date?[F(e,t.toISOString())]:typeof t==`string`||typeof t==`number`||typeof t==`boolean`||typeof t==`bigint`?[F(e,String(t))]:[]},L=(e,t,n,r)=>{if(e instanceof Date)return[F(t,e.toISOString())];if(Object.prototype.toString.call(e)===`[object Object]`)return r(e,t,n);let i=P(e,n);return i?[F(t,i)]:[]},R=(e,t,n,r)=>{if(n.has(e))throw Error(A);n.add(e);let i=[];for(let[a,o]of e.entries()){let e=`${t}[${a}]`;i.push(...I(e,o,n,r))}return n.delete(e),i},z=(e,t,n=new WeakSet)=>{let r=[];if(n.has(e))throw Error(A);n.add(e);for(let[i,a]of Object.entries(e)){if(a==null)continue;let e=t?`${t}.${i}`:i;Array.isArray(a)?r.push(...R(a,e,n,z)):typeof a==`object`?r.push(...L(a,e,n,z)):(typeof a==`string`||typeof a==`number`||typeof a==`boolean`||typeof a==`bigint`)&&r.push(F(e,String(a)))}return n.delete(e),r},B=(e,t=new WeakSet)=>z(e,void 0,t).join(`&`),V=e=>{if(!e)return``;let t=e.length;for(;t>0&&e[t-1]===`/`;)t--;return e.slice(0,t)},H=(e,t)=>{if(/^(?:https?:)?\/\//i.test(e))return e;let n=V(t?.baseURL),r=e.startsWith(`/`)?e:`/${e}`;return n?`${n}${r}`:r},U=(e,t)=>{let n=H(e,t);if(!t?.query)return n;let r=B(t.query);return r?`${n}${n.includes(`?`)?`&`:`?`}${r}`:n},W=(e,t)=>{let n=new Headers(e);if(t)for(let[e,r]of new Headers(t).entries())n.set(e,r);return n},G=(e,t,n=1)=>{let r=(async()=>{let r=null,i=!1,a=new AbortController;try{let o=t?.timeout??3e3;r=o>0?setTimeout(()=>{i=!0,a.abort()},o):null;let s=await w(t,n,W,a),c=U(e,t),l=await fetch(c,s);return r&&clearTimeout(r),await T(l,e,t,n,G)}catch(a){return r&&clearTimeout(r),await E(a,i,e,t,n,G)}})();return Object.assign(r,{getData:()=>r.then(e=>e.getData())})},K=Object.assign(G,{create:e=>(t,n)=>{let r={...e,...n,headers:W(e.headers,n?.headers),beforeRequest:D(e.beforeRequest,n?.beforeRequest),afterResponse:D(e.afterResponse,n?.afterResponse),onError:D(e.onError,n?.onError)};return G(t,r)}});export{e as HttpError,K as appFetch,D as composeInterceptors,p as exponentialBackoffRetry,f as getData,k as mergeFetchOptions,t as returnError,O as setInterceptors};
|
package/package.json
CHANGED