From 2d99a4ff7f3a47bb59a26ff3dad925f55fbea1d7 Mon Sep 17 00:00:00 2001 From: elbywan Date: Tue, 15 Oct 2019 17:47:59 +0200 Subject: [PATCH] :bug: Fix handling falsey values for body callback and payload. Fixes #58 --- dist/bundle/wretch.js | 2 +- dist/bundle/wretch.js.map | 2 +- dist/resolver.js | 4 ++-- dist/resolver.js.map | 2 +- rollup.config.js | 2 +- src/resolver.ts | 4 ++-- test/browser/spec/index.js | 5 +++++ test/mock.js | 4 ++++ test/node/wretch.spec.ts | 6 ++++++ 9 files changed, 23 insertions(+), 8 deletions(-) diff --git a/dist/bundle/wretch.js b/dist/bundle/wretch.js index 682cab7..9d633bb 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 d=function(){return(d=Object.assign||function(r){for(var t,e=1,n=arguments.length;e {\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","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 { 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":["onMatch","entries","name","callback","_performance","getEntriesByName","matches","length","reverse","clearMeasures","perfs","callbacks","delete","size","observer","disconnect","clearResourceTimings","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","Map","observe","conf","_observer","forEach","lazyObserver","entryTypes","set","Wretcher","url","options","_e","catchers","_f","resolvers","_g","middlewares","_h","deferredChain","mixin","method","replace","selfFactory","split","_url","_options","qp","appendQueryParams","headerValues","headers","headerValue","Accept","Content-Type","Authorization","errorId","catcher","newMap","_catchers","controller","signal","doResolve","clear","_resolvers","_deferredChain","_middlewares","body","baseWretcher","json","wretcher","opts","finalOptions","fetchController","catchersWrapper","promise","catch","err","timeout","has","status","get","bodyParser","funName","cb","throwingPromise","then","_","ref","clearTimeout","fetchRequest","fetchFunction","reduceRight","acc","curr","idx","middlewareHelper","response","ok","msg","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","convertFormUrl","queryString","usp","val","toString","encodeURIComponent","factory"],"mappings":"skBAEgB,SAAVA,EAAWC,EAASC,EAAMC,EAAUC,GACtC,IAAIH,EAAQI,iBACR,OAAO,EACX,IAAMC,EAAUL,EAAQI,iBAAiBH,GACzC,SAAGI,GAA4B,EAAjBA,EAAQC,UAClBJ,EAASG,EAAQE,UAAU,IACxBJ,EAAaK,eACZL,EAAaK,cAAcP,GAC/BQ,EAAMC,UAAUC,OAAOV,GAEpBQ,EAAMC,UAAUE,KAAO,IACtBH,EAAMI,SAASC,aACZX,EAAaY,sBACZZ,EAAaY,yBAGd,GClBR,IAAMC,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,EA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G0C,mBACAE,mBACAD,MALA/C,UAAA4B,EACA5B,cAAA6B,EACA7B,eAAAyC,EACAzC,gBAAA8C,EACA9C,kBAAAgD,EACAhD,oBAAA+C,EAsQf,IAAMhB,EAAoB,SAAClB,EAAaiB,EAAqBL,GACzD,IAAI+F,EAEJ,GAAiB,iBAAP1F,EACN0F,EAAc1F,MACX,CACH,IAAM2F,EAAMnH,EAAKjB,SAAS,kBAAmB,CAAEO,UAAU,IACzD,IAAI,IAAM+G,KAAO7E,EACb,GAAGA,EAAG6E,aAAgBjI,MAClB,IAAiB,QAAAa,EAAAuC,EAAG6E,GAAH9G,WAAAA,KAAb,IAAM6H,OACND,EAAIZ,OAAOF,EAAKe,QAEpBD,EAAIZ,OAAOF,EAAK7E,EAAG6E,IAG3Ba,EAAcC,EAAIE,WAGtB,IAAMhG,EAAQd,EAAIc,MAAM,KACxB,OAAGF,GAAWE,EAAMlE,OAAS,EAClBkE,EAAM,GAAK,IAAM6F,EAErB3G,EAAM,IAAM2G,GAiBvB,SAASH,EAAiBV,EAAaQ,GACnC,OAAOS,mBAAmBjB,GAC1B,IACAiB,mBACqB,iBAAVT,EACHX,KAAKC,UAAUU,GACnB,GAAKA,GGzUb,IAAMU,EAAUjH,EAASiH,eACzBA,EAAiB,QAAIjH,EAASiH"} \ No newline at end of file +{"version":3,"file":"wretch.js","sources":["../../src/perfs.ts","../../src/mix.ts","../../src/config.ts","../../src/wretcher.ts","../../src/resolver.ts","../../src/middleware.ts","../../src/index.umd.ts"],"sourcesContent":["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","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 { 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":["onMatch","entries","name","callback","_performance","getEntriesByName","matches","length","reverse","clearMeasures","perfs","callbacks","delete","size","observer","disconnect","clearResourceTimings","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","Map","observe","conf","_observer","forEach","lazyObserver","entryTypes","set","Wretcher","url","options","_e","catchers","_f","resolvers","_g","middlewares","_h","deferredChain","mixin","method","replace","selfFactory","split","_url","_options","qp","appendQueryParams","headerValues","headers","headerValue","Accept","Content-Type","Authorization","errorId","catcher","newMap","_catchers","controller","signal","doResolve","clear","_resolvers","_deferredChain","_middlewares","body","baseWretcher","json","wretcher","opts","finalOptions","fetchController","catchersWrapper","promise","catch","err","timeout","has","status","get","bodyParser","funName","cb","throwingPromise","then","_","ref","clearTimeout","fetchRequest","fetchFunction","reduceRight","acc","curr","idx","middlewareHelper","response","ok","msg","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","convertFormUrl","queryString","usp","val","toString","encodeURIComponent","factory"],"mappings":"skBAEgB,SAAVA,EAAWC,EAASC,EAAMC,EAAUC,GACtC,IAAIH,EAAQI,iBACR,OAAO,EACX,IAAMC,EAAUL,EAAQI,iBAAiBH,GACzC,SAAGI,GAA4B,EAAjBA,EAAQC,UAClBJ,EAASG,EAAQE,UAAU,IACxBJ,EAAaK,eACZL,EAAaK,cAAcP,GAC/BQ,EAAMC,UAAUC,OAAOV,GAEpBQ,EAAMC,UAAUE,KAAO,IACtBH,EAAMI,SAASC,aACZX,EAAaY,sBACZZ,EAAaY,yBAGd,GClBR,IAA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8D,EAAqBA,EAuD9C,SAAwBL,GACpB,OAAOM,OAAOC,KAAKP,GACdQ,IAAI,SAAAP,GACD,IAAMQ,EAAQT,EAAWC,GACzB,OAAGQ,aAAiBzI,MACTyI,EAAMD,IAAI,SAAAE,GAAK,OAAAC,EAAiBV,EAAKS,KAAIE,KAAK,KAElDD,EAAiBV,EAAKQ,KAEhCG,KAAK,KAhEwCC,CAAeR,IACxDR,QAAQ,yCAtQjB,WACW3E,EACAC,EACAY,EACAK,EACAE,EACAD,gBAHAN,MAAiGrC,kBACjG0C,mBACAE,mBACAD,MALA/C,UAAA4B,EACA5B,cAAA6B,EACA7B,eAAAyC,EACAzC,gBAAA8C,EACA9C,kBAAAgD,EACAhD,oBAAA+C,EAsQf,IAAMhB,EAAoB,SAAClB,EAAaiB,EAAqBL,GACzD,IAAI+F,EAEJ,GAAiB,iBAAP1F,EACN0F,EAAc1F,MACX,CACH,IAAM2F,EAAMnH,EAAKjB,SAAS,kBAAmB,CAAEO,UAAU,IACzD,IAAI,IAAM+G,KAAO7E,EACb,GAAGA,EAAG6E,aAAgBjI,MAClB,IAAiB,QAAAa,EAAAuC,EAAG6E,GAAH9G,WAAAA,KAAb,IAAM6H,OACND,EAAIZ,OAAOF,EAAKe,QAEpBD,EAAIZ,OAAOF,EAAK7E,EAAG6E,IAG3Ba,EAAcC,EAAIE,WAGtB,IAAMhG,EAAQd,EAAIc,MAAM,KACxB,OAAGF,GAAWE,EAAMlE,OAAS,EAClBkE,EAAM,GAAK,IAAM6F,EAErB3G,EAAM,IAAM2G,GAiBvB,SAASH,EAAiBV,EAAaQ,GACnC,OAAOS,mBAAmBjB,GAC1B,IACAiB,mBACqB,iBAAVT,EACHX,KAAKC,UAAUU,GACnB,GAAKA,GGzUb,IAAMU,EAAUjH,EAASiH,eACzBA,EAAiB,QAAIjH,EAASiH"} \ No newline at end of file diff --git a/dist/resolver.js b/dist/resolver.js index f4c6095..7dce916 100644 --- a/dist/resolver.js +++ b/dist/resolver.js @@ -51,9 +51,9 @@ export var resolver = function (wretcher) { }; var bodyParser = function (funName) { return function (cb) { return funName ? // If a callback is provided, then callback with the body result otherwise return the parsed body itself. - catchersWrapper(throwingPromise.then(function (_) { return _ && _[funName](); }).then(function (_) { return _ && cb && cb(_) || _; })) : + catchersWrapper(throwingPromise.then(function (_) { return _ && _[funName](); }).then(function (_) { return cb ? cb(_) : _; })) : // No body parsing method - return the response - catchersWrapper(throwingPromise.then(function (_) { return _ && cb && cb(_) || _; })); }; }; + catchersWrapper(throwingPromise.then(function (_) { return cb ? cb(_) : _; })); }; }; var responseChain = { /** * Retrieves the raw result as a promise. diff --git a/dist/resolver.js.map b/dist/resolver.js.map index c1d917c..2e91cc5 100644 --- a/dist/resolver.js.map +++ b/dist/resolver.js.map @@ -1 +1 @@ -{"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;AA4B/C,MAAM,CAAC,IAAM,QAAQ,GAAG,UAAC,QAAkB;IAEnC,IAAA,mBAAS,EACT,8BAAoB,EACpB,+BAAqB,EACrB,mCAAyB,EACzB,wBAAc,CACN;IACZ,IAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAA;IACnC,IAAM,YAAY,GAAG,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;IAC7C,IAAM,eAAe,GAAG,IAAI,CAAC,QAAQ,CAAC,iBAAiB,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAC5F,IAAG,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,eAAe,EAAE;QAC3C,YAAY,CAAC,QAAQ,CAAC,GAA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;;WAEG;QACH,OAAO,EAAE,UAAA,EAAE,IAAI,OAAA,aAAa,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,EAA5B,CAA4B;QAC3C;;WAEG;QACH,aAAa,EAAE,UAAA,EAAE,IAAI,OAAA,aAAa,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,EAA5B,CAA4B;QACjD;;WAEG;QACH,OAAO,EAAE,UAAA,EAAE,IAAI,OAAA,aAAa,CAAC,KAAK,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 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;AA4B/C,MAAM,CAAC,IAAM,QAAQ,GAAG,UAAC,QAAkB;IAEnC,IAAA,mBAAS,EACT,8BAAoB,EACpB,+BAAqB,EACrB,mCAAyB,EACzB,wBAAc,CACN;IACZ,IAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAA;IACnC,IAAM,YAAY,GAAG,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;IAC7C,IAAM,eAAe,GAAG,IAAI,CAAC,QAAQ,CAAC,iBAAiB,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAC5F,IAAG,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,eAAe,EAAE;QAC3C,YAAY,CAAC,QAAQ,CAAC,GAAG,eAAe,CAAC,MAAM,CAAA;KAClD;IACD,kBAAkB;IAClB,IAAM,OAAO,GAAG;QACZ,GAAG,EAAE,IAAI;QACT,KAAK;YACD,IAAG,OAAO,CAAC,GAAG,EAAE;gBACZ,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;gBACzB,OAAO,CAAC,GAAG,GAAG,IAAI,CAAA;aACrB;QACL,CAAC;KACJ,CAAA;IACD,8BAA8B;IAC9B,IAAM,YAAY,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,YAAY,CAAC,CAAA;IAC7F,0BAA0B;IAC1B,IAAM,eAAe,G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,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 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 diff --git a/rollup.config.js b/rollup.config.js index 92e557f..bd3011e 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -17,7 +17,7 @@ export default { typescript: require("typescript"), importHelpers: true }), - nodeResolve({ jsnext: true, main: true }), + nodeResolve(), uglify({}, minify) ], external: [ "url" ] diff --git a/src/resolver.ts b/src/resolver.ts index 647ada2..38942eb 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -87,9 +87,9 @@ export const resolver = (wretcher: Wretcher) => { type BodyParser = (funName: string | null) => (cb?: (type: Type) => Result) => Promise const bodyParser: BodyParser = (funName) => (cb) => funName ? // If a callback is provided, then callback with the body result otherwise return the parsed body itself. - catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => _ && cb && cb(_) || _)) : + catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) : // No body parsing method - return the response - catchersWrapper(throwingPromise.then(_ => _ && cb && cb(_) || _)) + catchersWrapper(throwingPromise.then(_ => cb ? cb(_) : _)) const responseChain: ResponseChain = { /** diff --git a/test/browser/spec/index.js b/test/browser/spec/index.js index 08d5054..fcced83 100644 --- a/test/browser/spec/index.js +++ b/test/browser/spec/index.js @@ -439,4 +439,9 @@ describe("Wretch", function() { expect(result).toBe("ok") }) + it("should handle falsey json", async function () { + expect(await wretch(`${_URL}/json/null`).get().json()).toEqual(null) + expect(await wretch(`${_URL}/json/null`).get().json(_ => true)).toEqual(true) + expect(await wretch(`${_URL}/json/null`).get().json(_ => false)).toEqual(false) + }) }) \ No newline at end of file diff --git a/test/mock.js b/test/mock.js index db253a4..8026e44 100644 --- a/test/mock.js +++ b/test/mock.js @@ -41,6 +41,10 @@ const mockServer = { res.end() }) + server.get("/json/null", (req,res) => { + res.json(null) + }) + server.opts("/options", (req, res) => { res.header("Allow", "OPTIONS") res.end() diff --git a/test/node/wretch.spec.ts b/test/node/wretch.spec.ts index ddc232f..3c95aaf 100644 --- a/test/node/wretch.spec.ts +++ b/test/node/wretch.spec.ts @@ -514,6 +514,12 @@ describe("Wretch", function () { expect(result).toBe("ok") }) + + it("should handle falsey json", async function () { + expect(await wretch(`${_URL}/json/null`).get().json()).toEqual(null) + expect(await wretch(`${_URL}/json/null`).get().json(_ => true)).toEqual(true) + expect(await wretch(`${_URL}/json/null`).get().json(_ => false)).toEqual(false) + }) }) describe("Mix", function () {