insomnia/app/lib/network.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-04-09 21:08:55 +00:00
import networkRequest from 'request'
2016-04-10 02:58:48 +00:00
import render from './render'
import * as db from '../database'
import {DEBOUNCE_MILLIS} from './constants'
2016-04-09 21:08:55 +00:00
2016-07-07 22:06:18 +00:00
function buildRequestConfig (request, patch = {}) {
2016-04-09 21:08:55 +00:00
const config = {
2016-04-10 02:58:48 +00:00
method: request.method,
body: request.body,
2016-04-28 07:30:26 +00:00
headers: {},
// Setup redirect rules
followRedirect: true,
maxRedirects: 10,
// Unzip gzipped responses
gzip: true
2016-04-09 21:08:55 +00:00
};
2016-04-17 04:28:57 +00:00
// Default the proto if it doesn't exist
if (request.url.indexOf('://') === -1) {
config.url = `https://${request.url}`;
2016-04-17 04:28:57 +00:00
} else {
config.url = request.url;
}
2016-04-17 04:28:57 +00:00
// Set basic auth if we need to
2016-04-09 21:08:55 +00:00
if (request.authentication.username) {
config.auth = {
user: request.authentication.username,
pass: request.authentication.password
}
}
2016-04-10 02:58:48 +00:00
for (let i = 0; i < request.headers.length; i++) {
let header = request.headers[i];
2016-04-12 00:39:49 +00:00
if (header.name) {
config.headers[header.name] = header.value;
}
2016-04-09 21:08:55 +00:00
}
2016-04-28 07:30:26 +00:00
// TODO: this needs to account for existing URL params (hint: use request)
2016-04-12 06:03:52 +00:00
config.url += request.params.map((p, i) => {
const name = encodeURIComponent(p.name);
const value = encodeURIComponent(p.value);
return `${i === 0 ? '?' : '&'}${name}=${value}`;
}).join('');
2016-04-29 08:15:37 +00:00
2016-04-28 07:30:26 +00:00
return Object.assign(config, patch);
}
2016-07-07 22:06:18 +00:00
function actuallySend (request, callback) {
2016-04-28 07:30:26 +00:00
// TODO: Handle cookies
2016-07-07 22:06:18 +00:00
let config = buildRequestConfig(request, {
jar: networkRequest.jar(),
2016-07-14 01:12:42 +00:00
followRedirect: true
2016-07-07 22:06:18 +00:00
}, true);
2016-04-09 21:41:27 +00:00
const startTime = Date.now();
networkRequest(config, function (err, response) {
2016-04-10 02:58:48 +00:00
if (err) {
2016-06-18 21:02:27 +00:00
db.responseCreate({
parentId: request._id,
millis: Date.now() - startTime,
error: err.toString()
});
console.warn(`Request to ${config.url} failed`, err);
2016-04-10 02:58:48 +00:00
} else {
2016-04-18 04:39:15 +00:00
db.responseCreate({
parentId: request._id,
2016-04-10 06:37:22 +00:00
statusCode: response.statusCode,
statusMessage: response.statusMessage,
2016-04-18 04:39:15 +00:00
contentType: response.headers['content-type'],
2016-06-19 07:21:43 +00:00
url: request.url,
2016-04-18 04:39:15 +00:00
millis: Date.now() - startTime,
bytes: response.connection.bytesRead,
body: response.body,
2016-04-10 06:37:22 +00:00
headers: Object.keys(response.headers).map(name => {
const value = response.headers[name];
return {name, value};
})
2016-04-10 02:58:48 +00:00
});
}
2016-04-28 07:30:26 +00:00
2016-04-18 04:39:15 +00:00
callback(err);
2016-04-09 21:08:55 +00:00
});
}
2016-07-07 22:06:18 +00:00
export function send (requestId, callback) {
// First, lets wait for all debounces to finish
setTimeout(() => {
db.requestById(requestId).then(request => {
db.requestGroupById(request.parentId).then(requestGroup => {
const environment = requestGroup ? requestGroup.environment : {};
2016-04-29 08:15:37 +00:00
if (environment) {
// SNEAKY HACK: Render nested object by converting it to JSON then rendering
const template = JSON.stringify(request);
request = JSON.parse(render(template, environment));
}
2016-04-29 08:15:37 +00:00
actuallySend(request, callback);
});
})
}, DEBOUNCE_MILLIS);
}