insomnia/packages/insomnia-app/app/models/request-version.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

import deepEqual from 'deep-equal';
import * as models from './index';
import * as db from '../common/database';
2018-06-25 17:42:50 +00:00
import { compressObject, decompressObject } from '../common/misc';
export const name = 'Request Version';
export const type = 'RequestVersion';
export const prefix = 'rvr';
export const canDuplicate = false;
const FIELDS_TO_IGNORE = [
'_id',
'type',
'created',
'modified',
'metaSortKey',
'description',
'parentId',
'name'
];
2018-06-25 17:42:50 +00:00
export function init() {
return {
compressedRequest: null
};
}
2018-06-25 17:42:50 +00:00
export function migrate(doc) {
return doc;
}
2018-06-25 17:42:50 +00:00
export function getById(id) {
return db.get(type, id);
}
2018-06-25 17:42:50 +00:00
export async function create(request) {
if (!request.type === models.request.type) {
2018-06-25 17:42:50 +00:00
throw new Error(
`New ${type} was not given a valid ${models.request.type} instance`
);
}
const parentId = request._id;
const latestRequestVersion = await getLatestByParentId(parentId);
const latestRequest = latestRequestVersion
? decompressObject(latestRequestVersion.compressedRequest)
: null;
const hasChanged = _diffRequests(latestRequest, request);
if (hasChanged) {
// Create a new version if the request has been modified
const compressedRequest = compressObject(request);
2018-06-25 17:42:50 +00:00
return db.docCreate(type, { parentId, compressedRequest });
} else {
// Re-use the latest version if not modified since
return latestRequestVersion;
}
}
2018-06-25 17:42:50 +00:00
export function getLatestByParentId(parentId) {
return db.getMostRecentlyModified(type, { parentId });
}
2018-06-25 17:42:50 +00:00
export async function restore(requestVersionId) {
const requestVersion = await getById(requestVersionId);
// Older responses won't have versions saved with them
if (!requestVersion) {
return null;
}
const requestPatch = decompressObject(requestVersion.compressedRequest);
const originalRequest = await models.request.getById(requestPatch._id);
// Only restore fields that aren't blacklisted
for (const field of FIELDS_TO_IGNORE) {
delete requestPatch[field];
}
return models.request.update(originalRequest, requestPatch);
}
2018-06-25 17:42:50 +00:00
function _diffRequests(rOld, rNew) {
if (!rOld) {
return true;
}
for (const key of Object.keys(rOld)) {
// Skip fields that aren't useful
if (FIELDS_TO_IGNORE.includes(key)) {
continue;
}
if (!deepEqual(rOld[key], rNew[key])) {
return true;
}
}
return false;
}