Skip to content
This repository was archived by the owner on Jul 3, 2023. It is now read-only.

Check for x-current-version for all paths #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,34 @@ Promise.promisifyAll(dir);
Promise.promisifyAll(SwaggerParser);

var constraints = {
requireCurrentVersion: {
inclusion: {
within: [true, false],
message: "'%{value}' is not allowed"
}
},
visibilityFilter: {
exclusion: {
inclusion: {
within: ["EXTERNAL", "INTERNAL"],
message: "'%{value}' is not allowed"
}
},
statusFilter: {
exclusion: {
inclusion: {
within: ["PROPOSED", "IN_DEVELOPMENT", "RELEASED"],
message: "'%{value}' is not allowed"
}
},
stripExtensions: {

}
};

exports.preparePromise = function(specDir, specFile, infoFile, host, schemes, basePath, options) {
validate(options, constraints);
var validationResult = validate(options, constraints);
if (validationResult) {
return Promise.reject("*** Invalid options:\n " + _.reduce(Object.keys(validationResult), (result, key) => {
return result + (key + ":\n " + validationResult[key].join("\n "));
}, ""))
}

return dir.promiseFiles(specDir)
.then((files) => {
return files.filter(function(file) {
Expand Down Expand Up @@ -61,46 +70,70 @@ exports.preparePromise = function(specDir, specFile, infoFile, host, schemes, ba
unsorted.tags = _.sortBy(unsorted.tags, 'name');
return unsorted;
})
.then((sorted) => {
if (options.visibilityFilter && options.statusFilter) {
.then((sorted) => {
if (options.requireCurrentVersion) {
paths = sorted.paths;
pathsWithoutVersion = [];
_.forIn(paths, function(value, path) {
version = _.get(value, "x-current-version", null);
if (!version) {
pathsWithoutVersion.push(path);
}
})

if (pathsWithoutVersion) {
return Promise.reject("*** Missing 'x-current-version'. Paths affected:\n" + pathsWithoutVersion.join("\n"));
}
}

return sorted;
})
.then((sorted) => {
paths = sorted.paths;
_.forIn(paths, function(value, path) {
// check visibility
if (options.visibilityFilter) {
visibility = _.get(value, "x-visibility", "");
stat = _.get(value, "x-status", "");
if (visibility !== options.visibilityFilter || visibility === "") {
delete sorted.paths[path];
}
if (stat !== options.statusFilter || stat === "") {
}

// check status
if (options.statusFilter) {
status = _.get(value, "x-status", "");

if (status !== options.statusFilter || status === "") {
delete sorted.paths[path];
}
});
}
}
});
return sorted;
})
.catch(e => {
Promise.reject(e);
return Promise.reject(e);
});
}

exports.prepare = function(specDir, specFile, infoFile, host, schemes, basePath, options, callback) {
this.preparePromise(specDir, specFile, infoFile, host, schemes, basePath, options)
.then((apis) => {
callback(null, apis);
callback(apis, null);
})
.catch(e => {
callback(e, null);
callback(null, e);
});
}

exports.write = function(specDir, specFile, infoFile, host, schemes, basePath, outputFile, options, callback) {
this.preparePromise(specDir, specFile, infoFile, host, schemes, basePath, outputFile, options)
this.preparePromise(specDir, specFile, infoFile, host, schemes, basePath, options)
.then((apis) => {
fs.writeFileAsync(outputFile, JSON.stringify(apis), {})
.then(() => {
callback(null);
});
})
.catch(e => {
callback(e, null);
callback(e);
});
}