diff --git a/README.md b/README.md index 048cf67..a06d696 100644 --- a/README.md +++ b/README.md @@ -706,7 +706,7 @@ wretch("...").opts() *Catchers can be chained.* -| [badRequest](#badrequestcb-error-wretchererror-originalrequest-wretcher--any) | [unauthorized](#unauthorizedcb-error-wretchererror-originalrequest-wretcher--any) | [forbidden](#forbiddencb-error-wretchererror-originalrequest-wretcher--any) | [notFound](#notfoundcb-error-wretchererror-originalrequest-wretcher--any) | [timeout](#timeoutcb-error-wretchererror-originalrequest-wretcher--any) | [internalError](#internalerrorcb-error-wretchererror-originalrequest-wretcher--any) | [error](#errorerrorid-number--string-cb-error-wretchererror-originalrequest-wretcher--any) | +| [badRequest](#badrequestcb-error-wretchererror-originalrequest-wretcher--any) | [unauthorized](#unauthorizedcb-error-wretchererror-originalrequest-wretcher--any) | [forbidden](#forbiddencb-error-wretchererror-originalrequest-wretcher--any) | [notFound](#notfoundcb-error-wretchererror-originalrequest-wretcher--any) | [timeout](#timeoutcb-error-wretchererror-originalrequest-wretcher--any) | [internalError](#internalerrorcb-error-wretchererror-originalrequest-wretcher--any) | [error](#errorerrorid-number--string-cb-error-wretchererror-originalrequest-wretcher--any) | [fetchError](#fetcherrorcb-error-networkerror-originalrequest-wretcher--any) |-----|-----|-----|-----|-----|-----|-----| ```ts @@ -723,6 +723,7 @@ wretch("...") .timeout(err => console.log(err.status)) .internalError(err => console.log(err.status)) .error(418, err => console.log(err.status)) + .fetchError(err => console.log(err)) .res() ``` @@ -754,6 +755,10 @@ Syntactic sugar for `error(500, cb)`. Catches a specific error given its code or name and perform the callback. +#### fetchError(cb: (error: NetworkError, originalRequest: Wretcher) => any) + +Catches any error thrown by the fetch function and perform the callback. + --------- The original request is passed along the error and can be used in order to perform an additional request. diff --git a/dist/bundle/wretch.esm.js b/dist/bundle/wretch.esm.js index f6c15af..7a47584 100644 --- a/dist/bundle/wretch.esm.js +++ b/dist/bundle/wretch.esm.js @@ -1,2 +1,2 @@ -var r=function(){return(r=Object.assign||function(r){for(var t,e=1,n=arguments.length;e0)&&(e(o.reverse()[0]),n.clearMeasures&&n.clearMeasures(t),i.callbacks.delete(t),i.callbacks.size<1&&(i.observer.disconnect(),n.clearResourceTimings&&n.clearResourceTimings()),!0)},i={callbacks:new Map,observer:null,observe:function(r,t){if(r&&t){var e=n.polyfill("performance",{doThrow:!1});(function(r,t){return!i.observer&&r&&t&&(i.observer=new t((function(t){i.callbacks.forEach((function(e,n){o(t,n,e,r)}))})),r.clearResourceTimings&&r.clearResourceTimings()),i.observer})(e,n.polyfill("PerformanceObserver",{doThrow:!1}))&&(o(e,r,t,e)||(i.callbacks.size<1&&i.observer.observe({entryTypes:["resource","measure"]}),i.callbacks.set(r,t)))}}},s=function(){function o(r,t,e,n,o,i){void 0===e&&(e=new Map),void 0===n&&(n=[]),void 0===o&&(o=[]),void 0===i&&(i=[]),this._url=r,this._options=t,this._catchers=e,this._resolvers=n,this._middlewares=o,this._deferredChain=i}return o.factory=function(r,t){return void 0===r&&(r=""),void 0===t&&(t={}),new o(r,t)},o.prototype.selfFactory=function(e){var n=void 0===e?{}:e,i=n.url,s=void 0===i?this._url:i,u=n.options,a=void 0===u?this._options:u,f=n.catchers,l=void 0===f?this._catchers:f,c=n.resolvers,p=void 0===c?this._resolvers:c,h=n.middlewares,d=void 0===h?this._middlewares:h,y=n.deferredChain,v=void 0===y?this._deferredChain:y;return new o(s,r({},a),new Map(l),t(p),t(d),t(v))},o.prototype.defaults=function(r,t){return void 0===t&&(t=!1),n.defaults=t?e(n.defaults,r):r,this},o.prototype.errorType=function(r){return n.errorType=r,this},o.prototype.polyfills=function(t){return n.polyfills=r(r({},n.polyfills),t),this},o.prototype.url=function(r,t){if(void 0===t&&(t=!1),t)return this.selfFactory({url:r});var e=this._url.split("?");return this.selfFactory({url:e.length>1?e[0]+r+"?"+e[1]:this._url+r})},o.prototype.options=function(r,t){return void 0===t&&(t=!0),this.selfFactory({options:t?e(this._options,r):r})},o.prototype.query=function(r,t){return void 0===t&&(t=!1),this.selfFactory({url:u(this._url,r,t)})},o.prototype.headers=function(r){return this.selfFactory({options:e(this._options,{headers:r||{}})})},o.prototype.accept=function(r){return this.headers({Accept:r})},o.prototype.content=function(r){return this.headers({"Content-Type":r})},o.prototype.auth=function(r){return this.headers({Authorization:r})},o.prototype.catcher=function(r,t){var e=new Map(this._catchers);return e.set(r,t),this.selfFactory({catchers:e})},o.prototype.signal=function(t){return this.selfFactory({options:r(r({},this._options),{signal:t.signal})})},o.prototype.resolve=function(r,e){return void 0===e&&(e=!1),this.selfFactory({resolvers:e?[r]:t(this._resolvers,[r])})},o.prototype.defer=function(r,e){return void 0===e&&(e=!1),this.selfFactory({deferredChain:e?[r]:t(this._deferredChain,[r])})},o.prototype.middlewares=function(r,e){return void 0===e&&(e=!1),this.selfFactory({middlewares:e?r:t(this._middlewares,r)})},o.prototype.method=function(t,o,s){void 0===o&&(o={}),void 0===s&&(s=null);var u=s?"object"==typeof s?this.json(s):this.body(s):this;return function(r){var t=r._url,o=r._catchers,s=r._resolvers,u=r._middlewares,a=r._options,f=new Map(o),l=e(n.defaults,a),c=n.polyfill("AbortController",{doThrow:!1,instance:!0});!l.signal&&c&&(l.signal=c.signal);var p={ref:null,clear:function(){p.ref&&(clearTimeout(p.ref),p.ref=null)}},h=function(r){return function(t){return 0===r.length?t:1===r.length?r[0](t):r.reduceRight((function(e,n,o){return o===r.length-2?n(e(t)):n(e)}))}}(u)(n.polyfill("fetch"))(t,l),d=h.then((function(r){return p.clear(),r.ok?r:r[n.errorType||"text"]().then((function(t){var e=new Error(t);throw e[n.errorType||"text"]=t,e.status=r.status,e.response=r,e}))})),y=function(t){return t.catch((function(t){if(p.clear(),f.has(t.status))return f.get(t.status)(t,r);if(f.has(t.name))return f.get(t.name)(t,r);throw t}))},v=function(r){return function(t){return y(r?d.then((function(t){return t&&t[r]()})).then((function(r){return t?t(r):r})):d.then((function(r){return t?t(r):r})))}},m={res:v(null),json:v("json"),blob:v("blob"),formData:v("formData"),arrayBuffer:v("arrayBuffer"),text:v("text"),perfs:function(r){return h.then((function(t){return i.observe(t.url,r)})),m},setTimeout:function(r,t){return void 0===t&&(t=c),p.clear(),p.ref=setTimeout((function(){return t.abort()}),r),m},controller:function(){return[c,m]},error:function(r,t){return f.set(r,t),m},badRequest:function(r){return m.error(400,r)},unauthorized:function(r){return m.error(401,r)},forbidden:function(r){return m.error(403,r)},notFound:function(r){return m.error(404,r)},timeout:function(r){return m.error(408,r)},internalError:function(r){return m.error(500,r)},onAbort:function(r){return m.error("AbortError",r)}};return s.reduce((function(t,e){return e(t,r)}),m)}((u=u.options(r(r({},o),{method:t})))._deferredChain.reduce((function(r,t){return t(r,r._url,r._options)}),u))},o.prototype.get=function(r){return this.method("GET",r)},o.prototype.delete=function(r){return this.method("DELETE",r)},o.prototype.put=function(r,t){return this.method("PUT",t,r)},o.prototype.post=function(r,t){return this.method("POST",t,r)},o.prototype.patch=function(r,t){return this.method("PATCH",t,r)},o.prototype.head=function(r){return this.method("HEAD",r)},o.prototype.opts=function(r){return this.method("OPTIONS",r)},o.prototype.replay=function(r){return this.method(this._options.method,r)},o.prototype.body=function(t){return this.selfFactory({options:r(r({},this._options),{body:t})})},o.prototype.json=function(r){return this.content("application/json").body(JSON.stringify(r))},o.prototype.formData=function(r){return this.body(function(r){var t=n.polyfill("FormData",{instance:!0});for(var e in r)if(r[e]instanceof Array)for(var o=0,i=r[e];o0)&&(e(o.reverse()[0]),n.clearMeasures&&n.clearMeasures(t),i.callbacks.delete(t),i.callbacks.size<1&&(i.observer.disconnect(),n.clearResourceTimings&&n.clearResourceTimings()),!0)},i={callbacks:new Map,observer:null,observe:function(r,t){if(r&&t){var e=n.polyfill("performance",{doThrow:!1});(function(r,t){return!i.observer&&r&&t&&(i.observer=new t((function(t){i.callbacks.forEach((function(e,n){o(t,n,e,r)}))})),r.clearResourceTimings&&r.clearResourceTimings()),i.observer})(e,n.polyfill("PerformanceObserver",{doThrow:!1}))&&(o(e,r,t,e)||(i.callbacks.size<1&&i.observer.observe({entryTypes:["resource","measure"]}),i.callbacks.set(r,t)))}}},s=function(r){this.error=r},u=function(){function o(r,t,e,n,o,i){void 0===e&&(e=new Map),void 0===n&&(n=[]),void 0===o&&(o=[]),void 0===i&&(i=[]),this._url=r,this._options=t,this._catchers=e,this._resolvers=n,this._middlewares=o,this._deferredChain=i}return o.factory=function(r,t){return void 0===r&&(r=""),void 0===t&&(t={}),new o(r,t)},o.prototype.selfFactory=function(e){var n=void 0===e?{}:e,i=n.url,s=void 0===i?this._url:i,u=n.options,a=void 0===u?this._options:u,c=n.catchers,f=void 0===c?this._catchers:c,l=n.resolvers,p=void 0===l?this._resolvers:l,h=n.middlewares,d=void 0===h?this._middlewares:h,y=n.deferredChain,v=void 0===y?this._deferredChain:y;return new o(s,r({},a),new Map(f),t(p),t(d),t(v))},o.prototype.defaults=function(r,t){return void 0===t&&(t=!1),n.defaults=t?e(n.defaults,r):r,this},o.prototype.errorType=function(r){return n.errorType=r,this},o.prototype.polyfills=function(t){return n.polyfills=r(r({},n.polyfills),t),this},o.prototype.url=function(r,t){if(void 0===t&&(t=!1),t)return this.selfFactory({url:r});var e=this._url.split("?");return this.selfFactory({url:e.length>1?e[0]+r+"?"+e[1]:this._url+r})},o.prototype.options=function(r,t){return void 0===t&&(t=!0),this.selfFactory({options:t?e(this._options,r):r})},o.prototype.query=function(r,t){return void 0===t&&(t=!1),this.selfFactory({url:a(this._url,r,t)})},o.prototype.headers=function(r){return this.selfFactory({options:e(this._options,{headers:r||{}})})},o.prototype.accept=function(r){return this.headers({Accept:r})},o.prototype.content=function(r){return this.headers({"Content-Type":r})},o.prototype.auth=function(r){return this.headers({Authorization:r})},o.prototype.catcher=function(r,t){var e=new Map(this._catchers);return e.set(r,t),this.selfFactory({catchers:e})},o.prototype.signal=function(t){return this.selfFactory({options:r(r({},this._options),{signal:t.signal})})},o.prototype.resolve=function(r,e){return void 0===e&&(e=!1),this.selfFactory({resolvers:e?[r]:t(this._resolvers,[r])})},o.prototype.defer=function(r,e){return void 0===e&&(e=!1),this.selfFactory({deferredChain:e?[r]:t(this._deferredChain,[r])})},o.prototype.middlewares=function(r,e){return void 0===e&&(e=!1),this.selfFactory({middlewares:e?r:t(this._middlewares,r)})},o.prototype.method=function(t,o,u){void 0===o&&(o={}),void 0===u&&(u=null);var a=u?"object"==typeof u?this.json(u):this.body(u):this;return function(r){var t=r._url,o=r._catchers,u=r._resolvers,a=r._middlewares,c=r._options,f=new Map(o),l=e(n.defaults,c),p=n.polyfill("AbortController",{doThrow:!1,instance:!0});!l.signal&&p&&(l.signal=p.signal);var h={ref:null,clear:function(){h.ref&&(clearTimeout(h.ref),h.ref=null)}},d=function(r){return function(t){return 0===r.length?t:1===r.length?r[0](t):r.reduceRight((function(e,n,o){return o===r.length-2?n(e(t)):n(e)}))}}(a)(n.polyfill("fetch"))(t,l),y=d.catch((function(r){throw new s(r)})).then((function(r){return h.clear(),r.ok?r:r[n.errorType||"text"]().then((function(t){var e=new Error(t);throw e[n.errorType||"text"]=t,e.status=r.status,e.response=r,e}))})),v=function(t){return t.catch((function(t){h.clear();var e=t instanceof s?t.error:t;if(t instanceof s&&f.has("__fromFetch"))return f.get("__fromFetch")(e,r);if(f.has(e.status))return f.get(e.status)(e,r);if(f.has(e.name))return f.get(e.name)(e,r);throw e}))},m=function(r){return function(t){return v(r?y.then((function(t){return t&&t[r]()})).then((function(r){return t?t(r):r})):y.then((function(r){return t?t(r):r})))}},b={res:m(null),json:m("json"),blob:m("blob"),formData:m("formData"),arrayBuffer:m("arrayBuffer"),text:m("text"),perfs:function(r){return d.then((function(t){return i.observe(t.url,r)})),b},setTimeout:function(r,t){return void 0===t&&(t=p),h.clear(),h.ref=setTimeout((function(){return t.abort()}),r),b},controller:function(){return[p,b]},error:function(r,t){return f.set(r,t),b},badRequest:function(r){return b.error(400,r)},unauthorized:function(r){return b.error(401,r)},forbidden:function(r){return b.error(403,r)},notFound:function(r){return b.error(404,r)},timeout:function(r){return b.error(408,r)},internalError:function(r){return b.error(500,r)},fetchError:function(r){return b.error("__fromFetch",r)},onAbort:function(r){return b.error("AbortError",r)}};return u.reduce((function(t,e){return e(t,r)}),b)}((a=a.options(r(r({},o),{method:t})))._deferredChain.reduce((function(r,t){return t(r,r._url,r._options)}),a))},o.prototype.get=function(r){return this.method("GET",r)},o.prototype.delete=function(r){return this.method("DELETE",r)},o.prototype.put=function(r,t){return this.method("PUT",t,r)},o.prototype.post=function(r,t){return this.method("POST",t,r)},o.prototype.patch=function(r,t){return this.method("PATCH",t,r)},o.prototype.head=function(r){return this.method("HEAD",r)},o.prototype.opts=function(r){return this.method("OPTIONS",r)},o.prototype.replay=function(r){return this.method(this._options.method,r)},o.prototype.body=function(t){return this.selfFactory({options:r(r({},this._options),{body:t})})},o.prototype.json=function(r){return this.content("application/json").body(JSON.stringify(r))},o.prototype.formData=function(r){return this.body(function(r){var t=n.polyfill("FormData",{instance:!0});for(var e in r)if(r[e]instanceof Array)for(var o=0,i=r[e];o {\n if(!entries.getEntriesByName)\n return false\n const matches = entries.getEntriesByName(name)\n if(matches && matches.length > 0) {\n callback(matches.reverse()[0])\n if(_performance.clearMeasures)\n _performance.clearMeasures(name)\n perfs.callbacks.delete(name)\n\n if(perfs.callbacks.size < 1) {\n perfs.observer.disconnect()\n if(_performance.clearResourceTimings) {\n _performance.clearResourceTimings()\n }\n }\n return true\n }\n return false\n}\n\nconst lazyObserver = (_performance, _observer) => {\n if(!perfs.observer && _performance && _observer) {\n perfs.observer = new _observer(entries => {\n perfs.callbacks.forEach((callback, name) => {\n onMatch(entries, name, callback, _performance)\n })\n })\n if(_performance.clearResourceTimings)\n _performance.clearResourceTimings()\n }\n return perfs.observer\n}\n\nconst perfs = {\n callbacks: new Map(),\n observer: null,\n observe: (name, callback) => {\n if(!name || !callback)\n return\n\n const _performance = conf.polyfill(\"performance\", { doThrow: false })\n const _observer = conf.polyfill(\"PerformanceObserver\", { doThrow: false })\n\n if(!lazyObserver(_performance, _observer))\n return\n\n if(!onMatch(_performance, name, callback, _performance)) {\n if(perfs.callbacks.size < 1)\n perfs.observer.observe({ entryTypes: [\"resource\", \"measure\"] })\n perfs.callbacks.set(name, callback)\n }\n\n }\n}\n\nexport default perfs\n","import { mix } from \"./mix\"\nimport conf from \"./config\"\nimport { resolver, WretcherError, ResponseChain } from \"./resolver\"\nimport { ConfiguredMiddleware } from \"./middleware\"\n\nexport type WretcherOptions = RequestInit & {\n [key: string]: any\n}\n\nexport type DeferredCallback = (wretcher: Wretcher, url: string, options: WretcherOptions) => Wretcher\n\n/**\n * The Wretcher class used to perform easy fetch requests.\n *\n * Immutability : almost every method of this class return a fresh Wretcher object.\n */\nexport class Wretcher {\n\n protected constructor(\n public _url: string,\n public _options: WretcherOptions,\n public _catchers: Map void> = new Map(),\n public _resolvers: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any> = [],\n public _middlewares: ConfiguredMiddleware[] = [],\n public _deferredChain: DeferredCallback[] = []) {}\n\n static factory(url = \"\", options: WretcherOptions = {}) { return new Wretcher(url, options) }\n private selfFactory({ url = this._url, options = this._options, catchers = this._catchers,\n resolvers = this._resolvers, middlewares = this._middlewares, deferredChain = this._deferredChain } = {}) {\n return new Wretcher(url, {...options}, new Map(catchers), [...resolvers], [...middlewares], [...deferredChain])\n }\n\n /**\n * Sets the default fetch options used for every subsequent fetch call.\n * @param options New default options\n * @param mixin If true, mixes in instead of replacing the existing options\n */\n defaults(options: WretcherOptions, mixin = false) {\n conf.defaults = mixin ? mix(conf.defaults, options) : options\n return this\n }\n\n /**\n * Sets the method (text, json ...) used to parse the data contained in the response body in case of an HTTP error.\n *\n * Persists for every subsequent requests.\n *\n * Default is \"text\".\n */\n errorType(method: \"text\" | \"json\") {\n conf.errorType = method\n return this\n }\n\n /**\n * Sets the non-global polyfills which will be used for every subsequent calls.\n *\n * Needed for libraries like [fetch-ponyfill](https://github.com/qubyte/fetch-ponyfill).\n *\n * @param polyfills An object containing the polyfills.\n */\n polyfills(polyfills: Partial) {\n conf.polyfills = { ...conf.polyfills, ...polyfills }\n return this\n }\n\n /**\n * Returns a new Wretcher object with the argument url appended and the same options.\n * @param url String url\n * @param replace Boolean If true, replaces the current url instead of appending\n */\n url(url: string, replace = false) {\n if(replace)\n return this.selfFactory({ url })\n const split = this._url.split(\"?\")\n return this.selfFactory({\n url: split.length > 1 ?\n split[0] + url + \"?\" + split[1] :\n this._url + url\n })\n }\n\n /**\n * Returns a new Wretcher object with the same url and new options.\n * @param options New options\n * @param mixin If true, mixes in instead of replacing the existing options\n */\n options(options: WretcherOptions, mixin = true) {\n return this.selfFactory({ options: mixin ? mix(this._options, options) : options })\n }\n\n /**\n * Converts a javascript object to query parameters,\n * then appends this query string to the current url.\n *\n * If given a string, use the string as the query verbatim.\n *\n * ```\n * let w = wretch(\"http://example.com\") // url is http://example.com\n *\n * // Chain query calls\n * w = w.query({ a: 1, b : 2 }) // url is now http://example.com?a=1&b=2\n * w = w.query(\"foo-bar-baz-woz\") // url is now http://example.com?a=1&b=2&foo-bar-baz-woz\n *\n * // Pass true as the second argument to replace existing query parameters\n * w = w.query(\"c=3&d=4\", true) // url is now http://example.com?c=3&d=4\n * ```\n *\n * @param qp An object which will be converted, or a string which will be used verbatim.\n */\n query(qp: object | string, replace: boolean = false) {\n return this.selfFactory({ url: appendQueryParams(this._url, qp, replace) })\n }\n\n /**\n * Set request headers.\n * @param headerValues An object containing header keys and values\n */\n headers(headerValues: { [headerName: string]: string }) {\n return this.selfFactory({ options: mix(this._options, { headers: headerValues || {} }) })\n }\n\n /**\n * Shortcut to set the \"Accept\" header.\n * @param headerValue Header value\n */\n accept(headerValue: string) {\n return this.headers({ Accept : headerValue })\n }\n\n /**\n * Shortcut to set the \"Content-Type\" header.\n * @param headerValue Header value\n */\n content(headerValue: string) {\n return this.headers({ \"Content-Type\" : headerValue })\n }\n\n /**\n * Shortcut to set the \"Authorization\" header.\n * @param headerValue Header value\n */\n auth(headerValue: string) {\n return this.headers({ Authorization: headerValue })\n }\n\n /**\n * Adds a default catcher which will be called on every subsequent request error when the error code matches.\n * @param errorId Error code or name\n * @param catcher: The catcher method\n */\n catcher(errorId: number | string, catcher: (error: WretcherError, originalRequest: Wretcher) => any) {\n const newMap = new Map(this._catchers)\n newMap.set(errorId, catcher)\n return this.selfFactory({ catchers: newMap })\n }\n\n /**\n * Associates a custom signal with the request.\n * @param controller : An AbortController\n */\n signal(controller: AbortController) {\n return this.selfFactory({ options: { ...this._options, signal: controller.signal }})\n }\n\n /**\n * Program a resolver to perform response chain tasks automatically.\n * @param doResolve : Resolver callback\n */\n resolve(doResolve: (chain: ResponseChain, originalRequest: Wretcher) => ResponseChain | Promise, clear: boolean = false) {\n return this.selfFactory({ resolvers: clear ? [ doResolve ] : [ ...this._resolvers, doResolve ]})\n }\n\n /**\n * Defer wretcher methods that will be chained and called just before the request is performed.\n */\n defer(callback: DeferredCallback, clear = false) {\n return this.selfFactory({\n deferredChain: clear ? [callback] : [ ...this._deferredChain, callback ]\n })\n }\n\n /**\n * Add middlewares to intercept a request before being sent.\n */\n middlewares(middlewares: ConfiguredMiddleware[], clear = false) {\n return this.selfFactory({\n middlewares: clear ? middlewares : [ ...this._middlewares, ...middlewares ]\n })\n }\n\n private method(method, options = {}, body = null) {\n let baseWretcher =\n !body ? this :\n typeof body === \"object\" ? this.json(body) :\n this.body(body)\n baseWretcher = baseWretcher.options({ ...options, method })\n const deferredWretcher = baseWretcher._deferredChain.reduce((acc: Wretcher, curr) => curr(acc, acc._url, acc._options), baseWretcher)\n return resolver(deferredWretcher)\n }\n\n /**\n * Performs a get request.\n */\n get(options?) {\n return this.method(\"GET\", options)\n }\n /**\n * Performs a delete request.\n */\n delete(options?) {\n return this.method(\"DELETE\", options)\n }\n /**\n * Performs a put request.\n */\n put(body?, options?) {\n return this.method(\"PUT\", options, body)\n }\n /**\n * Performs a post request.\n */\n post(body?, options?) {\n return this.method(\"POST\", options, body)\n }\n /**\n * Performs a patch request.\n */\n patch(body?, options?) {\n return this.method(\"PATCH\", options, body)\n }\n /**\n * Performs a head request.\n */\n head(options?) {\n return this.method(\"HEAD\", options)\n }\n /**\n * Performs an options request\n */\n opts(options?) {\n return this.method(\"OPTIONS\", options)\n }\n /**\n * Replay a request.\n */\n replay(options?) {\n return this.method(this._options.method, options)\n }\n\n /**\n * Sets the request body with any content.\n * @param contents The body contents\n */\n body(contents: any) {\n return this.selfFactory({ options: { ...this._options, body: contents }})\n }\n /**\n * Sets the content type header, stringifies an object and sets the request body.\n * @param jsObject An object which will be serialized into a JSON\n */\n json(jsObject: object) {\n return this.content(\"application/json\").body(JSON.stringify(jsObject))\n }\n /**\n * Converts the javascript object to a FormData and sets the request body.\n * @param formObject An object which will be converted to a FormData\n */\n formData(formObject: object) {\n return this.body(convertFormData(formObject))\n }\n /**\n * Converts the input to an url encoded string and sets the content-type header and body.\n * If the input argument is already a string, skips the conversion part.\n *\n * @param input An object to convert into an url encoded string or an already encoded string\n */\n formUrl(input: (object | string)) {\n return this\n .body(typeof input === \"string\" ? input : convertFormUrl(input))\n .content(\"application/x-www-form-urlencoded\")\n }\n}\n\n// Internal helpers\n\nconst appendQueryParams = (url: string, qp: object | string, replace: boolean) => {\n let queryString\n\n if(typeof qp === \"string\") {\n queryString = qp\n } else {\n const usp = conf.polyfill(\"URLSearchParams\", { instance: true })\n for(const key in qp) {\n if(qp[key] instanceof Array) {\n for(const val of qp[key])\n usp.append(key, val)\n } else {\n usp.append(key, qp[key])\n }\n }\n queryString = usp.toString()\n }\n\n const split = url.split(\"?\")\n if(replace || split.length < 2)\n return split[0] + \"?\" + queryString\n\n return url + \"&\" + queryString\n}\n\nfunction convertFormData(formObject: object) {\n const formData = conf.polyfill(\"FormData\", { instance: true })\n for(const key in formObject) {\n if(formObject[key] instanceof Array) {\n for(const item of formObject[key])\n formData.append(key + \"[]\", item)\n } else {\n formData.append(key, formObject[key])\n }\n }\n\n return formData\n}\n\nfunction encodeQueryValue(key: string, value: unknown) {\n return encodeURIComponent(key) +\n \"=\" +\n encodeURIComponent(\n typeof value === \"object\" ?\n JSON.stringify(value) :\n \"\" + value\n )\n}\nfunction convertFormUrl(formObject: object) {\n return Object.keys(formObject)\n .map(key => {\n const value = formObject[key]\n if(value instanceof Array) {\n return value.map(v => encodeQueryValue(key, v)).join(\"&\")\n }\n return encodeQueryValue(key, value)\n })\n .join(\"&\")\n}\n","import { Wretcher } from \"./wretcher\"\nimport { mix } from \"./mix\"\nimport conf from \"./config\"\nimport perfs from \"./perfs\"\nimport { middlewareHelper } from \"./middleware\"\n\nexport type WretcherError = Error & { status: number, response: WretcherResponse, text?: string, json?: any }\nexport type WretcherErrorCallback = (error: WretcherError, originalRequest: Wretcher) => any\nexport type WretcherResponse = Response & { [key: string]: any }\nexport type ResponseChain = {\n // Response types\n res: (cb?: (type: WretcherResponse) => Result) => Promise,\n json: (cb?: (type: {[key: string]: any}) => Result) => Promise,\n blob: (cb?: (type: Blob) => Result) => Promise,\n formData: (cb?: (type: FormData) => Result) => Promise,\n arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise,\n text: (cb?: (type: string) => Result) => Promise,\n // Extras\n perfs: (cb?: (timing: any) => void) => ResponseChain,\n setTimeout: (time: number, controller?: AbortController) => ResponseChain,\n controller: () => [any, ResponseChain],\n // Catchers\n error: (code: (number | string), cb: WretcherErrorCallback) => ResponseChain,\n badRequest: (cb: WretcherErrorCallback) => ResponseChain,\n unauthorized: (cb: WretcherErrorCallback) => ResponseChain,\n forbidden: (cb: WretcherErrorCallback) => ResponseChain,\n notFound: (cb: WretcherErrorCallback) => ResponseChain,\n timeout: (cb: WretcherErrorCallback) => ResponseChain,\n internalError: (cb: WretcherErrorCallback) => ResponseChain,\n onAbort: (cb: WretcherErrorCallback) => ResponseChain\n}\n\nexport const resolver = (wretcher: Wretcher) => {\n const {\n _url: url,\n _catchers: _catchers,\n _resolvers: resolvers,\n _middlewares: middlewares,\n _options: opts\n } = wretcher\n const catchers = new Map(_catchers)\n const finalOptions = mix(conf.defaults, opts)\n const fetchController = conf.polyfill(\"AbortController\", { doThrow: false, instance: true })\n if(!finalOptions[\"signal\"] && fetchController) {\n finalOptions[\"signal\"] = fetchController.signal\n }\n // Request timeout\n const timeout = {\n ref: null,\n clear() {\n if(timeout.ref) {\n clearTimeout(timeout.ref)\n timeout.ref = null\n }\n }\n }\n // The generated fetch request\n const fetchRequest = middlewareHelper(middlewares)(conf.polyfill(\"fetch\"))(url, finalOptions)\n // Throws on an http error\n const throwingPromise: Promise = fetchRequest.then(response => {\n timeout.clear()\n if (!response.ok) {\n return response[conf.errorType || \"text\"]().then(msg => {\n // Enhances the error object\n const err = new Error(msg)\n err[conf.errorType || \"text\"] = msg\n err[\"status\"] = response.status\n err[\"response\"] = response\n throw err\n })\n }\n return response\n })\n // Wraps the Promise in order to dispatch the error to a matching catcher\n const catchersWrapper = (promise: Promise): Promise => {\n return promise.catch(err => {\n timeout.clear()\n if(catchers.has(err.status))\n return catchers.get(err.status)(err, wretcher)\n else if(catchers.has(err.name))\n return catchers.get(err.name)(err, wretcher)\n else\n throw err\n })\n }\n // Enforces the proper promise type when a body parsing method is called.\n type BodyParser = (funName: string | null) => (cb?: (type: Type) => Result) => Promise\n const bodyParser: BodyParser = (funName) => (cb) => funName ?\n // If a callback is provided, then callback with the body result otherwise return the parsed body itself.\n catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) :\n // No body parsing method - return the response\n catchersWrapper(throwingPromise.then(_ => cb ? cb(_) : _))\n\n const responseChain: ResponseChain = {\n /**\n * Retrieves the raw result as a promise.\n */\n res: bodyParser(null),\n /**\n * Retrieves the result as a parsed JSON object.\n */\n json: bodyParser(\"json\"),\n /**\n * Retrieves the result as a Blob object.\n */\n blob: bodyParser(\"blob\"),\n /**\n * Retrieves the result as a FormData object.\n */\n formData: bodyParser(\"formData\"),\n /**\n * Retrieves the result as an ArrayBuffer object.\n */\n arrayBuffer: bodyParser(\"arrayBuffer\"),\n /**\n * Retrieves the result as a string.\n */\n text: bodyParser(\"text\"),\n /**\n * Performs a callback on the API performance timings of the request.\n *\n * Warning: Still experimental on browsers and node.js\n */\n perfs: cb => {\n fetchRequest.then(res => perfs.observe(res.url, cb))\n return responseChain\n },\n /**\n * Aborts the request after a fixed time.\n *\n * @param time Time in milliseconds\n * @param controller A custom controller\n */\n setTimeout: (time, controller = fetchController) => {\n timeout.clear()\n timeout.ref = setTimeout(() => controller.abort(), time)\n return responseChain\n },\n /**\n * Returns the automatically generated AbortController alongside the current wretch response as a pair.\n */\n controller: () => [ fetchController, responseChain ],\n /**\n * Catches an http response with a specific error code or name and performs a callback.\n */\n error(errorId, cb) {\n catchers.set(errorId, cb)\n return responseChain\n },\n /**\n * Catches a bad request (http code 400) and performs a callback.\n */\n badRequest: cb => responseChain.error(400, cb),\n /**\n * Catches an unauthorized request (http code 401) and performs a callback.\n */\n unauthorized: cb => responseChain.error(401, cb),\n /**\n * Catches a forbidden request (http code 403) and performs a callback.\n */\n forbidden: cb => responseChain.error(403, cb),\n /**\n * Catches a \"not found\" request (http code 404) and performs a callback.\n */\n notFound: cb => responseChain.error(404, cb),\n /**\n * Catches a timeout (http code 408) and performs a callback.\n */\n timeout: cb => responseChain.error(408, cb),\n /**\n * Catches an internal server error (http code 500) and performs a callback.\n */\n internalError: cb => responseChain.error(500, cb),\n /**\n * Catches an AbortError and performs a callback.\n */\n onAbort: cb => responseChain.error(\"AbortError\", cb)\n }\n\n return resolvers.reduce((chain, r) => r(chain, wretcher), responseChain) as (ResponseChain & Promise)\n}\n","import { WretcherOptions } from \"./wretcher\"\nimport { WretcherResponse } from \"./resolver\"\n\nexport type Middleware = (options?: {[key: string]: any}) => ConfiguredMiddleware\nexport type ConfiguredMiddleware = (next: FetchLike) => FetchLike\nexport type FetchLike = (url: string, opts: WretcherOptions) => Promise\n\nexport const middlewareHelper = (middlewares: ConfiguredMiddleware[]) => (fetchFunction: FetchLike): FetchLike => {\n return (\n middlewares.length === 0 ?\n fetchFunction :\n middlewares.length === 1 ?\n middlewares[0](fetchFunction) :\n middlewares.reduceRight((acc, curr, idx): any =>\n (idx === middlewares.length - 2) ? curr(acc(fetchFunction)) : curr(acc as any)\n )\n ) as FetchLike\n}\n","import { Wretcher } from \"./wretcher\"\n\nconst factory = Wretcher.factory\nfactory[\"default\"] = Wretcher.factory\n\n/**\n * Return a fresh Wretcher instance.\n */\nexport default factory\n"],"names":["mix","one","two","mergeArrays","clone","prop","hasOwnProperty","Array","config","defaults","errorType","polyfills","fetch","FormData","URLSearchParams","performance","PerformanceObserver","AbortController","polyfill","p","_a","_b","_c","doThrow","_d","instance","_i","args","res","this","self","global","Error","onMatch","entries","name","callback","_performance","getEntriesByName","matches","length","reverse","clearMeasures","perfs","callbacks","delete","size","observer","disconnect","clearResourceTimings","Map","observe","conf","_observer","forEach","lazyObserver","entryTypes","set","_url","_options","_catchers","_resolvers","_middlewares","_deferredChain","Wretcher","url","options","_e","catchers","_f","resolvers","_g","middlewares","_h","deferredChain","mixin","method","replace","selfFactory","split","qp","appendQueryParams","headerValues","headers","headerValue","Accept","Content-Type","Authorization","errorId","catcher","newMap","controller","signal","doResolve","clear","body","baseWretcher","json","wretcher","opts","finalOptions","fetchController","timeout","ref","clearTimeout","fetchRequest","fetchFunction","reduceRight","acc","curr","idx","middlewareHelper","throwingPromise","then","response","ok","msg","err","status","catchersWrapper","promise","catch","has","get","bodyParser","funName","cb","_","responseChain","blob","formData","arrayBuffer","text","setTimeout","time","abort","error","badRequest","unauthorized","forbidden","notFound","internalError","onAbort","reduce","chain","r","resolver","contents","jsObject","content","JSON","stringify","formObject","key","item","append","convertFormData","input","Object","keys","map","value","v","encodeQueryValue","join","queryString","usp","val","toString","encodeURIComponent","factory"],"mappings":"0YAAO,IAAMA,EAAM,SAAUC,EAAaC,EAAaC,GACnD,gBADmDA,OAC/CF,IAAQC,GAAsB,iBAARD,GAAmC,iBAARC,EACjD,OAAOD,EAEX,IAAMG,OAAaH,GACnB,IAAI,IAAMI,KAAQH,EACXA,EAAII,eAAeD,KACfH,EAAI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EAAS0G"} \ No newline at end of file +{"version":3,"file":"wretch.esm.js","sources":["../../src/mix.ts","../../src/config.ts","../../src/perfs.ts","../../src/resolver.ts","../../src/wretcher.ts","../../src/middleware.ts","../../src/index.umd.ts"],"sourcesContent":["export const mix = function (one: object, two: object, mergeArrays: boolean = false) {\n if(!one || !two || typeof one !== \"object\" || typeof two !== \"object\")\n return one\n\n const clone = { ...one }\n for(const prop in two) {\n if(two.hasOwnProperty(prop)) {\n if(two[prop] instanceof Array && one[prop] instanceof Array) {\n clone[prop] = mergeArrays ? [ ...one[prop], ...two[prop] ] : two[prop]\n } else if(typeof two[prop] === \"object\" && typeof one[prop] === \"object\") {\n clone[prop] = mix(one[prop], two[prop], mergeArrays)\n } else {\n clone[prop] = two[prop]\n }\n }\n }\n\n return clone\n}\n","declare const global\n\nconst config = {\n // Default options\n defaults: {},\n // Error type\n errorType: null,\n // Polyfills\n polyfills: {\n fetch: null,\n FormData: null,\n URLSearchParams: null,\n performance: null,\n PerformanceObserver: null,\n AbortController: null\n },\n polyfill(p: string, { doThrow = true, instance = false } = {}, ...args) {\n const res = this.polyfills[p] ||\n (typeof self !== \"undefined\" ? self[p] : null) ||\n (typeof global !== \"undefined\" ? global[p] : null)\n if(doThrow && !res) throw new Error(p + \" is not defined\")\n return instance && res ? new res(...args) : res\n }\n}\n\nexport default config\n","import conf from \"./config\"\n\nconst onMatch = (entries, name, callback, _performance) => {\n if(!entries.getEntriesByName)\n return false\n const matches = entries.getEntriesByName(name)\n if(matches && matches.length > 0) {\n callback(matches.reverse()[0])\n if(_performance.clearMeasures)\n _performance.clearMeasures(name)\n perfs.callbacks.delete(name)\n\n if(perfs.callbacks.size < 1) {\n perfs.observer.disconnect()\n if(_performance.clearResourceTimings) {\n _performance.clearResourceTimings()\n }\n }\n return true\n }\n return false\n}\n\nconst lazyObserver = (_performance, _observer) => {\n if(!perfs.observer && _performance && _observer) {\n perfs.observer = new _observer(entries => {\n perfs.callbacks.forEach((callback, name) => {\n onMatch(entries, name, callback, _performance)\n })\n })\n if(_performance.clearResourceTimings)\n _performance.clearResourceTimings()\n }\n return perfs.observer\n}\n\nconst perfs = {\n callbacks: new Map(),\n observer: null,\n observe: (name, callback) => {\n if(!name || !callback)\n return\n\n const _performance = conf.polyfill(\"performance\", { doThrow: false })\n const _observer = conf.polyfill(\"PerformanceObserver\", { doThrow: false })\n\n if(!lazyObserver(_performance, _observer))\n return\n\n if(!onMatch(_performance, name, callback, _performance)) {\n if(perfs.callbacks.size < 1)\n perfs.observer.observe({ entryTypes: [\"resource\", \"measure\"] })\n perfs.callbacks.set(name, callback)\n }\n\n }\n}\n\nexport default perfs\n","import { Wretcher } from \"./wretcher\"\nimport { mix } from \"./mix\"\nimport conf from \"./config\"\nimport perfs from \"./perfs\"\nimport { middlewareHelper } from \"./middleware\"\n\nexport type WretcherError = Error & { status: number, response: WretcherResponse, text?: string, json?: any }\nexport type WretcherErrorCallback = (error: WretcherError, originalRequest: Wretcher) => any\nexport type WretcherResponse = Response & { [key: string]: any }\nexport type ResponseChain = {\n // Response types\n res: (cb?: (type: WretcherResponse) => Result) => Promise,\n json: (cb?: (type: {[key: string]: any}) => Result) => Promise,\n blob: (cb?: (type: Blob) => Result) => Promise,\n formData: (cb?: (type: FormData) => Result) => Promise,\n arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise,\n text: (cb?: (type: string) => Result) => Promise,\n // Extras\n perfs: (cb?: (timing: any) => void) => ResponseChain,\n setTimeout: (time: number, controller?: AbortController) => ResponseChain,\n controller: () => [any, ResponseChain],\n // Catchers\n error: (code: (number | string), cb: WretcherErrorCallback) => ResponseChain,\n badRequest: (cb: WretcherErrorCallback) => ResponseChain,\n unauthorized: (cb: WretcherErrorCallback) => ResponseChain,\n forbidden: (cb: WretcherErrorCallback) => ResponseChain,\n notFound: (cb: WretcherErrorCallback) => ResponseChain,\n timeout: (cb: WretcherErrorCallback) => ResponseChain,\n internalError: (cb: WretcherErrorCallback) => ResponseChain,\n fetchError: (cb: WretcherErrorCallback) => ResponseChain,\n onAbort: (cb: WretcherErrorCallback) => ResponseChain\n}\n\nclass WretchErrorWrapper {\n constructor(public error: any) {}\n}\n\nexport const resolver = (wretcher: Wretcher) => {\n const {\n _url: url,\n _catchers: _catchers,\n _resolvers: resolvers,\n _middlewares: middlewares,\n _options: opts\n } = wretcher\n const catchers = new Map(_catchers)\n const finalOptions = mix(conf.defaults, opts)\n const fetchController = conf.polyfill(\"AbortController\", { doThrow: false, instance: true })\n if(!finalOptions[\"signal\"] && fetchController) {\n finalOptions[\"signal\"] = fetchController.signal\n }\n // Request timeout\n const timeout = {\n ref: null,\n clear() {\n if(timeout.ref) {\n clearTimeout(timeout.ref)\n timeout.ref = null\n }\n }\n }\n // The generated fetch request\n const fetchRequest = middlewareHelper(middlewares)(conf.polyfill(\"fetch\"))(url, finalOptions)\n // Throws on an http error\n const throwingPromise: Promise = fetchRequest\n .catch(error => {\n throw new WretchErrorWrapper(error)\n })\n .then(response => {\n timeout.clear()\n if (!response.ok) {\n return response[conf.errorType || \"text\"]().then(msg => {\n // Enhances the error object\n const err = new Error(msg)\n err[conf.errorType || \"text\"] = msg\n err[\"status\"] = response.status\n err[\"response\"] = response\n throw err\n })\n }\n return response\n })\n // Wraps the Promise in order to dispatch the error to a matching catcher\n const catchersWrapper = (promise: Promise): Promise => {\n return promise.catch(err => {\n timeout.clear()\n const error = err instanceof WretchErrorWrapper ? err.error : err\n if(err instanceof WretchErrorWrapper && catchers.has(\"__fromFetch\"))\n return catchers.get(\"__fromFetch\")(error, wretcher)\n else if(catchers.has(error.status))\n return catchers.get(error.status)(error, wretcher)\n else if(catchers.has(error.name))\n return catchers.get(error.name)(error, wretcher)\n else\n throw error\n })\n }\n // Enforces the proper promise type when a body parsing method is called.\n type BodyParser = (funName: string | null) => (cb?: (type: Type) => Result) => Promise\n const bodyParser: BodyParser = (funName) => (cb) => funName ?\n // If a callback is provided, then callback with the body result otherwise return the parsed body itself.\n catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) :\n // No body parsing method - return the response\n catchersWrapper(throwingPromise.then(_ => cb ? cb(_) : _))\n\n const responseChain: ResponseChain = {\n /**\n * Retrieves the raw result as a promise.\n */\n res: bodyParser(null),\n /**\n * Retrieves the result as a parsed JSON object.\n */\n json: bodyParser(\"json\"),\n /**\n * Retrieves the result as a Blob object.\n */\n blob: bodyParser(\"blob\"),\n /**\n * Retrieves the result as a FormData object.\n */\n formData: bodyParser(\"formData\"),\n /**\n * Retrieves the result as an ArrayBuffer object.\n */\n arrayBuffer: bodyParser(\"arrayBuffer\"),\n /**\n * Retrieves the result as a string.\n */\n text: bodyParser(\"text\"),\n /**\n * Performs a callback on the API performance timings of the request.\n *\n * Warning: Still experimental on browsers and node.js\n */\n perfs: cb => {\n fetchRequest.then(res => perfs.observe(res.url, cb))\n return responseChain\n },\n /**\n * Aborts the request after a fixed time.\n *\n * @param time Time in milliseconds\n * @param controller A custom controller\n */\n setTimeout: (time, controller = fetchController) => {\n timeout.clear()\n timeout.ref = setTimeout(() => controller.abort(), time)\n return responseChain\n },\n /**\n * Returns the automatically generated AbortController alongside the current wretch response as a pair.\n */\n controller: () => [ fetchController, responseChain ],\n /**\n * Catches an http response with a specific error code or name and performs a callback.\n */\n error(errorId, cb) {\n catchers.set(errorId, cb)\n return responseChain\n },\n /**\n * Catches a bad request (http code 400) and performs a callback.\n */\n badRequest: cb => responseChain.error(400, cb),\n /**\n * Catches an unauthorized request (http code 401) and performs a callback.\n */\n unauthorized: cb => responseChain.error(401, cb),\n /**\n * Catches a forbidden request (http code 403) and performs a callback.\n */\n forbidden: cb => responseChain.error(403, cb),\n /**\n * Catches a \"not found\" request (http code 404) and performs a callback.\n */\n notFound: cb => responseChain.error(404, cb),\n /**\n * Catches a timeout (http code 408) and performs a callback.\n */\n timeout: cb => responseChain.error(408, cb),\n /**\n * Catches an internal server error (http code 500) and performs a callback.\n */\n internalError: cb => responseChain.error(500, cb),\n /**\n * Catches errors thrown when calling the fetch function and performs a callback.\n */\n fetchError: cb => responseChain.error(\"__fromFetch\", cb),\n /**\n * Catches an AbortError and performs a callback.\n */\n onAbort: cb => responseChain.error(\"AbortError\", cb)\n }\n\n return resolvers.reduce((chain, r) => r(chain, wretcher), responseChain) as (ResponseChain & Promise)\n}\n","import { mix } from \"./mix\"\nimport conf from \"./config\"\nimport { resolver, WretcherError, ResponseChain } from \"./resolver\"\nimport { ConfiguredMiddleware } from \"./middleware\"\n\nexport type WretcherOptions = RequestInit & {\n [key: string]: any\n}\n\nexport type DeferredCallback = (wretcher: Wretcher, url: string, options: WretcherOptions) => Wretcher\n\n/**\n * The Wretcher class used to perform easy fetch requests.\n *\n * Immutability : almost every method of this class return a fresh Wretcher object.\n */\nexport class Wretcher {\n\n protected constructor(\n public _url: string,\n public _options: WretcherOptions,\n public _catchers: Map void> = new Map(),\n public _resolvers: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any> = [],\n public _middlewares: ConfiguredMiddleware[] = [],\n public _deferredChain: DeferredCallback[] = []) {}\n\n static factory(url = \"\", options: WretcherOptions = {}) { return new Wretcher(url, options) }\n private selfFactory({ url = this._url, options = this._options, catchers = this._catchers,\n resolvers = this._resolvers, middlewares = this._middlewares, deferredChain = this._deferredChain } = {}) {\n return new Wretcher(url, {...options}, new Map(catchers), [...resolvers], [...middlewares], [...deferredChain])\n }\n\n /**\n * Sets the default fetch options used for every subsequent fetch call.\n * @param options New default options\n * @param mixin If true, mixes in instead of replacing the existing options\n */\n defaults(options: WretcherOptions, mixin = false) {\n conf.defaults = mixin ? mix(conf.defaults, options) : options\n return this\n }\n\n /**\n * Sets the method (text, json ...) used to parse the data contained in the response body in case of an HTTP error.\n *\n * Persists for every subsequent requests.\n *\n * Default is \"text\".\n */\n errorType(method: \"text\" | \"json\") {\n conf.errorType = method\n return this\n }\n\n /**\n * Sets the non-global polyfills which will be used for every subsequent calls.\n *\n * Needed for libraries like [fetch-ponyfill](https://github.com/qubyte/fetch-ponyfill).\n *\n * @param polyfills An object containing the polyfills.\n */\n polyfills(polyfills: Partial) {\n conf.polyfills = { ...conf.polyfills, ...polyfills }\n return this\n }\n\n /**\n * Returns a new Wretcher object with the argument url appended and the same options.\n * @param url String url\n * @param replace Boolean If true, replaces the current url instead of appending\n */\n url(url: string, replace = false) {\n if(replace)\n return this.selfFactory({ url })\n const split = this._url.split(\"?\")\n return this.selfFactory({\n url: split.length > 1 ?\n split[0] + url + \"?\" + split[1] :\n this._url + url\n })\n }\n\n /**\n * Returns a new Wretcher object with the same url and new options.\n * @param options New options\n * @param mixin If true, mixes in instead of replacing the existing options\n */\n options(options: WretcherOptions, mixin = true) {\n return this.selfFactory({ options: mixin ? mix(this._options, options) : options })\n }\n\n /**\n * Converts a javascript object to query parameters,\n * then appends this query string to the current url.\n *\n * If given a string, use the string as the query verbatim.\n *\n * ```\n * let w = wretch(\"http://example.com\") // url is http://example.com\n *\n * // Chain query calls\n * w = w.query({ a: 1, b : 2 }) // url is now http://example.com?a=1&b=2\n * w = w.query(\"foo-bar-baz-woz\") // url is now http://example.com?a=1&b=2&foo-bar-baz-woz\n *\n * // Pass true as the second argument to replace existing query parameters\n * w = w.query(\"c=3&d=4\", true) // url is now http://example.com?c=3&d=4\n * ```\n *\n * @param qp An object which will be converted, or a string which will be used verbatim.\n */\n query(qp: object | string, replace: boolean = false) {\n return this.selfFactory({ url: appendQueryParams(this._url, qp, replace) })\n }\n\n /**\n * Set request headers.\n * @param headerValues An object containing header keys and values\n */\n headers(headerValues: { [headerName: string]: string }) {\n return this.selfFactory({ options: mix(this._options, { headers: headerValues || {} }) })\n }\n\n /**\n * Shortcut to set the \"Accept\" header.\n * @param headerValue Header value\n */\n accept(headerValue: string) {\n return this.headers({ Accept : headerValue })\n }\n\n /**\n * Shortcut to set the \"Content-Type\" header.\n * @param headerValue Header value\n */\n content(headerValue: string) {\n return this.headers({ \"Content-Type\" : headerValue })\n }\n\n /**\n * Shortcut to set the \"Authorization\" header.\n * @param headerValue Header value\n */\n auth(headerValue: string) {\n return this.headers({ Authorization: headerValue })\n }\n\n /**\n * Adds a default catcher which will be called on every subsequent request error when the error code matches.\n * @param errorId Error code or name\n * @param catcher: The catcher method\n */\n catcher(errorId: number | string, catcher: (error: WretcherError, originalRequest: Wretcher) => any) {\n const newMap = new Map(this._catchers)\n newMap.set(errorId, catcher)\n return this.selfFactory({ catchers: newMap })\n }\n\n /**\n * Associates a custom signal with the request.\n * @param controller : An AbortController\n */\n signal(controller: AbortController) {\n return this.selfFactory({ options: { ...this._options, signal: controller.signal }})\n }\n\n /**\n * Program a resolver to perform response chain tasks automatically.\n * @param doResolve : Resolver callback\n */\n resolve(doResolve: (chain: ResponseChain, originalRequest: Wretcher) => ResponseChain | Promise, clear: boolean = false) {\n return this.selfFactory({ resolvers: clear ? [ doResolve ] : [ ...this._resolvers, doResolve ]})\n }\n\n /**\n * Defer wretcher methods that will be chained and called just before the request is performed.\n */\n defer(callback: DeferredCallback, clear = false) {\n return this.selfFactory({\n deferredChain: clear ? [callback] : [ ...this._deferredChain, callback ]\n })\n }\n\n /**\n * Add middlewares to intercept a request before being sent.\n */\n middlewares(middlewares: ConfiguredMiddleware[], clear = false) {\n return this.selfFactory({\n middlewares: clear ? middlewares : [ ...this._middlewares, ...middlewares ]\n })\n }\n\n private method(method, options = {}, body = null) {\n let baseWretcher =\n !body ? this :\n typeof body === \"object\" ? this.json(body) :\n this.body(body)\n baseWretcher = baseWretcher.options({ ...options, method })\n const deferredWretcher = baseWretcher._deferredChain.reduce((acc: Wretcher, curr) => curr(acc, acc._url, acc._options), baseWretcher)\n return resolver(deferredWretcher)\n }\n\n /**\n * Performs a get request.\n */\n get(options?) {\n return this.method(\"GET\", options)\n }\n /**\n * Performs a delete request.\n */\n delete(options?) {\n return this.method(\"DELETE\", options)\n }\n /**\n * Performs a put request.\n */\n put(body?, options?) {\n return this.method(\"PUT\", options, body)\n }\n /**\n * Performs a post request.\n */\n post(body?, options?) {\n return this.method(\"POST\", options, body)\n }\n /**\n * Performs a patch request.\n */\n patch(body?, options?) {\n return this.method(\"PATCH\", options, body)\n }\n /**\n * Performs a head request.\n */\n head(options?) {\n return this.method(\"HEAD\", options)\n }\n /**\n * Performs an options request\n */\n opts(options?) {\n return this.method(\"OPTIONS\", options)\n }\n /**\n * Replay a request.\n */\n replay(options?) {\n return this.method(this._options.method, options)\n }\n\n /**\n * Sets the request body with any content.\n * @param contents The body contents\n */\n body(contents: any) {\n return this.selfFactory({ options: { ...this._options, body: contents }})\n }\n /**\n * Sets the content type header, stringifies an object and sets the request body.\n * @param jsObject An object which will be serialized into a JSON\n */\n json(jsObject: object) {\n return this.content(\"application/json\").body(JSON.stringify(jsObject))\n }\n /**\n * Converts the javascript object to a FormData and sets the request body.\n * @param formObject An object which will be converted to a FormData\n */\n formData(formObject: object) {\n return this.body(convertFormData(formObject))\n }\n /**\n * Converts the input to an url encoded string and sets the content-type header and body.\n * If the input argument is already a string, skips the conversion part.\n *\n * @param input An object to convert into an url encoded string or an already encoded string\n */\n formUrl(input: (object | string)) {\n return this\n .body(typeof input === \"string\" ? input : convertFormUrl(input))\n .content(\"application/x-www-form-urlencoded\")\n }\n}\n\n// Internal helpers\n\nconst appendQueryParams = (url: string, qp: object | string, replace: boolean) => {\n let queryString\n\n if(typeof qp === \"string\") {\n queryString = qp\n } else {\n const usp = conf.polyfill(\"URLSearchParams\", { instance: true })\n for(const key in qp) {\n if(qp[key] instanceof Array) {\n for(const val of qp[key])\n usp.append(key, val)\n } else {\n usp.append(key, qp[key])\n }\n }\n queryString = usp.toString()\n }\n\n const split = url.split(\"?\")\n if(replace || split.length < 2)\n return split[0] + \"?\" + queryString\n\n return url + \"&\" + queryString\n}\n\nfunction convertFormData(formObject: object) {\n const formData = conf.polyfill(\"FormData\", { instance: true })\n for(const key in formObject) {\n if(formObject[key] instanceof Array) {\n for(const item of formObject[key])\n formData.append(key + \"[]\", item)\n } else {\n formData.append(key, formObject[key])\n }\n }\n\n return formData\n}\n\nfunction encodeQueryValue(key: string, value: unknown) {\n return encodeURIComponent(key) +\n \"=\" +\n encodeURIComponent(\n typeof value === \"object\" ?\n JSON.stringify(value) :\n \"\" + value\n )\n}\nfunction convertFormUrl(formObject: object) {\n return Object.keys(formObject)\n .map(key => {\n const value = formObject[key]\n if(value instanceof Array) {\n return value.map(v => encodeQueryValue(key, v)).join(\"&\")\n }\n return encodeQueryValue(key, value)\n })\n .join(\"&\")\n}\n","import { WretcherOptions } from \"./wretcher\"\nimport { WretcherResponse } from \"./resolver\"\n\nexport type Middleware = (options?: {[key: string]: any}) => ConfiguredMiddleware\nexport type ConfiguredMiddleware = (next: FetchLike) => FetchLike\nexport type FetchLike = (url: string, opts: WretcherOptions) => Promise\n\nexport const middlewareHelper = (middlewares: ConfiguredMiddleware[]) => (fetchFunction: FetchLike): FetchLike => {\n return (\n middlewares.length === 0 ?\n fetchFunction :\n middlewares.length === 1 ?\n middlewares[0](fetchFunction) :\n middlewares.reduceRight((acc, curr, idx): any =>\n (idx === middlewares.length - 2) ? curr(acc(fetchFunction)) : curr(acc as any)\n )\n ) as FetchLike\n}\n","import { Wretcher } from \"./wretcher\"\n\nconst factory = Wretcher.factory\nfactory[\"default\"] = Wretcher.factory\n\n/**\n * Return a fresh Wretcher instance.\n */\nexport default factory\n"],"names":["mix","one","two","mergeArrays","clone","prop","hasOwnProperty","Array","config","defaults","errorType","polyfills","fetch","FormData","URLSearchParams","performance","PerformanceObserver","AbortController","polyfill","p","_a","_b","_c","doThrow","_d","instance","_i","args","res","this","self","global","Error","onMatch","entries","name","callback","_performance","getEntriesByName","matches","length","reverse","clearMeasures","perfs","callbacks","delete","size","observer","disconnect","clearResourceTimings","Map","observe","conf","_observer","forEach","lazyObserver","entryTypes","set","error","_url","_options","_catchers","_resolvers","_middlewares","_deferredChain","Wretcher","url","options","_e","catchers","_f","resolvers","_g","middlewares","_h","deferredChain","mixin","method","replace","selfFactory","split","qp","appendQueryParams","headerValues","headers","headerValue","Accept","Content-Type","Authorization","errorId","catcher","newMap","controller","signal","doResolve","clear","body","baseWretcher","json","wretcher","opts","finalOptions","fetchController","timeout","ref","clearTimeout","fetchRequest","fetchFunction","reduceRight","acc","curr","idx","middlewareHelper","throwingPromise","catch","WretchErrorWrapper","then","response","ok","msg","err","status","catchersWrapper","promise","has","get","bodyParser","funName","cb","_","responseChain","blob","formData","arrayBuffer","text","setTimeout","time","abort","badRequest","unauthorized","forbidden","notFound","internalError","fetchError","onAbort","reduce","chain","r","resolver","contents","jsObject","content","JSON","stringify","formObject","key","item","append","convertFormData","input","Object","keys","map","value","v","encodeQueryValue","join","queryString","usp","val","toString","encodeURIComponent","factory"],"mappings":"0YAAO,IAAMA,EAAM,SAAUC,EAAaC,EAAaC,GACnD,gBADmDA,OAC/CF,IAAQC,GAAsB,iBAARD,GAAmC,iBAARC,EACjD,OAAOD,EAEX,IAAMG,OAAaH,GACnB,IAAI,IAAMI,KAAQH,EACXA,EAAII,eAAeD,KACfH,EAAIG,aAAiBE,OAASN,EAAII,aAAiBE,MAClDH,EAAMC,GAAQF,IAAmBF,EAAII,GAAUH,EAAIG,IAAUH,EAAIG,GACtC,iBAAdH,EAAIG,IAA2C,iBAAdJ,EAAII,GAClDD,EAAMC,GAAQL,EAAIC,EAAII,GAAOH,EAAIG,GAAOF,GAExCC,EAAMC,GAAQH,EAAIG,IAK9B,OAAOD,GCfLI,EAAS,CAEXC,SAAU,GAEVC,UAAW,KAEXC,UAAW,CACPC,MAAO,KACPC,SAAU,KACVC,gBAAiB,KACjBC,YAAa,KACbC,oBAAqB,KACrBC,gBAAiB,MAErBC,SAAA,SAASC,EAAWC,WAAAC,kBAAEC,YAAAC,gBAAgBC,aAAAC,yBAAyBC,mBAAAA,IAAAC,oBAC3D,IAAMC,EAAMC,KAAKlB,UAAUQ,KACN,oBAATW,KAAuBA,KAAKX,GAAK,QACtB,oBAAXY,OAAyBA,OAAOZ,GAAK,MACjD,GAAGI,IAAYK,EAAK,MAAM,IAAII,MAAMb,EAAI,mBACxC,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DC,OAAOC,KAAKP,GACdQ,KAAI,SAAAP,GACD,IAAMQ,EAAQT,EAAWC,GACzB,OAAGQ,aAAiB5J,MACT4J,EAAMD,KAAI,SAAAE,GAAK,OAAAC,EAAiBV,EAAKS,MAAIE,KAAK,KAElDD,EAAiBV,EAAKQ,MAEhCG,KAAK,OA/DDf,QAAQ,qCAsDrB,IAAwBG,QAhDlBxE,EAAoB,SAAChB,EAAae,EAAqBH,GACzD,IAAIyF,EAEJ,GAAiB,iBAAPtF,EACNsF,EAActF,MACX,CACH,IAAMuF,EAAMpH,EAAKlC,SAAS,kBAAmB,CAAEO,UAAU,IACzD,IAAI,IAAMkI,KAAO1E,EACb,GAAGA,EAAG0E,aAAgBpJ,MAClB,IAAiB,QAAAa,EAAA6D,EAAG0E,GAAHjI,WAAAA,KAAb,IAAM+I,OACND,EAAIX,OAAOF,EAAKc,QAEpBD,EAAIX,OAAOF,EAAK1E,EAAG0E,IAG3BY,EAAcC,EAAIE,WAGtB,IAAM1F,EAAQd,EAAIc,MAAM,KACxB,OAAGF,GAAWE,EAAMxC,OAAS,EAClBwC,EAAM,GAAK,IAAMuF,EAErBrG,EAAM,IAAMqG,GAiBvB,SAASF,EAAiBV,EAAaQ,GACnC,OAAOQ,mBAAmBhB,GAC1B,IACAgB,mBACqB,iBAAVR,EACHX,KAAKC,UAAUU,GACnB,GAAKA,GEzUb,IAAMS,EAAU3G,EAAS2G,QACzBA,EAAiB,QAAI3G,EAAS2G"} \ No newline at end of file diff --git a/dist/bundle/wretch.js b/dist/bundle/wretch.js index cc6e543..17e2964 100644 --- a/dist/bundle/wretch.js +++ b/dist/bundle/wretch.js @@ -1,2 +1,2 @@ -!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(r=r||self).wretch=t()}(this,(function(){"use strict";var r=function(){return(r=Object.assign||function(r){for(var t,e=1,n=arguments.length;e0)&&(e(o.reverse()[0]),n.clearMeasures&&n.clearMeasures(t),i.callbacks.delete(t),i.callbacks.size<1&&(i.observer.disconnect(),n.clearResourceTimings&&n.clearResourceTimings()),!0)},i={callbacks:new Map,observer:null,observe:function(r,t){if(r&&t){var e=n.polyfill("performance",{doThrow:!1});(function(r,t){return!i.observer&&r&&t&&(i.observer=new t((function(t){i.callbacks.forEach((function(e,n){o(t,n,e,r)}))})),r.clearResourceTimings&&r.clearResourceTimings()),i.observer})(e,n.polyfill("PerformanceObserver",{doThrow:!1}))&&(o(e,r,t,e)||(i.callbacks.size<1&&i.observer.observe({entryTypes:["resource","measure"]}),i.callbacks.set(r,t)))}}},s=function(){function o(r,t,e,n,o,i){void 0===e&&(e=new Map),void 0===n&&(n=[]),void 0===o&&(o=[]),void 0===i&&(i=[]),this._url=r,this._options=t,this._catchers=e,this._resolvers=n,this._middlewares=o,this._deferredChain=i}return o.factory=function(r,t){return void 0===r&&(r=""),void 0===t&&(t={}),new o(r,t)},o.prototype.selfFactory=function(e){var n=void 0===e?{}:e,i=n.url,s=void 0===i?this._url:i,u=n.options,a=void 0===u?this._options:u,f=n.catchers,c=void 0===f?this._catchers:f,l=n.resolvers,p=void 0===l?this._resolvers:l,h=n.middlewares,d=void 0===h?this._middlewares:h,y=n.deferredChain,v=void 0===y?this._deferredChain:y;return new o(s,r({},a),new Map(c),t(p),t(d),t(v))},o.prototype.defaults=function(r,t){return void 0===t&&(t=!1),n.defaults=t?e(n.defaults,r):r,this},o.prototype.errorType=function(r){return n.errorType=r,this},o.prototype.polyfills=function(t){return n.polyfills=r(r({},n.polyfills),t),this},o.prototype.url=function(r,t){if(void 0===t&&(t=!1),t)return this.selfFactory({url:r});var e=this._url.split("?");return this.selfFactory({url:e.length>1?e[0]+r+"?"+e[1]:this._url+r})},o.prototype.options=function(r,t){return void 0===t&&(t=!0),this.selfFactory({options:t?e(this._options,r):r})},o.prototype.query=function(r,t){return void 0===t&&(t=!1),this.selfFactory({url:u(this._url,r,t)})},o.prototype.headers=function(r){return this.selfFactory({options:e(this._options,{headers:r||{}})})},o.prototype.accept=function(r){return this.headers({Accept:r})},o.prototype.content=function(r){return this.headers({"Content-Type":r})},o.prototype.auth=function(r){return this.headers({Authorization:r})},o.prototype.catcher=function(r,t){var e=new Map(this._catchers);return e.set(r,t),this.selfFactory({catchers:e})},o.prototype.signal=function(t){return this.selfFactory({options:r(r({},this._options),{signal:t.signal})})},o.prototype.resolve=function(r,e){return void 0===e&&(e=!1),this.selfFactory({resolvers:e?[r]:t(this._resolvers,[r])})},o.prototype.defer=function(r,e){return void 0===e&&(e=!1),this.selfFactory({deferredChain:e?[r]:t(this._deferredChain,[r])})},o.prototype.middlewares=function(r,e){return void 0===e&&(e=!1),this.selfFactory({middlewares:e?r:t(this._middlewares,r)})},o.prototype.method=function(t,o,s){void 0===o&&(o={}),void 0===s&&(s=null);var u=s?"object"==typeof s?this.json(s):this.body(s):this;return function(r){var t=r._url,o=r._catchers,s=r._resolvers,u=r._middlewares,a=r._options,f=new Map(o),c=e(n.defaults,a),l=n.polyfill("AbortController",{doThrow:!1,instance:!0});!c.signal&&l&&(c.signal=l.signal);var p={ref:null,clear:function(){p.ref&&(clearTimeout(p.ref),p.ref=null)}},h=function(r){return function(t){return 0===r.length?t:1===r.length?r[0](t):r.reduceRight((function(e,n,o){return o===r.length-2?n(e(t)):n(e)}))}}(u)(n.polyfill("fetch"))(t,c),d=h.then((function(r){return p.clear(),r.ok?r:r[n.errorType||"text"]().then((function(t){var e=new Error(t);throw e[n.errorType||"text"]=t,e.status=r.status,e.response=r,e}))})),y=function(t){return t.catch((function(t){if(p.clear(),f.has(t.status))return f.get(t.status)(t,r);if(f.has(t.name))return f.get(t.name)(t,r);throw t}))},v=function(r){return function(t){return y(r?d.then((function(t){return t&&t[r]()})).then((function(r){return t?t(r):r})):d.then((function(r){return t?t(r):r})))}},m={res:v(null),json:v("json"),blob:v("blob"),formData:v("formData"),arrayBuffer:v("arrayBuffer"),text:v("text"),perfs:function(r){return h.then((function(t){return i.observe(t.url,r)})),m},setTimeout:function(r,t){return void 0===t&&(t=l),p.clear(),p.ref=setTimeout((function(){return t.abort()}),r),m},controller:function(){return[l,m]},error:function(r,t){return f.set(r,t),m},badRequest:function(r){return m.error(400,r)},unauthorized:function(r){return m.error(401,r)},forbidden:function(r){return m.error(403,r)},notFound:function(r){return m.error(404,r)},timeout:function(r){return m.error(408,r)},internalError:function(r){return m.error(500,r)},onAbort:function(r){return m.error("AbortError",r)}};return s.reduce((function(t,e){return e(t,r)}),m)}((u=u.options(r(r({},o),{method:t})))._deferredChain.reduce((function(r,t){return t(r,r._url,r._options)}),u))},o.prototype.get=function(r){return this.method("GET",r)},o.prototype.delete=function(r){return this.method("DELETE",r)},o.prototype.put=function(r,t){return this.method("PUT",t,r)},o.prototype.post=function(r,t){return this.method("POST",t,r)},o.prototype.patch=function(r,t){return this.method("PATCH",t,r)},o.prototype.head=function(r){return this.method("HEAD",r)},o.prototype.opts=function(r){return this.method("OPTIONS",r)},o.prototype.replay=function(r){return this.method(this._options.method,r)},o.prototype.body=function(t){return this.selfFactory({options:r(r({},this._options),{body:t})})},o.prototype.json=function(r){return this.content("application/json").body(JSON.stringify(r))},o.prototype.formData=function(r){return this.body(function(r){var t=n.polyfill("FormData",{instance:!0});for(var e in r)if(r[e]instanceof Array)for(var o=0,i=r[e];o0)&&(e(o.reverse()[0]),n.clearMeasures&&n.clearMeasures(t),i.callbacks.delete(t),i.callbacks.size<1&&(i.observer.disconnect(),n.clearResourceTimings&&n.clearResourceTimings()),!0)},i={callbacks:new Map,observer:null,observe:function(r,t){if(r&&t){var e=n.polyfill("performance",{doThrow:!1});(function(r,t){return!i.observer&&r&&t&&(i.observer=new t((function(t){i.callbacks.forEach((function(e,n){o(t,n,e,r)}))})),r.clearResourceTimings&&r.clearResourceTimings()),i.observer})(e,n.polyfill("PerformanceObserver",{doThrow:!1}))&&(o(e,r,t,e)||(i.callbacks.size<1&&i.observer.observe({entryTypes:["resource","measure"]}),i.callbacks.set(r,t)))}}},s=function(r){this.error=r},u=function(){function o(r,t,e,n,o,i){void 0===e&&(e=new Map),void 0===n&&(n=[]),void 0===o&&(o=[]),void 0===i&&(i=[]),this._url=r,this._options=t,this._catchers=e,this._resolvers=n,this._middlewares=o,this._deferredChain=i}return o.factory=function(r,t){return void 0===r&&(r=""),void 0===t&&(t={}),new o(r,t)},o.prototype.selfFactory=function(e){var n=void 0===e?{}:e,i=n.url,s=void 0===i?this._url:i,u=n.options,f=void 0===u?this._options:u,a=n.catchers,c=void 0===a?this._catchers:a,l=n.resolvers,p=void 0===l?this._resolvers:l,h=n.middlewares,d=void 0===h?this._middlewares:h,y=n.deferredChain,v=void 0===y?this._deferredChain:y;return new o(s,r({},f),new Map(c),t(p),t(d),t(v))},o.prototype.defaults=function(r,t){return void 0===t&&(t=!1),n.defaults=t?e(n.defaults,r):r,this},o.prototype.errorType=function(r){return n.errorType=r,this},o.prototype.polyfills=function(t){return n.polyfills=r(r({},n.polyfills),t),this},o.prototype.url=function(r,t){if(void 0===t&&(t=!1),t)return this.selfFactory({url:r});var e=this._url.split("?");return this.selfFactory({url:e.length>1?e[0]+r+"?"+e[1]:this._url+r})},o.prototype.options=function(r,t){return void 0===t&&(t=!0),this.selfFactory({options:t?e(this._options,r):r})},o.prototype.query=function(r,t){return void 0===t&&(t=!1),this.selfFactory({url:f(this._url,r,t)})},o.prototype.headers=function(r){return this.selfFactory({options:e(this._options,{headers:r||{}})})},o.prototype.accept=function(r){return this.headers({Accept:r})},o.prototype.content=function(r){return this.headers({"Content-Type":r})},o.prototype.auth=function(r){return this.headers({Authorization:r})},o.prototype.catcher=function(r,t){var e=new Map(this._catchers);return e.set(r,t),this.selfFactory({catchers:e})},o.prototype.signal=function(t){return this.selfFactory({options:r(r({},this._options),{signal:t.signal})})},o.prototype.resolve=function(r,e){return void 0===e&&(e=!1),this.selfFactory({resolvers:e?[r]:t(this._resolvers,[r])})},o.prototype.defer=function(r,e){return void 0===e&&(e=!1),this.selfFactory({deferredChain:e?[r]:t(this._deferredChain,[r])})},o.prototype.middlewares=function(r,e){return void 0===e&&(e=!1),this.selfFactory({middlewares:e?r:t(this._middlewares,r)})},o.prototype.method=function(t,o,u){void 0===o&&(o={}),void 0===u&&(u=null);var f=u?"object"==typeof u?this.json(u):this.body(u):this;return function(r){var t=r._url,o=r._catchers,u=r._resolvers,f=r._middlewares,a=r._options,c=new Map(o),l=e(n.defaults,a),p=n.polyfill("AbortController",{doThrow:!1,instance:!0});!l.signal&&p&&(l.signal=p.signal);var h={ref:null,clear:function(){h.ref&&(clearTimeout(h.ref),h.ref=null)}},d=function(r){return function(t){return 0===r.length?t:1===r.length?r[0](t):r.reduceRight((function(e,n,o){return o===r.length-2?n(e(t)):n(e)}))}}(f)(n.polyfill("fetch"))(t,l),y=d.catch((function(r){throw new s(r)})).then((function(r){return h.clear(),r.ok?r:r[n.errorType||"text"]().then((function(t){var e=new Error(t);throw e[n.errorType||"text"]=t,e.status=r.status,e.response=r,e}))})),v=function(t){return t.catch((function(t){h.clear();var e=t instanceof s?t.error:t;if(t instanceof s&&c.has("__fromFetch"))return c.get("__fromFetch")(e,r);if(c.has(e.status))return c.get(e.status)(e,r);if(c.has(e.name))return c.get(e.name)(e,r);throw e}))},m=function(r){return function(t){return v(r?y.then((function(t){return t&&t[r]()})).then((function(r){return t?t(r):r})):y.then((function(r){return t?t(r):r})))}},b={res:m(null),json:m("json"),blob:m("blob"),formData:m("formData"),arrayBuffer:m("arrayBuffer"),text:m("text"),perfs:function(r){return d.then((function(t){return i.observe(t.url,r)})),b},setTimeout:function(r,t){return void 0===t&&(t=p),h.clear(),h.ref=setTimeout((function(){return t.abort()}),r),b},controller:function(){return[p,b]},error:function(r,t){return c.set(r,t),b},badRequest:function(r){return b.error(400,r)},unauthorized:function(r){return b.error(401,r)},forbidden:function(r){return b.error(403,r)},notFound:function(r){return b.error(404,r)},timeout:function(r){return b.error(408,r)},internalError:function(r){return b.error(500,r)},fetchError:function(r){return b.error("__fromFetch",r)},onAbort:function(r){return b.error("AbortError",r)}};return u.reduce((function(t,e){return e(t,r)}),b)}((f=f.options(r(r({},o),{method:t})))._deferredChain.reduce((function(r,t){return t(r,r._url,r._options)}),f))},o.prototype.get=function(r){return this.method("GET",r)},o.prototype.delete=function(r){return this.method("DELETE",r)},o.prototype.put=function(r,t){return this.method("PUT",t,r)},o.prototype.post=function(r,t){return this.method("POST",t,r)},o.prototype.patch=function(r,t){return this.method("PATCH",t,r)},o.prototype.head=function(r){return this.method("HEAD",r)},o.prototype.opts=function(r){return this.method("OPTIONS",r)},o.prototype.replay=function(r){return this.method(this._options.method,r)},o.prototype.body=function(t){return this.selfFactory({options:r(r({},this._options),{body:t})})},o.prototype.json=function(r){return this.content("application/json").body(JSON.stringify(r))},o.prototype.formData=function(r){return this.body(function(r){var t=n.polyfill("FormData",{instance:!0});for(var e in r)if(r[e]instanceof Array)for(var o=0,i=r[e];o {\n if(!entries.getEntriesByName)\n return false\n const matches = entries.getEntriesByName(name)\n if(matches && matches.length > 0) {\n callback(matches.reverse()[0])\n if(_performance.clearMeasures)\n _performance.clearMeasures(name)\n perfs.callbacks.delete(name)\n\n if(perfs.callbacks.size < 1) {\n perfs.observer.disconnect()\n if(_performance.clearResourceTimings) {\n _performance.clearResourceTimings()\n }\n }\n return true\n }\n return false\n}\n\nconst lazyObserver = (_performance, _observer) => {\n if(!perfs.observer && _performance && _observer) {\n perfs.observer = new _observer(entries => {\n perfs.callbacks.forEach((callback, name) => {\n onMatch(entries, name, callback, _performance)\n })\n })\n if(_performance.clearResourceTimings)\n _performance.clearResourceTimings()\n }\n return perfs.observer\n}\n\nconst perfs = {\n callbacks: new Map(),\n observer: null,\n observe: (name, callback) => {\n if(!name || !callback)\n return\n\n const _performance = conf.polyfill(\"performance\", { doThrow: false })\n const _observer = conf.polyfill(\"PerformanceObserver\", { doThrow: false })\n\n if(!lazyObserver(_performance, _observer))\n return\n\n if(!onMatch(_performance, name, callback, _performance)) {\n if(perfs.callbacks.size < 1)\n perfs.observer.observe({ entryTypes: [\"resource\", \"measure\"] })\n perfs.callbacks.set(name, callback)\n }\n\n }\n}\n\nexport default perfs\n","import { mix } from \"./mix\"\nimport conf from \"./config\"\nimport { resolver, WretcherError, ResponseChain } from \"./resolver\"\nimport { ConfiguredMiddleware } from \"./middleware\"\n\nexport type WretcherOptions = RequestInit & {\n [key: string]: any\n}\n\nexport type DeferredCallback = (wretcher: Wretcher, url: string, options: WretcherOptions) => Wretcher\n\n/**\n * The Wretcher class used to perform easy fetch requests.\n *\n * Immutability : almost every method of this class return a fresh Wretcher object.\n */\nexport class Wretcher {\n\n protected constructor(\n public _url: string,\n public _options: WretcherOptions,\n public _catchers: Map void> = new Map(),\n public _resolvers: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any> = [],\n public _middlewares: ConfiguredMiddleware[] = [],\n public _deferredChain: DeferredCallback[] = []) {}\n\n static factory(url = \"\", options: WretcherOptions = {}) { return new Wretcher(url, options) }\n private selfFactory({ url = this._url, options = this._options, catchers = this._catchers,\n resolvers = this._resolvers, middlewares = this._middlewares, deferredChain = this._deferredChain } = {}) {\n return new Wretcher(url, {...options}, new Map(catchers), [...resolvers], [...middlewares], [...deferredChain])\n }\n\n /**\n * Sets the default fetch options used for every subsequent fetch call.\n * @param options New default options\n * @param mixin If true, mixes in instead of replacing the existing options\n */\n defaults(options: WretcherOptions, mixin = false) {\n conf.defaults = mixin ? mix(conf.defaults, options) : options\n return this\n }\n\n /**\n * Sets the method (text, json ...) used to parse the data contained in the response body in case of an HTTP error.\n *\n * Persists for every subsequent requests.\n *\n * Default is \"text\".\n */\n errorType(method: \"text\" | \"json\") {\n conf.errorType = method\n return this\n }\n\n /**\n * Sets the non-global polyfills which will be used for every subsequent calls.\n *\n * Needed for libraries like [fetch-ponyfill](https://github.com/qubyte/fetch-ponyfill).\n *\n * @param polyfills An object containing the polyfills.\n */\n polyfills(polyfills: Partial) {\n conf.polyfills = { ...conf.polyfills, ...polyfills }\n return this\n }\n\n /**\n * Returns a new Wretcher object with the argument url appended and the same options.\n * @param url String url\n * @param replace Boolean If true, replaces the current url instead of appending\n */\n url(url: string, replace = false) {\n if(replace)\n return this.selfFactory({ url })\n const split = this._url.split(\"?\")\n return this.selfFactory({\n url: split.length > 1 ?\n split[0] + url + \"?\" + split[1] :\n this._url + url\n })\n }\n\n /**\n * Returns a new Wretcher object with the same url and new options.\n * @param options New options\n * @param mixin If true, mixes in instead of replacing the existing options\n */\n options(options: WretcherOptions, mixin = true) {\n return this.selfFactory({ options: mixin ? mix(this._options, options) : options })\n }\n\n /**\n * Converts a javascript object to query parameters,\n * then appends this query string to the current url.\n *\n * If given a string, use the string as the query verbatim.\n *\n * ```\n * let w = wretch(\"http://example.com\") // url is http://example.com\n *\n * // Chain query calls\n * w = w.query({ a: 1, b : 2 }) // url is now http://example.com?a=1&b=2\n * w = w.query(\"foo-bar-baz-woz\") // url is now http://example.com?a=1&b=2&foo-bar-baz-woz\n *\n * // Pass true as the second argument to replace existing query parameters\n * w = w.query(\"c=3&d=4\", true) // url is now http://example.com?c=3&d=4\n * ```\n *\n * @param qp An object which will be converted, or a string which will be used verbatim.\n */\n query(qp: object | string, replace: boolean = false) {\n return this.selfFactory({ url: appendQueryParams(this._url, qp, replace) })\n }\n\n /**\n * Set request headers.\n * @param headerValues An object containing header keys and values\n */\n headers(headerValues: { [headerName: string]: string }) {\n return this.selfFactory({ options: mix(this._options, { headers: headerValues || {} }) })\n }\n\n /**\n * Shortcut to set the \"Accept\" header.\n * @param headerValue Header value\n */\n accept(headerValue: string) {\n return this.headers({ Accept : headerValue })\n }\n\n /**\n * Shortcut to set the \"Content-Type\" header.\n * @param headerValue Header value\n */\n content(headerValue: string) {\n return this.headers({ \"Content-Type\" : headerValue })\n }\n\n /**\n * Shortcut to set the \"Authorization\" header.\n * @param headerValue Header value\n */\n auth(headerValue: string) {\n return this.headers({ Authorization: headerValue })\n }\n\n /**\n * Adds a default catcher which will be called on every subsequent request error when the error code matches.\n * @param errorId Error code or name\n * @param catcher: The catcher method\n */\n catcher(errorId: number | string, catcher: (error: WretcherError, originalRequest: Wretcher) => any) {\n const newMap = new Map(this._catchers)\n newMap.set(errorId, catcher)\n return this.selfFactory({ catchers: newMap })\n }\n\n /**\n * Associates a custom signal with the request.\n * @param controller : An AbortController\n */\n signal(controller: AbortController) {\n return this.selfFactory({ options: { ...this._options, signal: controller.signal }})\n }\n\n /**\n * Program a resolver to perform response chain tasks automatically.\n * @param doResolve : Resolver callback\n */\n resolve(doResolve: (chain: ResponseChain, originalRequest: Wretcher) => ResponseChain | Promise, clear: boolean = false) {\n return this.selfFactory({ resolvers: clear ? [ doResolve ] : [ ...this._resolvers, doResolve ]})\n }\n\n /**\n * Defer wretcher methods that will be chained and called just before the request is performed.\n */\n defer(callback: DeferredCallback, clear = false) {\n return this.selfFactory({\n deferredChain: clear ? [callback] : [ ...this._deferredChain, callback ]\n })\n }\n\n /**\n * Add middlewares to intercept a request before being sent.\n */\n middlewares(middlewares: ConfiguredMiddleware[], clear = false) {\n return this.selfFactory({\n middlewares: clear ? middlewares : [ ...this._middlewares, ...middlewares ]\n })\n }\n\n private method(method, options = {}, body = null) {\n let baseWretcher =\n !body ? this :\n typeof body === \"object\" ? this.json(body) :\n this.body(body)\n baseWretcher = baseWretcher.options({ ...options, method })\n const deferredWretcher = baseWretcher._deferredChain.reduce((acc: Wretcher, curr) => curr(acc, acc._url, acc._options), baseWretcher)\n return resolver(deferredWretcher)\n }\n\n /**\n * Performs a get request.\n */\n get(options?) {\n return this.method(\"GET\", options)\n }\n /**\n * Performs a delete request.\n */\n delete(options?) {\n return this.method(\"DELETE\", options)\n }\n /**\n * Performs a put request.\n */\n put(body?, options?) {\n return this.method(\"PUT\", options, body)\n }\n /**\n * Performs a post request.\n */\n post(body?, options?) {\n return this.method(\"POST\", options, body)\n }\n /**\n * Performs a patch request.\n */\n patch(body?, options?) {\n return this.method(\"PATCH\", options, body)\n }\n /**\n * Performs a head request.\n */\n head(options?) {\n return this.method(\"HEAD\", options)\n }\n /**\n * Performs an options request\n */\n opts(options?) {\n return this.method(\"OPTIONS\", options)\n }\n /**\n * Replay a request.\n */\n replay(options?) {\n return this.method(this._options.method, options)\n }\n\n /**\n * Sets the request body with any content.\n * @param contents The body contents\n */\n body(contents: any) {\n return this.selfFactory({ options: { ...this._options, body: contents }})\n }\n /**\n * Sets the content type header, stringifies an object and sets the request body.\n * @param jsObject An object which will be serialized into a JSON\n */\n json(jsObject: object) {\n return this.content(\"application/json\").body(JSON.stringify(jsObject))\n }\n /**\n * Converts the javascript object to a FormData and sets the request body.\n * @param formObject An object which will be converted to a FormData\n */\n formData(formObject: object) {\n return this.body(convertFormData(formObject))\n }\n /**\n * Converts the input to an url encoded string and sets the content-type header and body.\n * If the input argument is already a string, skips the conversion part.\n *\n * @param input An object to convert into an url encoded string or an already encoded string\n */\n formUrl(input: (object | string)) {\n return this\n .body(typeof input === \"string\" ? input : convertFormUrl(input))\n .content(\"application/x-www-form-urlencoded\")\n }\n}\n\n// Internal helpers\n\nconst appendQueryParams = (url: string, qp: object | string, replace: boolean) => {\n let queryString\n\n if(typeof qp === \"string\") {\n queryString = qp\n } else {\n const usp = conf.polyfill(\"URLSearchParams\", { instance: true })\n for(const key in qp) {\n if(qp[key] instanceof Array) {\n for(const val of qp[key])\n usp.append(key, val)\n } else {\n usp.append(key, qp[key])\n }\n }\n queryString = usp.toString()\n }\n\n const split = url.split(\"?\")\n if(replace || split.length < 2)\n return split[0] + \"?\" + queryString\n\n return url + \"&\" + queryString\n}\n\nfunction convertFormData(formObject: object) {\n const formData = conf.polyfill(\"FormData\", { instance: true })\n for(const key in formObject) {\n if(formObject[key] instanceof Array) {\n for(const item of formObject[key])\n formData.append(key + \"[]\", item)\n } else {\n formData.append(key, formObject[key])\n }\n }\n\n return formData\n}\n\nfunction encodeQueryValue(key: string, value: unknown) {\n return encodeURIComponent(key) +\n \"=\" +\n encodeURIComponent(\n typeof value === \"object\" ?\n JSON.stringify(value) :\n \"\" + value\n )\n}\nfunction convertFormUrl(formObject: object) {\n return Object.keys(formObject)\n .map(key => {\n const value = formObject[key]\n if(value instanceof Array) {\n return value.map(v => encodeQueryValue(key, v)).join(\"&\")\n }\n return encodeQueryValue(key, value)\n })\n .join(\"&\")\n}\n","import { Wretcher } from \"./wretcher\"\nimport { mix } from \"./mix\"\nimport conf from \"./config\"\nimport perfs from \"./perfs\"\nimport { middlewareHelper } from \"./middleware\"\n\nexport type WretcherError = Error & { status: number, response: WretcherResponse, text?: string, json?: any }\nexport type WretcherErrorCallback = (error: WretcherError, originalRequest: Wretcher) => any\nexport type WretcherResponse = Response & { [key: string]: any }\nexport type ResponseChain = {\n // Response types\n res: (cb?: (type: WretcherResponse) => Result) => Promise,\n json: (cb?: (type: {[key: string]: any}) => Result) => Promise,\n blob: (cb?: (type: Blob) => Result) => Promise,\n formData: (cb?: (type: FormData) => Result) => Promise,\n arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise,\n text: (cb?: (type: string) => Result) => Promise,\n // Extras\n perfs: (cb?: (timing: any) => void) => ResponseChain,\n setTimeout: (time: number, controller?: AbortController) => ResponseChain,\n controller: () => [any, ResponseChain],\n // Catchers\n error: (code: (number | string), cb: WretcherErrorCallback) => ResponseChain,\n badRequest: (cb: WretcherErrorCallback) => ResponseChain,\n unauthorized: (cb: WretcherErrorCallback) => ResponseChain,\n forbidden: (cb: WretcherErrorCallback) => ResponseChain,\n notFound: (cb: WretcherErrorCallback) => ResponseChain,\n timeout: (cb: WretcherErrorCallback) => ResponseChain,\n internalError: (cb: WretcherErrorCallback) => ResponseChain,\n onAbort: (cb: WretcherErrorCallback) => ResponseChain\n}\n\nexport const resolver = (wretcher: Wretcher) => {\n const {\n _url: url,\n _catchers: _catchers,\n _resolvers: resolvers,\n _middlewares: middlewares,\n _options: opts\n } = wretcher\n const catchers = new Map(_catchers)\n const finalOptions = mix(conf.defaults, opts)\n const fetchController = conf.polyfill(\"AbortController\", { doThrow: false, instance: true })\n if(!finalOptions[\"signal\"] && fetchController) {\n finalOptions[\"signal\"] = fetchController.signal\n }\n // Request timeout\n const timeout = {\n ref: null,\n clear() {\n if(timeout.ref) {\n clearTimeout(timeout.ref)\n timeout.ref = null\n }\n }\n }\n // The generated fetch request\n const fetchRequest = middlewareHelper(middlewares)(conf.polyfill(\"fetch\"))(url, finalOptions)\n // Throws on an http error\n const throwingPromise: Promise = fetchRequest.then(response => {\n timeout.clear()\n if (!response.ok) {\n return response[conf.errorType || \"text\"]().then(msg => {\n // Enhances the error object\n const err = new Error(msg)\n err[conf.errorType || \"text\"] = msg\n err[\"status\"] = response.status\n err[\"response\"] = response\n throw err\n })\n }\n return response\n })\n // Wraps the Promise in order to dispatch the error to a matching catcher\n const catchersWrapper = (promise: Promise): Promise => {\n return promise.catch(err => {\n timeout.clear()\n if(catchers.has(err.status))\n return catchers.get(err.status)(err, wretcher)\n else if(catchers.has(err.name))\n return catchers.get(err.name)(err, wretcher)\n else\n throw err\n })\n }\n // Enforces the proper promise type when a body parsing method is called.\n type BodyParser = (funName: string | null) => (cb?: (type: Type) => Result) => Promise\n const bodyParser: BodyParser = (funName) => (cb) => funName ?\n // If a callback is provided, then callback with the body result otherwise return the parsed body itself.\n catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) :\n // No body parsing method - return the response\n catchersWrapper(throwingPromise.then(_ => cb ? cb(_) : _))\n\n const responseChain: ResponseChain = {\n /**\n * Retrieves the raw result as a promise.\n */\n res: bodyParser(null),\n /**\n * Retrieves the result as a parsed JSON object.\n */\n json: bodyParser(\"json\"),\n /**\n * Retrieves the result as a Blob object.\n */\n blob: bodyParser(\"blob\"),\n /**\n * Retrieves the result as a FormData object.\n */\n formData: bodyParser(\"formData\"),\n /**\n * Retrieves the result as an ArrayBuffer object.\n */\n arrayBuffer: bodyParser(\"arrayBuffer\"),\n /**\n * Retrieves the result as a string.\n */\n text: bodyParser(\"text\"),\n /**\n * Performs a callback on the API performance timings of the request.\n *\n * Warning: Still experimental on browsers and node.js\n */\n perfs: cb => {\n fetchRequest.then(res => perfs.observe(res.url, cb))\n return responseChain\n },\n /**\n * Aborts the request after a fixed time.\n *\n * @param time Time in milliseconds\n * @param controller A custom controller\n */\n setTimeout: (time, controller = fetchController) => {\n timeout.clear()\n timeout.ref = setTimeout(() => controller.abort(), time)\n return responseChain\n },\n /**\n * Returns the automatically generated AbortController alongside the current wretch response as a pair.\n */\n controller: () => [ fetchController, responseChain ],\n /**\n * Catches an http response with a specific error code or name and performs a callback.\n */\n error(errorId, cb) {\n catchers.set(errorId, cb)\n return responseChain\n },\n /**\n * Catches a bad request (http code 400) and performs a callback.\n */\n badRequest: cb => responseChain.error(400, cb),\n /**\n * Catches an unauthorized request (http code 401) and performs a callback.\n */\n unauthorized: cb => responseChain.error(401, cb),\n /**\n * Catches a forbidden request (http code 403) and performs a callback.\n */\n forbidden: cb => responseChain.error(403, cb),\n /**\n * Catches a \"not found\" request (http code 404) and performs a callback.\n */\n notFound: cb => responseChain.error(404, cb),\n /**\n * Catches a timeout (http code 408) and performs a callback.\n */\n timeout: cb => responseChain.error(408, cb),\n /**\n * Catches an internal server error (http code 500) and performs a callback.\n */\n internalError: cb => responseChain.error(500, cb),\n /**\n * Catches an AbortError and performs a callback.\n */\n onAbort: cb => responseChain.error(\"AbortError\", cb)\n }\n\n return resolvers.reduce((chain, r) => r(chain, wretcher), responseChain) as (ResponseChain & Promise)\n}\n","import { WretcherOptions } from \"./wretcher\"\nimport { WretcherResponse } from \"./resolver\"\n\nexport type Middleware = (options?: {[key: string]: any}) => ConfiguredMiddleware\nexport type ConfiguredMiddleware = (next: FetchLike) => FetchLike\nexport type FetchLike = (url: string, opts: WretcherOptions) => Promise\n\nexport const middlewareHelper = (middlewares: ConfiguredMiddleware[]) => (fetchFunction: FetchLike): FetchLike => {\n return (\n middlewares.length === 0 ?\n fetchFunction :\n middlewares.length === 1 ?\n middlewares[0](fetchFunction) :\n middlewares.reduceRight((acc, curr, idx): any =>\n (idx === middlewares.length - 2) ? curr(acc(fetchFunction)) : curr(acc as any)\n )\n ) as FetchLike\n}\n","import { Wretcher } from \"./wretcher\"\n\nconst factory = Wretcher.factory\nfactory[\"default\"] = Wretcher.factory\n\n/**\n * Return a fresh Wretcher instance.\n */\nexport default factory\n"],"names":["mix","one","two","mergeArrays","clone","prop","hasOwnProperty","Array","config","defaults","errorType","polyfills","fetch","FormData","URLSearchParams","performance","PerformanceObserver","AbortController","polyfill","p","_a","_b","_c","doThrow","_d","instance","_i","args","res","this","self","global","Error","onMatch","entries","name","callback","_performance","getEntriesByName","matches","length","reverse","clearMeasures","perfs","callbacks","delete","size","observer","disconnect","clearResourceTimings","Map","observe","conf","_observer","forEach","lazyObserver","entryTypes","set","_url","_options","_catchers","_resolvers","_middlewares","_deferredChain","Wretcher","url","options","_e","catchers","_f","resolvers","_g","middlewares","_h","deferredChain","mixin","method","replace","selfFactory","split","qp","appendQueryParams","headerValues","headers","headerValue","Accept","Content-Type","Authorization","errorId","catcher","newMap","controller","signal","doResolve","clear","body","baseWretcher","json","wretcher","opts","finalOptions","fetchController","timeout","ref","clearTimeout","fetchRequest","fetchFunction","reduceRight","acc","curr","idx","middlewareHelper","throwingPromise","then","response","ok","msg","err","status","catchersWrapper","promise","catch","has","get","bodyParser","funName","cb","_","responseChain","blob","formData","arrayBuffer","text","setTimeout","time","abort","error","badRequest","unauthorized","forbidden","notFound","internalError","onAbort","reduce","chain","r","resolver","contents","jsObject","content","JSON","stringify","formObject","key","item","append","convertFormData","input","Object","keys","map","value","v","encodeQueryValue","join","queryString","usp","val","toString","encodeURIComponent","factory"],"mappings":"ukBAAO,IAAMA,EAAM,SAAUC,EAAaC,EAAaC,GACnD,gBADmDA,OAC/CF,IAAQC,GAAsB,iBAARD,GAAmC,iBAARC,EACjD,OAAOD,EAEX,IAAMG,OAAaH,GACnB,IAAI,IAAMI,KAAQH,EACXA,EAAII,eAAeD,KACfH,EAAIG,aAAiBE,OAASN,EAAII,aAAiBE,MAClDH,EAAMC,GAAQF,IAAmBF,EAAII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ts","../../src/perfs.ts","../../src/resolver.ts","../../src/wretcher.ts","../../src/middleware.ts","../../src/index.umd.ts"],"sourcesContent":["export const mix = function (one: object, two: object, mergeArrays: boolean = false) {\n if(!one || !two || typeof one !== \"object\" || typeof two !== \"object\")\n return one\n\n const clone = { ...one }\n for(const prop in two) {\n if(two.hasOwnProperty(prop)) {\n if(two[prop] instanceof Array && one[prop] instanceof Array) {\n clone[prop] = mergeArrays ? [ ...one[prop], ...two[prop] ] : two[prop]\n } else if(typeof two[prop] === \"object\" && typeof one[prop] === \"object\") {\n clone[prop] = mix(one[prop], two[prop], mergeArrays)\n } else {\n clone[prop] = two[prop]\n }\n }\n }\n\n return clone\n}\n","declare const global\n\nconst config = {\n // Default options\n defaults: {},\n // Error type\n errorType: null,\n // Polyfills\n polyfills: {\n fetch: null,\n FormData: null,\n URLSearchParams: null,\n performance: null,\n PerformanceObserver: null,\n AbortController: null\n },\n polyfill(p: string, { doThrow = true, instance = false } = {}, ...args) {\n const res = this.polyfills[p] ||\n (typeof self !== \"undefined\" ? self[p] : null) ||\n (typeof global !== \"undefined\" ? global[p] : null)\n if(doThrow && !res) throw new Error(p + \" is not defined\")\n return instance && res ? new res(...args) : res\n }\n}\n\nexport default config\n","import conf from \"./config\"\n\nconst onMatch = (entries, name, callback, _performance) => {\n if(!entries.getEntriesByName)\n return false\n const matches = entries.getEntriesByName(name)\n if(matches && matches.length > 0) {\n callback(matches.reverse()[0])\n if(_performance.clearMeasures)\n _performance.clearMeasures(name)\n perfs.callbacks.delete(name)\n\n if(perfs.callbacks.size < 1) {\n perfs.observer.disconnect()\n if(_performance.clearResourceTimings) {\n _performance.clearResourceTimings()\n }\n }\n return true\n }\n return false\n}\n\nconst lazyObserver = (_performance, _observer) => {\n if(!perfs.observer && _performance && _observer) {\n perfs.observer = new _observer(entries => {\n perfs.callbacks.forEach((callback, name) => {\n onMatch(entries, name, callback, _performance)\n })\n })\n if(_performance.clearResourceTimings)\n _performance.clearResourceTimings()\n }\n return perfs.observer\n}\n\nconst perfs = {\n callbacks: new Map(),\n observer: null,\n observe: (name, callback) => {\n if(!name || !callback)\n return\n\n const _performance = conf.polyfill(\"performance\", { doThrow: false })\n const _observer = conf.polyfill(\"PerformanceObserver\", { doThrow: false })\n\n if(!lazyObserver(_performance, _observer))\n return\n\n if(!onMatch(_performance, name, callback, _performance)) {\n if(perfs.callbacks.size < 1)\n perfs.observer.observe({ entryTypes: [\"resource\", \"measure\"] })\n perfs.callbacks.set(name, callback)\n }\n\n }\n}\n\nexport default perfs\n","import { Wretcher } from \"./wretcher\"\nimport { mix } from \"./mix\"\nimport conf from \"./config\"\nimport perfs from \"./perfs\"\nimport { middlewareHelper } from \"./middleware\"\n\nexport type WretcherError = Error & { status: number, response: WretcherResponse, text?: string, json?: any }\nexport type WretcherErrorCallback = (error: WretcherError, originalRequest: Wretcher) => any\nexport type WretcherResponse = Response & { [key: string]: any }\nexport type ResponseChain = {\n // Response types\n res: (cb?: (type: WretcherResponse) => Result) => Promise,\n json: (cb?: (type: {[key: string]: any}) => Result) => Promise,\n blob: (cb?: (type: Blob) => Result) => Promise,\n formData: (cb?: (type: FormData) => Result) => Promise,\n arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise,\n text: (cb?: (type: string) => Result) => Promise,\n // Extras\n perfs: (cb?: (timing: any) => void) => ResponseChain,\n setTimeout: (time: number, controller?: AbortController) => ResponseChain,\n controller: () => [any, ResponseChain],\n // Catchers\n error: (code: (number | string), cb: WretcherErrorCallback) => ResponseChain,\n badRequest: (cb: WretcherErrorCallback) => ResponseChain,\n unauthorized: (cb: WretcherErrorCallback) => ResponseChain,\n forbidden: (cb: WretcherErrorCallback) => ResponseChain,\n notFound: (cb: WretcherErrorCallback) => ResponseChain,\n timeout: (cb: WretcherErrorCallback) => ResponseChain,\n internalError: (cb: WretcherErrorCallback) => ResponseChain,\n fetchError: (cb: WretcherErrorCallback) => ResponseChain,\n onAbort: (cb: WretcherErrorCallback) => ResponseChain\n}\n\nclass WretchErrorWrapper {\n constructor(public error: any) {}\n}\n\nexport const resolver = (wretcher: Wretcher) => {\n const {\n _url: url,\n _catchers: _catchers,\n _resolvers: resolvers,\n _middlewares: middlewares,\n _options: opts\n } = wretcher\n const catchers = new Map(_catchers)\n const finalOptions = mix(conf.defaults, opts)\n const fetchController = conf.polyfill(\"AbortController\", { doThrow: false, instance: true })\n if(!finalOptions[\"signal\"] && fetchController) {\n finalOptions[\"signal\"] = fetchController.signal\n }\n // Request timeout\n const timeout = {\n ref: null,\n clear() {\n if(timeout.ref) {\n clearTimeout(timeout.ref)\n timeout.ref = null\n }\n }\n }\n // The generated fetch request\n const fetchRequest = middlewareHelper(middlewares)(conf.polyfill(\"fetch\"))(url, finalOptions)\n // Throws on an http error\n const throwingPromise: Promise = fetchRequest\n .catch(error => {\n throw new WretchErrorWrapper(error)\n })\n .then(response => {\n timeout.clear()\n if (!response.ok) {\n return response[conf.errorType || \"text\"]().then(msg => {\n // Enhances the error object\n const err = new Error(msg)\n err[conf.errorType || \"text\"] = msg\n err[\"status\"] = response.status\n err[\"response\"] = response\n throw err\n })\n }\n return response\n })\n // Wraps the Promise in order to dispatch the error to a matching catcher\n const catchersWrapper = (promise: Promise): Promise => {\n return promise.catch(err => {\n timeout.clear()\n const error = err instanceof WretchErrorWrapper ? err.error : err\n if(err instanceof WretchErrorWrapper && catchers.has(\"__fromFetch\"))\n return catchers.get(\"__fromFetch\")(error, wretcher)\n else if(catchers.has(error.status))\n return catchers.get(error.status)(error, wretcher)\n else if(catchers.has(error.name))\n return catchers.get(error.name)(error, wretcher)\n else\n throw error\n })\n }\n // Enforces the proper promise type when a body parsing method is called.\n type BodyParser = (funName: string | null) => (cb?: (type: Type) => Result) => Promise\n const bodyParser: BodyParser = (funName) => (cb) => funName ?\n // If a callback is provided, then callback with the body result otherwise return the parsed body itself.\n catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) :\n // No body parsing method - return the response\n catchersWrapper(throwingPromise.then(_ => cb ? cb(_) : _))\n\n const responseChain: ResponseChain = {\n /**\n * Retrieves the raw result as a promise.\n */\n res: bodyParser(null),\n /**\n * Retrieves the result as a parsed JSON object.\n */\n json: bodyParser(\"json\"),\n /**\n * Retrieves the result as a Blob object.\n */\n blob: bodyParser(\"blob\"),\n /**\n * Retrieves the result as a FormData object.\n */\n formData: bodyParser(\"formData\"),\n /**\n * Retrieves the result as an ArrayBuffer object.\n */\n arrayBuffer: bodyParser(\"arrayBuffer\"),\n /**\n * Retrieves the result as a string.\n */\n text: bodyParser(\"text\"),\n /**\n * Performs a callback on the API performance timings of the request.\n *\n * Warning: Still experimental on browsers and node.js\n */\n perfs: cb => {\n fetchRequest.then(res => perfs.observe(res.url, cb))\n return responseChain\n },\n /**\n * Aborts the request after a fixed time.\n *\n * @param time Time in milliseconds\n * @param controller A custom controller\n */\n setTimeout: (time, controller = fetchController) => {\n timeout.clear()\n timeout.ref = setTimeout(() => controller.abort(), time)\n return responseChain\n },\n /**\n * Returns the automatically generated AbortController alongside the current wretch response as a pair.\n */\n controller: () => [ fetchController, responseChain ],\n /**\n * Catches an http response with a specific error code or name and performs a callback.\n */\n error(errorId, cb) {\n catchers.set(errorId, cb)\n return responseChain\n },\n /**\n * Catches a bad request (http code 400) and performs a callback.\n */\n badRequest: cb => responseChain.error(400, cb),\n /**\n * Catches an unauthorized request (http code 401) and performs a callback.\n */\n unauthorized: cb => responseChain.error(401, cb),\n /**\n * Catches a forbidden request (http code 403) and performs a callback.\n */\n forbidden: cb => responseChain.error(403, cb),\n /**\n * Catches a \"not found\" request (http code 404) and performs a callback.\n */\n notFound: cb => responseChain.error(404, cb),\n /**\n * Catches a timeout (http code 408) and performs a callback.\n */\n timeout: cb => responseChain.error(408, cb),\n /**\n * Catches an internal server error (http code 500) and performs a callback.\n */\n internalError: cb => responseChain.error(500, cb),\n /**\n * Catches errors thrown when calling the fetch function and performs a callback.\n */\n fetchError: cb => responseChain.error(\"__fromFetch\", cb),\n /**\n * Catches an AbortError and performs a callback.\n */\n onAbort: cb => responseChain.error(\"AbortError\", cb)\n }\n\n return resolvers.reduce((chain, r) => r(chain, wretcher), responseChain) as (ResponseChain & Promise)\n}\n","import { mix } from \"./mix\"\nimport conf from \"./config\"\nimport { resolver, WretcherError, ResponseChain } from \"./resolver\"\nimport { ConfiguredMiddleware } from \"./middleware\"\n\nexport type WretcherOptions = RequestInit & {\n [key: string]: any\n}\n\nexport type DeferredCallback = (wretcher: Wretcher, url: string, options: WretcherOptions) => Wretcher\n\n/**\n * The Wretcher class used to perform easy fetch requests.\n *\n * Immutability : almost every method of this class return a fresh Wretcher object.\n */\nexport class Wretcher {\n\n protected constructor(\n public _url: string,\n public _options: WretcherOptions,\n public _catchers: Map void> = new Map(),\n public _resolvers: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any> = [],\n public _middlewares: ConfiguredMiddleware[] = [],\n public _deferredChain: DeferredCallback[] = []) {}\n\n static factory(url = \"\", options: WretcherOptions = {}) { return new Wretcher(url, options) }\n private selfFactory({ url = this._url, options = this._options, catchers = this._catchers,\n resolvers = this._resolvers, middlewares = this._middlewares, deferredChain = this._deferredChain } = {}) {\n return new Wretcher(url, {...options}, new Map(catchers), [...resolvers], [...middlewares], [...deferredChain])\n }\n\n /**\n * Sets the default fetch options used for every subsequent fetch call.\n * @param options New default options\n * @param mixin If true, mixes in instead of replacing the existing options\n */\n defaults(options: WretcherOptions, mixin = false) {\n conf.defaults = mixin ? mix(conf.defaults, options) : options\n return this\n }\n\n /**\n * Sets the method (text, json ...) used to parse the data contained in the response body in case of an HTTP error.\n *\n * Persists for every subsequent requests.\n *\n * Default is \"text\".\n */\n errorType(method: \"text\" | \"json\") {\n conf.errorType = method\n return this\n }\n\n /**\n * Sets the non-global polyfills which will be used for every subsequent calls.\n *\n * Needed for libraries like [fetch-ponyfill](https://github.com/qubyte/fetch-ponyfill).\n *\n * @param polyfills An object containing the polyfills.\n */\n polyfills(polyfills: Partial) {\n conf.polyfills = { ...conf.polyfills, ...polyfills }\n return this\n }\n\n /**\n * Returns a new Wretcher object with the argument url appended and the same options.\n * @param url String url\n * @param replace Boolean If true, replaces the current url instead of appending\n */\n url(url: string, replace = false) {\n if(replace)\n return this.selfFactory({ url })\n const split = this._url.split(\"?\")\n return this.selfFactory({\n url: split.length > 1 ?\n split[0] + url + \"?\" + split[1] :\n this._url + url\n })\n }\n\n /**\n * Returns a new Wretcher object with the same url and new options.\n * @param options New options\n * @param mixin If true, mixes in instead of replacing the existing options\n */\n options(options: WretcherOptions, mixin = true) {\n return this.selfFactory({ options: mixin ? mix(this._options, options) : options })\n }\n\n /**\n * Converts a javascript object to query parameters,\n * then appends this query string to the current url.\n *\n * If given a string, use the string as the query verbatim.\n *\n * ```\n * let w = wretch(\"http://example.com\") // url is http://example.com\n *\n * // Chain query calls\n * w = w.query({ a: 1, b : 2 }) // url is now http://example.com?a=1&b=2\n * w = w.query(\"foo-bar-baz-woz\") // url is now http://example.com?a=1&b=2&foo-bar-baz-woz\n *\n * // Pass true as the second argument to replace existing query parameters\n * w = w.query(\"c=3&d=4\", true) // url is now http://example.com?c=3&d=4\n * ```\n *\n * @param qp An object which will be converted, or a string which will be used verbatim.\n */\n query(qp: object | string, replace: boolean = false) {\n return this.selfFactory({ url: appendQueryParams(this._url, qp, replace) })\n }\n\n /**\n * Set request headers.\n * @param headerValues An object containing header keys and values\n */\n headers(headerValues: { [headerName: string]: string }) {\n return this.selfFactory({ options: mix(this._options, { headers: headerValues || {} }) })\n }\n\n /**\n * Shortcut to set the \"Accept\" header.\n * @param headerValue Header value\n */\n accept(headerValue: string) {\n return this.headers({ Accept : headerValue })\n }\n\n /**\n * Shortcut to set the \"Content-Type\" header.\n * @param headerValue Header value\n */\n content(headerValue: string) {\n return this.headers({ \"Content-Type\" : headerValue })\n }\n\n /**\n * Shortcut to set the \"Authorization\" header.\n * @param headerValue Header value\n */\n auth(headerValue: string) {\n return this.headers({ Authorization: headerValue })\n }\n\n /**\n * Adds a default catcher which will be called on every subsequent request error when the error code matches.\n * @param errorId Error code or name\n * @param catcher: The catcher method\n */\n catcher(errorId: number | string, catcher: (error: WretcherError, originalRequest: Wretcher) => any) {\n const newMap = new Map(this._catchers)\n newMap.set(errorId, catcher)\n return this.selfFactory({ catchers: newMap })\n }\n\n /**\n * Associates a custom signal with the request.\n * @param controller : An AbortController\n */\n signal(controller: AbortController) {\n return this.selfFactory({ options: { ...this._options, signal: controller.signal }})\n }\n\n /**\n * Program a resolver to perform response chain tasks automatically.\n * @param doResolve : Resolver callback\n */\n resolve(doResolve: (chain: ResponseChain, originalRequest: Wretcher) => ResponseChain | Promise, clear: boolean = false) {\n return this.selfFactory({ resolvers: clear ? [ doResolve ] : [ ...this._resolvers, doResolve ]})\n }\n\n /**\n * Defer wretcher methods that will be chained and called just before the request is performed.\n */\n defer(callback: DeferredCallback, clear = false) {\n return this.selfFactory({\n deferredChain: clear ? [callback] : [ ...this._deferredChain, callback ]\n })\n }\n\n /**\n * Add middlewares to intercept a request before being sent.\n */\n middlewares(middlewares: ConfiguredMiddleware[], clear = false) {\n return this.selfFactory({\n middlewares: clear ? middlewares : [ ...this._middlewares, ...middlewares ]\n })\n }\n\n private method(method, options = {}, body = null) {\n let baseWretcher =\n !body ? this :\n typeof body === \"object\" ? this.json(body) :\n this.body(body)\n baseWretcher = baseWretcher.options({ ...options, method })\n const deferredWretcher = baseWretcher._deferredChain.reduce((acc: Wretcher, curr) => curr(acc, acc._url, acc._options), baseWretcher)\n return resolver(deferredWretcher)\n }\n\n /**\n * Performs a get request.\n */\n get(options?) {\n return this.method(\"GET\", options)\n }\n /**\n * Performs a delete request.\n */\n delete(options?) {\n return this.method(\"DELETE\", options)\n }\n /**\n * Performs a put request.\n */\n put(body?, options?) {\n return this.method(\"PUT\", options, body)\n }\n /**\n * Performs a post request.\n */\n post(body?, options?) {\n return this.method(\"POST\", options, body)\n }\n /**\n * Performs a patch request.\n */\n patch(body?, options?) {\n return this.method(\"PATCH\", options, body)\n }\n /**\n * Performs a head request.\n */\n head(options?) {\n return this.method(\"HEAD\", options)\n }\n /**\n * Performs an options request\n */\n opts(options?) {\n return this.method(\"OPTIONS\", options)\n }\n /**\n * Replay a request.\n */\n replay(options?) {\n return this.method(this._options.method, options)\n }\n\n /**\n * Sets the request body with any content.\n * @param contents The body contents\n */\n body(contents: any) {\n return this.selfFactory({ options: { ...this._options, body: contents }})\n }\n /**\n * Sets the content type header, stringifies an object and sets the request body.\n * @param jsObject An object which will be serialized into a JSON\n */\n json(jsObject: object) {\n return this.content(\"application/json\").body(JSON.stringify(jsObject))\n }\n /**\n * Converts the javascript object to a FormData and sets the request body.\n * @param formObject An object which will be converted to a FormData\n */\n formData(formObject: object) {\n return this.body(convertFormData(formObject))\n }\n /**\n * Converts the input to an url encoded string and sets the content-type header and body.\n * If the input argument is already a string, skips the conversion part.\n *\n * @param input An object to convert into an url encoded string or an already encoded string\n */\n formUrl(input: (object | string)) {\n return this\n .body(typeof input === \"string\" ? input : convertFormUrl(input))\n .content(\"application/x-www-form-urlencoded\")\n }\n}\n\n// Internal helpers\n\nconst appendQueryParams = (url: string, qp: object | string, replace: boolean) => {\n let queryString\n\n if(typeof qp === \"string\") {\n queryString = qp\n } else {\n const usp = conf.polyfill(\"URLSearchParams\", { instance: true })\n for(const key in qp) {\n if(qp[key] instanceof Array) {\n for(const val of qp[key])\n usp.append(key, val)\n } else {\n usp.append(key, qp[key])\n }\n }\n queryString = usp.toString()\n }\n\n const split = url.split(\"?\")\n if(replace || split.length < 2)\n return split[0] + \"?\" + queryString\n\n return url + \"&\" + queryString\n}\n\nfunction convertFormData(formObject: object) {\n const formData = conf.polyfill(\"FormData\", { instance: true })\n for(const key in formObject) {\n if(formObject[key] instanceof Array) {\n for(const item of formObject[key])\n formData.append(key + \"[]\", item)\n } else {\n formData.append(key, formObject[key])\n }\n }\n\n return formData\n}\n\nfunction encodeQueryValue(key: string, value: unknown) {\n return encodeURIComponent(key) +\n \"=\" +\n encodeURIComponent(\n typeof value === \"object\" ?\n JSON.stringify(value) :\n \"\" + value\n )\n}\nfunction convertFormUrl(formObject: object) {\n return Object.keys(formObject)\n .map(key => {\n const value = formObject[key]\n if(value instanceof Array) {\n return value.map(v => encodeQueryValue(key, v)).join(\"&\")\n }\n return encodeQueryValue(key, value)\n })\n .join(\"&\")\n}\n","import { WretcherOptions } from \"./wretcher\"\nimport { WretcherResponse } from \"./resolver\"\n\nexport type Middleware = (options?: {[key: string]: any}) => ConfiguredMiddleware\nexport type ConfiguredMiddleware = (next: FetchLike) => FetchLike\nexport type FetchLike = (url: string, opts: WretcherOptions) => Promise\n\nexport const middlewareHelper = (middlewares: ConfiguredMiddleware[]) => (fetchFunction: FetchLike): FetchLike => {\n return (\n middlewares.length === 0 ?\n fetchFunction :\n middlewares.length === 1 ?\n middlewares[0](fetchFunction) :\n middlewares.reduceRight((acc, curr, idx): any =>\n (idx === middlewares.length - 2) ? curr(acc(fetchFunction)) : curr(acc as any)\n )\n ) as FetchLike\n}\n","import { Wretcher } from \"./wretcher\"\n\nconst factory = Wretcher.factory\nfactory[\"default\"] = Wretcher.factory\n\n/**\n * Return a fresh Wretcher instance.\n */\nexport default factory\n"],"names":["mix","one","two","mergeArrays","clone","prop","hasOwnProperty","Array","config","defaults","errorType","polyfills","fetch","FormData","URLSearchParams","performance","PerformanceObserver","AbortController","polyfill","p","_a","_b","_c","doThrow","_d","instance","_i","args","res","this","self","global","Error","onMatch","entries","name","callback","_performance","getEntriesByName","matches","length","reverse","clearMeasures","perfs","callbacks","delete","size","observer","disconnect","clearResourceTimings","Map","observe","conf","_observer","forEach","lazyObserver","entryTypes","set","error","_url","_options","_catchers","_resolvers","_middlewares","_deferredChain","Wretcher","url","options","_e","catchers","_f","resolvers","_g","middlewares","_h","deferredChain","mixin","method","replace","selfFactory","split","qp","appendQueryParams","headerValues","headers","headerValue","Accept","Content-Type","Authorization","errorId","catcher","newMap","controller","signal","doResolve","clear","body","baseWretcher","json","wretcher","opts","finalOptions","fetchController","timeout","ref","clearTimeout","fetchRequest","fetchFunction","reduceRight","acc","curr","idx","middlewareHelper","throwingPromise","catch","WretchErrorWrapper","then","response","ok","msg","err","status","catchersWrapper","promise","has","get","bodyParser","funName","cb","_","responseChain","blob","formData","arrayBuffer","text","setTimeout","time","abort","badRequest","unauthorized","forbidden","notFound","internalError","fetchError","onAbort","reduce","chain","r","resolver","contents","jsObject","content","JSON","stringify","formObject","key","item","append","convertFormData","input","Object","keys","map","value","v","encodeQueryValue","join","queryString","usp","val","toString","encodeURIComponent","factory"],"mappings":"ukBAAO,IAAMA,EAAM,SAAUC,EAAaC,EAAaC,GACnD,gBADmDA,OAC/CF,IAAQC,GAAsB,iBAARD,GAAmC,iBAARC,EACjD,OAAOD,EAEX,IAAMG,OAAaH,GACnB,IAAI,IAAMI,KAAQH,EACXA,EAAII,eAAeD,KACfH,EAAIG,aAAiBE,OAASN,EAAII,aAAiBE,MAClDH,EAAMC,GAAQF,IAAmBF,EAAII,GAAUH,EAAIG,IAAUH,EAAIG,GACtC,iBAAdH,EAAIG,IAA2C,iBAAdJ,EAAII,GAClDD,EAAMC,GAAQL,EAAIC,EAAII,GAAOH,EAAIG,GAAOF,GAExCC,EAAMC,GAAQH,EAAIG,IAK9B,OAAOD,GCfLI,EAAS,CAEXC,SAAU,GAEVC,UAAW,KAEXC,UAAW,CACPC,MAAO,KACPC,SAAU,KACVC,gBAAiB,KACjBC,YAAa,KACbC,oBAAqB,KACrBC,gBAAiB,MAErBC,SAAA,SAASC,EAAWC,WAAAC,kBAAEC,YAAAC,gBAAgBC,aAAAC,yBAAyBC,mBAAAA,IAAAC,oBAC3D,IAAMC,EAAMC,KAAKlB,UAAUQ,KACN,oBAATW,KAAuBA,KAAKX,GAAK,QACtB,oBAAXY,OAAyBA,OAAOZ,GAAK,MACjD,GAAGI,IAAYK,EAAK,MAAM,IAAII,MAAMb,EAAI,mBACxC,OAAOM,GAAYG,MAAUA,aAAAA,aAAOD,KAAQC,ICnB9CK,EAAU,SAACC,EAAS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zB,OAAGQ,aAAiB5J,MACT4J,EAAMD,KAAI,SAAAE,GAAK,OAAAC,EAAiBV,EAAKS,MAAIE,KAAK,KAElDD,EAAiBV,EAAKQ,MAEhCG,KAAK,OA/DDf,QAAQ,qCAsDrB,IAAwBG,QAhDlBxE,EAAoB,SAAChB,EAAae,EAAqBH,GACzD,IAAIyF,EAEJ,GAAiB,iBAAPtF,EACNsF,EAActF,MACX,CACH,IAAMuF,EAAMpH,EAAKlC,SAAS,kBAAmB,CAAEO,UAAU,IACzD,IAAI,IAAMkI,KAAO1E,EACb,GAAGA,EAAG0E,aAAgBpJ,MAClB,IAAiB,QAAAa,EAAA6D,EAAG0E,GAAHjI,WAAAA,KAAb,IAAM+I,OACND,EAAIX,OAAOF,EAAKc,QAEpBD,EAAIX,OAAOF,EAAK1E,EAAG0E,IAG3BY,EAAcC,EAAIE,WAGtB,IAAM1F,EAAQd,EAAIc,MAAM,KACxB,OAAGF,GAAWE,EAAMxC,OAAS,EAClBwC,EAAM,GAAK,IAAMuF,EAErBrG,EAAM,IAAMqG,GAiBvB,SAASF,EAAiBV,EAAaQ,GACnC,OAAOQ,mBAAmBhB,GAC1B,IACAgB,mBACqB,iBAAVR,EACHX,KAAKC,UAAUU,GACnB,GAAKA,GEzUb,IAAMS,EAAU3G,EAAS2G,eACzBA,EAAiB,QAAI3G,EAAS2G"} \ No newline at end of file diff --git a/dist/resolver.d.ts b/dist/resolver.d.ts index 25481c1..a73375e 100644 --- a/dist/resolver.d.ts +++ b/dist/resolver.d.ts @@ -30,6 +30,7 @@ export declare type ResponseChain = { notFound: (cb: WretcherErrorCallback) => ResponseChain; timeout: (cb: WretcherErrorCallback) => ResponseChain; internalError: (cb: WretcherErrorCallback) => ResponseChain; + fetchError: (cb: WretcherErrorCallback) => ResponseChain; onAbort: (cb: WretcherErrorCallback) => ResponseChain; }; export declare const resolver: (wretcher: Wretcher) => ResponseChain & Promise; diff --git a/dist/resolver.js b/dist/resolver.js index 7dce916..5d1079e 100644 --- a/dist/resolver.js +++ b/dist/resolver.js @@ -2,6 +2,12 @@ import { mix } from "./mix"; import conf from "./config"; import perfs from "./perfs"; import { middlewareHelper } from "./middleware"; +var WretchErrorWrapper = /** @class */ (function () { + function WretchErrorWrapper(error) { + this.error = error; + } + return WretchErrorWrapper; +}()); export var resolver = function (wretcher) { var url = wretcher._url, _catchers = wretcher._catchers, resolvers = wretcher._resolvers, middlewares = wretcher._middlewares, opts = wretcher._options; var catchers = new Map(_catchers); @@ -23,7 +29,11 @@ export var resolver = function (wretcher) { // The generated fetch request var fetchRequest = middlewareHelper(middlewares)(conf.polyfill("fetch"))(url, finalOptions); // Throws on an http error - var throwingPromise = fetchRequest.then(function (response) { + var throwingPromise = fetchRequest + .catch(function (error) { + throw new WretchErrorWrapper(error); + }) + .then(function (response) { timeout.clear(); if (!response.ok) { return response[conf.errorType || "text"]().then(function (msg) { @@ -41,12 +51,15 @@ export var resolver = function (wretcher) { var catchersWrapper = function (promise) { return promise.catch(function (err) { timeout.clear(); - if (catchers.has(err.status)) - return catchers.get(err.status)(err, wretcher); - else if (catchers.has(err.name)) - return catchers.get(err.name)(err, wretcher); + var error = err instanceof WretchErrorWrapper ? err.error : err; + if (err instanceof WretchErrorWrapper && catchers.has("__fromFetch")) + return catchers.get("__fromFetch")(error, wretcher); + else if (catchers.has(error.status)) + return catchers.get(error.status)(error, wretcher); + else if (catchers.has(error.name)) + return catchers.get(error.name)(error, wretcher); else - throw err; + throw error; }); }; var bodyParser = function (funName) { return function (cb) { return funName ? @@ -135,6 +148,10 @@ export var resolver = function (wretcher) { * Catches an internal server error (http code 500) and performs a callback. */ internalError: function (cb) { return responseChain.error(500, cb); }, + /** + * Catches errors thrown when calling the fetch function and performs a callback. + */ + fetchError: function (cb) { return responseChain.error("__fromFetch", cb); }, /** * Catches an AbortError and performs a callback. */ diff --git a/dist/resolver.js.map b/dist/resolver.js.map index 2e91cc5..ee76b8a 100644 --- a/dist/resolver.js.map +++ b/dist/resolver.js.map @@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CAAC,EAAlB,CAAkB,EAAE,aAAa,CAAmC,CAAA;AAC9G,CAAC,CAAA","sourcesContent":["import { Wretcher } from \"./wretcher\"\nimport { mix } from \"./mix\"\nimport conf from \"./config\"\nimport perfs from \"./perfs\"\nimport { middlewareHelper } from \"./middleware\"\n\nexport type WretcherError = Error & { status: number, response: WretcherResponse, text?: string, json?: any }\nexport type WretcherErrorCallback = (error: WretcherError, originalRequest: Wretcher) => any\nexport type WretcherResponse = Response & { [key: string]: any }\nexport type ResponseChain = {\n // Response types\n res: (cb?: (type: WretcherResponse) => Result) => Promise,\n json: (cb?: (type: {[key: string]: any}) => Result) => Promise,\n blob: (cb?: (type: Blob) => Result) => Promise,\n formData: (cb?: (type: FormData) => Result) => Promise,\n arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise,\n text: (cb?: (type: string) => Result) => Promise,\n // Extras\n perfs: (cb?: (timing: any) => void) => ResponseChain,\n setTimeout: (time: number, controller?: AbortController) => ResponseChain,\n controller: () => [any, ResponseChain],\n // Catchers\n error: (code: (number | string), cb: WretcherErrorCallback) => ResponseChain,\n badRequest: (cb: WretcherErrorCallback) => ResponseChain,\n unauthorized: (cb: WretcherErrorCallback) => ResponseChain,\n forbidden: (cb: WretcherErrorCallback) => ResponseChain,\n notFound: (cb: WretcherErrorCallback) => ResponseChain,\n timeout: (cb: WretcherErrorCallback) => ResponseChain,\n internalError: (cb: WretcherErrorCallback) => ResponseChain,\n onAbort: (cb: WretcherErrorCallback) => ResponseChain\n}\n\nexport const resolver = (wretcher: Wretcher) => {\n const {\n _url: url,\n _catchers: _catchers,\n _resolvers: resolvers,\n _middlewares: middlewares,\n _options: opts\n } = wretcher\n const catchers = new Map(_catchers)\n const finalOptions = mix(conf.defaults, opts)\n const fetchController = conf.polyfill(\"AbortController\", { doThrow: false, instance: true })\n if(!finalOptions[\"signal\"] && fetchController) {\n finalOptions[\"signal\"] = fetchController.signal\n }\n // Request timeout\n const timeout = {\n ref: null,\n clear() {\n if(timeout.ref) {\n clearTimeout(timeout.ref)\n timeout.ref = null\n }\n }\n }\n // The generated fetch request\n const fetchRequest = middlewareHelper(middlewares)(conf.polyfill(\"fetch\"))(url, finalOptions)\n // Throws on an http error\n const throwingPromise: Promise = fetchRequest.then(response => {\n timeout.clear()\n if (!response.ok) {\n return response[conf.errorType || \"text\"]().then(msg => {\n // Enhances the error object\n const err = new Error(msg)\n err[conf.errorType || \"text\"] = msg\n err[\"status\"] = response.status\n err[\"response\"] = response\n throw err\n })\n }\n return response\n })\n // Wraps the Promise in order to dispatch the error to a matching catcher\n const catchersWrapper = (promise: Promise): Promise => {\n return promise.catch(err => {\n timeout.clear()\n if(catchers.has(err.status))\n return catchers.get(err.status)(err, wretcher)\n else if(catchers.has(err.name))\n return catchers.get(err.name)(err, wretcher)\n else\n throw err\n })\n }\n // Enforces the proper promise type when a body parsing method is called.\n type BodyParser = (funName: string | null) => (cb?: (type: Type) => Result) => Promise\n const bodyParser: BodyParser = (funName) => (cb) => funName ?\n // If a callback is provided, then callback with the body result otherwise return the parsed body itself.\n catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) :\n // No body parsing method - return the response\n catchersWrapper(throwingPromise.then(_ => cb ? cb(_) : _))\n\n const responseChain: ResponseChain = {\n /**\n * Retrieves the raw result as a promise.\n */\n res: bodyParser(null),\n /**\n * Retrieves the result as a parsed JSON object.\n */\n json: bodyParser(\"json\"),\n /**\n * Retrieves the result as a Blob object.\n */\n blob: bodyParser(\"blob\"),\n /**\n * Retrieves the result as a FormData object.\n */\n formData: bodyParser(\"formData\"),\n /**\n * Retrieves the result as an ArrayBuffer object.\n */\n arrayBuffer: bodyParser(\"arrayBuffer\"),\n /**\n * Retrieves the result as a string.\n */\n text: bodyParser(\"text\"),\n /**\n * Performs a callback on the API performance timings of the request.\n *\n * Warning: Still experimental on browsers and node.js\n */\n perfs: cb => {\n fetchRequest.then(res => perfs.observe(res.url, cb))\n return responseChain\n },\n /**\n * Aborts the request after a fixed time.\n *\n * @param time Time in milliseconds\n * @param controller A custom controller\n */\n setTimeout: (time, controller = fetchController) => {\n timeout.clear()\n timeout.ref = setTimeout(() => controller.abort(), time)\n return responseChain\n },\n /**\n * Returns the automatically generated AbortController alongside the current wretch response as a pair.\n */\n controller: () => [ fetchController, responseChain ],\n /**\n * Catches an http response with a specific error code or name and performs a callback.\n */\n error(errorId, cb) {\n catchers.set(errorId, cb)\n return responseChain\n },\n /**\n * Catches a bad request (http code 400) and performs a callback.\n */\n badRequest: cb => responseChain.error(400, cb),\n /**\n * Catches an unauthorized request (http code 401) and performs a callback.\n */\n unauthorized: cb => responseChain.error(401, cb),\n /**\n * Catches a forbidden request (http code 403) and performs a callback.\n */\n forbidden: cb => responseChain.error(403, cb),\n /**\n * Catches a \"not found\" request (http code 404) and performs a callback.\n */\n notFound: cb => responseChain.error(404, cb),\n /**\n * Catches a timeout (http code 408) and performs a callback.\n */\n timeout: cb => responseChain.error(408, cb),\n /**\n * Catches an internal server error (http code 500) and performs a callback.\n */\n internalError: cb => responseChain.error(500, cb),\n /**\n * Catches an AbortError and performs a callback.\n */\n onAbort: cb => responseChain.error(\"AbortError\", cb)\n }\n\n return resolvers.reduce((chain, r) => r(chain, wretcher), responseChain) as (ResponseChain & Promise)\n}\n"]} \ No newline at end of file +{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAA;AAC3B,OAAO,IAAI,MAAM,UAAU,CAAA;AAC3B,OAAO,KAAK,MAAM,SAAS,CAAA;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AA6B/C;IACI,4BAAmB,KAAU;QAAV,UAAK,GAAL,KAAK,CAAK;IAAG,CAAC;IACrC,yBAAC;AAAD,CAAC,AAFD,IAEC;AAED,MAAM,CAAC,IAAM,QAAQ,GAAG,UAAC,QAAkB;IAEnC,IAAA,mBAAS,EACT,8BA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K,CAAC,YAAY,EAAE,EAAE,CAAC,EAArC,CAAqC;KACvD,CAAA;IAED,OAAO,SAAS,CAAC,MAAM,CAAC,UAAC,KAAK,EAAE,CAAC,IAAK,OAAA,CAAC,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAlB,CAAkB,EAAE,aAAa,CAAmC,CAAA;AAC9G,CAAC,CAAA","sourcesContent":["import { Wretcher } from \"./wretcher\"\nimport { mix } from \"./mix\"\nimport conf from \"./config\"\nimport perfs from \"./perfs\"\nimport { middlewareHelper } from \"./middleware\"\n\nexport type WretcherError = Error & { status: number, response: WretcherResponse, text?: string, json?: any }\nexport type WretcherErrorCallback = (error: WretcherError, originalRequest: Wretcher) => any\nexport type WretcherResponse = Response & { [key: string]: any }\nexport type ResponseChain = {\n // Response types\n res: (cb?: (type: WretcherResponse) => Result) => Promise,\n json: (cb?: (type: {[key: string]: any}) => Result) => Promise,\n blob: (cb?: (type: Blob) => Result) => Promise,\n formData: (cb?: (type: FormData) => Result) => Promise,\n arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise,\n text: (cb?: (type: string) => Result) => Promise,\n // Extras\n perfs: (cb?: (timing: any) => void) => ResponseChain,\n setTimeout: (time: number, controller?: AbortController) => ResponseChain,\n controller: () => [any, ResponseChain],\n // Catchers\n error: (code: (number | string), cb: WretcherErrorCallback) => ResponseChain,\n badRequest: (cb: WretcherErrorCallback) => ResponseChain,\n unauthorized: (cb: WretcherErrorCallback) => ResponseChain,\n forbidden: (cb: WretcherErrorCallback) => ResponseChain,\n notFound: (cb: WretcherErrorCallback) => ResponseChain,\n timeout: (cb: WretcherErrorCallback) => ResponseChain,\n internalError: (cb: WretcherErrorCallback) => ResponseChain,\n fetchError: (cb: WretcherErrorCallback) => ResponseChain,\n onAbort: (cb: WretcherErrorCallback) => ResponseChain\n}\n\nclass WretchErrorWrapper {\n constructor(public error: any) {}\n}\n\nexport const resolver = (wretcher: Wretcher) => {\n const {\n _url: url,\n _catchers: _catchers,\n _resolvers: resolvers,\n _middlewares: middlewares,\n _options: opts\n } = wretcher\n const catchers = new Map(_catchers)\n const finalOptions = mix(conf.defaults, opts)\n const fetchController = conf.polyfill(\"AbortController\", { doThrow: false, instance: true })\n if(!finalOptions[\"signal\"] && fetchController) {\n finalOptions[\"signal\"] = fetchController.signal\n }\n // Request timeout\n const timeout = {\n ref: null,\n clear() {\n if(timeout.ref) {\n clearTimeout(timeout.ref)\n timeout.ref = null\n }\n }\n }\n // The generated fetch request\n const fetchRequest = middlewareHelper(middlewares)(conf.polyfill(\"fetch\"))(url, finalOptions)\n // Throws on an http error\n const throwingPromise: Promise = fetchRequest\n .catch(error => {\n throw new WretchErrorWrapper(error)\n })\n .then(response => {\n timeout.clear()\n if (!response.ok) {\n return response[conf.errorType || \"text\"]().then(msg => {\n // Enhances the error object\n const err = new Error(msg)\n err[conf.errorType || \"text\"] = msg\n err[\"status\"] = response.status\n err[\"response\"] = response\n throw err\n })\n }\n return response\n })\n // Wraps the Promise in order to dispatch the error to a matching catcher\n const catchersWrapper = (promise: Promise): Promise => {\n return promise.catch(err => {\n timeout.clear()\n const error = err instanceof WretchErrorWrapper ? err.error : err\n if(err instanceof WretchErrorWrapper && catchers.has(\"__fromFetch\"))\n return catchers.get(\"__fromFetch\")(error, wretcher)\n else if(catchers.has(error.status))\n return catchers.get(error.status)(error, wretcher)\n else if(catchers.has(error.name))\n return catchers.get(error.name)(error, wretcher)\n else\n throw error\n })\n }\n // Enforces the proper promise type when a body parsing method is called.\n type BodyParser = (funName: string | null) => (cb?: (type: Type) => Result) => Promise\n const bodyParser: BodyParser = (funName) => (cb) => funName ?\n // If a callback is provided, then callback with the body result otherwise return the parsed body itself.\n catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) :\n // No body parsing method - return the response\n catchersWrapper(throwingPromise.then(_ => cb ? cb(_) : _))\n\n const responseChain: ResponseChain = {\n /**\n * Retrieves the raw result as a promise.\n */\n res: bodyParser(null),\n /**\n * Retrieves the result as a parsed JSON object.\n */\n json: bodyParser(\"json\"),\n /**\n * Retrieves the result as a Blob object.\n */\n blob: bodyParser(\"blob\"),\n /**\n * Retrieves the result as a FormData object.\n */\n formData: bodyParser(\"formData\"),\n /**\n * Retrieves the result as an ArrayBuffer object.\n */\n arrayBuffer: bodyParser(\"arrayBuffer\"),\n /**\n * Retrieves the result as a string.\n */\n text: bodyParser(\"text\"),\n /**\n * Performs a callback on the API performance timings of the request.\n *\n * Warning: Still experimental on browsers and node.js\n */\n perfs: cb => {\n fetchRequest.then(res => perfs.observe(res.url, cb))\n return responseChain\n },\n /**\n * Aborts the request after a fixed time.\n *\n * @param time Time in milliseconds\n * @param controller A custom controller\n */\n setTimeout: (time, controller = fetchController) => {\n timeout.clear()\n timeout.ref = setTimeout(() => controller.abort(), time)\n return responseChain\n },\n /**\n * Returns the automatically generated AbortController alongside the current wretch response as a pair.\n */\n controller: () => [ fetchController, responseChain ],\n /**\n * Catches an http response with a specific error code or name and performs a callback.\n */\n error(errorId, cb) {\n catchers.set(errorId, cb)\n return responseChain\n },\n /**\n * Catches a bad request (http code 400) and performs a callback.\n */\n badRequest: cb => responseChain.error(400, cb),\n /**\n * Catches an unauthorized request (http code 401) and performs a callback.\n */\n unauthorized: cb => responseChain.error(401, cb),\n /**\n * Catches a forbidden request (http code 403) and performs a callback.\n */\n forbidden: cb => responseChain.error(403, cb),\n /**\n * Catches a \"not found\" request (http code 404) and performs a callback.\n */\n notFound: cb => responseChain.error(404, cb),\n /**\n * Catches a timeout (http code 408) and performs a callback.\n */\n timeout: cb => responseChain.error(408, cb),\n /**\n * Catches an internal server error (http code 500) and performs a callback.\n */\n internalError: cb => responseChain.error(500, cb),\n /**\n * Catches errors thrown when calling the fetch function and performs a callback.\n */\n fetchError: cb => responseChain.error(\"__fromFetch\", cb),\n /**\n * Catches an AbortError and performs a callback.\n */\n onAbort: cb => responseChain.error(\"AbortError\", cb)\n }\n\n return resolvers.reduce((chain, r) => r(chain, wretcher), responseChain) as (ResponseChain & Promise)\n}\n"]} \ No newline at end of file diff --git a/src/resolver.ts b/src/resolver.ts index 38942eb..60addd0 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -27,9 +27,14 @@ export type ResponseChain = { notFound: (cb: WretcherErrorCallback) => ResponseChain, timeout: (cb: WretcherErrorCallback) => ResponseChain, internalError: (cb: WretcherErrorCallback) => ResponseChain, + fetchError: (cb: WretcherErrorCallback) => ResponseChain, onAbort: (cb: WretcherErrorCallback) => ResponseChain } +class WretchErrorWrapper { + constructor(public error: any) {} +} + export const resolver = (wretcher: Wretcher) => { const { _url: url, @@ -57,30 +62,37 @@ export const resolver = (wretcher: Wretcher) => { // The generated fetch request const fetchRequest = middlewareHelper(middlewares)(conf.polyfill("fetch"))(url, finalOptions) // Throws on an http error - const throwingPromise: Promise = fetchRequest.then(response => { - timeout.clear() - if (!response.ok) { - return response[conf.errorType || "text"]().then(msg => { - // Enhances the error object - const err = new Error(msg) - err[conf.errorType || "text"] = msg - err["status"] = response.status - err["response"] = response - throw err - }) - } - return response - }) + const throwingPromise: Promise = fetchRequest + .catch(error => { + throw new WretchErrorWrapper(error) + }) + .then(response => { + timeout.clear() + if (!response.ok) { + return response[conf.errorType || "text"]().then(msg => { + // Enhances the error object + const err = new Error(msg) + err[conf.errorType || "text"] = msg + err["status"] = response.status + err["response"] = response + throw err + }) + } + return response + }) // Wraps the Promise in order to dispatch the error to a matching catcher const catchersWrapper = (promise: Promise): Promise => { return promise.catch(err => { timeout.clear() - if(catchers.has(err.status)) - return catchers.get(err.status)(err, wretcher) - else if(catchers.has(err.name)) - return catchers.get(err.name)(err, wretcher) + const error = err instanceof WretchErrorWrapper ? err.error : err + if(err instanceof WretchErrorWrapper && catchers.has("__fromFetch")) + return catchers.get("__fromFetch")(error, wretcher) + else if(catchers.has(error.status)) + return catchers.get(error.status)(error, wretcher) + else if(catchers.has(error.name)) + return catchers.get(error.name)(error, wretcher) else - throw err + throw error }) } // Enforces the proper promise type when a body parsing method is called. @@ -171,6 +183,10 @@ export const resolver = (wretcher: Wretcher) => { * Catches an internal server error (http code 500) and performs a callback. */ internalError: cb => responseChain.error(500, cb), + /** + * Catches errors thrown when calling the fetch function and performs a callback. + */ + fetchError: cb => responseChain.error("__fromFetch", cb), /** * Catches an AbortError and performs a callback. */ diff --git a/test/node/wretch.spec.ts b/test/node/wretch.spec.ts index 3c95aaf..e5fff02 100644 --- a/test/node/wretch.spec.ts +++ b/test/node/wretch.spec.ts @@ -214,11 +214,30 @@ describe("Wretch", function () { .notFound(_ => check++) .error(444, _ => check++) .unauthorized(_ => check++) + .fetchError(_ => check++) .res(_ => expect(_).toBe(undefined)) expect(check).toBe(1) + + wretch().polyfills({ + fetch: () => Promise.reject("Error") + }) + + check = 0 + await wretch(`${_URL}/444`) + .get() + .notFound(_ => check++) + .error(444, _ => check++) + .unauthorized(_ => check++) + .fetchError(_ => check--) + .res(_ => expect(_).toBe(undefined)) + expect(check).toBe(-1) }) it("should set and catch errors with global catchers", async function () { + wretch().polyfills({ + fetch: fetchPolyfill() + }) + let check = 0 const w = wretch(_URL) .catcher(404, err => check++)