UNPKG

14.8 kBJavaScriptView Raw
1"use strict";var flighty=function(){'use strict';function a(a,b,c,d,e){return a.reduce((a,f)=>(a!==b&&e&&(a=a.then(e)),f[c]&&(a=a.then(a=>f[c](...[].concat(a)))),f[d]&&(a=a.catch(f[d])),a),b)}var b=Object.prototype.hasOwnProperty,d=function(){for(var a=[],b=0;256>b;++b)a.push("%"+((16>b?"0":"")+b.toString(16)).toUpperCase());return a}(),e=function(a){for(;1<a.length;){var b=a.pop(),c=b.obj[b.prop];if(Array.isArray(c)){for(var d=[],e=0;e<c.length;++e)"undefined"!=typeof c[e]&&d.push(c[e]);b.obj[b.prop]=d}}},f=function(a,b){for(var c=b&&b.plainObjects?Object.create(null):{},d=0;d<a.length;++d)"undefined"!=typeof a[d]&&(c[d]=a[d]);return c},g={arrayToObject:f,assign:function(a,b){return Object.keys(b).reduce(function(a,c){return a[c]=b[c],a},a)},combine:function(c,a){return[].concat(c,a)},compact:function(a){for(var b=[{obj:{o:a},prop:"o"}],c=[],d=0;d<b.length;++d)for(var f=b[d],g=f.obj[f.prop],h=Object.keys(g),k=0;k<h.length;++k){var l=h[k],m=g[l];"object"==typeof m&&null!==m&&-1===c.indexOf(m)&&(b.push({obj:g,prop:l}),c.push(m))}return e(b),a},decode:function(a,b,c){var d=a.replace(/\+/g," ");if("iso-8859-1"===c)// unescape never throws, no try...catch needed:
2return d.replace(/%[0-9a-f]{2}/gi,unescape);// utf-8
3try{return decodeURIComponent(d)}catch(a){return d}},encode:function(a,b,e){// This code was originally written by Brian White (mscdex) for the io.js core querystring library.
4// It has been adapted here for stricter adherence to RFC 3986
5if(0===a.length)return a;var f="string"==typeof a?a:a+"";if("iso-8859-1"===e)return escape(f).replace(/%u[0-9a-f]{4}/gi,function(a){return"%26%23"+parseInt(a.slice(2),16)+"%3B"});for(var g,h="",j=0;j<f.length;++j){if(g=f.charCodeAt(j),45===g// -
6||46===g// .
7||95===g// _
8||126===g// ~
9||48<=g&&57>=g// 0-9
10||65<=g&&90>=g// a-z
11||97<=g&&122>=g// A-Z
12){h+=f.charAt(j);continue}if(128>g){h+=d[g];continue}if(2048>g){h+=d[192|g>>6]+d[128|63&g];continue}if(55296>g||57344<=g){h+=d[224|g>>12]+d[128|63&g>>6]+d[128|63&g];continue}j+=1,g=65536+((1023&g)<<10|1023&f.charCodeAt(j)),h+=d[240|g>>18]+d[128|63&g>>12]+d[128|63&g>>6]+d[128|63&g]}return h},isBuffer:function(a){return null!==a&&"undefined"!=typeof a&&!!(a.constructor&&a.constructor.isBuffer&&a.constructor.isBuffer(a))},isRegExp:function(a){return"[object RegExp]"===Object.prototype.toString.call(a)},merge:function a(c,d,e){if(!d)return c;if("object"!=typeof d){if(Array.isArray(c))c.push(d);else if("object"==typeof c)(e&&(e.plainObjects||e.allowPrototypes)||!b.call(Object.prototype,d))&&(c[d]=!0);else return[c,d];return c}if("object"!=typeof c)return[c].concat(d);var g=c;return Array.isArray(c)&&!Array.isArray(d)&&(g=f(c,e)),Array.isArray(c)&&Array.isArray(d)?(d.forEach(function(d,f){b.call(c,f)?c[f]&&"object"==typeof c[f]?c[f]=a(c[f],d,e):c.push(d):c[f]=d}),c):Object.keys(d).reduce(function(c,f){var g=d[f];return c[f]=b.call(c,f)?a(c[f],g,e):g,c},g)}},h=String.prototype.replace,i=/%20/g,j={default:"RFC3986",formatters:{RFC1738:function(a){return h.call(a,i,"+")},RFC3986:function(a){return a}},RFC1738:"RFC1738",RFC3986:"RFC3986"},k={brackets:function(a){// eslint-disable-line func-name-matching
13return a+"[]"},indices:function(a,b){// eslint-disable-line func-name-matching
14return a+"["+b+"]"},repeat:function(a){// eslint-disable-line func-name-matching
15return a}},l=Array.isArray,m=Array.prototype.push,n=function(a,b){m.apply(a,l(b)?b:[b])},o=Date.prototype.toISOString,p={addQueryPrefix:!1,allowDots:!1,charset:"utf-8",charsetSentinel:!1,delimiter:"&",encode:!0,encoder:g.encode,encodeValuesOnly:!1,// deprecated
16indices:!1,serializeDate:function(a){// eslint-disable-line func-name-matching
17return o.call(a)},skipNulls:!1,strictNullHandling:!1},q=function a(// eslint-disable-line func-name-matching
18b,c,d,e,f,h,j,k,l,m,o,q,r){var s=b;if("function"==typeof j?s=j(c,s):s instanceof Date&&(s=m(s)),null===s){if(e)return h&&!q?h(c,p.encoder,r):c;s=""}if("string"==typeof s||"number"==typeof s||"boolean"==typeof s||g.isBuffer(s)){if(h){var t=q?c:h(c,p.encoder,r);return[o(t)+"="+o(h(s,p.encoder,r))]}return[o(c)+"="+o(s+"")]}var u=[];if("undefined"==typeof s)return u;var v;if(Array.isArray(j))v=j;else{var w=Object.keys(s);v=k?w.sort(k):w}for(var x,y=0;y<v.length;++y)(x=v[y],!(f&&null===s[x]))&&(Array.isArray(s)?n(u,a(s[x],d(c,x),d,e,f,h,j,k,l,m,o,q,r)):n(u,a(s[x],c+(l?"."+x:"["+x+"]"),d,e,f,h,j,k,l,m,o,q,r)));return u},r=function(a,b){var c=a,d=b?g.assign({},b):{};if(null!==d.encoder&&void 0!==d.encoder&&"function"!=typeof d.encoder)throw new TypeError("Encoder has to be a function.");var e="undefined"==typeof d.delimiter?p.delimiter:d.delimiter,f="boolean"==typeof d.strictNullHandling?d.strictNullHandling:p.strictNullHandling,h="boolean"==typeof d.skipNulls?d.skipNulls:p.skipNulls,l="boolean"==typeof d.encode?d.encode:p.encode,m="function"==typeof d.encoder?d.encoder:p.encoder,o="function"==typeof d.sort?d.sort:null,r="undefined"==typeof d.allowDots?p.allowDots:!!d.allowDots,s="function"==typeof d.serializeDate?d.serializeDate:p.serializeDate,t="boolean"==typeof d.encodeValuesOnly?d.encodeValuesOnly:p.encodeValuesOnly,u=d.charset||p.charset;if("undefined"!=typeof d.charset&&"utf-8"!==d.charset&&"iso-8859-1"!==d.charset)throw new Error("The charset option must be either utf-8, iso-8859-1, or undefined");if("undefined"==typeof d.format)d.format=j["default"];else if(!Object.prototype.hasOwnProperty.call(j.formatters,d.format))throw new TypeError("Unknown format option provided.");var v,w,x=j.formatters[d.format];"function"==typeof d.filter?(w=d.filter,c=w("",c)):Array.isArray(d.filter)&&(w=d.filter,v=w);var y=[];if("object"!=typeof c||null===c)return"";var z=d.arrayFormat in k?d.arrayFormat:"indices"in d?d.indices?"indices":"repeat":"indices";var A=k[z];v||(v=Object.keys(c)),o&&v.sort(o);for(var B,C=0;C<v.length;++C)(B=v[C],!(h&&null===c[B]))&&n(y,q(c[B],B,A,f,h,l?m:null,w,o,r,s,x,t,u));var D=y.join(e),E=!0===d.addQueryPrefix?"?":"";return d.charsetSentinel&&("iso-8859-1"===u?E+="utf8=%26%2310003%3B&":E+="utf8=%E2%9C%93&"),0<D.length?E+D:""},s=Object.prototype.hasOwnProperty,t={allowDots:!1,allowPrototypes:!1,arrayLimit:20,charset:"utf-8",charsetSentinel:!1,decoder:g.decode,delimiter:"&",depth:5,ignoreQueryPrefix:!1,interpretNumericEntities:!1,parameterLimit:1e3,parseArrays:!0,plainObjects:!1,strictNullHandling:!1},u=function(a){return a.replace(/&#(\d+);/g,function(a,b){return String.fromCharCode(parseInt(b,10))})},v=function(a,b){var c,d={},e=b.ignoreQueryPrefix?a.replace(/^\?/,""):a,f=b.parameterLimit===1/0?void 0:b.parameterLimit,h=e.split(b.delimiter,f),j=-1,k=b.charset;if(b.charsetSentinel)for(c=0;c<h.length;++c)0===h[c].indexOf("utf8=")&&(h[c]==="utf8=%E2%9C%93"?k="utf-8":h[c]==="utf8=%26%2310003%3B"&&(k="iso-8859-1"),j=c,c=h.length);for(c=0;c<h.length;++c)if(c!==j){var l,m,n=h[c],o=n.indexOf("]="),p=-1===o?n.indexOf("="):o+1;-1===p?(l=b.decoder(n,t.decoder,k),m=b.strictNullHandling?null:""):(l=b.decoder(n.slice(0,p),t.decoder,k),m=b.decoder(n.slice(p+1),t.decoder,k)),m&&b.interpretNumericEntities&&"iso-8859-1"===k&&(m=u(m)),d[l]=s.call(d,l)?g.combine(d[l],m):m}return d},w=function(a,b,c){for(var d=b,e=a.length-1;0<=e;--e){var f,g=a[e];if("[]"===g&&c.parseArrays)f=[].concat(d);else{f=c.plainObjects?Object.create(null):{};var h="["===g.charAt(0)&&"]"===g.charAt(g.length-1)?g.slice(1,-1):g,j=parseInt(h,10);c.parseArrays||""!==h?!isNaN(j)&&g!==h&&j+""===h&&0<=j&&c.parseArrays&&j<=c.arrayLimit?(f=[],f[j]=d):f[h]=d:f={0:d}}d=f}return d},x=function(a,b,c){if(a){// Transform dot notation to bracket notation
19var d=c.allowDots?a.replace(/\.([^.[]+)/g,"[$1]"):a,e=/(\[[^[\]]*])/,f=/(\[[^[\]]*])/g,g=e.exec(d),h=g?d.slice(0,g.index):d,j=[];// The regex chunks
20if(h){// If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
21if(!c.plainObjects&&s.call(Object.prototype,h)&&!c.allowPrototypes)return;j.push(h)}// Loop through children appending to the array until we hit depth
22for(var k=0;null!==(g=f.exec(d))&&k<c.depth;){if(k+=1,!c.plainObjects&&s.call(Object.prototype,g[1].slice(1,-1))&&!c.allowPrototypes)return;j.push(g[1])}// If there's a remainder, just add whatever is left
23return g&&j.push("["+d.slice(g.index)+"]"),w(j,b,c)}},y={formats:j,parse:function(a,b){var c=b?g.assign({},b):{};if(null!==c.decoder&&void 0!==c.decoder&&"function"!=typeof c.decoder)throw new TypeError("Decoder has to be a function.");if(c.ignoreQueryPrefix=!0===c.ignoreQueryPrefix,c.delimiter="string"==typeof c.delimiter||g.isRegExp(c.delimiter)?c.delimiter:t.delimiter,c.depth="number"==typeof c.depth?c.depth:t.depth,c.arrayLimit="number"==typeof c.arrayLimit?c.arrayLimit:t.arrayLimit,c.parseArrays=!1!==c.parseArrays,c.decoder="function"==typeof c.decoder?c.decoder:t.decoder,c.allowDots="undefined"==typeof c.allowDots?t.allowDots:!!c.allowDots,c.plainObjects="boolean"==typeof c.plainObjects?c.plainObjects:t.plainObjects,c.allowPrototypes="boolean"==typeof c.allowPrototypes?c.allowPrototypes:t.allowPrototypes,c.parameterLimit="number"==typeof c.parameterLimit?c.parameterLimit:t.parameterLimit,c.strictNullHandling="boolean"==typeof c.strictNullHandling?c.strictNullHandling:t.strictNullHandling,"undefined"!=typeof c.charset&&"utf-8"!==c.charset&&"iso-8859-1"!==c.charset)throw new Error("The charset option must be either utf-8, iso-8859-1, or undefined");if("undefined"==typeof c.charset&&(c.charset=t.charset),""===a||null===a||"undefined"==typeof a)return c.plainObjects?Object.create(null):{};for(var d="string"==typeof a?v(a,c):a,e=c.plainObjects?Object.create(null):{},f=Object.keys(d),h=0;h<f.length;++h){var j=f[h],k=x(j,d[j],c);e=g.merge(e,k,c)}return g.compact(e)},stringify:r},z="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?{}:self:global:window,A=function(a,b){return b={exports:{}},a(b,b.exports),b.exports}(function(a){(function(b,c,d){a.exports?a.exports=d():c[b]=d()})("urljoin",z,function(){function a(a){var b=[];// If the first part is a plain protocol, we combine it with the next part.
24if(a[0].match(/^[^/:]+:\/*$/)&&1<a.length){var c=a.shift();a[0]=c+a[0]}// There must be two or three slashes in the file protocol, two slashes in anything else.
25a[0]=a[0].match(/^file:\/\/\//)?a[0].replace(/^([^/:]+):\/*/,"$1:///"):a[0].replace(/^([^/:]+):\/*/,"$1://");for(var d,e=0;e<a.length;e++){if(d=a[e],"string"!=typeof d)throw new TypeError("Url must be a string. Received "+d);""!==d&&(0<e&&(d=d.replace(/^[\/]+/,"")),d=e<a.length-1?d.replace(/[\/]+$/,""):d.replace(/[\/]+$/,"/"),b.push(d))}var f=b.join("/");// Each input component is now separated by a single slash except the possible first plain protocol part.
26// remove trailing slash before parameters or hash
27f=f.replace(/\/(\?|&|#[^!])/g,"$1");// replace ? in parameters with &
28var g=f.split("?");return f=g.shift()+(0<g.length?"?":"")+g.join("&"),f}return function(){var b;return b="object"==typeof arguments[0]?arguments[0]:[].slice.call(arguments),a(b)}})});const B=(a,b)=>{if(a){const c=b.get(a);!c||--c.count||b.delete(a)}},C=({abortToken:a,signal:b},c,d)=>{// if there is no token or signal, use Flighty abortController
29if(!a&&!b)return c.signal;// otherwise, use an abortController local to this request
30let e=new AbortController;if(a){// allow to use a single token to cancel multiple requests
31const b=d.get(a)||{controller:e,count:0};b.count++,d.set(a,b),e=b.controller}// the user has defined their own signal. We won't use it directly, but we'll listen to it
32return b&&b.addEventListener("abort",()=>e.abort()),c.signal.addEventListener("abort",()=>e.abort()),e.signal};if("undefined"==typeof fetch)throw new Error("You need a fetch implementation. Try npm install cross-fetch");if("undefined"==typeof AbortController)throw new Error("You're missing an AbortController implementation. Try npm install abortcontroller-polyfill");const D=["GET","POST","PUT","HEAD","OPTIONS","DELETE","PATCH"],E=(a,b,c,d)=>{// keep abortToken out of the fetch params
33const{abortToken:e,...f}=d,g={...f,method:a,headers:{...(b.headers||{}),...d.headers}};g.headers=Object.keys(g.headers).reduce((a,b)=>{const c=g.headers[b];return c?{[b]:c,...a}:a},{}),g.body||"POST"!==a||(g.body=""),"GET"===a&&g.body&&(c+=`?${y.stringify(g.body,{arrayFormat:b.arrayFormat})}`,delete g.body),g.body&&"object"==typeof g.body&&(g.body=JSON.stringify(g.body));const h=b.baseURI?A(b.baseURI,c):c;return fetch(h,g)},F=async(b,c,{path:d,options:e},f,g=0)=>{// don't let the interceptors modify the abort signal - it's the one
34// attached to flighty's abortController so if they do, it will break our
35// "abortAll" method
36const h=C(e,c.abortController,c.abortTokenMap),i={...e},j={...f},k=d,l=[],m=Array.from(c.interceptors),n=a(m,Promise.resolve([d,e,{...f},g]),"request","requestError",// don't let interceptors modify the extra or retryCount data
37a=>{const[b,c]=a.slice(0,2);return l.push([b,{...c}]),[b,c].concat([{...f},g])}),o=a(m.reverse(),(async()=>{// stuff from the interceptors
38const[a,d]=await n;l.push([a,{...d}]);const e=await E(b,c,a,{...d,signal:h});// add in the json and text responses to extra to make life easier
39// for people - they can still await them if they want
40if(e.flighty={method:b,retryCount:g,// the values flighty was called with
41call:{path:k,options:i,extra:j},// the values that were returned from each request interceptor - useful for debugging!
42intercepted:l,// retry method
43retry:async()=>(g++,await F(b,c,{path:k,options:i},j,g))},e){try{e.flighty.json=await e.clone().json()}catch(a){}try{e.flighty.text=await e.clone().text()}catch(a){}}return e})(),"response","responseError");try{await o}catch(a){}return B(e.abortToken,c.abortTokenMap),o};return class{constructor(a={}){D.forEach(a=>this[a.toLowerCase()]=(b="/",c={},d={})=>F(a,this,{path:b,options:c},d));let b;const c=new Set,d=new Map;Object.defineProperties(this,{headers:{get(){return a.headers},set(b={}){a={...a,headers:b}}},arrayFormat:{get(){return a.arrayFormat||"indicies"}},baseURI:{get(){return a.baseURI},set(b){a={...a,baseURI:b}}},interceptors:{get(){return c}},interceptor:{get(){return{register:a=>this.registerInterceptor(a),unregister:a=>this.removeInterceptor(a),clear:()=>this.clearInterceptors()}}},abortController:{get(){return b||(b=new AbortController,b.signal.addEventListener("abort",()=>{b=null})),b}},abortTokenMap:{get(){return d}}})}abort(a){const b=this.abortTokenMap.get(a);return b&&b.controller.abort()}abortAll(){this.abortController.abort()}registerInterceptor(a){if(!a)throw new Error("cannot register a null interceptor");return this.interceptors.add(a),()=>this.interceptors.delete(a)}clearInterceptors(){this.interceptors.clear()}removeInterceptor(a){this.interceptors.delete(a)}jwt(a){return this.headers={...this.headers,Authorization:a?`Bearer ${a}`:null},this}}}();
44//# sourceMappingURL=flighty.min.js.map