mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 23:00:30 +00:00
549ce23ce8
* All projects into monorepo * Update CI * More CI updates * Extracted a bunch of things into packages * Publish - insomnia-plugin-base64@1.0.1 - insomnia-plugin-default-headers@1.0.2 - insomnia-plugin-file@1.0.1 - insomnia-plugin-hash@1.0.1 - insomnia-plugin-now@1.0.1 - insomnia-plugin-request@1.0.1 - insomnia-plugin-response@1.0.1 - insomnia-plugin-uuid@1.0.1 - insomnia-cookies@0.0.2 - insomnia-importers@1.5.2 - insomnia-prettify@0.0.3 - insomnia-url@0.0.2 - insomnia-xpath@0.0.2 * A bunch of small fixes * Improved build script * Fixed * Merge dangling files * Usability refactor * Handle duplicate plugin names
90 lines
2.1 KiB
JavaScript
90 lines
2.1 KiB
JavaScript
import deepEqual from 'deep-equal';
|
|
import * as models from './index';
|
|
import * as db from '../common/database';
|
|
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_IN_REQUEST_DIFF = [
|
|
'_id',
|
|
'type',
|
|
'created',
|
|
'modified',
|
|
'metaSortKey',
|
|
'description',
|
|
'name'
|
|
];
|
|
|
|
export function init () {
|
|
return {
|
|
compressedRequest: null
|
|
};
|
|
}
|
|
|
|
export function migrate (doc) {
|
|
return doc;
|
|
}
|
|
|
|
export function getById (id) {
|
|
return db.get(type, id);
|
|
}
|
|
|
|
export async function create (request) {
|
|
if (!request.type === models.request.type) {
|
|
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);
|
|
return db.docCreate(type, {parentId, compressedRequest});
|
|
} else {
|
|
// Re-use the latest version if not modified since
|
|
return latestRequestVersion;
|
|
}
|
|
}
|
|
|
|
export function getLatestByParentId (parentId) {
|
|
return db.getMostRecentlyModified(type, {parentId});
|
|
}
|
|
|
|
export async function restore (requestVersionId) {
|
|
const requestVersion = await getById(requestVersionId);
|
|
|
|
// Older responses won't have versions saved with them
|
|
if (!requestVersion) {
|
|
return null;
|
|
}
|
|
|
|
const request = decompressObject(requestVersion.compressedRequest);
|
|
return models.request.update(request);
|
|
}
|
|
|
|
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_IN_REQUEST_DIFF.includes(key)) {
|
|
continue;
|
|
}
|
|
|
|
if (!deepEqual(rOld[key], rNew[key])) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|