The configuration object type.
import { ConfigAccessor } from "@daiso-tech/core/config-accessor";
import { z } from "zod";
const config = {}
const schema = z.object({
// Supports primitive string, number, boolean values
a: z.string(),
// Supports nested object with fields of string, number, boolean values
b: z.object({
a: z.string(),
}),
// Supports array with item of string, number, boolean values
c: z.string().array(),
// Supports array of object with fields of string, number, boolean values
d: z.object({
a: z.string(),
})
.array(),
})
const accessor = new ConfigAccessor({
config,
// Schema is optional, you can pass in a type
schema,
})
// Return the value of field a
accessor.get("a")
// Return the value of field b which is an object
accessor.get("b")
// Return the value of field b.a which is an primitive
accessor.get("b.a")
// Return the first item of field c which an primitive
accessor.get("c.1")
// Return the first item of field d which an object
accessor.get("d.2")
Get a value from the config, or null if missing/undefined.
The config value path to retrieve.
The value or null if not present.
Get a value from the config, or return default value.
The config value path to retrieve.
The default value when not found.
The value or default value.
ConfigAccessorprovides type-safe access to configuration objects, with optional schema validation.Note: Only exactly 1 level of nesting is supported for config fields (e.g., "app.foo"). Paths with more than 1 level of nesting are not supported.