UNPKG

@common-grants/cli

Version:
150 lines (149 loc) 6.37 kB
"use strict"; 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 check_args_1 = require("./check-args"); const error_utils_1 = require("./utils/error-utils"); const convert_openapi_v3_1 = require("./utils/convert-openapi-v3"); const path = __importStar(require("path")); const fs = __importStar(require("fs")); const yaml = __importStar(require("js-yaml")); 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) { // Get the base spec and implementation spec const baseSpecPath = options.base || getBaseSpecPath(options.protocolVersion); const baseDoc = await loadAndParseSpec(baseSpecPath); const implDoc = await loadAndParseSpec(specPath); // Validate the specs const errors = validateSpecs(baseDoc, implDoc); // If there are errors, throw an error 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; /** * Validate the specs against the base spec. * * This involves: * 1) Checking for required routes that are missing * 2) Checking for unexpected routes prefixed with /common-grants/ * 3) Checking that matching routes are compatible */ function validateSpecs(baseDoc, implDoc) { const errors = new error_utils_1.ErrorCollection(); errors.addErrors((0, check_missing_routes_1.checkMissingRequiredRoutes)(baseDoc, implDoc).getAllErrors()); errors.addErrors((0, check_extra_routes_1.checkExtraRoutes)(baseDoc, implDoc).getAllErrors()); errors.addErrors((0, check_matching_routes_1.checkMatchingRoutes)(baseDoc, implDoc).getAllErrors()); return errors; } /** * Load and parse a spec file. * * This involves: * 1) Reading the spec file from the file system * 2) Parsing the spec content using the appropriate parser * 3) Converting the spec to OpenAPI v3.0 if needed * 4) Dereferencing the spec, ignoring circular references */ async function loadAndParseSpec(specPath) { const specContent = fs.readFileSync(specPath, "utf8"); // Detect format based on file extension const fileExtension = path.extname(specPath).toLowerCase(); let rawSpec; // Parse the spec content using the appropriate parser switch (fileExtension) { case ".json": rawSpec = JSON.parse(specContent); break; case ".yaml": case ".yml": rawSpec = yaml.load(specContent); break; default: throw new Error(`Unsupported file extension: ${fileExtension}`); } // Convert the spec to OpenAPI v3.0 if needed const convertedSpec = (0, convert_openapi_v3_1.convertOpenApiToV3)(rawSpec); // Dereference the spec, ignoring circular references to prevent stack overflows return (await swagger_parser_1.default.dereference(convertedSpec, { dereference: { circular: "ignore" }, })); } /** * Get the path to the default base spec file packaged with the CLI. */ function getBaseSpecPath(version) { const openapiDir = path.resolve(__dirname, "../../../lib/openapi"); // Check that OpenAPI directory exists and contains at least one version if (!fs.existsSync(openapiDir)) { throw new Error(`OpenAPI directory not found at ${openapiDir}`); } if (check_args_1.availableVersions.length === 0) { throw new Error(`No OpenAPI spec files found in ${openapiDir}`); } // Use the provided version number, or default to latest version const selectedVersion = version || check_args_1.availableVersions[0]; const specPath = path.join(openapiDir, `openapi.${selectedVersion}.yaml`); if (!fs.existsSync(specPath)) { throw new Error(`Base spec file not found at ${specPath}`); } return specPath; }