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

View File

@@ -1,5 +1,14 @@
const { join } = require('path'); const { join } = require('path');
const common = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ];
const request = require(join(__dirname, 'model', 'WumpRequest.js'));
module.exports = (url, method) => { module.exports = (url, method) => {
return new(require(join( __dirname, 'model', 'WumpRequest.js')))(url, method); return new request(url, method);
}; };
common.forEach((v) => {
module.exports[v.toLowerCase()] = (url, method = v) => {
return new request(url, method);
}
});

View File

@@ -24,9 +24,9 @@ module.exports = class WumpRequest {
'data' : obj && o.data ? o.data : obj && o.form ? stringify( o.form ) : undefined, 'data' : obj && o.data ? o.data : obj && o.form ? stringify( o.form ) : undefined,
'parse' : obj && o.parse ? o.parse : undefined, 'parse' : obj && o.parse ? o.parse : undefined,
'follow' : !!( obj && o.followRedirects ), 'follow' : !!( obj && o.followRedirects ),
'rHeaders' : obj && typeof o.headers === 'object' ? o.headers : {},
'streamed' : !!( obj && o.streamed ), 'streamed' : !!( obj && o.streamed ),
'compressed' : !!( obj && o.compressed ), 'compressed' : !!( obj && o.compressed ),
'rHeaders' : obj && typeof o.headers === 'object' ? o.headers : {},
'timeoutTime': obj && typeof o.timeout === 'number' ? o.timeout : null, 'timeoutTime': obj && typeof o.timeout === 'number' ? o.timeout : null,
'coreOptions': obj && typeof o.coreOptions === 'object' ? o.coreOptions : {} 'coreOptions': obj && typeof o.coreOptions === 'object' ? o.coreOptions : {}
}; };

15
test.js
View File

@@ -2,9 +2,22 @@ const u = require('util');
const l = require('lumah'); const l = require('lumah');
const w = require('./createRequest.js'); const w = require('./createRequest.js');
l.register('Fetch a cat image', async (end) => { console.log(w)
l.register('Fetch cat image A', async (end) => {
const r = await w({ url: 'https://aws.random.cat/meow', parse: 'json' }).send(); const r = await w({ url: 'https://aws.random.cat/meow', parse: 'json' }).send();
end(r.statusCode === 200 ? true : false, r.statusCode === 200 ? u.inspect(r.body) : r.statusCode); end(r.statusCode === 200 ? true : false, r.statusCode === 200 ? u.inspect(r.body) : r.statusCode);
}); });
l.register('Fetch cat image B', (end) => {
w({ url: 'https://aws.random.cat/meow', parse: 'json' }).send().then((r) => {
end(r.statusCode === 200 ? true : false, r.statusCode === 200 ? u.inspect(r.body) : r.statusCode)
});
});
l.register('Fetch cat image C', async (end) => {
const r = await w.get({ url: 'https://aws.random.cat/meow', parse: 'json' }).send();
end(r.statusCode === 200 ? true : false, r.statusCode === 200 ? u.inspect(r.body) : r.statusCode);
});
l.start(); l.start();