UNPKG

14.6 kBJavaScriptView Raw
1"use strict";var flighty=function(){'use strict';var a=String.prototype,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)return d.replace(/%[0-9a-f]{2}/gi,unescape);try{return decodeURIComponent(d)}catch(a){return d}},encode:function(a,b,e){if(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||46===g||95===g||126===g||48<=g&&57>=g||65<=g&&90>=g||97<=g&&122>=g){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=a.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){return a+"[]"},indices:function(a,b){return a+"["+b+"]"},repeat:function(a){return 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,indices:!1,serializeDate:function(a){return o.call(a)},skipNulls:!1,strictNullHandling:!1},q=function a(b,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",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){var c=String.fromCharCode;return c(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=")&&("utf8=%E2%9C%93"===h[c]?k="utf-8":"utf8=%26%2310003%3B"===h[c]&&(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){var d=c.allowDots?a.replace(/\.([^.[]+)/g,"[$1]"):a,e=/(\[[^[\]]*])/,f=/(\[[^[\]]*])/g,g=e.exec(d),h=g?d.slice(0,g.index):d,j=[];if(h){if(!c.plainObjects&&s.call(Object.prototype,h)&&!c.allowPrototypes)return;j.push(h)}for(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])}return 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(a[0].match(/^[^/:]+:\/*$/)&&1<a.length){var c=a.shift();a[0]=c+a[0]}a[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("/");f=f.replace(/\/(\?|&|#[^!])/g,"$1");var 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(!a&&!b)return c.signal;let e=new AbortController;if(a){const b=d.get(a)||{controller:e,count:0};b.count++,d.set(a,b),e=b.controller}return b&&(b.aborted?e.abort():b.addEventListener("abort",()=>e.abort())),c.signal.addEventListener("abort",()=>e.abort()),e.signal},D=a=>new Promise(b=>setTimeout(()=>b(),a)),E=(a,b)=>{if("function"!=typeof a)throw new Error(b)},F=async(a,{retries:c=0,retryDelay:d=1e3,retryFn:b})=>{var e=Number.isFinite,f=Number.isInteger;if(E(a,"retry function is not a function"),"number"!=typeof c||!f(c)||0>c)throw new Error("retries must be a number greater than or equal to 0");if(d&&("number"!=typeof d||!e(d)))throw new Error("retryDelay must be a number (milliseconds)");if(b&&"function"!=typeof b)throw new Error("retryFn must be callable");const g=async(...a)=>b?b(...a):D(d);let h=-1;const i=async b=>{try{return h++,await a(h)}catch(a){if(!b)throw a;return await g(h+1,a),i(b-1)}},j=await i(c);return{count:h,res:j}},G=async(a,{retries:b,retryDelay:c,retryFn:d,retryOn:f=[],signal:e})=>{if(E(a,"retry function is not a function"),f&&!Array.isArray(f))throw new Error("retryOn must be an array of response statii");if(null!=e&&"boolean"!=typeof e.aborted)throw new Error("signal must have boolean \"aborted\" property");return F(async c=>{const d=await a();if(-1===f.indexOf(d.status)||b===c)return d;throw new Error(d)},{retries:b,retryFn:async(a,b)=>{if(e&&e.aborted)throw b;return d?d(a,b):D(c)}})};if("undefined"==typeof fetch)throw new Error("You're missing a fetch implementation. Try var Flighty = require('flighty/fetch') or import Flighty from 'flighty/fetch'");if("undefined"==typeof AbortController)throw new Error("You're missing an AbortController implementation. Try var Flighty = require('flighty/abort') or import Flighty from 'flighty/abort'");const H=a=>Object.keys(a).some(b=>{const c=a[b];return"content-type"===b.toLowerCase()&&"application/json"===c.toLowerCase().split(";")[0]}),I=["GET","POST","PUT","HEAD","OPTIONS","DEL","PATCH"],J=(a,b,c,d)=>{const e={...d,method:"del"===a?"DELETE":a.toUpperCase(),headers:{...(b.headers||{}),...d.headers}};e.headers=Object.keys(e.headers).reduce((a,b)=>{const c=e.headers[b];return c?{[b]:c,...a}:a},{}),e.body||"POST"!==a||(e.body="");let f=c;"GET"===a&&e.body&&(f+=`?${y.stringify(e.body,{arrayFormat:b.arrayFormat})}`,delete e.body),H(e.headers)&&e.body&&(e.body=JSON.stringify(e.body));const g=b.baseURI?A(b.baseURI,f):f;return fetch(g,e)},K=async(a,b,{path:c,options:d},e,f=0)=>{const{retries:k=0,retryDelay:l=1e3,retryOn:m=[],retryFn:g,abortToken:h,signal:i,...j}=d,n=C({abortToken:h,signal:i},b.abortController,b.abortTokenMap);let o=f;const p={method:a,call:{path:c,options:{...d},extra:{...e}},retry:()=>(o+=1,K(a,b,{path:c,options:{...d}},{...e},o))},q=Array.from(b.interceptors),r=q.reduce((a,b)=>{let c=a;return c=c.then(a=>a.slice(0,2).concat([{...e},f])),b.request&&(c=c.then(a=>b.request(...a))),b.requestError&&(c=c.catch(b.requestError)),c},Promise.resolve([c,j])),s=q.reverse().reduce((a,b)=>{let c=a;return b.response&&(c=c.then(b.response)),b.responseError&&(c=c.catch(b.responseError)),c},(async()=>{const[c,d]=await r,{count:e,res:f}=await G(()=>J(a,b,c,{...d,signal:n}),{retries:k,retryDelay:l,retryOn:m,retryFn:g,signal:n});o+=e,f.flighty=p;let h,i;try{h=await f.clone().json()}catch(a){}try{i=await f.clone().text()}catch(a){}return f.flighty={...p,retryCount:o,json:h,text:i},f})());try{return await s}catch(a){throw a}finally{B(h,b.abortTokenMap)}};class L{constructor(a={}){let b={...a};I.forEach(a=>{this[a.toLowerCase()]=(b="/",c={},d={})=>K(a,this,{path:b,options:c},d)});let c;const d=new Set,e=new Map;Object.defineProperties(this,{headers:{get(){return b.headers},set(a={}){b={...b,headers:a}}},arrayFormat:{get(){return b.arrayFormat||"indicies"}},baseURI:{get(){return b.baseURI},set(a){b={...b,baseURI:a}}},interceptors:{get(){return d}},interceptor:{get(){return{register:a=>this.registerInterceptor(a),unregister:a=>this.removeInterceptor(a),clear:()=>this.clearInterceptors()}}},abortController:{get(){return c||(c=new AbortController,c.signal.addEventListener("abort",()=>{c=null})),c}},abortTokenMap:{get(){return e}}})}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)}auth(a,b){this.headers={...this.headers,Authorization:a&&b?`Basic ${(a=>"undefined"==typeof btoa?Buffer.from(a).toString("base64"):btoa(a))(`${a}:${b}`)}`:null}}jwt(a){return this.headers={...this.headers,Authorization:a?`Bearer ${a}`:null},this}}const M=new L;return M.create=a=>new L(a),M}();