UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

88 lines (87 loc) 3.16 kB
/** * @module ConfigAccessor */ import { type StandardSchemaV1 } from "@standard-schema/spec"; import { type BaseConfig, type IConfigAccessor, type RestrictedPaths, type PathValue } from "../../config-accessor/contracts/_module.js"; /** * Settings for configuring an {@link ConfigAccessor | `ConfigAccessor`} instance. * * @template TConfig The configuration object type. * @group Implementations */ export type ConfigAccessorSettings<TOutputConfig extends BaseConfig = BaseConfig, TInputConfig = TOutputConfig> = { /** * The configuration object to be accessed. * This is the raw config that will be used directly or validated against the schema if provided. */ config: TInputConfig; /** * The optional schema used to validate and type the config. */ schema?: StandardSchemaV1<TInputConfig, TOutputConfig>; }; /** * `ConfigAccessor` provides 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. * * @template TOutputConfig The configuration object type. * @group Implementations */ export declare class ConfigAccessor<TInputConfig extends BaseConfig, TOutputConfig extends BaseConfig = TInputConfig> implements IConfigAccessor<TOutputConfig> { private readonly configObject; /** * @example * ```ts * 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") * ``` */ constructor(settings: ConfigAccessorSettings<TOutputConfig, TInputConfig>); private extractPathSegments; get<TPath extends RestrictedPaths<TOutputConfig>>(path: TPath): PathValue<TOutputConfig, TPath>; getOr<TPath extends RestrictedPaths<TOutputConfig>>(path: TPath, defaultValue: NonNullable<PathValue<TOutputConfig, TPath>>): NonNullable<PathValue<TOutputConfig, TPath>>; }