uberfetch
A thin layer over fetch which makes JSON the default, and turns HTTP errors into Promise rejections.
;
get JSON
;
which is equivalent to:
{ if resok return res; return Promise;} ;
post JSON
let updatedCat = id: 10 name: 'Keith'; uberfetch;
which is equivalent to:
let updatedCat = id: 10 name: 'Keith'; ;
get HTML
// get some html ;
which is equivalent to:
;
catch typed errors
let cat = id: 10 name: 'Keith'; uberfetch ;
Or with ES7 async/await
{ let cat = id: 10 name: 'Keith'; try let response = await uberfetch let body = await response; FlashMessage; catch err if err instanceof uberfetchRequestError if errstatus == 422 let body = errresponse AlertModal; return; return Promise; }
API
Use uberfetch(url, opts)
exactly as you would fetch(url, opts)
, with the
following additional opts which can be provided in the opts object:
accept: string
shorthand for setting an accept header, which takes either a mime type, or a convenient short name likeform
,html
,text
etc.contentType: string
shorthand for setting an content-type header, which takes either a mime type, or a convenient short name likeform
,html
,text
etc.body: any
works like the normalfetch
body field, except that known content-types will be automatically serialized. Whenbody
is present, the default http method becomes POST.
In addition to the uberfetch()
function, the following convenience helpers are
available:
uberfetch.get()
automatically setsmethod: 'get'
uberfetch.post()
automatically setsmethod: 'post'
uberfetch.put()
automatically setsmethod: 'put'
uberfetch.patch()
automatically setsmethod: 'patch'
uberfetch.delete()
automatically setsmethod: 'delete'