UNPKG

xior

Version:

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

1 lines 10.7 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.xior=t():e.xior=t()}(this,(function(){return function(){"use strict";var e={99:function(e,t,r){var n=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 o=n(r(629));t.buildSortedURL=o.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"},340:function(e,t){Object.defineProperty(t,"__esModule",{value:!0})},393:function(e,t,r){var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{a(n.next(e))}catch(e){i(e)}}function u(e){try{a(n.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,u)}a((n=n.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){return n(this,void 0,void 0,(function*(){const t=e[o.qs]||i.encodeParams,r=!1!==e.encodeURI,n=e[o.m]&&e[o.m].toUpperCase();let s=e.url;const a=s,h=d&&e.data instanceof URLSearchParams,p=h?o.O.fromEntries(e.data.entries()):e.data;let y=p;const b=(null==e?void 0:e[o.h])?Object.assign({},e[o.h]):{};let O=e.params;const v=function(e=o.GET){return[o.HEAD,o.GET,o.OPTIONS].includes(e)}(n);if(p&&typeof p.append!==o.f){let r="",n="content-type";if(null==e?void 0:e[o.h]){const t=(0,o.keys)(e[o.h]).find((e=>e.toLowerCase()===n));t&&(n=t,r=e[o.h][t])}r&&!h||(r=v||h?u:l,b[n]=r),typeof p===o.o&&(v&&O&&(O=(0,i.merge)(p,O)),f.test(r)?y=JSON.stringify((0,i.trimUndefined)(p)):!v&&c.test(r)&&(y=t(p)))}if(O&&(0,o.keys)(O).length>0){const e=t(O,r);s+=s.includes("?")?`&${e}`:`?${e}`}return Object.assign(Object.assign({},e),{_data:y,_url:s,data:p,url:a,method:n,[o.h]:b,isGet:v})}))};const o=r(330),i=r(976),s="application/",u=`${s}x-www-form-urlencoded`,a=RegExp,c=new a(`^${u}`,"i"),l=`${s}${o.json}`,f=new a(`^${s}.*${o.json}.*`,"i"),d=typeof URLSearchParams!=`${o.undefinedValue}`},509:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0});const i=r(895);o(r(895),t),o(r(340),t),o(r(976),t);const s=Object.assign(i.Xior.create(),{create:i.Xior.create,VERSION:i.Xior.VERSION});t.default=s},629:function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r){const n=t?r(t):"",o=e.includes("?")?"&":"?",i=n?`${e}${o}${n}`:e,[s,u]=i.split("?");if(!u)return i;return`${s}?${u.split("&").sort().join("&")}`}},699:function(e,t,r){Object.defineProperty(t,"__esModule",{value:!0}),t.anySignal=function(e,t){const r=new AbortController;function o(e){r.abort(e),s()}const i=[];for(const t of e){if(!0===(null==t?void 0:t.aborted)){o(t.reason);break}if(null==t?void 0:t[n.addEventListener]){const e=()=>{o(t.reason)};i.push((()=>{var r;null===(r=t.removeEventListener)||void 0===r||r.call(t,n.abort,e)})),t[n.addEventListener](n.abort,e)}}function s(){i.forEach((e=>e())),null==t||t()}const u=r.signal;return u.clear=s,u};const n=r(330)},729:function(e,t,r){Object.defineProperty(t,"__esModule",{value:!0}),t.merge=void 0;const n=r(330),o=n.O.getPrototypeOf,i=e=>{if(e!==n.nullValue&&typeof e===n.o){if(typeof o===n.f){const t=o(e);return t===n.op||t===n.nullValue}return n.op.toString.call(e)===`[${n.o} Object]`}return!1};t.merge=(...e)=>e.reduce(((e,r)=>{if((0,n.isArray)(r))throw new TypeError(`Arguments must be ${n.o}s, not arrays.`);return(0,n.keys)(r).forEach((o=>{["__proto__","constructor",n.p].includes(o)||((0,n.isArray)(e[o])&&(0,n.isArray)(r[o])?e[o]=Array.from(new Set(e[o].concat(r[o]))):i(e[o])&&i(r[o])?e[o]=(0,t.merge)(e[o],r[o]):e[o]=r[o])})),e}),{})},895:function(e,t,r){var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{a(n.next(e))}catch(e){i(e)}}function u(e){try{a(n.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,u)}a((n=n.apply(e,t||[])).next())}))},o=this&&this.__rest||function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(e);o<n.length;o++)t.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(e,n[o])&&(r[n[o]]=e[n[o]])}return r},i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.Xior=void 0;const s=i(r(393)),u=r(330),a=r(976),c=typeof AbortController!=`${u.undefinedValue}`;class l{constructor(e){this.REQI=[],this.RESI=[],this.P=[],this.config=e,this.defaults={params:{},[u.h]:{}}}get interceptors(){return{request:{use:(e,t,r)=>(this.REQI.push(e),e),eject:e=>{this.REQI=this.REQI.filter((t=>t!==e))},clear:()=>{this.REQI=[]}},response:{use:(e,t)=>(this.RESI.push({fn:e,onRejected:t}),e),eject:e=>{this.RESI=this.RESI.filter((t=>t.fn!==e))},clear:()=>{this.RESI=[]}}}}get plugins(){return{use:e=>(this.P.push(e),e),eject:e=>{this.P=this.P.filter((t=>t!==e))},clear:()=>{this.P=[]}}}request(e){return n(this,void 0,void 0,(function*(){let t=(0,a.merge)(this.config||{},this.defaults,"string"==typeof e?{url:e}:e);const r="credentials";t.withCredentials&&!t[r]&&(t[r]="include"),t[u.qs]||(t[u.qs]=a.encodeParams);for(const e of this.REQI)t=yield e(t);let n=this._.bind(this);this.P.forEach((e=>{n=e(n,this)}));let o=n(t);if(!t._did){let e=0;const t=[];for(this.RESI.forEach((function(e){t.push(e.fn,e.onRejected)}));t.length>e;)o=o.then(t[e++],t[e++])}return o}))}_(e){return n(this,void 0,void 0,(function*(){const t=yield(0,s.default)(e),{url:r,method:i,headers:l,timeout:f,signal:d,data:h,_data:p,_url:y,isGet:b,fetch:O}=t,v=o(t,["url","method","headers","timeout","signal","data","_data","_url","isGet","fetch"]);let m;e._url=y;const E=[];let _=u.undefinedValue;if(f&&c){const t=new AbortController;_=setTimeout((()=>{t.abort(new a.XiorTimeoutError(`timeout of ${f}ms exceeded`,e))}),f),E.push(t.signal)}d&&E.push(d),m=E[0],E.length>1&&(m=(0,a.anySignal)(E,(()=>{clearTimeout(_)})));let P=y||r;const j="baseURL";return e[j]&&!(0,a.isAbsoluteURL)(P)&&(P=(0,a.joinPath)(e[j],P)),(O||fetch)(P,Object.assign(Object.assign({body:b?u.undefinedValue:p},v),{signal:m,method:i,headers:l})).then((t=>n(this,void 0,void 0,(function*(){const{responseType:r}=e,o=yield function(e,t){return n(this,void 0,void 0,(function*(){let r;if(t&&e.ok&&![u.text,u.json].includes(t)){if("blob"===t)return e.blob();if("arraybuffer"===t)return e.arrayBuffer()}else if(r=yield e[u.text](),r&&t!==u.text)try{r=JSON.parse(r)}catch(e){}return r}))}(t,r),i={data:o,response:t,config:e,request:e,[u.status]:t[u.status],statusText:t.statusText,[u.h]:t[u.h]};if(!t.ok){const r=new a.XiorError(t[u.status]?`Request failed with status code ${t[u.status]}`:"Network error",e,i);return Promise.reject(r)}return i})))).finally((()=>{var e;_&&clearTimeout(_),null===(e=null==m?void 0:m.clear)||void 0===e||e.call(m)}))}))}cG(e){return(t,r)=>this.request(r?Object.assign(Object.assign({},r),{method:e,url:t}):{method:e,url:t})}cP(e){return(t,r,n)=>this.request(n?Object.assign(Object.assign({},n),{method:e,url:t,data:r}):{method:e,url:t,data:r})}get(e,t){return this.cG(u.GET)(e,t)}head(e,t){return this.cG(u.HEAD)(e,t)}post(e,t,r){return this.cP(u.POST)(e,t,r)}put(e,t,r){return this.cP(u.PUT)(e,t,r)}patch(e,t,r){return this.cP(u.PATCH)(e,t,r)}delete(e,t){return this.cG(u.DELETE)(e,t)}options(e,t){return this.cG(u.OPTIONS)(e,t)}}t.Xior=l,l.create=e=>new l(e),l.VERSION="0.7.7"},976:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),t.XiorTimeoutError=t.XiorError=void 0,t.encodeParams=function e(t,r=!0,n=i.nullValue,o){if(t===i.undefinedValue||t===i.nullValue)return"";const s=[],u=r?encodeURIComponent:e=>e,a=(0,i.isArray)(t),{arrayFormat:c,allowDots:l,serializeDate:f}=o||{},d=e=>{if(l&&!a)return`.${e}`;if(a){if("brackets"===c)return"[]";if("repeat"===c)return""}return`[${e}]`};for(const a in t)if(i.op.hasOwnProperty.call(t,a)){let c=t[a];if(c!==i.undefinedValue){const t=n?`${n}${d(a)}`:u(a);if(!isNaN(c)&&c instanceof Date&&(c=f?f(c):c.toISOString()),typeof c===i.o){const n=e(c,r,t,o);""!==n&&s.push(n)}else s.push(`${u(t)}=${u(c)}`)}}return s.join("&")},t.trimUndefined=function e(t){return(0,i.isArray)(t)?t.map(e):(t&&typeof t===i.o&&(0,i.keys)(t).forEach((r=>{const n=t[r];n===i.undefinedValue?delete t[r]:e(n)})),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)===s||(null==e?void 0:e.name)===u};const i=r(330);o(r(699),t),o(r(729),t),o(r(99),t);const s="XiorError",u="XiorTimeoutError";class a extends Error{constructor(e,t,r){super(e),this.name=s,this.request=t,this.config=t,this.response=r}}t.XiorError=a,t.XiorTimeoutError=class extends a{constructor(e,t,r){super(e,t,r),this.name=u}}}},t={},r=function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n].call(i.exports,i,i.exports,r),i.exports}(509);return r.default}()}));