insomnia-plugin-uu-sync
Version:
Import manager and table viewer for Insomnia
70 lines • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const insomnia_file_1 = require("./insomnia-file");
class FileNormalizer {
normalizeExport(oneLineJson) {
const content = JSON.parse(oneLineJson);
content.__export_date = "2020-01-01T00:00:00.000Z";
content.resources.forEach(this.setTimestamps);
content.resources.sort(this.compareResources.bind(this));
// Delete obsolete
content.resources.forEach((resource) => {
if (insomnia_file_1.InsomniaFile.isRequestResource(resource) && resource.body.__uuSyncText) {
delete resource.body.__uuSyncText;
}
});
return content;
}
normalizeImport(content) {
this.getRequestsWithBody(content.resources).forEach(this.importMultiLineRequests);
return content;
}
getRequestsWithBody(resources) {
return resources.filter((resource) => insomnia_file_1.InsomniaFile.isRequestResource(resource) && resource.body);
}
setTimestamps(resource) {
if (resource.modified) {
resource.modified = 1600000000000;
}
if (resource.created) {
resource.created = 1600000000000;
}
}
compareResources(a, b) {
return this.getSortKey(a).localeCompare(this.getSortKey(b));
}
getSortKey(resource) {
let key = "";
switch (resource._type) {
case "workspace":
key = "0";
break;
case "request_group":
key = "1";
break;
case "request":
key = "2";
break;
// this elements should be somewhere at the end
case "cookie_jar":
key = "7";
break;
case "api_spec":
key = "8";
break;
case "environment":
key = "9";
break;
}
key += resource._id;
return key;
}
importMultiLineRequests(resource) {
if (resource.body.__uuSyncText) {
resource.body.text = resource.body.__uuSyncText.join("\n");
delete resource.body.__uuSyncText;
}
}
}
exports.default = FileNormalizer;
//# sourceMappingURL=file-normalizer.js.map