diff --git a/dist/bundle/wretch.js b/dist/bundle/wretch.js index c363b8d..714a2b7 100644 --- a/dist/bundle/wretch.js +++ b/dist/bundle/wretch.js @@ -1,2 +1,2 @@ -!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.wretch=n()}(this,function(){"use strict";const t=Object.assign||function(t){for(var n,r=1;r {\n const usp = new URLSearchParams()\n const index = url.indexOf(\"?\")\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 return ~index ?\n `${url.substring(0, index)}?${usp.toString()}` :\n `${url}?${usp.toString()}`\n}\n\ntype WretcherError = Error & { status: number, response: Response, text?: string, json?: Object }\n\nconst doFetch = url => (opts = {}) => {\n const req = fetch(url, mix(defaults, opts))\n let wrapper : Promise = req.then(response => {\n if (!response.ok) {\n return response[errorType || \"text\"]().then(_ => {\n const err = new Error(_)\n err[errorType] = _\n err[\"status\"] = response.status\n err[\"response\"] = response\n throw err\n })\n }\n return response\n })\n let catchers = []\n const doCatch = (promise : Promise) : Promise => catchers.reduce((accumulator, catcher) => accumulator.catch(catcher), promise)\n const responseTypes = {\n /**\n * Retrieves the raw result as a promise.\n */\n res: (cb?: (response : Response) => any) => doCatch(wrapper.then(_ => _ && cb && cb(_) || _)),\n /**\n * Retrieves the result as a parsed JSON object.\n */\n json: (cb?: (json : Object) => any) => doCatch(wrapper\n .then(_ => _ && _.json())\n .then(_ => _ && cb && cb(_) || _)),\n /**\n * Retrieves the result as a Blob object.\n */\n blob: (cb?: (blob : Blob) => any) => doCatch(wrapper\n .then(_ => _ && _.blob())\n .then(_ => _ && cb && cb(_) || _)),\n /**\n * Retrieves the result as a FormData object.\n */\n formData: (cb?: (fd : FormData) => any) => doCatch(wrapper\n .then(_ => _ && _.formData())\n .then(_ => _ && cb && cb(_) || _)),\n /**\n * Retrieves the result as an ArrayBuffer object.\n */\n arrayBuffer: (cb?: (ab : ArrayBuffer) => any) => doCatch(wrapper\n .then(_ => _ && _.arrayBuffer())\n .then(_ => _ && cb && cb(_) || _)),\n /**\n * Retrieves the result as a string.\n */\n text: (cb?: (text : string) => any) => doCatch(wrapper\n .then(_ => _ && _.text())\n .then(_ => _ && cb && cb(_) || _)),\n /**\n * Catches an http response with a specific error code and performs a callback.\n */\n error: (code: number, cb) => {\n catchers.push(err => {\n if(err.status === code) cb(err)\n else throw err\n })\n return responseTypes\n },\n /**\n * Catches a bad request (http code 400) and performs a callback.\n */\n badRequest: (cb: (error: WretcherError) => any) => responseTypes.error(400, cb),\n /**\n * Catches an unauthorized request (http code 401) and performs a callback.\n */\n unauthorized: (cb: (error: WretcherError) => any) => responseTypes.error(401, cb),\n /**\n * Catches a forbidden request (http code 403) and performs a callback.\n */\n forbidden: (cb: (error: WretcherError) => any) => responseTypes.error(403, cb),\n /**\n * Catches a \"not found\" request (http code 404) and performs a callback.\n */\n notFound: (cb: (error: WretcherError) => any) => responseTypes.error(404, cb),\n /**\n * Catches a timeout (http code 408) and performs a callback.\n */\n timeout: (cb: (error: WretcherError) => any) => responseTypes.error(408, cb),\n /**\n * Catches an internal server error (http code 500) and performs a callback.\n */\n internalError: (cb: (error: WretcherError) => any) => responseTypes.error(500, cb)\n }\n\n return responseTypes\n}","import { Wretcher } from \"./wretcher\"\n\n// URLSearchParams for node.js\ndeclare const global\ndeclare const require\nif(typeof self === \"undefined\") {\n global.URLSearchParams = require(\"url\").URLSearchParams\n}\n\n/**\n * Return a fresh Wretcher instance.\n */\nexport default (url = \"\", opts = {}) => new Wretcher(url, opts)"],"names":["mix","one","two","mergeArrays","clone","prop","hasOwnProperty","Array","defaults","errorType","_url","_options","this","Wretcher","opts","method","url","options","qp","appendQueryParams","what","headers","Accept","doFetch","jsObject","Content-Type","body","JSON","stringify","formObject","formData","FormData","key","_a","_i","item","append","usp","URLSearchParams","index","indexOf","val","substring","toString","wrapper","fetch","then","response","ok","_","err","Error","status","catchers","doCatch","promise","reduce","accumulator","catcher","catch","responseTypes","res","cb","json","blob","arrayBuffer","text","error","code","push","badRequest","unauthorized","forbidden","notFound","timeout","internalError","self","global","require"],"mappings":"0VAAO,IAAMA,EAAM,SAASC,EAAaC,EAAaC,GAClD,gBADkDA,OAC9CF,IAAQC,GAAsB,iBAARD,GAAmC,iBAARC,EACjD,OAAOD,EAEX,IAAMG,OAAaH,EAAQC,GAC3B,IAAI,IAAMG,KAAQH,EACXA,EAAII,eAAeD,KACfH,EAAIG,aAAiBE,OAASN,EAAII,aAAiBE,MAClDH,EAAMC,GAAQF,EAAmBF,EAAII,UAAUH,EAAIG,IAAUD,EAAMC,GAAQH,EAAIG,GACpD,iBAAdH,EAAIG,IAA2C,iBAAdJ,EAAII,KAClDD,EAAMC,GAAQL,EAAIC,EAAII,GAAOH,EAAIG,GAAOF,KAKpD,OAAOC,GCZPI,KACAC,EAAY,kBASZ,WACYC,EACAC,gBAAAA,MADAC,UAAAF,EACAE,cAAAD,EAuIhB,OAjIIE,qBAAA,SAASC,GAEL,OADAN,EAAWM,EACJF,MAOXC,wBAAA,SAAYC,GAER,OADAN,EAAWR,EAAIQ,EAAUM,GAClBF,MAUXC,sBAAA,SAAUE,GAEN,OADAN,EAAYM,EACLH,MAOXC,gBAAA,SAAIG,GACA,OAAO,IAAIH,EAASG,EAAKJ,KAAKD,WAOlCE,oBAAA,SAAQI,GACJ,OAAO,IAAIJ,EAASD,KAAKF,KAAMO,IAcnCJ,kBAAA,SAAMK,GACF,OAAO,IAAIL,EAASM,EAAkBP,KAAKF,KAAMQ,GAAKN,KAAKD,WAO/DE,mBAAA,SAAOO,GACH,OAAO,IAAIP,EAASD,KAAKF,KAAMV,EAAIY,KAAKD,UAAYU,SAAWC,OAAWF,OAM9EP,gBAAA,SAAIC,GACA,oBADAA,MACOS,EAAQX,KAAKF,MAAMV,EAAIc,EAAMF,KAAKD,YAK7CE,mBAAA,SAAOC,GACH,oBADGA,MACIS,EAAQX,KAAKF,WAAWV,EAA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SAAAnB,GACV,GAAGA,EAAIE,SAAWgB,EACb,MAAMlB,EADaY,EAAGZ,KAGxBU,GAKXU,WAAY,SAACR,GAAsC,OAAAF,EAAcO,MAAM,IAAKL,IAI5ES,aAAc,SAACT,GAAsC,OAAAF,EAAcO,MAAM,IAAKL,IAI9EU,UAAW,SAACV,GAAsC,OAAAF,EAAcO,MAAM,IAAKL,IAI3EW,SAAU,SAACX,GAAsC,OAAAF,EAAcO,MAAM,IAAKL,IAI1EY,QAAS,SAACZ,GAAsC,OAAAF,EAAcO,MAAM,IAAKL,IAIzEa,cAAe,SAACb,GAAsC,OAAAF,EAAcO,MAAM,IAAKL,KAGnF,OAAOF,IC9PQ,oBAATgB,OACNC,OAAOvC,gBAAkBwC,QAAQ,OAAOxC,wBAM7B,SAACtB,EAAUF,GAAc,oBAAxBE,mBAAUF,MAAc,IAAID,EAASG,EAAKF"} \ No newline at end of file +{"version":3,"file":"wretch.js","sources":["../../src/mix.ts","../../src/wretcher.ts","../../src/index.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, ...two }\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] ] : clone[prop] = two[prop]\n } else if(typeof two[prop] === \"object\" && typeof one[prop] === \"object\") {\n clone[prop] = mix(one[prop], two[prop], mergeArrays)\n }\n }\n }\n\n return clone\n}\n","import { mix } from \"./mix\"\n\n// Default options\nlet defaults = {}\nlet errorType = null\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 constructor(\n private _url: string,\n private _options = {}) {}\n\n /**\n * Sets the default fetch options used for every subsequent fetch call.\n * @param opts New default options\n */\n defaults(opts: Object) {\n defaults = opts\n return this\n }\n\n /**\n * Mixins the default fetch options used for every subsequent fetch calls.\n * @param opts Options to mixin with the current default options\n */\n mixdefaults(opts: Object) {\n defaults = mix(defaults, opts)\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 errorType = method\n return this\n }\n\n /**\n * Returns a new Wretcher object with the url specified and the same options.\n * @param url String url\n */\n url(url: string) {\n return new Wretcher(url, this._options)\n }\n\n /**\n * Returns a new Wretcher object with the same url and new options.\n * @param options New options\n */\n options(options: Object) {\n return new Wretcher(this._url, options)\n }\n\n /**\n * Converts a javascript object to query parameters,\n * then appends this query string to the current url.\n *\n * ```\n * let w = wretch(\"http://example.com\") // url is http://example.com\n * w = w.query({ a: 1, b : 2 }) // url is now http://example.com?a=1&b=2\n * ```\n *\n * @param qp An object which will be converted.\n */\n query(qp: Object) {\n return new Wretcher(appendQueryParams(this._url, qp), this._options)\n }\n\n /**\n * Shortcut to set the \"Accept\" header.\n * @param what Header value\n */\n accept(what: string) {\n return new Wretcher(this._url, mix(this._options, { headers: { \"Accept\" : what }}))\n }\n\n /**\n * Performs a get request.\n */\n get(opts = {}) {\n return doFetch(this._url)(mix(opts, this._options))\n }\n /**\n * Performs a delete request.\n */\n delete(opts = {}) {\n return doFetch(this._url)({ ...mix(opts, this._options), method: \"DELETE\" })\n }\n /**\n * Performs a put request.\n */\n put(opts = {}) {\n return doFetch(this._url)({ ...mix(opts, this._options), method: \"PUT\" })\n }\n /**\n * Performs a post request.\n */\n post(opts = {}) {\n return doFetch(this._url)({ ...mix(opts, this._options), method: \"POST\" })\n }\n /**\n * Performs a patch request.\n */\n patch(opts = {}) {\n return doFetch(this._url)({ ...mix(opts, this._options), method: \"PATCH\" })\n }\n\n /**\n * Sets the content type header, stringifies an object and sets the request body.\n * @param jsObject An object\n */\n json(jsObject: Object) {\n return new Wretcher(this._url,\n { \n ...this._options,\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(jsObject)\n })\n }\n /**\n * Converts the javascript object to a FormData and sets the request body.\n * @param formObject An object\n */\n formData(formObject: Object) {\n const formData = new FormData()\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 new Wretcher(this._url,\n { \n ...this._options,\n body: formData\n })\n }\n}\n\n// Internal helpers\n\nconst appendQueryParams = (url: string, qp: Object) => {\n const usp = new URLSearchParams()\n const index = url.indexOf(\"?\")\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 return ~index ?\n `${url.substring(0, index)}?${usp.toString()}` :\n `${url}?${usp.toString()}`\n}\n\ntype WretcherError = Error & { status: number, response: Response, text?: string, json?: any }\n\nconst doFetch = url => (opts = {}) => {\n const req = fetch(url, mix(defaults, opts))\n let wrapper : Promise = req.then(response => {\n if (!response.ok) {\n return response[errorType || \"text\"]().then(_ => {\n const err = new Error(_)\n err[errorType] = _\n err[\"status\"] = response.status\n err[\"response\"] = response\n throw err\n })\n }\n return response\n })\n\n type TypeParser = (funName: string | null) => (cb?: (type : Type) => Result) => Promise\n\n let catchers = []\n const doCatch = (promise : Promise) : Promise => catchers.reduce((accumulator, catcher) => accumulator.catch(catcher), promise)\n const wrapTypeParser : TypeParser = (funName) => (cb) => funName ?\n doCatch(wrapper.then(_ => _ && _[funName]()).then(_ => _ && cb && cb(_) || _)) :\n doCatch(wrapper.then(_ => _ && cb && cb(_) || _))\n\n\n const responseTypes : {\n res: (cb?: (type: void) => Result) => Promise,\n json: (cb?: (type: Object) => 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 error: (code: number, cb: any) => typeof responseTypes,\n badRequest: (cb: (error: WretcherError) => void) => typeof responseTypes,\n unauthorized: (cb: (error: WretcherError) => void) => typeof responseTypes,\n forbidden: (cb: (error: WretcherError) => void) => typeof responseTypes,\n notFound: (cb: (error: WretcherError) => void) => typeof responseTypes,\n timeout: (cb: (error: WretcherError) => void) => typeof responseTypes,\n internalError: (cb: (error: WretcherError) => void) => typeof responseTypes\n } = {\n /**\n * Retrieves the raw result as a promise.\n */\n res: wrapTypeParser(null),\n /**\n * Retrieves the result as a parsed JSON object.\n */\n json: wrapTypeParser(\"json\"),\n /**\n * Retrieves the result as a Blob object.\n */\n blob: wrapTypeParser(\"blob\"),\n /**\n * Retrieves the result as a FormData object.\n */\n formData: wrapTypeParser(\"formData\"),\n /**\n * Retrieves the result as an ArrayBuffer object.\n */\n arrayBuffer: wrapTypeParser(\"arrayBuffer\"),\n /**\n * Retrieves the result as a string.\n */\n text: wrapTypeParser(\"text\"),\n /**\n * Catches an http response with a specific error code and performs a callback.\n */\n error: function(code: number, cb) {\n catchers.push(err => {\n if(err.status === code) cb(err)\n else throw err\n })\n return responseTypes\n },\n /**\n * Catches a bad request (http code 400) and performs a callback.\n */\n badRequest: cb => responseTypes.error(400, cb),\n /**\n * Catches an unauthorized request (http code 401) and performs a callback.\n */\n unauthorized: cb => responseTypes.error(401, cb),\n /**\n * Catches a forbidden request (http code 403) and performs a callback.\n */\n forbidden: cb => responseTypes.error(403, cb),\n /**\n * Catches a \"not found\" request (http code 404) and performs a callback.\n */\n notFound: cb => responseTypes.error(404, cb),\n /**\n * Catches a timeout (http code 408) and performs a callback.\n */\n timeout: cb => responseTypes.error(408, cb),\n /**\n * Catches an internal server error (http code 500) and performs a callback.\n */\n internalError: cb => responseTypes.error(500, cb)\n }\n\n return responseTypes\n}","import { Wretcher } from \"./wretcher\"\n\n// URLSearchParams for node.js\ndeclare const global\ndeclare const require\nif(typeof self === \"undefined\") {\n global.URLSearchParams = require(\"url\").URLSearchParams\n}\n\n/**\n * Return a fresh Wretcher instance.\n */\nexport default (url = \"\", opts = {}) => new Wretcher(url, opts)"],"names":["mix","one","two","mergeArrays","clone","prop","hasOwnProperty","Array","defaults","errorType","_url","_options","this","Wretcher","opts","method","url","options","qp","appendQueryParams","what","headers","Accept","doFetch","jsObject","Content-Type","body","JSON","stringify","formObject","formData","FormData","key","_a","_i","item","append","usp","URLSearchParams","index","indexOf","val","substring","toString","wrapper","fetch","then","response","ok","_","err","Error","status","catchers","doCatch","promise","reduce","accumulator","catcher","catch","wrapTypeParser","funName","cb","responseTypes","res","json","blob","arrayBuffer","text","error","code","push","badRequest","unauthorized","forbidden","notFound","timeout","internalError","self","global","require"],"mappings":"0VAAO,IAAMA,EAAM,SAASC,EAAaC,EAAaC,GAClD,gBADkDA,OAC9CF,IAAQC,GAAsB,iBAARD,GAAmC,iBAARC,EACjD,OAAOD,EAEX,IAAMG,OAAaH,EAAQC,GAC3B,IAAI,IAAMG,KAAQH,EACXA,EAAII,eAAeD,KACfH,EAAIG,aAAiBE,OAASN,EAAII,aAAiBE,MAClDH,EAAMC,GAAQF,EAAmBF,EAAII,UAAUH,EAAIG,IAAUD,EAAMC,GAAQH,EAAIG,GACpD,iBAAdH,EAAIG,IAA2C,iBAAdJ,EAAII,KAClDD,EAAMC,GAAQL,EAAIC,EAAII,GAAOH,EAAIG,GAAOF,KAKpD,OAAOC,GCZPI,KACAC,EAAY,kBASZ,WACYC,EACAC,gBAAAA,MADAC,UAAAF,EACAE,cAAAD,EAuIhB,OAjIIE,qBAAA,SAASC,GAEL,OADAN,EAAWM,EACJF,MAOXC,wBAAA,SAAYC,GAER,OADAN,EAAWR,EAAIQ,EAAUM,GAClBF,MAUXC,sBAAA,SAAUE,GAEN,OADAN,EAAYM,EACLH,MAOXC,gBAAA,SAAIG,GACA,OAAO,IAAIH,EAASG,EAAKJ,KAAKD,WAOlCE,oBAAA,SAAQI,GACJ,OAAO,IAAIJ,EAASD,KAAKF,KAAMO,IAcnCJ,kBAAA,SAAMK,GACF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I7CY,UAAW,SAAAZ,GAAM,OAAAC,EAAcM,MAAM,IAAKP,IAI1Ca,SAAU,SAAAb,GAAM,OAAAC,EAAcM,MAAM,IAAKP,IAIzCc,QAAS,SAAAd,GAAM,OAAAC,EAAcM,MAAM,IAAKP,IAIxCe,cAAe,SAAAf,GAAM,OAAAC,EAAcM,MAAM,IAAKP,KAGlD,OAAOC,IC1QQ,oBAATe,OACNC,OAAOzC,gBAAkB0C,QAAQ,OAAO1C,wBAM7B,SAACtB,EAAUF,GAAc,oBAAxBE,mBAAUF,MAAc,IAAID,EAASG,EAAKF"} \ No newline at end of file diff --git a/dist/wretcher.d.ts b/dist/wretcher.d.ts index ec22ba8..06eab64 100644 --- a/dist/wretcher.d.ts +++ b/dist/wretcher.d.ts @@ -56,241 +56,241 @@ export declare class Wretcher { * Performs a get request. */ get(opts?: {}): { - res: (cb?: (response: Response) => any) => Promise; - json: (cb?: (json: Object) => any) => Promise; - blob: (cb?: (blob: Blob) => any) => Promise; - formData: (cb?: (fd: FormData) => any) => Promise; - arrayBuffer: (cb?: (ab: ArrayBuffer) => any) => Promise; - text: (cb?: (text: string) => any) => Promise; + res: (cb?: (type: void) => Result) => Promise; + json: (cb?: (type: Object) => Result) => Promise; + blob: (cb?: (type: Blob) => Result) => Promise; + formData: (cb?: (type: FormData) => Result) => Promise; + arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise; + text: (cb?: (type: string) => Result) => Promise; error: (code: number, cb: any) => any; badRequest: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; unauthorized: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; forbidden: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; notFound: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; timeout: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; internalError: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; }; /** * Performs a delete request. */ delete(opts?: {}): { - res: (cb?: (response: Response) => any) => Promise; - json: (cb?: (json: Object) => any) => Promise; - blob: (cb?: (blob: Blob) => any) => Promise; - formData: (cb?: (fd: FormData) => any) => Promise; - arrayBuffer: (cb?: (ab: ArrayBuffer) => any) => Promise; - text: (cb?: (text: string) => any) => Promise; + res: (cb?: (type: void) => Result) => Promise; + json: (cb?: (type: Object) => Result) => Promise; + blob: (cb?: (type: Blob) => Result) => Promise; + formData: (cb?: (type: FormData) => Result) => Promise; + arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise; + text: (cb?: (type: string) => Result) => Promise; error: (code: number, cb: any) => any; badRequest: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; unauthorized: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; forbidden: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; notFound: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; timeout: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; internalError: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; }; /** * Performs a put request. */ put(opts?: {}): { - res: (cb?: (response: Response) => any) => Promise; - json: (cb?: (json: Object) => any) => Promise; - blob: (cb?: (blob: Blob) => any) => Promise; - formData: (cb?: (fd: FormData) => any) => Promise; - arrayBuffer: (cb?: (ab: ArrayBuffer) => any) => Promise; - text: (cb?: (text: string) => any) => Promise; + res: (cb?: (type: void) => Result) => Promise; + json: (cb?: (type: Object) => Result) => Promise; + blob: (cb?: (type: Blob) => Result) => Promise; + formData: (cb?: (type: FormData) => Result) => Promise; + arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise; + text: (cb?: (type: string) => Result) => Promise; error: (code: number, cb: any) => any; badRequest: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; unauthorized: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; forbidden: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; notFound: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; timeout: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; internalError: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; }; /** * Performs a post request. */ post(opts?: {}): { - res: (cb?: (response: Response) => any) => Promise; - json: (cb?: (json: Object) => any) => Promise; - blob: (cb?: (blob: Blob) => any) => Promise; - formData: (cb?: (fd: FormData) => any) => Promise; - arrayBuffer: (cb?: (ab: ArrayBuffer) => any) => Promise; - text: (cb?: (text: string) => any) => Promise; + res: (cb?: (type: void) => Result) => Promise; + json: (cb?: (type: Object) => Result) => Promise; + blob: (cb?: (type: Blob) => Result) => Promise; + formData: (cb?: (type: FormData) => Result) => Promise; + arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise; + text: (cb?: (type: string) => Result) => Promise; error: (code: number, cb: any) => any; badRequest: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; unauthorized: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; forbidden: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; notFound: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; timeout: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; internalError: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; }; /** * Performs a patch request. */ patch(opts?: {}): { - res: (cb?: (response: Response) => any) => Promise; - json: (cb?: (json: Object) => any) => Promise; - blob: (cb?: (blob: Blob) => any) => Promise; - formData: (cb?: (fd: FormData) => any) => Promise; - arrayBuffer: (cb?: (ab: ArrayBuffer) => any) => Promise; - text: (cb?: (text: string) => any) => Promise; + res: (cb?: (type: void) => Result) => Promise; + json: (cb?: (type: Object) => Result) => Promise; + blob: (cb?: (type: Blob) => Result) => Promise; + formData: (cb?: (type: FormData) => Result) => Promise; + arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise; + text: (cb?: (type: string) => Result) => Promise; error: (code: number, cb: any) => any; badRequest: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; unauthorized: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; forbidden: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; notFound: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; timeout: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; internalError: (cb: (error: Error & { status: number; response: Response; text?: string; - json?: Object; - }) => any) => any; + json?: any; + }) => void) => any; }; /** * Sets the content type header, stringifies an object and sets the request body. diff --git a/dist/wretcher.js b/dist/wretcher.js index bc704fe..1c4be30 100644 --- a/dist/wretcher.js +++ b/dist/wretcher.js @@ -183,41 +183,34 @@ var doFetch = function (url) { return function (opts) { }); var catchers = []; var doCatch = function (promise) { return catchers.reduce(function (accumulator, catcher) { return accumulator.catch(catcher); }, promise); }; + var wrapTypeParser = function (funName) { return function (cb) { return funName ? + doCatch(wrapper.then(function (_) { return _ && _[funName](); }).then(function (_) { return _ && cb && cb(_) || _; })) : + doCatch(wrapper.then(function (_) { return _ && cb && cb(_) || _; })); }; }; var responseTypes = { /** * Retrieves the raw result as a promise. */ - res: function (cb) { return doCatch(wrapper.then(function (_) { return _ && cb && cb(_) || _; })); }, + res: wrapTypeParser(null), /** * Retrieves the result as a parsed JSON object. */ - json: function (cb) { return doCatch(wrapper - .then(function (_) { return _ && _.json(); }) - .then(function (_) { return _ && cb && cb(_) || _; })); }, + json: wrapTypeParser("json"), /** * Retrieves the result as a Blob object. */ - blob: function (cb) { return doCatch(wrapper - .then(function (_) { return _ && _.blob(); }) - .then(function (_) { return _ && cb && cb(_) || _; })); }, + blob: wrapTypeParser("blob"), /** * Retrieves the result as a FormData object. */ - formData: function (cb) { return doCatch(wrapper - .then(function (_) { return _ && _.formData(); }) - .then(function (_) { return _ && cb && cb(_) || _; })); }, + formData: wrapTypeParser("formData"), /** * Retrieves the result as an ArrayBuffer object. */ - arrayBuffer: function (cb) { return doCatch(wrapper - .then(function (_) { return _ && _.arrayBuffer(); }) - .then(function (_) { return _ && cb && cb(_) || _; })); }, + arrayBuffer: wrapTypeParser("arrayBuffer"), /** * Retrieves the result as a string. */ - text: function (cb) { return doCatch(wrapper - .then(function (_) { return _ && _.text(); }) - .then(function (_) { return _ && cb && cb(_) || _; })); }, + text: wrapTypeParser("text"), /** * Catches an http response with a specific error code and performs a callback. */ diff --git a/dist/wretcher.js.map b/dist/wretcher.js.map index 0d8262b..4aa8fff 100644 --- a/dist/wretcher.js.map +++ b/dist/wretcher.js.map @@ -1 +1 @@ -{"version":3,"file":"wretcher.js","sourceRoot":"","sources":["../src/wretcher.ts"],"names":[],"mappings":";;;;;;;;AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAA;AAE3B,kBAAkB;AACl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UAAA,EAAE,IAAI,OAAA,aAAa,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,EAA5B,CAA4B;QAChD;;WAEG;QACH,SAAS,EAAE,UAAA,EAAE,IAAI,OAAA,aAAa,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,EAA5B,CAA4B;QAC7C;;WAEG;QACH,QAAQ,EAAE,UAAA,EAAE,IAAI,OAAA,aAAa,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,EAA5B,CAA4B;QAC5C;;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;KACpD,CAAA;IAED,MAAM,CAAC,aAAa,CAAA;AACxB,CAAC,EApGsB,CAoGtB,CAAA"} \ No newline at end of file diff --git a/src/wretcher.ts b/src/wretcher.ts index ce88d6d..0220dc3 100644 --- a/src/wretcher.ts +++ b/src/wretcher.ts @@ -168,7 +168,7 @@ const appendQueryParams = (url: string, qp: Object) => { `${url}?${usp.toString()}` } -type WretcherError = Error & { status: number, response: Response, text?: string, json?: Object } +type WretcherError = Error & { status: number, response: Response, text?: string, json?: any } const doFetch = url => (opts = {}) => { const req = fetch(url, mix(defaults, opts)) @@ -184,47 +184,59 @@ const doFetch = url => (opts = {}) => { } return response }) + + type TypeParser = (funName: string | null) => (cb?: (type : Type) => Result) => Promise + let catchers = [] const doCatch = (promise : Promise) : Promise => catchers.reduce((accumulator, catcher) => accumulator.catch(catcher), promise) - const responseTypes = { + const wrapTypeParser : TypeParser = (funName) => (cb) => funName ? + doCatch(wrapper.then(_ => _ && _[funName]()).then(_ => _ && cb && cb(_) || _)) : + doCatch(wrapper.then(_ => _ && cb && cb(_) || _)) + + + const responseTypes : { + res: (cb?: (type: void) => Result) => Promise, + json: (cb?: (type: Object) => Result) => Promise, + blob: (cb?: (type: Blob) => Result) => Promise, + formData: (cb?: (type: FormData) => Result) => Promise, + arrayBuffer: (cb?: (type: ArrayBuffer) => Result) => Promise, + text: (cb?: (type: string) => Result) => Promise, + error: (code: number, cb: any) => typeof responseTypes, + badRequest: (cb: (error: WretcherError) => void) => typeof responseTypes, + unauthorized: (cb: (error: WretcherError) => void) => typeof responseTypes, + forbidden: (cb: (error: WretcherError) => void) => typeof responseTypes, + notFound: (cb: (error: WretcherError) => void) => typeof responseTypes, + timeout: (cb: (error: WretcherError) => void) => typeof responseTypes, + internalError: (cb: (error: WretcherError) => void) => typeof responseTypes + } = { /** * Retrieves the raw result as a promise. */ - res: (cb?: (response : Response) => any) => doCatch(wrapper.then(_ => _ && cb && cb(_) || _)), + res: wrapTypeParser(null), /** * Retrieves the result as a parsed JSON object. */ - json: (cb?: (json : Object) => any) => doCatch(wrapper - .then(_ => _ && _.json()) - .then(_ => _ && cb && cb(_) || _)), + json: wrapTypeParser("json"), /** * Retrieves the result as a Blob object. */ - blob: (cb?: (blob : Blob) => any) => doCatch(wrapper - .then(_ => _ && _.blob()) - .then(_ => _ && cb && cb(_) || _)), + blob: wrapTypeParser("blob"), /** * Retrieves the result as a FormData object. */ - formData: (cb?: (fd : FormData) => any) => doCatch(wrapper - .then(_ => _ && _.formData()) - .then(_ => _ && cb && cb(_) || _)), + formData: wrapTypeParser("formData"), /** * Retrieves the result as an ArrayBuffer object. */ - arrayBuffer: (cb?: (ab : ArrayBuffer) => any) => doCatch(wrapper - .then(_ => _ && _.arrayBuffer()) - .then(_ => _ && cb && cb(_) || _)), + arrayBuffer: wrapTypeParser("arrayBuffer"), /** * Retrieves the result as a string. */ - text: (cb?: (text : string) => any) => doCatch(wrapper - .then(_ => _ && _.text()) - .then(_ => _ && cb && cb(_) || _)), + text: wrapTypeParser("text"), /** * Catches an http response with a specific error code and performs a callback. */ - error: (code: number, cb) => { + error: function(code: number, cb) { catchers.push(err => { if(err.status === code) cb(err) else throw err @@ -234,27 +246,27 @@ const doFetch = url => (opts = {}) => { /** * Catches a bad request (http code 400) and performs a callback. */ - badRequest: (cb: (error: WretcherError) => any) => responseTypes.error(400, cb), + badRequest: cb => responseTypes.error(400, cb), /** * Catches an unauthorized request (http code 401) and performs a callback. */ - unauthorized: (cb: (error: WretcherError) => any) => responseTypes.error(401, cb), + unauthorized: cb => responseTypes.error(401, cb), /** * Catches a forbidden request (http code 403) and performs a callback. */ - forbidden: (cb: (error: WretcherError) => any) => responseTypes.error(403, cb), + forbidden: cb => responseTypes.error(403, cb), /** * Catches a "not found" request (http code 404) and performs a callback. */ - notFound: (cb: (error: WretcherError) => any) => responseTypes.error(404, cb), + notFound: cb => responseTypes.error(404, cb), /** * Catches a timeout (http code 408) and performs a callback. */ - timeout: (cb: (error: WretcherError) => any) => responseTypes.error(408, cb), + timeout: cb => responseTypes.error(408, cb), /** * Catches an internal server error (http code 500) and performs a callback. */ - internalError: (cb: (error: WretcherError) => any) => responseTypes.error(500, cb) + internalError: cb => responseTypes.error(500, cb) } return responseTypes