From c581ae33b0a75c16f49422c9ef5fbe72ad9ae172 Mon Sep 17 00:00:00 2001 From: Wessel Tip Date: Fri, 12 Apr 2019 18:23:18 +0200 Subject: [PATCH] Add chaining option and update README --- README.md | 84 +++++++++++++++++++++++++--------------- __test__/main.js | 4 +- lib/model/WumpRequest.js | 7 +++- 3 files changed, 59 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index ddbb5b8..751231f 100644 --- a/README.md +++ b/README.md @@ -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) + + -> A lightweight and fast Node.js HTTP client which can be used in various ways +> Wumpfetch - 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) + +
## 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 (📂) diff --git a/__test__/main.js b/__test__/main.js index f558180..8110d35 100644 --- a/__test__/main.js +++ b/__test__/main.js @@ -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`); diff --git a/lib/model/WumpRequest.js b/lib/model/WumpRequest.js index 77d5e15..e52cef9 100644 --- a/lib/model/WumpRequest.js +++ b/lib/model/WumpRequest.js @@ -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]));