apiveritas
Version:
Lightweight CLI tool for consumer-driven API contract testing via JSON schema and payload comparisons.
67 lines (66 loc) • 3.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigLoader = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const Logger_1 = require("../utils/Logger");
const PathValidator_1 = require("./PathValidator");
const chalk_1 = __importDefault(require("chalk"));
class ConfigLoader {
constructor(configFolder) {
this.logger = new Logger_1.Logger({ level: 'info' });
// Default to 'apiveritas' folder in cwd if none provided
this.configFolder = configFolder
? path_1.default.resolve(configFolder)
: path_1.default.resolve(process.cwd(), 'apiveritas');
this.configPath = path_1.default.join(this.configFolder, 'config.json');
}
loadConfig() {
const defaultPayloadsPath = path_1.default.resolve(this.configFolder, 'payloads');
const defaultReportsPath = path_1.default.resolve(this.configFolder, 'reports');
if (fs_1.default.existsSync(this.configPath)) {
try {
const rawData = fs_1.default.readFileSync(this.configPath, 'utf-8');
const parsed = JSON.parse(rawData);
const validatedPayloadsPath = PathValidator_1.PathValidator.validateFolderPath(parsed.payloadsPath, defaultPayloadsPath, 'payloadsPath', this.logger);
const validatedReportsPath = PathValidator_1.PathValidator.validateFolderPath(parsed.reportsPath, defaultReportsPath, 'reportsPath', this.logger);
const baseUrl = parsed.baseUrl ?? 'http://localhost:8080';
return {
strictSchema: parsed.strictSchema !== false,
strictValues: parsed.strictValues !== false,
tolerateEmptyResponses: parsed.tolerateEmptyResponses === true,
payloadsPath: validatedPayloadsPath,
reportsPath: validatedReportsPath,
baseUrl,
enableMockServer: parsed.enableMockServer === true,
};
}
catch (err) {
this.logger.warn('! Failed to parse config.json, using defaults.');
}
}
else {
console.log(chalk_1.default.yellow(`\nconfig.json not found at ${this.configPath}.\n\nRun 'apiveritas init' to create a working folder structure\n`));
}
// Defaults fallback
return {
strictSchema: true,
strictValues: false,
tolerateEmptyResponses: true,
payloadsPath: defaultPayloadsPath,
reportsPath: defaultReportsPath,
baseUrl: 'http://localhost:8080',
enableMockServer: true,
};
}
updateConfig(newValues) {
const config = this.loadConfig();
const updated = { ...config, ...newValues };
fs_1.default.writeFileSync(this.configPath, JSON.stringify(updated, null, 2), 'utf-8');
console.log(`Config updated successfully at ${this.configPath}. Run: "apiveritas config" to verify changes.\n`);
}
}
exports.ConfigLoader = ConfigLoader;