openapi-diff
Version:
A CLI tool to identify differences between Swagger/OpenAPI specs.
67 lines (66 loc) • 4.32 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findDiffsInPaths = void 0;
const get_added_keys_from_objects_1 = require("./common/get-added-keys-from-objects");
const get_common_keys_from_objects_1 = require("./common/get-common-keys-from-objects");
const get_removed_keys_from_objects_1 = require("./common/get-removed-keys-from-objects");
const create_difference_1 = require("./create-difference");
const find_diffs_in_operations_1 = require("./find-diffs-in-operations");
const normalize_path_1 = require("./normalize-path");
const findAddedPathDifferences = (sourcePathItems, destinationPathItems) => {
return (0, get_added_keys_from_objects_1.getAddedKeysFromObjects)(sourcePathItems, destinationPathItems)
.map((addedPathName) => {
const addedDestinationPathItem = destinationPathItems[addedPathName];
return (0, create_difference_1.createDifference)({
action: 'add',
destinationSpecOrigins: [addedDestinationPathItem.originalValue],
propertyName: 'path',
source: 'openapi-diff',
sourceSpecOrigins: []
});
});
};
const findRemovedPathDifferences = (sourcePathItems, destinationPathItems) => {
return (0, get_removed_keys_from_objects_1.getRemovedKeysFromObjects)(sourcePathItems, destinationPathItems)
.map((removedPathName) => {
const removedSourcePathItem = sourcePathItems[removedPathName];
return (0, create_difference_1.createDifference)({
action: 'remove',
destinationSpecOrigins: [],
propertyName: 'path',
source: 'openapi-diff',
sourceSpecOrigins: [removedSourcePathItem.originalValue]
});
});
};
const findDifferencesInMatchingPaths = (sourcePathItems, destinationPathItems) => __awaiter(void 0, void 0, void 0, function* () {
const matchingPaths = (0, get_common_keys_from_objects_1.getCommonKeysFromObjects)(sourcePathItems, destinationPathItems);
const whenFindDifferencesInAllOperations = matchingPaths.map((matchingPathItem) => (0, find_diffs_in_operations_1.findDifferencesInOperations)(sourcePathItems[matchingPathItem].operations, destinationPathItems[matchingPathItem].operations));
const differencesByOperation = yield Promise.all(whenFindDifferencesInAllOperations);
const flattenDifferences = differencesByOperation.reduce((allDifferences, operationDifferences) => [...allDifferences, ...operationDifferences], []);
return flattenDifferences;
});
const normalizePathItems = (parsedPathItems) => Object.keys(parsedPathItems).reduce((normalizedParsedPathItems, pathName) => {
const parsedPathItem = parsedPathItems[pathName];
const normalizedPathName = (0, normalize_path_1.normalizePath)(pathName);
normalizedParsedPathItems[normalizedPathName] = parsedPathItem;
return normalizedParsedPathItems;
}, {});
const findDiffsInPaths = (sourcePathItems, destinationPathItems) => __awaiter(void 0, void 0, void 0, function* () {
const normalizedSourcePathItems = normalizePathItems(sourcePathItems);
const normalizedDestinationPathItems = normalizePathItems(destinationPathItems);
const addedPaths = findAddedPathDifferences(normalizedSourcePathItems, normalizedDestinationPathItems);
const removedPaths = findRemovedPathDifferences(normalizedSourcePathItems, normalizedDestinationPathItems);
const matchingPaths = yield findDifferencesInMatchingPaths(normalizedSourcePathItems, normalizedDestinationPathItems);
return [...addedPaths, ...removedPaths, ...matchingPaths];
});
exports.findDiffsInPaths = findDiffsInPaths;