Replaced unirest with request-promise. Tried using the built it http

stuff but it's just silly.
This commit is contained in:
David Diaz 2017-10-20 18:59:37 -06:00
parent d1dfcb8c7d
commit 7ecd5428c6
6 changed files with 2505 additions and 36 deletions

View File

@ -1,5 +1,5 @@
version: 1 version: 1
host: 'https://jsonplaceholder.typicode.com' host: 'http://jsonplaceholder.typicode.com'
GET /posts/: GET /posts/:
alias: posts alias: posts
@ -9,3 +9,5 @@ POST /posts/:
GET /users/$posts.body.0.userId: GET /users/$posts.body.0.userId:
alias: post-user alias: post-user
params:
hello: 'world'

2448
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,13 @@
{ {
"name": "beau", "name": "beau",
"version": "0.2.1", "version": "0.2.2",
"description": "A tool for testing JSON APIs", "description": "A tool for testing JSON APIs",
"main": "beau.js", "main": "beau.js",
"author": "Sergio Diaz <seich@martianwabbit.com>", "author": "Sergio Diaz <seich@martianwabbit.com>",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "jest" "test": "jest",
"watch": "jest --watch"
}, },
"dependencies": { "dependencies": {
"cli-color": "^1.1.0", "cli-color": "^1.1.0",
@ -14,8 +15,9 @@
"commander": "^2.9.0", "commander": "^2.9.0",
"eyes": "^0.1.8", "eyes": "^0.1.8",
"js-yaml": "^3.7.0", "js-yaml": "^3.7.0",
"requireg": "^0.1.6", "request": "^2.83.0",
"unirest": "^0.5.1" "request-promise-native": "^1.0.5",
"requireg": "^0.1.6"
}, },
"repository": "git@github.com:Seich/Beau.git", "repository": "git@github.com:Seich/Beau.git",
"devDependencies": { "devDependencies": {
@ -23,7 +25,7 @@
}, },
"jest": { "jest": {
"testEnvironment": "node", "testEnvironment": "node",
"notify": false "notify": true
}, },
"bin": { "bin": {
"beau": "./bin/beau" "beau": "./bin/beau"

View File

@ -1,4 +1,4 @@
const unirest = require('unirest'); const request = require('request-promise-native');
const {httpVerbs, requestRegex, replacementRegex} = require('./shared'); const {httpVerbs, requestRegex, replacementRegex} = require('./shared');
const RequestList = require('./requestList'); const RequestList = require('./requestList');
const RequestCache = require('./requestCache'); const RequestCache = require('./requestCache');
@ -71,39 +71,41 @@ class Request {
} }
}); });
let request = unirest(settings.method, settings.endpoint); return request({
url: settings.endpoint,
method: settings.method,
headers: settings.headers,
qs: settings.query,
body: settings.payload,
json: true,
simple: false,
resolveWithFullResponse: true
})
request.headers(settings.headers); .then(response => {
request.query(settings.query); let results = {
request.send(settings.payload); request: {
headers: response.request.headers,
body: response.request.body,
endpoint: response.request.uri.href
},
response: {
status: response.statusCode,
headers: response.headers,
body: response.body
},
body: response.body
};
return new Promise((resolve, reject) => { cache.add(`$${this.ALIAS}`, results);
request.end(res => {
if (res.error !== false) {
let error = typeof res.error.code === 'undefined' ? `Invalid Request ${res.error}` : res.error.code;
return reject(`HTTP Request failed: ${error}`);
}
let results = { return results;
request: { })
headers: res.request.headers,
body: res.request.body,
endpoint: settings.endpoint
},
response: {
status: res.status,
headers: res.headers,
body: res.body,
}
};
results.body = results.response.body; .catch(function({error}) {
throw new Error(error.message);
cache.add(`$${this.ALIAS}`, results);
return resolve(results);
});
}); });
}); });
} }
} }

View File

@ -60,6 +60,10 @@ class RequestList {
} }
loadPlugins() { loadPlugins() {
if (typeof this.config.PLUGINS === 'undefined') {
return;
}
return this.config.PLUGINS.map(plugin => { return this.config.PLUGINS.map(plugin => {
let name = plugin; let name = plugin;
let settings = null; let settings = null;

View File

@ -1,4 +1,15 @@
const httpVerbs = ['POST', 'GET', 'PUT', 'PATCH', 'DELETE']; const httpVerbs = [
'GET',
'HEAD',
'POST',
'PUT',
'DELETE',
'CONNECT',
'OPTIONS',
'TRACE',
'PATCH'
];
const requestRegex = new RegExp(`(${httpVerbs.join('|')})\\s(.*)`, 'i'); const requestRegex = new RegExp(`(${httpVerbs.join('|')})\\s(.*)`, 'i');
const replacementRegex = /\$([a-zA-Z\.\d\-\_]*)/g; const replacementRegex = /\$([a-zA-Z\.\d\-\_]*)/g;