apiveritas
Version:
Lightweight CLI tool for consumer-driven API contract testing via JSON schema and payload comparisons.
90 lines (89 loc) • 3.48 kB
JavaScript
;
/**
* @file ResponseSaver.ts
* @author Mario Galea
* @description
* Handles saving API responses to timestamped payload folders during test runs.
* Each suite's responses are organized into subfolders under the suite/timestamp directory.
* This supports historical test data tracking and diffing between runs.
*
* Example folder structure:
* payloads/<suite>/<timestamp>/<name>.json
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResponseSaver = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const Logger_1 = require("../utils/Logger");
/**
* Saves API responses to timestamped directories for tracking test outputs over time.
*/
class ResponseSaver {
/**
* Initializes the `ResponseSaver`, preparing the base payload directory.
*/
constructor() {
this.logger = new Logger_1.Logger();
this.baseFolder = path_1.default.join(process.cwd(), 'apiveritas', 'payloads');
this.timestampFolder = this.generateTimestamp();
}
/**
* Generates a unique timestamp folder name in the format `YYYY.MM.DD.HHmmss`.
*
* @returns {string} The timestamp string used for the current run.
*/
generateTimestamp() {
const now = new Date();
return `${now.getFullYear()}.${(now.getMonth() + 1).toString().padStart(2, '0')}.${now.getDate().toString().padStart(2, '0')}.${now.getHours()
.toString()
.padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}${now
.getSeconds()
.toString()
.padStart(2, '0')}`;
}
/**
* Ensures that the given folder exists. Creates it recursively if it does not.
*
* @param {string} folderPath - The absolute path of the folder to check/create.
*/
ensureFolderExists(folderPath) {
if (!fs_1.default.existsSync(folderPath)) {
fs_1.default.mkdirSync(folderPath, { recursive: true });
this.logger.info(`\n Storage folder: ${chalk_1.default.white(folderPath)}\n`);
}
}
/**
* Saves a JSON API response to the correct suite and test name folder.
*
* @param {string} suite - The name of the test suite (used as subfolder).
* @param {string} name - The individual test name (filename).
* @param {any} data - The API response payload to save.
*/
saveResponse(suite, name, data) {
const suiteTimestampFolder = path_1.default.join(this.baseFolder, suite, this.timestampFolder);
this.ensureFolderExists(suiteTimestampFolder);
const filePath = path_1.default.join(suiteTimestampFolder, `${name}.json`);
fs_1.default.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
}
/**
* Returns the folder name used for this run (e.g., "2025.06.13.143005").
*
* @returns {string} The timestamp folder name.
*/
getTimestampFolderName() {
return this.timestampFolder;
}
/**
* Returns the absolute path to the base folder where payloads are stored.
*
* @returns {string} Absolute path to the payload base folder.
*/
getBaseFolderPath() {
return this.baseFolder;
}
}
exports.ResponseSaver = ResponseSaver;