UNPKG

@swan-io/request

Version:

Wrapper for XMLHttpRequest with better data-structures

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