UNPKG

mockit-cli

Version:

Mock any API in seconds. Config-driven. CLI-powered. No backend required.

100 lines (99 loc) 3.79 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const package_json_1 = __importDefault(require("../package.json")); const fs_1 = __importDefault(require("fs")); const zod_1 = __importDefault(require("zod")); const utils_1 = require("./utils"); const createServer_1 = require("./createServer"); const configSchema_1 = require("./configSchema"); const program = new commander_1.Command(); program .name(package_json_1.default.name) .description(package_json_1.default.description) .version(package_json_1.default.version); program .command('start') .description('Start the mock server') .option('-c, --config <path>', 'Path to the mock config file') .option('-h, --host <host>', 'Host to bind the server to', 'localhost') .option('-p, --port <port>', 'Port to bind the server to', '3000') .option('-r, --randomize', 'Randomize success/failure response') .action((options) => { if (!options.config) { console.error(`Error: No config file provided. Use -c or --config to specify the path to the JSON config file. Use --help for more information.`); process.exit(1); } if (!fs_1.default.existsSync(options.config)) { console.error(`Error: Config file not found at ${options.config}`); process.exit(1); } let routes = []; try { const configFileContent = fs_1.default.readFileSync(options.config, 'utf8'); const config = JSON.parse(configFileContent); const validatedConfig = configSchema_1.configSchema.parse(config); if ((0, utils_1.getType)(config) !== '[object Object]') { console.error(`Error: Config file is not a valid JSON object.`); process.exit(1); } routes = Object.entries(validatedConfig).map(([path, routeDefinition]) => ({ path, method: routeDefinition.method, response: routeDefinition.response, delay: routeDefinition.delay, })); const host = options.host; const port = options.port; console.log(`Here`, options.randomize); (0, createServer_1.createServer)({ host, port }, routes, options.randomize) .then(() => { console.log(`Server running at http://${host}:${port}`); }) .catch((error) => { console.error(`Error starting server: ${error.message}`); }); } catch (error) { if (error instanceof zod_1.default.ZodError) { console.error(`Validation Error: ${error.errors.map((e) => e.message).join(', ')}`); } else { console.error(`Error: Config file is not a valid JSON. ${error}`); } process.exit(1); } }); program .command('generate') .description('Generate a JSON config file with example routes') .option('-n, --endpoints <number>', 'Number of endpoints to generate', '5') .action((options) => { const count = parseInt(options.endpoints, 10); if (isNaN(count) || count <= 0) { console.error('Error: Invalid number of endpoints. Please provide a positive integer.'); process.exit(1); } const config = {}; for (let i = 1; i <= count; i++) { config[`/endpoint${i}`] = { method: 'GET', delay: 0, response: { success: { status: 200 }, error: { status: 500 } } }; } console.log(JSON.stringify(config, null, 2)); }); program.parse();