openapi-diff
Version:
A CLI tool to identify differences between Swagger/OpenAPI specs.
70 lines (69 loc) • 4.26 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.findDifferencesInOperations = 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_request_bodies_1 = require("./find-diffs-in-request-bodies");
const find_diffs_in_responses_1 = require("./find-diffs-in-responses");
const findAddedMethodDifferences = (sourceOperations, destinationOperations) => {
return (0, get_added_keys_from_objects_1.getAddedKeysFromObjects)(sourceOperations, destinationOperations)
.map((addedMethod) => {
const addedDestinationOperation = destinationOperations[addedMethod];
return (0, create_difference_1.createDifference)({
action: 'add',
destinationSpecOrigins: [addedDestinationOperation.originalValue],
propertyName: 'method',
source: 'openapi-diff',
sourceSpecOrigins: []
});
});
};
const findRemovedMethodDifferences = (sourceOperations, destinationOperations) => {
return (0, get_removed_keys_from_objects_1.getRemovedKeysFromObjects)(sourceOperations, destinationOperations)
.map((removedMethod) => {
const removedSourceOperation = sourceOperations[removedMethod];
return (0, create_difference_1.createDifference)({
action: 'remove',
destinationSpecOrigins: [],
propertyName: 'method',
source: 'openapi-diff',
sourceSpecOrigins: [removedSourceOperation.originalValue]
});
});
};
const findDifferencesInMatchingMethods = (sourceOperations, destinationOperations) => __awaiter(void 0, void 0, void 0, function* () {
const whenDifferencesForAllMatchingMethods = (0, get_common_keys_from_objects_1.getCommonKeysFromObjects)(sourceOperations, destinationOperations)
.map((matchingMethod) => __awaiter(void 0, void 0, void 0, function* () {
const matchingSourceOperation = sourceOperations[matchingMethod];
const matchingDestinationOperation = destinationOperations[matchingMethod];
const requestBodyDifferences = yield (0, find_diffs_in_request_bodies_1.findDifferencesInRequestBodies)(matchingSourceOperation.requestBody, matchingDestinationOperation.requestBody);
const responsesDifferences = yield (0, find_diffs_in_responses_1.findDifferencesInResponses)(matchingSourceOperation.responses, matchingDestinationOperation.responses);
return [
...requestBodyDifferences,
...responsesDifferences
];
}));
const differencesByMethod = yield Promise.all(whenDifferencesForAllMatchingMethods);
return differencesByMethod
.reduce((allDifferences, methodDifferences) => [...allDifferences, ...methodDifferences], []);
});
const findDifferencesInOperations = (sourceOperations, destinationOperations) => __awaiter(void 0, void 0, void 0, function* () {
const matchingMethodsDifferences = yield findDifferencesInMatchingMethods(sourceOperations, destinationOperations);
return [
...findAddedMethodDifferences(sourceOperations, destinationOperations),
...findRemovedMethodDifferences(sourceOperations, destinationOperations),
...matchingMethodsDifferences
];
});
exports.findDifferencesInOperations = findDifferencesInOperations;