fetch.io
Extends the whatwg fetch
- fetch spec api,
makes it easier to use. Both node & browser supported.
- install
npm install fetch.io
- import
- for TypeScript users
npm install @types/fetch.io
APIs
-
.config() - set options
-
.set() - set http header
-
.type() - set content type
-
.send() - send body data
-
.query() - set query string
-
.append() - append form data
-
.text() - convert response body to
string
-
.json(strict = true) - convert response body to
object
(strict JSON mode default)
Options
- beforeRequest -
Function
, a pre-request hook function, returningfalse
will cancel the request - afterResponse -
Function
, a post-response hook function - afterJSON -
Function
, add a handler for.json()
, to check the response data - prefix -
String
, url prefix - Other whatwg-fetch options
Usage
const request = prefix: 'http://example.com/api/v1'
- default options
prefix: '' mode: 'cors' cache: 'no-cache' credentials: 'include'
request
- get json body
request
- get text body
request text
- send json
request
- send urlencoded
request
- send urlencoded
request type'form' // equal to: // .type('urlencoded') // equal to: // .set('content-type', 'application/x-www-form-urlencoded')
- set header
request
- send form (multipart) (upload file)
request
License
MIT