insomnia/app/models/request.js

73 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-11-10 05:56:23 +00:00
import {METHOD_GET, PREVIEW_MODE_SOURCE} from '../common/constants';
import * as db from '../common/database';
2016-11-10 17:33:28 +00:00
import * as misc from '../common/misc';
import {getContentTypeHeader} from '../common/misc';
export const type = 'Request';
export const prefix = 'req';
export function init () {
2016-11-10 01:15:27 +00:00
return {
url: '',
name: 'New Request',
method: METHOD_GET,
body: '',
parameters: [],
headers: [],
authentication: {},
metaPreviewMode: PREVIEW_MODE_SOURCE,
metaResponseFilter: '',
metaSortKey: -1 * Date.now()
2016-11-10 01:15:27 +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);
}
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 updateContentType (request, contentType) {
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
if (!contentType) {
// Remove the contentType header if we are un-setting it
headers = headers.filter(h => h !== contentTypeHeader);
} else if (contentTypeHeader) {
contentTypeHeader.value = contentType;
} else {
headers.push({name: 'Content-Type', value: contentType})
}
2016-11-10 17:33:28 +00:00
return update(request, {headers});
}
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);
}