insomnia/app/models/request.js

161 lines
4.0 KiB
JavaScript
Raw Normal View History

import {METHOD_GET, getContentTypeFromHeaders, CONTENT_TYPE_FORM_URLENCODED, CONTENT_TYPE_FORM_DATA} 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';
import {deconstructToParams} from '../common/querystring';
import {CONTENT_TYPE_JSON} from '../common/constants';
2016-11-22 22:26:52 +00:00
import {CONTENT_TYPE_XML} from '../common/constants';
import {CONTENT_TYPE_FILE} from '../common/constants';
import {CONTENT_TYPE_TEXT} from '../common/constants';
export const name = 'Request';
export const type = 'Request';
export const prefix = 'req';
export function init () {
2016-11-10 01:15:27 +00:00
return {
_schema: 1,
url: '',
name: 'New Request',
method: METHOD_GET,
body: {},
parameters: [],
headers: [],
authentication: {},
metaSortKey: -1 * Date.now()
2016-11-10 01:15:27 +00:00
};
}
export function newBodyRaw (rawBody, contentType) {
if (!contentType) {
return {text: rawBody};
}
const mimeType = contentType.split(';')[0];
return {mimeType, text: rawBody};
}
export function newBodyFormUrlEncoded (parameters) {
// Remove any properties (eg. fileName) that might not fit
parameters = (parameters || []).map(
2016-11-24 01:24:59 +00:00
p => ({name: p.name, value: p.value, disabled: p.disabled})
);
return {
mimeType: CONTENT_TYPE_FORM_URLENCODED,
params: parameters
}
}
2016-11-22 22:26:52 +00:00
export function newBodyFile (path) {
return {
mimeType: CONTENT_TYPE_FILE,
fileName: path
}
}
export function newBodyForm (parameters) {
return {
mimeType: CONTENT_TYPE_FORM_DATA,
params: parameters
}
}
export function migrate (doc) {
const schema = doc._schema || 0;
if (schema <= 0) {
doc = migrateTo1(doc);
doc._schema = 1;
}
return doc;
}
export function create (patch = {}) {
2016-09-21 20:32:45 +00:00
if (!patch.parentId) {
throw new Error('New Requests missing `parentId`', patch);
}
return db.docCreate(type, patch);
}
2016-09-21 20:32:45 +00:00
export function getById (id) {
return db.get(type, id);
}
2016-09-21 20:32:45 +00:00
export function findByParentId (parentId) {
return db.find(type, {parentId: parentId});
}
2016-09-21 20:32:45 +00:00
export function update (request, patch) {
2016-09-21 20:32:45 +00:00
return db.docUpdate(request, patch);
}
2016-09-21 20:32:45 +00:00
export function updateMimeType (request, mimeType) {
2016-09-21 20:32:45 +00:00
let headers = [...request.headers];
2016-11-10 17:33:28 +00:00
const contentTypeHeader = getContentTypeHeader(headers);
2016-09-21 20:32:45 +00:00
// 1. Update Content-Type header
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) {
contentTypeHeader.value = mimeType;
2016-09-21 20:32:45 +00:00
} else {
headers.push({name: 'Content-Type', value: mimeType})
}
// 2. Make a new request body
// TODO: When switching mime-type, try to convert formats nicely
let body;
if (mimeType === request.body.mimeType) {
body = request.body;
} else if (mimeType === CONTENT_TYPE_FORM_URLENCODED) {
body = newBodyFormUrlEncoded(request.body.params);
} else if (mimeType === CONTENT_TYPE_FORM_DATA) {
body = newBodyForm(request.body.params || []);
2016-11-22 22:26:52 +00:00
} else if (mimeType === CONTENT_TYPE_FILE) {
body = newBodyFile('');
} else {
body = newBodyRaw(request.body.text || '', mimeType);
2016-09-21 20:32:45 +00:00
}
return update(request, {headers, body});
}
2016-09-21 20:32:45 +00:00
export function duplicate (request) {
2016-09-21 20:32:45 +00:00
const name = `${request.name} (Copy)`;
2016-11-10 01:15:27 +00:00
const metaSortKey = request.metaSortKey + 1;
return db.duplicate(request, {name, metaSortKey})
}
2016-09-21 20:32:45 +00:00
export function remove (request) {
2016-09-21 20:32:45 +00:00
return db.remove(request);
}
export function all () {
return db.all(type);
}
// ~~~~~~~~~~ //
// Migrations //
// ~~~~~~~~~~ //
function migrateTo1 (request) {
// 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);
} else {
request.body = newBodyRaw(request.body, contentType);
}
return request;
}