Package Exports
- @m59/fetch
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@m59/fetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@m59/fetch
Fetch API with conveniences I like.
This is shimmed for server side usage with node-fetch
. The server code is ignored when bundling for the browser.
install
$ npm install @m59/fetch
example
const fetch = require('@m59/fetch')
fetch.json({
url: URL + '/posts',
query: { userId: 1 }
}).then(({response, body}) => body[0].id) // 1
api
fetch([url], options)
The first argument can be a url or options object. If you pass only an options object, it should have a url property. Put your normal fetch
options in options
.
options.query
options.query
is an object that will be used to create a query string that will set or overwrite the query string in the given url
{
url: '/foo'
query: { bar: 123 }
} // => /foo?bar=123
options.body
If you set options.body
to an instance of FormData
, the form data content type header will be set automatically. If you set options.body
to a regular object, it will be JSON stringified and the json content type header will be set automatically.
helpers
If you want the response body in a certain format, save some code by using the corresponding helper. json()
and text()
will also set the appropriate accept
header to ensure the server knows what format you want. The result will be in the format { response, body }
.
fetch.arrayBuffer()
fetch.blob()
fetch.json()
fetch.text()
fetch.formData()