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