YAML loader for zfig configuration library
npm install @zfig/yaml-loader
YAML file support for zfig.
``bash`
npm install zfig @zfig/yaml-loader
Import for side-effects to enable YAML config files:
`typescript
import "@zfig/yaml-loader";
import { resolve } from "zfig";
const config = resolve(configSchema, { configPath: "./config.yaml" });
`
On import, registers loaders for .yaml and .yml extensions:
`typescript`
registerLoader(".yaml", loadYaml);
registerLoader(".yml", loadYaml);
After import, resolve() automatically handles YAML files.
Parses a YAML file and returns its contents.
`typescript
import { loadYaml } from "@zfig/yaml-loader";
const data = loadYaml("./config.yaml");
// { db: { host: "localhost", port: 5432 } }
`
Returns:
- Record - parsed YAML contentundefined
- - if file doesn't exist
Throws:
- ConfigError - if YAML syntax is invalid
`yamlconfig.yaml
db:
host: localhost
port: 5432
logging:
level: info
`
`typescript
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" });
`
`typescript
import { loadYaml } from "@zfig/yaml-loader";
const data = loadYaml("./config.yml");
if (data) {
console.log(data.db.host);
}
``
MIT