@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.
46 lines • 1.59 kB
JavaScript
import { readFileSync } from 'fs';
import { load } from 'js-yaml';
import { BadRequestError, NotFoundError, ValidationError } from '../error/index.js';
import { isNodeError, isZodError } from '../utils/index.js';
export const loadYML = (filePath, schema) => {
try {
const content = readFileSync(filePath, 'utf-8');
const data = load(content);
if (schema) {
return schema.parse(data);
}
return data;
}
catch (error) {
if (isNodeError(error) && error.code === 'ENOENT') {
throw new NotFoundError({
message: `YAML file not found: ${filePath}`,
component: 'YAMLLoader',
operation: 'loadYML',
});
}
if (error instanceof Error && error.name === 'YAMLException') {
throw new BadRequestError({
message: `Invalid YAML in file: ${filePath}`,
reason: error.message,
component: 'YAMLLoader',
operation: 'loadYML',
});
}
if (isZodError(error)) {
throw new ValidationError({
message: `YAML validation failed for file: ${filePath}`,
reason: "Data doesn't match the expected schema",
component: 'YAMLLoader',
operation: 'loadYML',
metadata: {
filePath,
issues: error.issues,
},
});
}
throw error;
}
};
export const loadYAML = loadYML;
//# sourceMappingURL=yml.js.map