https://github.com/caub/fetchu
🔧 Universal http-client, gotta fetch'em all
https://github.com/caub/fetchu
Last synced: about 1 year ago
JSON representation
🔧 Universal http-client, gotta fetch'em all
- Host: GitHub
- URL: https://github.com/caub/fetchu
- Owner: caub
- Created: 2017-12-01T02:24:37.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-03-01T12:47:20.000Z (over 5 years ago)
- Last Synced: 2025-04-12T13:13:09.330Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 29.3 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fetchu: universal http-client
[![npm version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
### Differences with fetch:
- automatically handle json body (`JSON.stringify` a body that is an objet literal, and add the right header)
- throws on http error status
### Files:
- fetchu-node.js: using `http`/`https` modules for NodeJS, light drop-in replacement for `node-fetch`
- fetchu-browser.js: it wraps `window.fetch`
```js
import fetchu from 'https://unpkg.com/fetchu';
await fetchu('https://cors-anywhere.herokuapp.com/http://example.com').then(r => r.text()) // r.json()) // { args: { test: 'foo' }, ...
await fetchu('https://httpbin.org/post', {method: 'POST', body: {test: 'foo'}}).then(r => r.json()) // { args: {},..
// abort:
const ac = new AbortController()
fetchu('https://httpbin.org/get?test=foo', { signal: ac.signal }).then(console.log, console.error)
delay(10).then(() => ac.abort())
```
NodeJS specificities (coming from http/https builtins):
```js
const fetchu = require('fetchu');
await fetchu({ path: '/v1.37/containers/json', socketPath: '/var/run/docker.sock' }).then(r => r.json()) // [ { Id: 'aa6...
// abort:
require('abortcontroller-polyfill/dist/abortcontroller-polyfill-only');
const ac = new AbortController()
fetchu('https://httpbin.org/get?test=foo', { signal: ac.signal }).then(console.log, console.error)
delay(10).then(() => ac.abort());
// or
const signal = new EventEmitter(); // node.js native events.EventEmitter
fetchu('https://httpbin.org/get?test=foo', { signal }).then(console.log, console.error)
delay(10).then(() => signal.emit('abort'))
```
[npm-image]: https://img.shields.io/npm/v/fetchu.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/fetchu
[travis-image]: https://img.shields.io/travis/caub/fetchu.svg?style=flat-square
[travis-url]: https://travis-ci.org/caub/fetchu