UNPKG

@sentzunhat/zacatl

Version:

A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.

51 lines 1.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadJSON = void 0; const fs_1 = require("fs"); const error_1 = require("../error/index.js"); const utils_1 = require("../utils/index.js"); const loadJSON = (filePath, schema) => { try { const content = (0, fs_1.readFileSync)(filePath, 'utf-8'); const cleanContent = filePath.endsWith('.jsonc') ? content.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, '') : content; const data = JSON.parse(cleanContent); if (schema) { return schema.parse(data); } return data; } catch (error) { if ((0, utils_1.isNodeError)(error) && error.code === 'ENOENT') { throw new error_1.NotFoundError({ message: `JSON file not found: ${filePath}`, component: 'JSONLoader', operation: 'loadJSON', }); } if ((0, utils_1.isSyntaxError)(error)) { throw new error_1.BadRequestError({ message: `Invalid JSON in file: ${filePath}`, reason: error.message, component: 'JSONLoader', operation: 'loadJSON', }); } if ((0, utils_1.isZodError)(error)) { throw new error_1.ValidationError({ message: `JSON validation failed for file: ${filePath}`, reason: "Data doesn't match the expected schema", component: 'JSONLoader', operation: 'loadJSON', metadata: { filePath, issues: error.issues, }, }); } throw error; } }; exports.loadJSON = loadJSON; //# sourceMappingURL=json.js.map