Comparing version 0.1.1 to 0.1.2
@@ -29,2 +29,5 @@ export { merge } from 'ts-deepmerge'; | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -73,9 +76,10 @@ interface XiorResponse<T = any> { | ||
declare function isAbsoluteURL(url: string): boolean; | ||
declare class XiorTimeoutError extends Error { | ||
} | ||
declare class XiorError extends Error { | ||
request?: XiorRequestConfig; | ||
config?: XiorRequestConfig; | ||
response?: XiorResponse; | ||
constructor(message: string, request?: XiorRequestConfig, response?: XiorResponse); | ||
} | ||
declare class XiorTimeoutError extends XiorError { | ||
} | ||
@@ -82,0 +86,0 @@ declare class xior { |
@@ -1,1 +0,1 @@ | ||
import{merge as e}from"ts-deepmerge";export{merge}from"ts-deepmerge";function t(e,t){let r=new globalThis.AbortController;function s(e){r.abort(e),o()}let n=[];for(let t of e){if((null==t?void 0:t.aborted)===!0){s(t.reason);break}if((null==t?void 0:t.addEventListener)!=null){let e=()=>{s(t.reason)};n.push(()=>{(null==t?void 0:t.removeEventListener)!=null&&t.removeEventListener("abort",e)}),t.addEventListener("abort",e)}}function o(){n.forEach(e=>{e()}),null==t||t()}let a=r.signal;return a.clear=o,a}function r(e,t,r){let s=t?r(t):"";s&&(s=e.includes("?")?e+"&"+s:e+"?"+s);let[n,o]=s.split("?");if(o){let e=o.split("&");return`${n}?${e.sort().join("&")}`}return s||e}function s(e){return new Promise(t=>{setTimeout(()=>{t("ok")},e)})}function n(e,t=!0,r=null){if(null==e)return"";let s=[],o=t?encodeURIComponent:e=>e;for(let a in e)if(Object.prototype.hasOwnProperty.call(e,a)){let i=e[a],u=r?`${r}[${o(a)}]`:o(a);if("object"==typeof i){let e=n(i,t,u);""!==e&&s.push(e)}else Array.isArray(i)?i.forEach((e,t)=>{let r=`${u}[${t}]`;s.push(`${o(r)}=${o(e)}`)}):s.push(`${o(u)}=${o(i)}`)}return s.join("&")}function o(e){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(e)}class a extends Error{}class i extends Error{constructor(e,t,r){super(e),this.request=t,this.response=r}}function u(e,t,r,s,n,o,a){try{var i=e[o](a),u=i.value}catch(e){r(e);return}i.done?t(u):Promise.resolve(u).then(s,n)}function l(){return(l=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var s in r)Object.prototype.hasOwnProperty.call(r,s)&&(e[s]=r[s])}return e}).apply(this,arguments)}let c="application/x-www-form-urlencoded",p="application/json";function d(e){return h.apply(this,arguments)}function h(){var t;return t=function*(t){let r=t.encode||t.paramsSerializer||n,s=!1!==t.encodeURI,o=t.method?t.method.toUpperCase():"GET",a=t.url||"",i=a,u=t.data,d=!1,h=(null==t?void 0:t.headers)||{};if(u&&!(u instanceof FormData)){let n="";if(null==t?void 0:t.headers){let e=Object.keys(t.headers).find(e=>"content-type"===e.toLowerCase());e&&(n=t.headers[e])}if(n||(n="GET"===o||"HEAD"===o?c:p,h["Content-Type"]=n),n===c&&("object"==typeof u||t.params)){d=!0;let n=r(e(u||{},t.params||{}),s);n&&(a=a.includes("?")?a+=`&${n}`:`${a}?${n}`),u=null}else n===p&&(u=JSON.stringify(u))}return!d&&t.params&&Object.keys(t.params).length>0&&(a=a.includes("?")?a+=`&${r(t.params,s)}`:`${a}?${r(t.params,s)}`),l({},t,{data:u,url:i,_url:a,method:o,headers:h,encode:r,paramsSerializer:r})},(h=function(){var e=this,r=arguments;return new Promise(function(s,n){var o=t.apply(e,r);function a(e){u(o,s,n,a,i,"next",e)}function i(e){u(o,s,n,a,i,"throw",e)}a(void 0)})}).apply(this,arguments)}function f(e,t,r,s,n,o,a){try{var i=e[o](a),u=i.value}catch(e){r(e);return}i.done?t(u):Promise.resolve(u).then(s,n)}function y(e){return function(){var t=this,r=arguments;return new Promise(function(s,n){var o=e.apply(t,r);function a(e){f(o,s,n,a,i,"next",e)}function i(e){f(o,s,n,a,i,"throw",e)}a(void 0)})}}function m(){return(m=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var s in r)Object.prototype.hasOwnProperty.call(r,s)&&(e[s]=r[s])}return e}).apply(this,arguments)}let v="undefined"!=typeof AbortController;class g{static create(e){return new g(e)}get interceptors(){return{request:{use:(e,t)=>(this.requestInterceptors.push(e),e),eject:e=>{this.requestInterceptors=this.requestInterceptors.filter(t=>t!==e)},clear:()=>{this.requestInterceptors=[this.requestInterceptors[0]]}},response:{use:(e,t)=>(this.responseInterceptors.push({fn:e,onRejected:t}),e),eject:e=>{this.responseInterceptors=this.responseInterceptors.filter(t=>t.fn!==e)},clear:()=>{this.responseInterceptors=[]}}}}get plugins(){return{use:e=>(this._plugins.push(e),e),eject:e=>{this._plugins=this._plugins.filter(t=>t!==e)},clear:()=>{this._plugins=[]}}}request(t){var r=this;return y(function*(){let s=e(r.config||{},"string"==typeof t?{url:t}:t||{},{headers:{},params:{}});for(let e of r.requestInterceptors)s=yield e(s);let n=r.handlerFetch.bind(r);return r._plugins.forEach(e=>{n=e(n)}),n(s)})()}handlerFetch(e){var r=this;return y(function*(){var s;let n,u;let{url:l,method:c,headers:p,timeout:d,signal:h,data:f}=e,y=function(e,t){if(null==e)return{};var r,s,n={},o=Object.keys(e);for(s=0;s<o.length;s++)t.indexOf(r=o[s])>=0||(n[r]=e[r]);return n}(e,["url","method","headers","timeout","signal","data"]),g=[];if(d&&v){let e=new AbortController;u=setTimeout(()=>{e.abort(new a(`timeout of ${d}ms exceeded`))},d),g.push(e.signal)}h&&g.push(h),n=g[0],g.length>1&&(n=t(g,()=>{clearTimeout(u)}));let T=e._url||l||"";e.baseURL&&!o(T)&&(T=(e.baseURL.endsWith("/")?e.baseURL:e.baseURL+"/")+(T.startsWith("/")?T.slice(1):T));let b=yield fetch(T,m({body:["HEAD","GET"].includes(e.method||"GET")?void 0:f},y,{signal:n,method:c,headers:p}));if(u&&clearTimeout(u),null==n||null==(s=n.clear)||s.call(n),!b.ok){let t;try{(t=yield b.text())&&(t=JSON.parse(t))}catch(e){}let s=new i(b.status?`Request failed with status code ${b.status}`:"Network error",e,{response:b,data:t,config:e,status:b.status,statusText:b.statusText,headers:b.headers});for(let e of r.responseInterceptors)e.onRejected&&(yield e.onRejected(s));throw s}if("HEAD"===e.method)return{data:void 0,request:e,config:e,response:b,headers:b.headers,status:b.status,statusText:b.statusText};if(!e.responseType||"json"===e.responseType||"text"===e.responseType){let t;try{t={data:yield b.text()};try{t.data&&"text"!==e.responseType&&(t.data=JSON.parse(t.data))}catch(e){}}catch(e){t={data:void 0}}let s={data:t.data,request:e,response:b};for(let e of r.responseInterceptors)s=yield e.fn(s);return{data:s.data,request:e,config:e,response:b,headers:b.headers,status:b.status,statusText:b.statusText}}return{data:void 0,request:e,config:e,response:b,headers:b.headers,status:b.status,statusText:b.statusText}})()}createGetHandler(e){return(t,r)=>this.request(r?m({},r,{method:e,url:t}):{method:e,url:t})}createPostHandler(e){return(t,r,s)=>this.request(s?m({},s,{method:e,url:t,data:r}):{method:e,url:t,data:r})}get(e,t){return this.createGetHandler("GET")(e,t)}head(e,t){return this.createGetHandler("HEAD")(e,t)}post(e,t,r){return this.createPostHandler("POST")(e,t,r)}put(e,t,r){return this.createPostHandler("PUT")(e,t,r)}patch(e,t,r){return this.createPostHandler("PATCH")(e,t,r)}delete(e,t){return this.createGetHandler("DELETE")(e,t)}options(e,t,r){return this.createPostHandler("OPTIONS")(e,t,r)}constructor(e){this.requestInterceptors=[d],this.responseInterceptors=[],this._plugins=[],this.config=e}}export{i as XiorError,a as XiorTimeoutError,t as anySignal,r as buildSortedURL,g as default,s as delay,n as encodeParams,o as isAbsoluteURL,g as xior}; | ||
import{merge as e}from"ts-deepmerge";export{merge}from"ts-deepmerge";function t(e,t){let r=new globalThis.AbortController;function s(e){r.abort(e),o()}let n=[];for(let t of e){if((null==t?void 0:t.aborted)===!0){s(t.reason);break}if((null==t?void 0:t.addEventListener)!=null){let e=()=>{s(t.reason)};n.push(()=>{(null==t?void 0:t.removeEventListener)!=null&&t.removeEventListener("abort",e)}),t.addEventListener("abort",e)}}function o(){n.forEach(e=>{e()}),null==t||t()}let a=r.signal;return a.clear=o,a}function r(e,t,r){let s=t?r(t):"";s&&(s=e.includes("?")?e+"&"+s:e+"?"+s);let[n,o]=s.split("?");if(o){let e=o.split("&");return`${n}?${e.sort().join("&")}`}return s||e}function s(e){return new Promise(t=>{setTimeout(()=>{t("ok")},e)})}function n(e,t=!0,r=null){if(null==e)return"";let s=[],o=t?encodeURIComponent:e=>e;for(let a in e)if(Object.prototype.hasOwnProperty.call(e,a)){let i=e[a],u=r?`${r}[${o(a)}]`:o(a);if("object"==typeof i){let e=n(i,t,u);""!==e&&s.push(e)}else Array.isArray(i)?i.forEach((e,t)=>{let r=`${u}[${t}]`;s.push(`${o(r)}=${o(e)}`)}):s.push(`${o(u)}=${o(i)}`)}return s.join("&")}function o(e){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(e)}class a extends Error{constructor(e,t,r){super(e),this.request=t,this.config=t,this.response=r}}class i extends a{}function u(e,t,r,s,n,o,a){try{var i=e[o](a),u=i.value}catch(e){r(e);return}i.done?t(u):Promise.resolve(u).then(s,n)}function l(){return(l=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var s in r)Object.prototype.hasOwnProperty.call(r,s)&&(e[s]=r[s])}return e}).apply(this,arguments)}let c="application/x-www-form-urlencoded",p="application/json";function d(e){return h.apply(this,arguments)}function h(){var t;return t=function*(t){let r=t.encode||t.paramsSerializer||n,s=!1!==t.encodeURI,o=t.method?t.method.toUpperCase():"GET",a=t.url||"",i=a,u=t.data,d=!1,h=(null==t?void 0:t.headers)||{};if(u&&!(u instanceof FormData)){let n="";if(null==t?void 0:t.headers){let e=Object.keys(t.headers).find(e=>"content-type"===e.toLowerCase());e&&(n=t.headers[e])}if(n||(n="GET"===o||"HEAD"===o?c:p,h["Content-Type"]=n),n===c&&("object"==typeof u||t.params)){d=!0;let n=r(e(u||{},t.params||{}),s);n&&(a=a.includes("?")?a+=`&${n}`:`${a}?${n}`),u=null}else n===p&&(u=JSON.stringify(u))}return!d&&t.params&&Object.keys(t.params).length>0&&(a=a.includes("?")?a+=`&${r(t.params,s)}`:`${a}?${r(t.params,s)}`),l({},t,{data:u,url:i,_url:a,method:o,headers:h,encode:r,paramsSerializer:r})},(h=function(){var e=this,r=arguments;return new Promise(function(s,n){var o=t.apply(e,r);function a(e){u(o,s,n,a,i,"next",e)}function i(e){u(o,s,n,a,i,"throw",e)}a(void 0)})}).apply(this,arguments)}function f(e,t,r,s,n,o,a){try{var i=e[o](a),u=i.value}catch(e){r(e);return}i.done?t(u):Promise.resolve(u).then(s,n)}function y(e){return function(){var t=this,r=arguments;return new Promise(function(s,n){var o=e.apply(t,r);function a(e){f(o,s,n,a,i,"next",e)}function i(e){f(o,s,n,a,i,"throw",e)}a(void 0)})}}function m(){return(m=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var s in r)Object.prototype.hasOwnProperty.call(r,s)&&(e[s]=r[s])}return e}).apply(this,arguments)}let v="undefined"!=typeof AbortController;class g{static create(e){return new g(e)}get interceptors(){return{request:{use:(e,t)=>(this.requestInterceptors.push(e),e),eject:e=>{this.requestInterceptors=this.requestInterceptors.filter(t=>t!==e)},clear:()=>{this.requestInterceptors=[this.requestInterceptors[0]]}},response:{use:(e,t)=>(this.responseInterceptors.push({fn:e,onRejected:t}),e),eject:e=>{this.responseInterceptors=this.responseInterceptors.filter(t=>t.fn!==e)},clear:()=>{this.responseInterceptors=[]}}}}get plugins(){return{use:e=>(this._plugins.push(e),e),eject:e=>{this._plugins=this._plugins.filter(t=>t!==e)},clear:()=>{this._plugins=[]}}}request(t){var r=this;return y(function*(){let s=e({},r.config||{},"string"==typeof t?{url:t}:t||{},{headers:{},params:{}});for(let e of r.requestInterceptors)s=yield e(s);let n=r.handlerFetch.bind(r);return r._plugins.forEach(e=>{n=e(n)}),n(s)})()}handlerFetch(e){var r=this;return y(function*(){var s;let n,u;let{url:l,method:c,headers:p,timeout:d,signal:h,data:f}=e,y=function(e,t){if(null==e)return{};var r,s,n={},o=Object.keys(e);for(s=0;s<o.length;s++)t.indexOf(r=o[s])>=0||(n[r]=e[r]);return n}(e,["url","method","headers","timeout","signal","data"]),g=[];if(d&&v){let t=new AbortController;u=setTimeout(()=>{t.abort(new i(`timeout of ${d}ms exceeded`,e))},d),g.push(t.signal)}h&&g.push(h),n=g[0],g.length>1&&(n=t(g,()=>{clearTimeout(u)}));let T=e._url||l||"";e.baseURL&&!o(T)&&(T=(e.baseURL.endsWith("/")?e.baseURL:e.baseURL+"/")+(T.startsWith("/")?T.slice(1):T));let b=yield fetch(T,m({body:["HEAD","GET"].includes(e.method||"GET")?void 0:f},y,{signal:n,method:c,headers:p}));if(u&&clearTimeout(u),null==n||null==(s=n.clear)||s.call(n),!b.ok){let t;try{(t=yield b.text())&&(t=JSON.parse(t))}catch(e){}let s=new a(b.status?`Request failed with status code ${b.status}`:"Network error",e,{response:b,data:t,config:e,status:b.status,statusText:b.statusText,headers:b.headers});for(let e of r.responseInterceptors)e.onRejected&&(yield e.onRejected(s));throw s}if("HEAD"===e.method)return{data:void 0,request:e,config:e,response:b,headers:b.headers,status:b.status,statusText:b.statusText};if(!e.responseType||"json"===e.responseType||"text"===e.responseType){let t;try{t={data:yield b.text()};try{t.data&&"text"!==e.responseType&&(t.data=JSON.parse(t.data))}catch(e){}}catch(e){t={data:void 0}}let s={data:t.data,request:e,response:b};for(let e of r.responseInterceptors)s=yield e.fn(s);return{data:s.data,request:e,config:e,response:b,headers:b.headers,status:b.status,statusText:b.statusText}}return{data:void 0,request:e,config:e,response:b,headers:b.headers,status:b.status,statusText:b.statusText}})()}createGetHandler(e){return(t,r)=>this.request(r?m({},r,{method:e,url:t}):{method:e,url:t})}createPostHandler(e){return(t,r,s)=>this.request(s?m({},s,{method:e,url:t,data:r}):{method:e,url:t,data:r})}get(e,t){return this.createGetHandler("GET")(e,t)}head(e,t){return this.createGetHandler("HEAD")(e,t)}post(e,t,r){return this.createPostHandler("POST")(e,t,r)}put(e,t,r){return this.createPostHandler("PUT")(e,t,r)}patch(e,t,r){return this.createPostHandler("PATCH")(e,t,r)}delete(e,t){return this.createGetHandler("DELETE")(e,t)}options(e,t,r){return this.createPostHandler("OPTIONS")(e,t,r)}constructor(e){this.requestInterceptors=[d],this.responseInterceptors=[],this._plugins=[],this.config=e}}export{a as XiorError,i as XiorTimeoutError,t as anySignal,r as buildSortedURL,g as default,s as delay,n as encodeParams,o as isAbsoluteURL,g as xior}; |
@@ -29,2 +29,5 @@ export { merge } from 'ts-deepmerge'; | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -73,9 +76,10 @@ interface XiorResponse<T = any> { | ||
declare function isAbsoluteURL(url: string): boolean; | ||
declare class XiorTimeoutError extends Error { | ||
} | ||
declare class XiorError extends Error { | ||
request?: XiorRequestConfig; | ||
config?: XiorRequestConfig; | ||
response?: XiorResponse; | ||
constructor(message: string, request?: XiorRequestConfig, response?: XiorResponse); | ||
} | ||
declare class XiorTimeoutError extends XiorError { | ||
} | ||
@@ -82,0 +86,0 @@ declare class xior { |
{ | ||
"name": "xior", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A lite request lib based on **fetch** with plugins support, and axios similar API", | ||
@@ -113,2 +113,3 @@ "repository": "suhaotian/xior", | ||
"axios alternatives", | ||
"xior", | ||
"adapter", | ||
@@ -115,0 +116,0 @@ "plugins", |
@@ -36,2 +36,5 @@ type ICacheLike<T> = { | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -63,2 +66,5 @@ interface XiorResponse<T = any> { | ||
} | ||
interface XiorResponse { | ||
fromCache?: boolean; | ||
} | ||
} | ||
@@ -65,0 +71,0 @@ declare function xiorCachePlugin(options?: XiorCacheOptions): XiorPlugin; |
@@ -36,2 +36,5 @@ type ICacheLike<T> = { | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -63,2 +66,5 @@ interface XiorResponse<T = any> { | ||
} | ||
interface XiorResponse { | ||
fromCache?: boolean; | ||
} | ||
} | ||
@@ -65,0 +71,0 @@ declare function xiorCachePlugin(options?: XiorCacheOptions): XiorPlugin; |
@@ -1,1 +0,1 @@ | ||
import{lru as t}from"tiny-lru";function e(t,e,n,r,i,u,o){try{var l=t[u](o),c=l.value}catch(t){n(t);return}l.done?e(c):Promise.resolve(c).then(r,i)}function n(t){return function(){var n=this,r=arguments;return new Promise(function(i,u){var o=t.apply(n,r);function l(t){e(o,i,u,l,c,"next",t)}function c(t){e(o,i,u,l,c,"throw",t)}l(void 0)})}}function r(e={}){let{enableCache:r,defaultCache:i=t(100,3e5)}=e;return function(t){return n(function*(e){let{enableCache:u=r,forceUpdate:o,defaultCache:l=i,_url:c,encode:f,data:a}=e,d="GET"===e.method,h=typeof u;if("undefined"===h?d:"function"===h?u(e):!!u){let r=function(t,e,n){let r=e?n(e):"";r&&(r=t.includes("?")?t+"&"+r:t+"?"+r);let[i,u]=r.split("?");if(u){let t=u.split("&");return`${i}?${t.sort().join("&")}`}return r||t}(c,a,f),i=l.get(r);return(!i||o)&&(i=n(function*(){try{return yield t(e)}catch(t){throw"delete"in l?l.delete(r):l.del(r),t}})(),l.set(r,i)),i}return t(e)})}}export{r as default}; | ||
import{lru as t}from"tiny-lru";function e(t,e,n,r,i,u,o){try{var c=t[u](o),l=c.value}catch(t){n(t);return}c.done?e(l):Promise.resolve(l).then(r,i)}function n(t){return function(){var n=this,r=arguments;return new Promise(function(i,u){var o=t.apply(n,r);function c(t){e(o,i,u,c,l,"next",t)}function l(t){e(o,i,u,c,l,"throw",t)}c(void 0)})}}function r(e={}){let{enableCache:r,defaultCache:i=t(100,3e5)}=e;return function(t){return n(function*(e){let{enableCache:u=r,forceUpdate:o,defaultCache:c=i,_url:l,encode:f,data:a}=e,d="GET"===e.method,h=typeof u;if("undefined"===h?d:"function"===h?u(e):!!u){let r=function(t,e,n){let r=e?n(e):"";r&&(r=t.includes("?")?t+"&"+r:t+"?"+r);let[i,u]=r.split("?");if(u){let t=u.split("&");return`${i}?${t.sort().join("&")}`}return r||t}(l,a,f),i=c.get(r);return!i||o?(i=n(function*(){try{return yield t(e)}catch(t){throw"delete"in c?c.delete(r):c.del(r),t}})(),c.set(r,i),i):i.then(t=>(t.fromCache=!0,t))}return t(e)})}}export{r as default}; |
@@ -27,2 +27,5 @@ interface XiorRequestConfig<T = any> extends Omit<RequestInit, 'body'> { | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -42,2 +45,3 @@ interface XiorResponse<T = any> { | ||
request?: XiorRequestConfig; | ||
config?: XiorRequestConfig; | ||
response?: XiorResponse; | ||
@@ -44,0 +48,0 @@ constructor(message: string, request?: XiorRequestConfig, response?: XiorResponse); |
@@ -27,2 +27,5 @@ interface XiorRequestConfig<T = any> extends Omit<RequestInit, 'body'> { | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -42,2 +45,3 @@ interface XiorResponse<T = any> { | ||
request?: XiorRequestConfig; | ||
config?: XiorRequestConfig; | ||
response?: XiorResponse; | ||
@@ -44,0 +48,0 @@ constructor(message: string, request?: XiorRequestConfig, response?: XiorResponse); |
@@ -1,1 +0,1 @@ | ||
import"ts-deepmerge";class t extends Error{}class e extends Error{constructor(t,e,r){super(t),this.request=e,this.response=r}}function r(t,e,r,n,o,i,u){try{var s=t[i](u),c=s.value}catch(t){r(t);return}s.done?e(c):Promise.resolve(c).then(n,o)}function n(t){return function(){var e=this,n=arguments;return new Promise(function(o,i){var u=t.apply(e,n);function s(t){r(u,o,i,s,c,"next",t)}function c(t){r(u,o,i,s,c,"throw",t)}s(void 0)})}}function o(r={}){let{retryTimes:o,retryInterval:i,enableRetry:u}=r||{retryTimes:2,retryInterval:3e3};return function(r){return n(function*(s){let{retryTimes:c=o,retryInterval:f=i,enableRetry:a=u}=s,l=0;function y(){return h.apply(this,arguments)}function h(){return(h=n(function*(){try{return yield r(s)}catch(r){if(r instanceof e||r instanceof t){let t="GET"===s.method,e=typeof a,n="undefined"===e?t:"function"===e?a(s,r):!!a;if(c===l||!n)throw r;return f&&f>0&&l>0&&(yield new Promise(t=>{setTimeout(()=>{t("ok")},f)})),l++,y()}throw r}})).apply(this,arguments)}return y()})}}export{o as default}; | ||
import"ts-deepmerge";class t extends Error{constructor(t,e,r){super(t),this.request=e,this.config=e,this.response=r}}class e extends t{}function r(t,e,r,n,i,o,u){try{var s=t[o](u),c=s.value}catch(t){r(t);return}s.done?e(c):Promise.resolve(c).then(n,i)}function n(t){return function(){var e=this,n=arguments;return new Promise(function(i,o){var u=t.apply(e,n);function s(t){r(u,i,o,s,c,"next",t)}function c(t){r(u,i,o,s,c,"throw",t)}s(void 0)})}}function i(r={}){let{retryTimes:i,retryInterval:o,enableRetry:u}=r||{retryTimes:2,retryInterval:3e3};return function(r){return n(function*(s){let{retryTimes:c=i,retryInterval:f=o,enableRetry:a=u}=s,l=0;function y(){return h.apply(this,arguments)}function h(){return(h=n(function*(){try{return yield r(s)}catch(r){if(r instanceof t||r instanceof e){let t="GET"===s.method,e=typeof a,n="undefined"===e?t:"function"===e?a(s,r):!!a;if(c===l||!n)throw r;return f&&f>0&&l>0&&(yield new Promise(t=>{setTimeout(()=>{t("ok")},f)})),l++,y()}throw r}})).apply(this,arguments)}return y()})}}export{i as default}; |
@@ -27,2 +27,5 @@ interface XiorRequestConfig<T = any> extends Omit<RequestInit, 'body'> { | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -29,0 +32,0 @@ interface XiorResponse<T = any> { |
@@ -27,2 +27,5 @@ interface XiorRequestConfig<T = any> extends Omit<RequestInit, 'body'> { | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -29,0 +32,0 @@ interface XiorResponse<T = any> { |
@@ -36,2 +36,5 @@ type ICacheLike<T> = { | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -38,0 +41,0 @@ interface XiorResponse<T = any> { |
@@ -36,2 +36,5 @@ type ICacheLike<T> = { | ||
params: Record<string, any>; | ||
url: string; | ||
_url: string; | ||
method: string; | ||
}; | ||
@@ -38,0 +41,0 @@ interface XiorResponse<T = any> { |
@@ -54,2 +54,3 @@ [![Build](https://github.com/suhaotian/xior/actions/workflows/check.yml/badge.svg)](https://github.com/suhaotian/xior/actions/workflows/check.yml) | ||
- [Download file with `responseType: 'stream'` (In Node.JS)](#download-file-with-responsetype-stream-in-nodejs) | ||
- [Use stream](#use-stream) | ||
- [Migrate from `fetch` to **xior**](#migrate-from-fetch-to-xior) | ||
@@ -445,3 +446,7 @@ - [GET](#get-1) | ||
http.post('/users', { enableCache: true }); // make real http request | ||
http.post('/users', { enableCache: true }); // get cache from previous request | ||
const res = await http.post('/users', { enableCache: true }); // get cache from previous request | ||
if (res.fromCache) { | ||
// if fromCache is true, means data from cache! | ||
console.log('data from cache!'); | ||
} | ||
``` | ||
@@ -664,3 +669,2 @@ | ||
import xior from 'xior'; | ||
// or import { xior } from 'xior'; | ||
@@ -736,3 +740,2 @@ const axios = xior.create(); | ||
import xior from 'xior'; | ||
// or import { xior } from 'xior'; | ||
@@ -811,2 +814,53 @@ const axios = xior.create(); | ||
### Use stream | ||
axios: | ||
```ts | ||
import axios from 'axios'; | ||
import { Readable } from 'stream'; | ||
const http = axios.create(); | ||
async function getStream(url: string, params: Record<string, any>) { | ||
const { data } = await http.get(url, { | ||
params, | ||
responseType: 'stream', | ||
}); | ||
return data; | ||
} | ||
``` | ||
xior: | ||
```ts | ||
import axxios from 'xior'; | ||
import { Readable } from 'stream'; | ||
const http = axios.create(); | ||
async function getStream(url: string, params: Record<string, any>) { | ||
const { response } = await http.get(url, { | ||
params, | ||
responseType: 'stream', | ||
}); | ||
const stream = convertResponseToReadable(response); | ||
return stream; | ||
} | ||
function convertResponseToReadable(response: Response): Readable { | ||
const reader = response.body.getReader(); | ||
return new Readable({ | ||
async read() { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
this.push(null); | ||
} else { | ||
this.push(Buffer.from(value)); | ||
} | ||
}, | ||
}); | ||
} | ||
``` | ||
## Migrate from `fetch` to **xior** | ||
@@ -813,0 +867,0 @@ |
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
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
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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
130474
977
1189