mirror of
https://github.com/Wessel/wumpfetch.git
synced 2026-06-05 23:25:42 +02:00
Add chaining option and update README
This commit is contained in:
84
README.md
84
README.md
@@ -1,17 +1,25 @@
|
||||
# Wumpfetch
|
||||
[](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)
|
||||
[](/LICENSE) [](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 (📂)
|
||||
|
||||
@@ -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`);
|
||||
|
||||
@@ -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]));
|
||||
|
||||
Reference in New Issue
Block a user