@swan-io/request
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -19,5 +19,10 @@ import { Future, Option, Result } from "@swan-io/boxed"; | ||
type Method = "GET" | "POST" | "OPTIONS" | "PATCH" | "PUT" | "DELETE"; | ||
declare class NetworkError extends Error { | ||
export declare class NetworkError extends Error { | ||
url: string; | ||
constructor(url: string); | ||
} | ||
declare class TimeoutError extends Error { | ||
export declare class TimeoutError extends Error { | ||
url: string; | ||
timeout: number | undefined; | ||
constructor(url: string, timeout?: number); | ||
} | ||
@@ -36,2 +41,3 @@ type Config<T extends ResponseType> = { | ||
export type Response<T> = { | ||
url: string; | ||
status: number; | ||
@@ -42,6 +48,12 @@ ok: boolean; | ||
}; | ||
declare class BadStatusError extends Error { | ||
export declare class BadStatusError extends Error { | ||
url: string; | ||
status: number; | ||
response: unknown; | ||
constructor(url: string, status: number, response?: unknown); | ||
} | ||
export declare const badStatusToError: <T>(response: Response<T>) => Result<Response<T>, BadStatusError>; | ||
declare class EmptyResponseError extends Error { | ||
export declare class EmptyResponseError extends Error { | ||
url: string; | ||
constructor(url: string); | ||
} | ||
@@ -48,0 +60,0 @@ export declare const emptyToError: <T>(response: Response<T>) => Result<T, EmptyResponseError>; |
@@ -1,2 +0,2 @@ | ||
var t=require("@swan-io/boxed");function e(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(e=function(){return!!t})()}function r(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,o(t,e)}function n(t){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},n(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function u(t){var r="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(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!==r){if(r.has(t))return r.get(t);r.set(t,u)}function u(){return function(t,r,n){if(e())return Reflect.construct.apply(null,arguments);var u=[null];u.push.apply(u,r);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)}var i=/*#__PURE__*/function(t){function e(){return t.apply(this,arguments)||this}return r(e,t),e}(/*#__PURE__*/u(Error)),s=/*#__PURE__*/function(t){function e(){return t.apply(this,arguments)||this}return r(e,t),e}(/*#__PURE__*/u(Error)),c=/*#__PURE__*/function(t){function e(){return t.apply(this,arguments)||this}return r(e,t),e}(/*#__PURE__*/u(Error)),a=/*#__PURE__*/function(t){function e(){return t.apply(this,arguments)||this}return r(e,t),e}(/*#__PURE__*/u(Error));exports.Request={make:function(e){var r=e.url,n=e.method,o=void 0===n?"GET":n,u=e.responseType,c=e.body,a=e.headers,p=e.withCredentials,l=void 0!==p&&p,f=e.onLoadStart,d=e.onProgress,v=e.timeout;return t.Future.make(function(e){var n=new XMLHttpRequest;n.withCredentials=l,n.open(o,r,!0),null!=u&&(n.responseType=u),null!=v&&(n.timeout=v),null!=a&&t.Dict.entries(a).forEach(function(t){return n.setRequestHeader(t[0],t[1])});var p=function(){h(),e(t.Result.Error(new i))},y=function(){h(),e(t.Result.Error(new s))},E=function(){h();var r=n.status,o=t.Option.fromNullable(n.response);e(t.Result.Ok({status:r,ok:r>=200&&r<300,response:o,xhr:n}))},h=function(){n.removeEventListener("error",p),n.removeEventListener("load",E),n.removeEventListener("timeout",y),null!=f&&n.removeEventListener("loadstart",f),null!=d&&n.removeEventListener("progress",d)};return n.addEventListener("error",p),n.addEventListener("load",E),n.addEventListener("timeout",y),null!=f&&n.addEventListener("loadstart",f),null!=d&&n.addEventListener("progress",d),n.send(c),function(){h(),n.abort()}})}},exports.badStatusToError=function(e){return e.ok?t.Result.Ok(e):t.Result.Error(new c("Received HTTP status "+e.status))},exports.emptyToError=function(t){return t.response.toResult(new a)}; | ||
var t=require("@swan-io/boxed");function e(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(e=function(){return!!t})()}function r(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,o(t,e)}function n(t){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},n(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function u(t){var r="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(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!==r){if(r.has(t))return r.get(t);r.set(t,u)}function u(){return function(t,r,n){if(e())return Reflect.construct.apply(null,arguments);var u=[null];u.push.apply(u,r);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)}var i=/*#__PURE__*/function(t){function e(e){var r;return(r=t.call(this,"Request to "+e+" failed")||this).url=void 0,r.url=e,r}return r(e,t),e}(/*#__PURE__*/u(Error)),s=/*#__PURE__*/function(t){function e(e,r){var n;return null==r?((n=t.call(this,"Request to "+e+" timed out")||this).url=void 0,n.timeout=void 0):((n=t.call(this,"Request to "+e+" timed out (> "+r+"ms)")||this).url=void 0,n.timeout=void 0),n.url=e,n.timeout=r,function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(n)}return r(e,t),e}(/*#__PURE__*/u(Error)),l=/*#__PURE__*/function(t){function e(e,r,n){var o;return(o=t.call(this,"Request to "+e+" gave status "+r)||this).url=void 0,o.status=void 0,o.response=void 0,o.url=e,o.status=r,o.response=n,o}return r(e,t),e}(/*#__PURE__*/u(Error)),a=/*#__PURE__*/function(t){function e(e){var r;return(r=t.call(this,"Request to "+e+" gave an empty response")||this).url=void 0,r.url=e,r}return r(e,t),e}(/*#__PURE__*/u(Error)),c={make:function(e){var r=e.url,n=e.method,o=void 0===n?"GET":n,u=e.responseType,l=e.body,a=e.headers,c=e.withCredentials,p=void 0!==c&&c,f=e.onLoadStart,d=e.onProgress,v=e.timeout;return t.Future.make(function(e){var n=new XMLHttpRequest;n.withCredentials=p,n.open(o,r,!0),null!=u&&(n.responseType=u),null!=v&&(n.timeout=v),null!=a&&t.Dict.entries(a).forEach(function(t){return n.setRequestHeader(t[0],t[1])});var c=function(){y(),e(t.Result.Error(new i(r)))},E=function(){y(),e(t.Result.Error(new s(r,v)))},h=function(){y();var o=n.status,u=t.Option.fromNullable(n.response);e(t.Result.Ok({url:r,status:o,ok:o>=200&&o<300,response:u,xhr:n}))},y=function(){n.removeEventListener("error",c),n.removeEventListener("load",h),n.removeEventListener("timeout",E),null!=f&&n.removeEventListener("loadstart",f),null!=d&&n.removeEventListener("progress",d)};return n.addEventListener("error",c),n.addEventListener("load",h),n.addEventListener("timeout",E),null!=f&&n.addEventListener("loadstart",f),null!=d&&n.addEventListener("progress",d),n.send(l),function(){y(),n.abort()}})}};exports.BadStatusError=l,exports.EmptyResponseError=a,exports.NetworkError=i,exports.Request=c,exports.TimeoutError=s,exports.badStatusToError=function(e){return e.ok?t.Result.Ok(e):t.Result.Error(new l(e.url,e.status,e.response.toUndefined()))},exports.emptyToError=function(t){return t.response.toResult(new a(t.url))}; | ||
//# sourceMappingURL=Request.js.map |
@@ -0,1 +1,7 @@ | ||
# 1.0.2 | ||
## Changes | ||
- Export error types | ||
# 1.0.1 | ||
@@ -2,0 +8,0 @@ |
{ | ||
"name": "@swan-io/request", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Wrapper for XMLHttpRequest with better data-structures", | ||
@@ -5,0 +5,0 @@ "main": "dist/Request.js", |
@@ -38,3 +38,3 @@ <img width="108" alt="@swan-io/request logo" src="https://github.com/swan-io/request/blob/main/logo.svg?raw=true"> | ||
// Timeout | ||
Request.make({ url: "/api/health", timeout: 50 }).onResolve(console.log); | ||
Request.make({ url: "/api/health", timeout: 2000 }).onResolve(console.log); | ||
// Result.Error(TimeoutError) | ||
@@ -41,0 +41,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32913
91