UNPKG

xior

Version:

A lite http request lib based on fetch with plugins support and similar API to axios.

2 lines 8.43 kB
/*! For license information please see error-cache.umd.js.LICENSE.txt */ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.xiorErrorCache=t():e.xiorErrorCache=t()}(this,(function(){return function(){"use strict";var e={99:function(e,t,r){var i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.buildSortedURL=void 0,t.delay=function(e){return new Promise((t=>{setTimeout((()=>{t(1)}),e)}))};const n=i(r(629));t.buildSortedURL=n.default},330:function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.DELETE=t.PATCH=t.PUT=t.POST=t.OPTIONS=t.GET=t.HEAD=t.m=t.isArray=t.keys=t.op=t.nullValue=t.undefinedValue=t.O=t.h=t.status=t.addEventListener=t.qs=t.abort=t.json=t.text=t.p=t.f=t.o=void 0,t.o="object",t.f="function",t.p="prototype",t.text="text",t.json="json",t.abort="abort",t.qs="paramsSerializer",t.addEventListener="addEventListener",t.status="status",t.h="headers",t.O=Object,t.undefinedValue=void 0,t.nullValue=null,t.op=t.O[t.p],t.keys=t.O.keys,t.isArray=Array.isArray,t.m="method",t.HEAD="HEAD",t.GET="GET",t.OPTIONS="OPTIONS",t.POST="POST",t.PUT="PUT",t.PATCH="PATCH",t.DELETE="DELETE"},431:function(e,t,r){var i=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))((function(n,s){function o(e){try{u(i.next(e))}catch(e){s(e)}}function l(e){try{u(i.throw(e))}catch(e){s(e)}}function u(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,l)}u((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e={}){const{enableCache:t,defaultCache:r=(0,n.lru)(e.cacheItems||100),useCacheFirst:l,onCacheRequest:u}=e;return function(e){return n=>i(this,void 0,void 0,(function*(){const{enableCache:a=t,defaultCache:c=r,useCacheFirst:h=l,onCacheRequest:f=u,paramsSerializer:d}=n,p=n.method===o.GET||n.isGet,v=typeof a,m=v===o.f;let y=o.undefinedValue;if(m&&(y=a(n)),y===o.undefinedValue&&(y=m||v===`${o.undefinedValue}`?p:Boolean(a)),!y)return e(n);const b=c,x=(0,s.buildSortedURL)(n.url&&(0,s.isAbsoluteURL)(n.url)?n.url:(0,s.joinPath)(n.baseURL,n.url),{a:n.data,b:n.params},d);try{if(h){const t=b.get(x);if(b.set(x,{loading:!0,res:null==t?void 0:t.res,cacheTime:null==t?void 0:t.cacheTime}),null==t?void 0:t.res)return(null==t?void 0:t.loading)||(null==f||f(n),(()=>{i(this,void 0,void 0,(function*(){try{const t=yield e(n);b.set(x,{loading:!1,res:t,cacheTime:Date.now()})}catch(e){const t=b.get(x);h&&b.set(x,{loading:!1,res:null==t?void 0:t.res,cacheTime:null==t?void 0:t.cacheTime})}}))})()),t.res.fromCache=!0,t.res.cacheTime=null==t?void 0:t.cacheTime,null==t?void 0:t.res}const t=yield e(n);return b.set(x,{loading:!1,res:t,cacheTime:Date.now()}),t}catch(e){const t=b.get(x);if(h&&b.set(x,{loading:!1,res:null==t?void 0:t.res,cacheTime:null==t?void 0:t.cacheTime}),null==t?void 0:t.res)return t.res.fromCache=!0,t.res.error=e,t.res.cacheTime=null==t?void 0:t.cacheTime,t.res;throw e}}))}};const n=r(432),s=r(976),o=r(330)},432:function(e,t){class r{constructor(e=0,t=0,r=!1){this.first=null,this.items=Object.create(null),this.last=null,this.max=e,this.resetTtl=r,this.size=0,this.ttl=t}clear(){return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this}delete(e){if(this.has(e)){const t=this.items[e];delete this.items[e],this.size--,null!==t.prev&&(t.prev.next=t.next),null!==t.next&&(t.next.prev=t.prev),this.first===t&&(this.first=t.next),this.last===t&&(this.last=t.prev)}return this}entries(e=this.keys()){return e.map((e=>[e,this.get(e)]))}evict(e=!1){if(e||this.size>0){const e=this.first;delete this.items[e.key],0==--this.size?(this.first=null,this.last=null):(this.first=e.next,this.first.prev=null)}return this}expiresAt(e){let t;return this.has(e)&&(t=this.items[e].expiry),t}get(e){let t;if(this.has(e)){const r=this.items[e];this.ttl>0&&r.expiry<=Date.now()?this.delete(e):(t=r.value,this.set(e,t,!0))}return t}has(e){return e in this.items}keys(){const e=[];let t=this.first;for(;null!==t;)e.push(t.key),t=t.next;return e}set(e,t,r=!1,i=this.resetTtl){let n;if(r||this.has(e)){if(n=this.items[e],n.value=t,!1===r&&i&&(n.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.last!==n){const e=this.last,t=n.next,r=n.prev;this.first===n&&(this.first=n.next),n.next=null,n.prev=this.last,e.next=n,null!==r&&(r.next=t),null!==t&&(t.prev=r)}}else this.max>0&&this.size===this.max&&this.evict(!0),n=this.items[e]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:e,prev:this.last,next:null,value:t},1==++this.size?this.first=n:this.last.next=n;return this.last=n,this}values(e=this.keys()){return e.map((e=>this.get(e)))}}t.LRU=r,t.lru=function(e=1e3,t=0,i=!1){if(isNaN(e)||e<0)throw new TypeError("Invalid max value");if(isNaN(t)||t<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof i)throw new TypeError("Invalid resetTtl value");return new r(e,t,i)}},629:function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r){const i=t?r(t):"",n=e.includes("?")?"&":"?",s=i?`${e}${n}${i}`:e,[o,l]=s.split("?");if(!l)return s;return`${o}?${l.split("&").sort().join("&")}`}},699:function(e,t,r){Object.defineProperty(t,"__esModule",{value:!0}),t.anySignal=function(e,t){const r=new AbortController;function n(e){r.abort(e),o()}const s=[];for(const t of e){if(!0===(null==t?void 0:t.aborted)){n(t.reason);break}if(null==t?void 0:t[i.addEventListener]){const e=()=>{n(t.reason)};s.push((()=>{var r;null===(r=t.removeEventListener)||void 0===r||r.call(t,i.abort,e)})),t[i.addEventListener](i.abort,e)}}function o(){s.forEach((e=>e())),null==t||t()}const l=r.signal;return l.clear=o,l};const i=r(330)},729:function(e,t,r){Object.defineProperty(t,"__esModule",{value:!0}),t.merge=void 0;const i=r(330),n=i.O.getPrototypeOf,s=e=>{if(e!==i.nullValue&&typeof e===i.o){if(typeof n===i.f){const t=n(e);return t===i.op||t===i.nullValue}return i.op.toString.call(e)===`[${i.o} Object]`}return!1};t.merge=(...e)=>e.reduce(((e,r)=>{if((0,i.isArray)(r))throw new TypeError(`Arguments must be ${i.o}s, not arrays.`);return(0,i.keys)(r).forEach((n=>{["__proto__","constructor",i.p].includes(n)||((0,i.isArray)(e[n])&&(0,i.isArray)(r[n])?e[n]=Array.from(new Set(e[n].concat(r[n]))):s(e[n])&&s(r[n])?e[n]=(0,t.merge)(e[n],r[n]):e[n]=r[n])})),e}),{})},976:function(e,t,r){var i=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var n=Object.getOwnPropertyDescriptor(t,r);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,n)}:function(e,t,r,i){void 0===i&&(i=r),e[i]=t[r]}),n=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||i(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),t.XiorTimeoutError=t.XiorError=void 0,t.encodeParams=function e(t,r=!0,i=s.nullValue,n){if(t===s.undefinedValue||t===s.nullValue)return"";const o=[],l=r?encodeURIComponent:e=>e,u=(0,s.isArray)(t),{arrayFormat:a,allowDots:c,serializeDate:h}=n||{},f=e=>{if(c&&!u)return`.${e}`;if(u){if("brackets"===a)return"[]";if("repeat"===a)return""}return`[${e}]`};for(const u in t)if(s.op.hasOwnProperty.call(t,u)){let a=t[u];if(a!==s.undefinedValue){const t=i?`${i}${f(u)}`:l(u);if(!isNaN(a)&&a instanceof Date&&(a=h?h(a):a.toISOString()),typeof a===s.o){const i=e(a,r,t,n);""!==i&&o.push(i)}else o.push(`${l(t)}=${l(a)}`)}}return o.join("&")},t.trimUndefined=function e(t){return(0,s.isArray)(t)?t.map(e):(t&&typeof t===s.o&&(0,s.keys)(t).forEach((r=>{const i=t[r];i===s.undefinedValue?delete t[r]:e(i)})),t)},t.isAbsoluteURL=function(e){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(e)},t.joinPath=function(e,t){return e?t?(e.endsWith("/")?e:e+"/")+("/"===t[0]?t.slice(1):t):e:t||""},t.isXiorError=function(e){return(null==e?void 0:e.name)===o||(null==e?void 0:e.name)===l};const s=r(330);n(r(699),t),n(r(729),t),n(r(99),t);const o="XiorError",l="XiorTimeoutError";class u extends Error{constructor(e,t,r){super(e),this.name=o,this.request=t,this.config=t,this.response=r}}t.XiorError=u,t.XiorTimeoutError=class extends u{constructor(e,t,r){super(e,t,r),this.name=l}}}},t={},r=function r(i){var n=t[i];if(void 0!==n)return n.exports;var s=t[i]={exports:{}};return e[i].call(s.exports,s,s.exports,r),s.exports}(431);return r.default}()}));