Yet another convenient wrapper around fetch, for browser and node.js.
fetch
is good, but not easy enough- Minimal code for modern browsers.
axios
is still too big(5kb gzipped) - Minimal config
- Focused to RESTFul API
Using npm:
$ npm i -D zero-fetch
Using cdn:
<script src="https://unpkg.com/zero-fetch/dist/index.js"></script>
Performing a GET
request
import zeroFetch from 'zero-fetch'
// Make a request for a user with a given id
zeroFetch('/user?id=12345')
.then(data => console.log(data))
.catch(error => console.log(error))
// Optionally the request above could also be done as
async getUser = () => {
try {
const data = await zeroFetch('/user', {
data: {
id: 12345
}
})
console.log(data)
} catch (error) {
console.error(error)
}
}
If data
is a plain Javascript object, it will be encoded automatically and sent as search parameters, or request body, based on method
and requestType
.
When converting data
to search parameters, nested objects are encoded to be understanded by express
, so that req.query
is same as data
.
TODO: FormData application/x-www-form-urlencoded TODO: base64 encode inner Blob
data
: the data to be sent as search parameters(GET
) or the request body(other method).
Response body is automatically converted by proper method, based on Content-Type
of response header. Supported types are:
application/json
:.json
text/plain
:.text
application/octect-stream
:.blob