UNPKG

@swan-io/request

Version:

Wrapper for XMLHttpRequest with better data-structures

3 lines (2 loc) 4.8 kB
var t=require("@swan-io/boxed");function r(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(r=function(){return!!t})()}function e(t,r){t.prototype=Object.create(r.prototype),t.prototype.constructor=t,o(t,r)}function n(t){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},n(t)}function o(t,r){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,r){return t.__proto__=r,t},o(t,r)}function u(t){var e="function"==typeof Map?new Map:void 0;return u=function(t){if(null===t||!function(t){try{return-1!==Function.toString.call(t).indexOf("[native code]")}catch(r){return"function"==typeof t}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,u)}function u(){return function(t,e,n){if(r())return Reflect.construct.apply(null,arguments);var u=[null];u.push.apply(u,e);var i=new(t.bind.apply(t,u));return n&&o(i,n.prototype),i}(t,arguments,n(this).constructor)}return u.prototype=Object.create(t.prototype,{constructor:{value:u,enumerable:!1,writable:!0,configurable:!0}}),o(u,t)},u(t)}function i(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var c=/*#__PURE__*/function(t){function r(e){var n;return(n=t.call(this,"Request to "+e+" failed")||this).url=void 0,Object.setPrototypeOf(i(n),r.prototype),n.name="NetworkError",n.url=e,n}return e(r,t),r}(/*#__PURE__*/u(Error)),s=/*#__PURE__*/function(t){function r(e,n){var o;return null==n?((o=t.call(this,"Request to "+e+" timed out")||this).url=void 0,o.timeout=void 0):((o=t.call(this,"Request to "+e+" timed out (> "+n+"ms)")||this).url=void 0,o.timeout=void 0),Object.setPrototypeOf(i(o),r.prototype),o.name="TimeoutError",o.url=e,o.timeout=n,i(o)}return e(r,t),r}(/*#__PURE__*/u(Error)),a=/*#__PURE__*/function(t){function r(){var e;return e=t.call(this)||this,Object.setPrototypeOf(i(e),r.prototype),e.name="CanceledError",e}return e(r,t),r}(/*#__PURE__*/u(Error)),f=Promise.resolve(),l=/*#__PURE__*/function(t){function r(e,n,o){var u;return(u=t.call(this,"Request to "+e+" gave status "+n)||this).url=void 0,u.status=void 0,u.response=void 0,Object.setPrototypeOf(i(u),r.prototype),u.name="BadStatusError",u.url=e,u.status=n,u.response=o,u}return e(r,t),r}(/*#__PURE__*/u(Error)),p=/*#__PURE__*/function(t){function r(e){var n;return(n=t.call(this,"Request to "+e+" gave an empty response")||this).url=void 0,Object.setPrototypeOf(i(n),r.prototype),n.name="EmptyResponseError",n.url=e,n}return e(r,t),r}(/*#__PURE__*/u(Error)),h={make:function(r){var e=r.url,n=r.method,o=r.type,u=r.body,i=r.headers,l=r.credentials,p=r.timeout,h=r.cache,v=r.integrity,y=r.keepalive,d=r.mode,m=r.priority,O=r.redirect,b=r.referrer,E=r.referrerPolicy,w=r.window;return t.Future.make(function(r){var P=new AbortController;return p&&setTimeout(function(){P.abort(new s(e,p))},p),function(){try{return Promise.resolve(fetch(e,{method:n,credentials:l,headers:i,signal:P.signal,body:u,cache:h,integrity:v,keepalive:y,mode:d,priority:m,redirect:O,referrer:b,referrerPolicy:E,window:w})).then(function(r){function n(){return{url:e,status:r.status,ok:r.ok,headers:r.headers,response:u}}var u,i=function(e,n){try{var i=function(){function e(){function e(){function e(){var e=function(){if("text"===o){var e=t.Option.Some;return Promise.resolve(r.text()).then(function(r){u=e.call(t.Option,r)})}}();if(e&&e.then)return e.then(function(){})}var n=function(){if("json"===o){var e=t.Option.Some;return Promise.resolve(r.json()).then(function(r){u=e.call(t.Option,r)})}}();return n&&n.then?n.then(e):e()}var n=function(){if("blob"===o){var e=t.Option.Some;return Promise.resolve(r.blob()).then(function(r){u=e.call(t.Option,r)})}}();return n&&n.then?n.then(e):e()}var n=function(){if("arraybuffer"===o){var e=t.Option.Some;return Promise.resolve(r.arrayBuffer()).then(function(r){u=e.call(t.Option,r)})}}();return n&&n.then?n.then(e):e()}()}catch(t){return n()}return i&&i.then?i.then(void 0,n):i}(0,function(){u=t.Option.None()});return i&&i.then?i.then(n):n()})}catch(t){return Promise.reject(t)}}().then(function(e){return r(t.Result.Ok(e))},function(n){return n instanceof a?f:n instanceof s?(r(t.Result.Error(n)),f):(r(t.Result.Error(new c(e))),f)}),function(){P.abort(new a)}})}};exports.BadStatusError=l,exports.CanceledError=a,exports.EmptyResponseError=p,exports.NetworkError=c,exports.Request=h,exports.TimeoutError=s,exports.badStatusToError=function(r){return r.ok?t.Result.Ok(r):t.Result.Error(new l(r.url,r.status,r.response.toUndefined()))},exports.emptyToError=function(t){return t.response.toResult(new p(t.url))}; //# sourceMappingURL=Request.js.map