Add most common used methods (w.get, w.post, w.patch, etc.)

This commit is contained in:
Wessel T
2019-02-19 19:13:17 +01:00
parent e2cf4b5056
commit dd20fe8ab5
4 changed files with 44 additions and 20 deletions

View File

@@ -27,43 +27,45 @@ $ node test.js
```
### Sending data in a JSON body to a server
#### Chaining methods
```js
const w = require( 'wumpfetch' );
const w = require('wumpfetch');
;( async() => {
const r = await w( 'https://my-site.com/postboi', 'POST' )
.query( 'video', 'wumpboye' ) // Add a query
.header({ 'Authorization': 'Pablito' }) // Set a header
;(async() => {
const r = await w('https://my-site.com/postboi', 'POST')
.timeout( 1000 ) // Set a 1s timeout
.query('video', 'wumpboye') // Add a query
.header({ 'Authorization': 'Pablito' }) // Set a header
.body({ x: 'y', z: 1, beep: 'boop', chocolate: true }) // Send a json body
.timeout( 1000 ) // Set a 1s timeout
.send();
.send(); // Finish the chain by sending the rquest
console.log( r.json() );
console.log(r.json()); // Returns the response in a JSON format
})();
```
or
#### Object
```js
const w = require( 'wumpfetch' );
const w = require('wumpfetch');
;( async() => {
;(async() => {
const r = await w({
url: 'https://my-site.com/postboi',
method: 'GET',
data: { 'bear': 'cop' },
method: 'POST',
headers: {
'Authorization': 'Pablo'
}
});
console.log( r.json() );
console.log(r.json());
})();
```
or
```js
const w = require( 'wumpfetch' );
const w = require('wumpfetch');
;( async() => {
const r = await w( 'https://my-site.com/postboi', { method: 'GET' });
console.log( r.json() );
;(async() => {
const r = await w('https://my-site.com/postboi', { method: 'GET' });
console.log(r.json());
})();
```