0.0.2 • Published 7 years ago
zero-fetch v0.0.2
zero-fetch
Yet another convenient wrapper around fetch, for browser and node.js.
Why
fetchis good, but not easy enough- Minimal code for modern browsers.
axiosis still too big(5kb gzipped) - Minimal config
- Focused to RESTFul API
Installing
Using npm:
$ npm i -D zero-fetch
Using cdn:
<script src="https://unpkg.com/zero-fetch/dist/index.js"></script>
Example
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)
}
}Smart Request Body
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.
Smart Search Parameters
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).
Smart Response
Response body is automatically converted by proper method, based on Content-Type of response header. Supported types are:
application/json:.jsontext/plain:.textapplication/octect-stream:.blob