openapi-diff
Version:
A CLI tool to identify differences between Swagger/OpenAPI specs.
64 lines (63 loc) • 4.07 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
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_response_bodies_1 = require("./find-diffs-in-response-bodies");
const find_diffs_in_response_headers_1 = require("./find-diffs-in-response-headers");
const findAddedDifferencesInResponses = (sourceResponses, destinationResponses) => {
const addedResponses = get_added_keys_from_objects_1.getAddedKeysFromObjects(sourceResponses, destinationResponses);
return addedResponses.map((statusCode) => {
const parsedResponse = destinationResponses[statusCode];
return create_difference_1.createDifference({
action: 'add',
destinationSpecOrigins: [parsedResponse.originalValue],
propertyName: 'response.status-code',
source: 'openapi-diff',
sourceSpecOrigins: []
});
});
};
const findRemovedDifferencesInResponses = (sourceResponses, destinationResponses) => {
const removedResponses = get_removed_keys_from_objects_1.getRemovedKeysFromObjects(sourceResponses, destinationResponses);
return removedResponses.map((statusCode) => {
const parsedResponse = sourceResponses[statusCode];
return create_difference_1.createDifference({
action: 'remove',
destinationSpecOrigins: [],
propertyName: 'response.status-code',
source: 'openapi-diff',
sourceSpecOrigins: [parsedResponse.originalValue]
});
});
};
const findDifferencesInMatchingResponses = (sourceResponses, destinationResponses) => __awaiter(this, void 0, void 0, function* () {
const whenDifferencesForAllMatchingResponses = get_common_keys_from_objects_1.getCommonKeysFromObjects(sourceResponses, destinationResponses)
.map((matchingResponse) => __awaiter(this, void 0, void 0, function* () {
const matchingSourceResponse = sourceResponses[matchingResponse];
const matchingDestinationResponse = destinationResponses[matchingResponse];
const responseBodiesDifferences = yield find_diffs_in_response_bodies_1.findDifferencesInResponseBodies(matchingSourceResponse, matchingDestinationResponse);
const responseHeadersDifferences = find_diffs_in_response_headers_1.findDifferencesInResponseHeaders(matchingSourceResponse.headers, matchingDestinationResponse.headers);
return [...responseBodiesDifferences, ...responseHeadersDifferences];
}));
const differencesByResponse = yield Promise.all(whenDifferencesForAllMatchingResponses);
return differencesByResponse
.reduce((allDifferences, responseDifferences) => [...allDifferences, ...responseDifferences], []);
});
exports.findDifferencesInResponses = (sourceResponses, destinationResponses) => __awaiter(this, void 0, void 0, function* () {
const matchingResponsesDifferences = yield findDifferencesInMatchingResponses(sourceResponses, destinationResponses);
return [
...findAddedDifferencesInResponses(sourceResponses, destinationResponses),
...findRemovedDifferencesInResponses(sourceResponses, destinationResponses),
...matchingResponsesDifferences
];
});