2016-11-27 21:42:38 +00:00
|
|
|
import {METHOD_GET, getContentTypeFromHeaders, CONTENT_TYPE_FORM_URLENCODED, CONTENT_TYPE_FORM_DATA, CONTENT_TYPE_FILE} from '../common/constants';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2016-11-10 17:33:28 +00:00
|
|
|
import {getContentTypeHeader} from '../common/misc';
|
2017-01-27 01:00:27 +00:00
|
|
|
import {deconstructToParams, buildFromParams} from '../common/querystring';
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Request';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Request';
|
|
|
|
export const prefix = 'req';
|
|
|
|
|
|
|
|
export function init () {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2016-10-02 20:57:00 +00:00
|
|
|
url: '',
|
|
|
|
name: 'New Request',
|
|
|
|
method: METHOD_GET,
|
2016-11-22 19:42:10 +00:00
|
|
|
body: {},
|
2016-10-02 20:57:00 +00:00
|
|
|
parameters: [],
|
|
|
|
headers: [],
|
|
|
|
authentication: {},
|
2017-03-03 20:09:08 +00:00
|
|
|
metaSortKey: -1 * Date.now()
|
2016-11-10 01:15:27 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export function newBodyRaw (rawBody, contentType) {
|
2016-11-25 19:01:41 +00:00
|
|
|
if (typeof contentType !== 'string') {
|
2016-11-22 19:42:10 +00:00
|
|
|
return {text: rawBody};
|
|
|
|
}
|
|
|
|
|
|
|
|
const mimeType = contentType.split(';')[0];
|
|
|
|
return {mimeType, text: rawBody};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function newBodyFormUrlEncoded (parameters) {
|
2016-11-23 19:33:24 +00:00
|
|
|
// Remove any properties (eg. fileName) that might not fit
|
2017-03-01 21:15:56 +00:00
|
|
|
parameters = (parameters || []).map(parameter => {
|
|
|
|
const newParameter = {
|
|
|
|
name: parameter.name,
|
|
|
|
value: parameter.value
|
|
|
|
};
|
|
|
|
|
|
|
|
if (parameter.hasOwnProperty('id')) {
|
|
|
|
newParameter.id = parameter.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parameter.hasOwnProperty('disabled')) {
|
|
|
|
newParameter.disabled = parameter.disabled;
|
|
|
|
} else {
|
|
|
|
newParameter.disabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newParameter;
|
|
|
|
});
|
2016-11-23 19:33:24 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_URLENCODED,
|
|
|
|
params: parameters
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 22:26:52 +00:00
|
|
|
export function newBodyFile (path) {
|
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_FILE,
|
|
|
|
fileName: path
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-11-22 22:26:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export function newBodyForm (parameters) {
|
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_DATA,
|
2017-01-27 01:00:27 +00:00
|
|
|
params: parameters || []
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function migrate (doc) {
|
2016-12-08 20:29:40 +00:00
|
|
|
doc = migrateBody(doc);
|
|
|
|
doc = migrateWeirdUrls(doc);
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function create (patch = {}) {
|
2016-09-21 20:32:45 +00:00
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New Requests missing `parentId`', patch);
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function getById (id) {
|
|
|
|
return db.get(type, id);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function findByParentId (parentId) {
|
|
|
|
return db.find(type, {parentId: parentId});
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function update (request, patch) {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.docUpdate(request, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
export function updateMimeType (request, mimeType, doCreate = false) {
|
|
|
|
let headers = request.headers ? [...request.headers] : [];
|
2016-11-10 17:33:28 +00:00
|
|
|
const contentTypeHeader = getContentTypeHeader(headers);
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-01-27 01:00:27 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// 1. Update Content-Type header //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
2016-11-22 19:42:10 +00:00
|
|
|
|
|
|
|
if (!mimeType) {
|
2016-09-21 20:32:45 +00:00
|
|
|
// Remove the contentType header if we are un-setting it
|
|
|
|
headers = headers.filter(h => h !== contentTypeHeader);
|
|
|
|
} else if (contentTypeHeader) {
|
2016-11-22 19:42:10 +00:00
|
|
|
contentTypeHeader.value = mimeType;
|
2016-09-21 20:32:45 +00:00
|
|
|
} else {
|
2017-03-03 20:09:08 +00:00
|
|
|
headers.push({name: 'Content-Type', value: mimeType});
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 01:00:27 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// 2. Make a new request body //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
2016-11-23 19:33:24 +00:00
|
|
|
let body;
|
2017-01-27 01:00:27 +00:00
|
|
|
|
2016-11-23 19:33:24 +00:00
|
|
|
if (mimeType === request.body.mimeType) {
|
2017-01-27 01:00:27 +00:00
|
|
|
// Unchanged
|
2016-11-23 19:33:24 +00:00
|
|
|
body = request.body;
|
|
|
|
} else if (mimeType === CONTENT_TYPE_FORM_URLENCODED) {
|
2017-01-27 01:00:27 +00:00
|
|
|
// Urlencoded
|
2017-03-03 20:09:08 +00:00
|
|
|
body = request.body.params
|
|
|
|
? newBodyFormUrlEncoded(request.body.params)
|
|
|
|
: newBodyFormUrlEncoded(deconstructToParams(request.body.text));
|
2016-11-22 19:42:10 +00:00
|
|
|
} else if (mimeType === CONTENT_TYPE_FORM_DATA) {
|
2017-01-27 01:00:27 +00:00
|
|
|
// Form Data
|
2017-03-03 20:09:08 +00:00
|
|
|
body = request.body.params
|
|
|
|
? newBodyForm(request.body.params)
|
|
|
|
: newBodyForm(deconstructToParams(request.body.text));
|
2016-11-22 22:26:52 +00:00
|
|
|
} else if (mimeType === CONTENT_TYPE_FILE) {
|
2017-01-27 01:00:27 +00:00
|
|
|
// File
|
2016-11-23 19:33:24 +00:00
|
|
|
body = newBodyFile('');
|
2016-12-21 23:37:48 +00:00
|
|
|
} else if (typeof mimeType !== 'string') {
|
2017-01-27 01:00:27 +00:00
|
|
|
// No body
|
2016-12-21 23:37:48 +00:00
|
|
|
body = newBodyRaw('');
|
2016-11-22 19:42:10 +00:00
|
|
|
} else {
|
2017-01-27 01:00:27 +00:00
|
|
|
// Raw Content-Type (ex: application/json)
|
2017-03-03 20:09:08 +00:00
|
|
|
body = request.body.params
|
|
|
|
? newBodyRaw(buildFromParams(request.body.params, false), mimeType)
|
|
|
|
: newBodyRaw(request.body.text || '', mimeType);
|
2016-09-21 20:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 01:00:27 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// 2. create/update request //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
if (doCreate) {
|
|
|
|
return create(Object.assign({}, request, {headers, body}));
|
|
|
|
} else {
|
|
|
|
return update(request, {headers, body});
|
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-02-27 22:54:56 +00:00
|
|
|
export async function duplicate (request) {
|
2016-09-21 20:32:45 +00:00
|
|
|
const name = `${request.name} (Copy)`;
|
2017-02-27 22:54:56 +00:00
|
|
|
|
|
|
|
// Get sort key of next request
|
|
|
|
const q = {metaSortKey: {$gt: request.metaSortKey}};
|
|
|
|
const [nextRequest] = await db.find(type, q, {metaSortKey: 1});
|
|
|
|
const nextSortKey = nextRequest ? nextRequest.metaSortKey : request.metaSortKey + 100;
|
|
|
|
|
|
|
|
// Calculate new sort key
|
|
|
|
const sortKeyIncrement = (nextSortKey - request.metaSortKey) / 2;
|
|
|
|
const metaSortKey = request.metaSortKey + sortKeyIncrement;
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
return db.duplicate(request, {name, metaSortKey});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function remove (request) {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.remove(request);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function all () {
|
|
|
|
return db.all(type);
|
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
|
|
|
|
// ~~~~~~~~~~ //
|
|
|
|
// Migrations //
|
|
|
|
// ~~~~~~~~~~ //
|
|
|
|
|
2016-12-08 20:29:40 +00:00
|
|
|
function migrateBody (request) {
|
2016-12-01 18:48:49 +00:00
|
|
|
if (request.body && typeof request.body === 'object') {
|
|
|
|
return request;
|
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
|
|
|
|
// Second, convert all existing urlencoded bodies to new format
|
|
|
|
const contentType = getContentTypeFromHeaders(request.headers) || '';
|
|
|
|
const wasFormUrlEncoded = !!contentType.match(/^application\/x-www-form-urlencoded/i);
|
|
|
|
|
|
|
|
if (wasFormUrlEncoded) {
|
|
|
|
// Convert old-style form-encoded request bodies to new style
|
|
|
|
const params = deconstructToParams(request.body, false);
|
|
|
|
request.body = newBodyFormUrlEncoded(params);
|
2016-12-01 18:48:49 +00:00
|
|
|
} else if (!request.body && !contentType) {
|
|
|
|
request.body = {};
|
2016-11-22 19:42:10 +00:00
|
|
|
} else {
|
|
|
|
request.body = newBodyRaw(request.body, contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
2016-12-08 20:29:40 +00:00
|
|
|
|
|
|
|
function migrateWeirdUrls (request) {
|
|
|
|
// Some people seem to have requests with URLs that don't have the indexOf
|
|
|
|
// function. This should clear that up. This can be removed at a later date.
|
|
|
|
|
|
|
|
if (typeof request.url !== 'string') {
|
|
|
|
request.url = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|