Class: ConfigBound
Defined in: packages/config-bound/src/configBound.ts:238
A ConfigBound is the top level object that contains all the Sections and Binds. It is used to retrieve the values of the Elements from its binds.
Implements
Constructors
Constructor
new ConfigBound(
name,
binds?,
sections?,
logger?): ConfigBound;Defined in: packages/config-bound/src/configBound.ts:244
Parameters
| Parameter | Type | Default value |
|---|---|---|
name | string | undefined |
binds | Bind[] | [] |
sections | Section[] | [] |
logger? | Logger | undefined |
Returns
ConfigBound
Properties
| Property | Modifier | Type | Defined in |
|---|---|---|---|
binds | readonly | Bind[] | packages/config-bound/src/configBound.ts:241 |
name | readonly | string | packages/config-bound/src/configBound.ts:239 |
sections | readonly | Section[] | packages/config-bound/src/configBound.ts:242 |
Methods
addBind()
addBind(bind): void;Defined in: packages/config-bound/src/configBound.ts:265
Adds a Bind to the ConfigBound
Parameters
| Parameter | Type | Description |
|---|---|---|
bind | Bind | The Bind to add |
Returns
void
addSection()
addSection(section): void;Defined in: packages/config-bound/src/configBound.ts:274
Adds a Section to the ConfigBound
Parameters
| Parameter | Type | Description |
|---|---|---|
section | Section | The Section to add |
Returns
void
get()
get<T>(sectionName, elementName): Promise<T | undefined>;Defined in: packages/config-bound/src/configBound.ts:308
Gets the value of an Element using the first available Bind.
Type Parameters
| Type Parameter | Default type |
|---|---|
T | unknown |
Parameters
| Parameter | Type | Description |
|---|---|---|
sectionName | string | The name of the section |
elementName | string | The name of the element |
Returns
Promise<T | undefined>
The value of the element, or undefined if not found
Throws
SectionNotFoundException If section doesn't exist
Throws
ElementNotFoundException If element doesn't exist in section
Throws
ConfigInvalidException If value fails validation
See
Implementation of
getOrThrow()
getOrThrow<T>(sectionName, elementName): Promise<T>;Defined in: packages/config-bound/src/configBound.ts:387
Gets the value of an Element, throwing an error if the value is undefined. This is useful for required configuration values.
Error Handling:
- Throws
SectionNotFoundExceptionif the section doesn't exist - Throws
ElementNotFoundExceptionif the element doesn't exist OR if no value is found - Throws
ConfigInvalidExceptionif the value fails validation
Type Parameters
| Type Parameter | Default type |
|---|---|
T | unknown |
Parameters
| Parameter | Type | Description |
|---|---|---|
sectionName | string | The name of the section |
elementName | string | The name of the element |
Returns
Promise<T>
The value of the element (never undefined)
Throws
SectionNotFoundException If section doesn't exist
Throws
ElementNotFoundException If element doesn't exist or value is undefined
Throws
ConfigInvalidException If value fails validation
getSections()
getSections(): Section[];Defined in: packages/config-bound/src/configBound.ts:291
Gets the Sections of the ConfigBound
Returns
Section[]
The Sections
getValidationErrors()
getValidationErrors(): Promise<object[]>;Defined in: packages/config-bound/src/configBound.ts:422
Gets all validation errors for the current configuration without throwing. Useful for collecting all errors at once or implementing custom error handling.
Returns
Promise<object[]>
Array of validation errors with path and message
validate()
validate(): Promise<void>;Defined in: packages/config-bound/src/configBound.ts:403
Validates all configuration values eagerly without retrieving them. This allows you to catch configuration errors at startup rather than at first access.
Returns
Promise<void>
Throws
ConfigInvalidException if any value fails validation
See
- getValidationErrors for information about how validation errors are returned
- ConfigInvalidException
createConfig()
static createConfig<T>(schema, options?): Promise<TypedConfigBound<T>>;Defined in: packages/config-bound/src/configBound.ts:484
Creates a ConfigBound instance from a declarative schema with full type safety. This is the recommended way to create configuration objects.
Type Parameters
| Type Parameter |
|---|
T extends ConfigSchema<Record<string, unknown>> |
Parameters
| Parameter | Type |
|---|---|
schema | T |
options? | { binds?: Bind[]; logger?: Logger; name?: string; validateOnInit?: boolean; } |
options.binds? | Bind[] |
options.logger? | Logger |
options.name? | string |
options.validateOnInit? | boolean |
Returns
Promise<TypedConfigBound<T>>
Example
const config = await ConfigBound.createConfig(
{
port: {
default: 3000,
validator: z.number(),
description: 'Server port'
},
database: {
properties: {
host: { default: 'localhost', validator: z.string() },
port: { default: 5432, validator: z.number() }
}
}
},
{
binds: [new EnvVarBind()]
}
);
const port = await config.get('app', 'port'); // Fully type-safe with autocomplete!