@common-grants/cli
Version:
The CommonGrants protocol CLI tool
89 lines (88 loc) • 4.02 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultCheckService = void 0;
const swagger_parser_1 = __importDefault(require("@apidevtools/swagger-parser"));
const check_extra_routes_1 = require("./utils/check-extra-routes");
const check_matching_routes_1 = require("./utils/check-matching-routes");
const check_missing_routes_1 = require("./utils/check-missing-routes");
const error_utils_1 = require("./utils/error-utils");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
class DefaultCheckService {
/** Check that an API implementation matches its spec. */
async checkApi(apiUrl, specPath, options) {
console.log("Mock: Checking API", { apiUrl, specPath, options });
}
/**
* Check that a spec is valid and compliant with CommonGrants base spec.
*
* This involves:
* 1) Fetching and parsing both specs
* 2) Checking for required routes that are missing
* 3) Checking for unexpected routes prefixed with /common-grants/
* 4) Checking that matching routes are compatible
*/
async checkSpec(specPath, options) {
// Parse and dereference the spec
const doc = (await swagger_parser_1.default.dereference(specPath));
// If no base spec provided, compile from TypeSpec
const baseSpecPath = options.base || getBaseSpecPath();
const baseDoc = (await swagger_parser_1.default.dereference(baseSpecPath));
const errors = new error_utils_1.ErrorCollection();
errors.addErrors((0, check_missing_routes_1.checkMissingRequiredRoutes)(baseDoc, doc).getAllErrors());
errors.addErrors((0, check_extra_routes_1.checkExtraRoutes)(baseDoc, doc).getAllErrors());
errors.addErrors((0, check_matching_routes_1.checkMatchingRoutes)(baseDoc, doc).getAllErrors());
if (errors.getAllErrors().length > 0) {
const message = new error_utils_1.ErrorFormatter(errors).format();
throw new Error(`Spec validation failed:\n${message}`);
}
else {
console.log("Spec is valid and compliant with base spec");
}
}
}
exports.DefaultCheckService = DefaultCheckService;
function getBaseSpecPath() {
const baseSpecPath = path.resolve(__dirname, "../../../lib/openapi.yaml");
if (fs.existsSync(baseSpecPath)) {
return baseSpecPath;
}
console.log("Not found", baseSpecPath);
throw new Error(`Could not find base spec file at ${baseSpecPath}`);
}