@zfig/yaml-loader
v0.9.15
Published
YAML loader for zfig configuration library
Readme
@zfig/yaml-loader
YAML file support for zfig.
Install
npm install zfig @zfig/yaml-loaderUsage
Import for side-effects to enable YAML config files:
import "@zfig/yaml-loader";
import { resolve } from "zfig";
const config = resolve(configSchema, { configPath: "./config.yaml" });How It Works
On import, registers loaders for .yaml and .yml extensions:
registerLoader(".yaml", loadYaml);
registerLoader(".yml", loadYaml);After import, resolve() automatically handles YAML files.
API
loadYaml(path: string)
Parses a YAML file and returns its contents.
import { loadYaml } from "@zfig/yaml-loader";
const data = loadYaml("./config.yaml");
// { db: { host: "localhost", port: 5432 } }Returns:
Record<string, unknown>- parsed YAML contentundefined- if file doesn't exist
Throws:
ConfigError- if YAML syntax is invalid
Examples
Config file
# config.yaml
db:
host: localhost
port: 5432
logging:
level: infoWith zfig
import "@zfig/yaml-loader";
import { schema, field, resolve } from "zfig";
import { z } from "zod";
const configSchema = schema({
db: {
host: field({ type: z.string(), default: "localhost" }),
port: field({ type: z.number(), default: 5432 }),
},
logging: {
level: field({ type: z.enum(["debug", "info", "warn", "error"]) }),
},
});
const config = resolve(configSchema, { configPath: "./config.yaml" });Direct usage
import { loadYaml } from "@zfig/yaml-loader";
const data = loadYaml("./config.yml");
if (data) {
console.log(data.db.host);
}License
MIT
