server-mockup-api
Version:
To get full fake REST API with no efforts. Add/Modify API endpoint and response through JSON file.
36 lines (35 loc) • 1.32 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class Utils {
readAllFiles(sourcePath, jsonContent) {
const dirs = fs_1.default.readdirSync(sourcePath);
dirs.forEach((val) => {
const pth = path_1.default.join(sourcePath, val);
const stats = fs_1.default.statSync(pth);
if (stats.isFile()) {
jsonContent = Object.assign(Object.assign({}, jsonContent), this.readFileContent(pth));
}
else if (stats.isDirectory()) {
jsonContent = Object.assign(Object.assign({}, jsonContent), this.readAllFiles(pth, jsonContent));
}
return jsonContent;
});
return jsonContent;
}
readFileContent(filePath) {
const buf = fs_1.default.readFileSync(filePath);
try {
return JSON.parse(buf.toString());
}
catch (e) {
throw new Error(`Invalid Json Format in ${filePath}`);
}
}
}
exports.default = Utils;