Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vasanthv/fetch-lite
A lightweight module to send HTTP(s) requests from Node.js.
https://github.com/vasanthv/fetch-lite
curl fetch fetch-api http-client http-request nodejs
Last synced: about 2 months ago
JSON representation
A lightweight module to send HTTP(s) requests from Node.js.
- Host: GitHub
- URL: https://github.com/vasanthv/fetch-lite
- Owner: vasanthv
- License: mit
- Created: 2018-03-05T15:43:32.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-13T10:08:26.000Z (over 5 years ago)
- Last Synced: 2024-09-16T12:56:56.735Z (3 months ago)
- Topics: curl, fetch, fetch-api, http-client, http-request, nodejs
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 41
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/vasanthv/fetch-lite.svg?branch=master)](https://travis-ci.org/vasanthv/fetch-lite)
# fetch-lite
> A lightweight module to send HTTP(s) requests from Node.js.- Not dependent on any third party modules.
- Purely based on Node.Js `http.request` client.
- Written in less than **30** lines. 😳
- Implements just the fetch() function and nothing else.
- Returns *Promise* (so works with *async/await*).> Whats missing?
> - File Upload (multipart/form-data)
> - Streaming.## Install
Install using [npm](https://npmjs.com/).
```
npm install --save fetch-lite
```*OR*
Using [yarn](https://yarnpkg.com/).
```
yarn install --save fetch-lite
```## Usage
The idea of fetch-lite is to create a minimal version of `fetch()` API described [here](https://fetch.spec.whatwg.org/#fetch-api). A basic GET request is shown below.```js
const fetch = require('fetch-lite');fetch('https://httpbin.org/get')
.then(response => console.log(response))
.catch(err => console.error(err));
```Using async/await.
```js
(async () => {
const response = await fetch('https://httpbin.org/get');
console.log(response);
})();
```### Post
Posting a post body.
```js
fetch('http://httpbin.org/post', {
method: 'POST',
body: {
hello: 'world'
}
})
.then(response => console.log(response))
.catch(err => console.error(err));
```Posting a JSON body.
```js
fetch('http://httpbin.org/post', {
method: 'POST',
body: {
hello: 'world'
},
headers: {
'content-type': 'application/json'
}
})
.then(response => console.log(response))
.catch(err => console.error(err));
```Posting a Buffer.
```js
fetch('http://httpbin.org/post', {
method: 'POST',
body: Buffer.from("Hello World")
})
.then(response => console.log(response))
.catch(err => console.error(err));
```### Basic auth
```js
fetch('https://httpbin.org/basic-auth/user/passwd', {
auth: "user:passwd"
})
.then(response => assertEq(response.status, 200))
.catch(err => breakWithErr(err));
```## API
The module export one function which accepts only 2 params.
1. URL. String *(Required)*
2. Options. Object. *(Optional)*
- method. String. (POST | GET | PUT etc). *Default GET*.
- body. String or [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) or [Buffer](https://nodejs.org/api/buffer.html).
- headers. [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object).
- auth. String. Used to set the Basic auth header. Format *"username/password"*.
- followredirect. Boolean. *Default True*.**Output Params:**
- headers. [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
- statusText. String
- status. Number
- url. String
- body. String or [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)## Licence
MIT