Add chaining option and update README

This commit is contained in:
Wessel Tip
2019-04-12 18:23:18 +02:00
parent 8848ec10c8
commit c581ae33b0
3 changed files with 59 additions and 36 deletions

View File

@@ -1,17 +1,25 @@
# Wumpfetch
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FPassTheWessel%2Fwumpfetch.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FPassTheWessel%2Fwumpfetch?ref=badge_shield)
<img src="https://wessel.meek.moe/wumpfetch/logo.svg" align="left" style="border-radius: 5%;" width="192px" height="192px"/>
<img align="left" width="0" height="192px" hspace="10"/>
> A lightweight and fast Node.js HTTP client which can be used in various ways
> <a href="https://github.com/PassTheWessel/wumpfetch">Wumpfetch</a> - A fast and easy to use HTTP client
> [Typings](https://github.com/PassTheWessel/wumpfetch-typings) **|** [GitHub](https://github.com/PassTheWessel/wumpfetch) **|** [NPM](https://npmjs.com/package/wumpfetch)
[![MIT License](https://img.shields.io/badge/license-MIT-007EC7.svg?style=flat-square)](/LICENSE) [![Travis Build Status](https://img.shields.io/travis/com/PassTheWessel/wumpfetch.svg?style=flat-square)](https://travis-ci.com/PassTheWessel/wumpfetch)
Wumpfetch is a fast, lightweight and easy to use HTTP client for Node.JS.
> [`Typings`](https://github.com/PassTheWessel/wumpfetch-typings) **|** [`GitHub`](https://github.com/PassTheWessel/wumpfetch) **|** [`NPM`](https://npmjs.com/package/wumpfetch)
<br>
## Installing
Wumpfetch can be installed with any package manager that supports the NPM registry, but the ones listed below are the most used ones.
```sh
$ yarn add wumpfetch # Install w/ Yarn (the superior package manager)
$ yarn add wumpfetch # Install w/ Yarn
$ npm i wumpfetch # Install w/ NPM
```
## Usage
## Example usage
##### Code
```js
const w = require('wumpfetch');
@@ -29,12 +37,46 @@ $ node test.js
```
### Sending data in a JSON body to a server
#### Getting a response from a REST API
**Code**:
```js
const w = require('wumpfetch');
// Using an URL and custom options
;(async() => {
let req = await w('https://aws.random.cat/meow', {
headers: {
'User-Agent': 'Project/0.0.1'
}
});
// Only URL
req = await w('https://aws.random.cat/meow');
// Only options
req = await w({
url: 'https://aws.random.cat/meow',
headers: {
'User-Agent': 'Project/0.0.1'
}
});
console.log(req.json());
})();
```
**Result**:
```sh
$ node test.js
{ file: 'https://purr.objects-us-east-1.dream.io/i/100_-_rURSo7L.gif' }
```
#### Chaining methods
You can also chain methods by adding `chaining: true` to `options`
```js
const w = require('wumpfetch');
;(async() => {
const r = await w('https://my-site.com/postboi', 'POST')
const r = await w('https://my-site.com/postboi', { method: 'POST', chaining: true })
.timeout(1000) // Set a 1s timeout
.query('video', 'wumpboye') // Add a query
.header({ 'Authorization': 'Pablito' }) // Set a header
@@ -44,36 +86,14 @@ const w = require('wumpfetch');
console.log(r.json()); // Returns the response in a JSON format
})();
```
#### Object
```js
const w = require('wumpfetch');
;(async() => {
const r = await w({
url: 'https://my-site.com/postboi',
data: { 'bear': 'cop' },
method: 'POST',
headers: { 'Authorization': 'Pablo' }
});
console.log(r.json());
})();
```
or
```js
const w = require('wumpfetch');
;(async() => {
const r = await w('https://my-site.com/postboi', { method: 'GET' });
console.log(r.json());
})();
```
## Projects using Wumpfetch
> If you want your own project listed, either create a pull request or an issue with the following content:
> * Package name
> * Project name
> * a **short** description of your package
>
> Also add the following if your project is a package/module/library:
> * NPM link (🔩)
> * Packagephobia link (⚖)
> * GitHub repository link (📂)

View File

@@ -5,8 +5,8 @@ console.log(`Using wumpfetch v${wump.version} [${wump.userAgent}]\n\n`);
;(async() => {
const requests = [
await wump({ url: 'https://jsonplaceholder.typicode.com/todos/1', parse: 'json' }).send(),
await wump('https://jsonplaceholder.typicode.com/todos/1').send()
await wump({ url: 'https://jsonplaceholder.typicode.com/todos/1', parse: 'json', chaining: true }).send(),
await wump('https://jsonplaceholder.typicode.com/todos/1')
];
console.log(`Test 1: \n${util.inspect(requests[0].body)}\n\n`);

View File

@@ -23,7 +23,8 @@ module.exports = class WumpRequest {
'url': typeof url === 'string' ? new u.URL(url) : obj && typeof o.url === 'string' ? new u.URL(o.url) : url,
'SDA': obj && typeof o.sendDataAs === 'string' ? o.sendDataAs : obj && o.data && typeof o.data === 'object' ? 'json' : undefined,
'data': obj && o.body ? o.body : obj && o.data ? o.data : obj && o.json ? o.json : obj && o.form ? qs.stringify(o.form) : undefined,
'parse': obj && o.parse ? o.parse : undefined,
'parse': obj && o.parse ? o.parse : undefined,
'chain': !!(obj && o.chaining),
'follow': !!(obj && o.followRedirects),
'rHeaders': obj && typeof o.headers === 'object' ? o.headers : {},
'streamed': !!(obj && o.streamed),
@@ -34,8 +35,10 @@ module.exports = class WumpRequest {
if (typeof o.core === 'object') Object.keys(o.core).forEach((v) => this.option(v, o.core[v]));
if (!this.o.chain) return this.send();
return this;
}
}
query (a, b) {
if (typeof a === 'object') Object.keys(a).forEach((v) => this.o.url.searchParams.append(v, a[v]));