@maroonedsoftware/appconfig 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +310 -0
- package/dist/app.config.builder.d.ts +76 -0
- package/dist/app.config.builder.d.ts.map +1 -0
- package/dist/app.config.d.ts +114 -0
- package/dist/app.config.d.ts.map +1 -0
- package/dist/app.config.provider.d.ts +43 -0
- package/dist/app.config.provider.d.ts.map +1 -0
- package/dist/app.config.source.d.ts +25 -0
- package/dist/app.config.source.d.ts.map +1 -0
- package/dist/app.config.source.options.d.ts +16 -0
- package/dist/app.config.source.options.d.ts.map +1 -0
- package/dist/helpers.d.ts +8 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +581 -0
- package/dist/index.js.map +1 -0
- package/dist/object.visitor.d.ts +52 -0
- package/dist/object.visitor.d.ts.map +1 -0
- package/dist/providers/app.config.provider.dotenv.d.ts +81 -0
- package/dist/providers/app.config.provider.dotenv.d.ts.map +1 -0
- package/dist/providers/app.config.provider.gcp.secrets.d.ts +95 -0
- package/dist/providers/app.config.provider.gcp.secrets.d.ts.map +1 -0
- package/dist/sources/app.config.source.dotenv.d.ts +40 -0
- package/dist/sources/app.config.source.dotenv.d.ts.map +1 -0
- package/dist/sources/app.config.source.json.d.ts +47 -0
- package/dist/sources/app.config.source.json.d.ts.map +1 -0
- package/dist/sources/app.config.source.yaml.d.ts +48 -0
- package/dist/sources/app.config.source.yaml.d.ts.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Marooned Software
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
# @maroonedsoftware/appconfig
|
|
2
|
+
|
|
3
|
+
A flexible, type-safe configuration management library with support for multiple sources and value transformation.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Type-safe access** - Full TypeScript support with generics
|
|
8
|
+
- **Multiple sources** - Load configuration from JSON files, YAML files, `.env` files, and more
|
|
9
|
+
- **Value transformation** - Resolve environment variables and GCP secrets in configuration values
|
|
10
|
+
- **Deep merging** - Combine configurations from multiple sources with predictable override behavior
|
|
11
|
+
- **Extensible** - Create custom sources and providers for your specific needs
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @maroonedsoftware/appconfig
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Basic Usage
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { AppConfig } from '@maroonedsoftware/appconfig';
|
|
25
|
+
|
|
26
|
+
const config = new AppConfig({
|
|
27
|
+
database: { host: 'localhost', port: 5432 },
|
|
28
|
+
api: { timeout: 5000 },
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const host = config.get('database').host; // Type-safe access
|
|
32
|
+
const port = config.getNumber('port'); // Returns as number
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Using the Builder
|
|
36
|
+
|
|
37
|
+
The `AppConfigBuilder` allows you to load configuration from multiple sources and apply transformations:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import {
|
|
41
|
+
AppConfigBuilder,
|
|
42
|
+
AppConfigSourceJson,
|
|
43
|
+
AppConfigSourceYaml,
|
|
44
|
+
AppConfigSourceDotenv,
|
|
45
|
+
AppConfigProviderDotenv,
|
|
46
|
+
} from '@maroonedsoftware/appconfig';
|
|
47
|
+
|
|
48
|
+
const config = await new AppConfigBuilder()
|
|
49
|
+
.addSource(new AppConfigSourceJson('./config.json'))
|
|
50
|
+
.addSource(new AppConfigSourceYaml('./config.yaml'))
|
|
51
|
+
.addSource(new AppConfigSourceDotenv('.env'))
|
|
52
|
+
.addProvider(new AppConfigProviderDotenv())
|
|
53
|
+
.build<MyConfigType>();
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Environment Variable Resolution
|
|
57
|
+
|
|
58
|
+
Use `${env:VAR_NAME}` syntax in your JSON config files to reference environment variables:
|
|
59
|
+
|
|
60
|
+
**config.json:**
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"database": {
|
|
65
|
+
"host": "${env:DB_HOST}",
|
|
66
|
+
"port": "${env:DB_PORT}",
|
|
67
|
+
"url": "${env:DATABASE_URL}"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**.env:**
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
DB_HOST=localhost
|
|
76
|
+
DB_PORT=5432
|
|
77
|
+
DATABASE_URL=postgres://localhost:5432/mydb
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**app.ts:**
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const config = await new AppConfigBuilder()
|
|
84
|
+
.addSource(new AppConfigSourceJson('./config.json'))
|
|
85
|
+
.addSource(new AppConfigSourceDotenv('.env'))
|
|
86
|
+
.addProvider(new AppConfigProviderDotenv())
|
|
87
|
+
.build();
|
|
88
|
+
|
|
89
|
+
// Values are resolved from environment variables
|
|
90
|
+
// Numeric values like DB_PORT are automatically parsed as numbers
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### GCP Secret Manager Integration
|
|
94
|
+
|
|
95
|
+
Use `${gcp:SECRET_NAME}` syntax to resolve secrets from Google Cloud Platform Secret Manager:
|
|
96
|
+
|
|
97
|
+
**config.json:**
|
|
98
|
+
|
|
99
|
+
```json
|
|
100
|
+
{
|
|
101
|
+
"database": {
|
|
102
|
+
"password": "${gcp:DB_PASSWORD}",
|
|
103
|
+
"connectionString": "${gcp:DATABASE_CONNECTION_STRING}"
|
|
104
|
+
},
|
|
105
|
+
"api": {
|
|
106
|
+
"key": "${gcp:API_SECRET_KEY}"
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**app.ts:**
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { AppConfigBuilder, AppConfigSourceJson, AppConfigProviderGcpSecrets } from '@maroonedsoftware/appconfig';
|
|
115
|
+
|
|
116
|
+
const config = await new AppConfigBuilder()
|
|
117
|
+
.addSource(new AppConfigSourceJson('./config.json'))
|
|
118
|
+
.addProvider(new AppConfigProviderGcpSecrets('my-gcp-project-id'))
|
|
119
|
+
.build();
|
|
120
|
+
|
|
121
|
+
// Secrets are fetched from GCP Secret Manager
|
|
122
|
+
// The provider fetches: projects/my-gcp-project-id/secrets/DB_PASSWORD/versions/latest
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
> **Note:** The GCP secrets provider requires valid GCP credentials. It uses Application Default Credentials (ADC), so ensure you have authenticated via `gcloud auth application-default login` or have set up a service account.
|
|
126
|
+
|
|
127
|
+
## API
|
|
128
|
+
|
|
129
|
+
### AppConfig
|
|
130
|
+
|
|
131
|
+
The configuration container providing type-safe access to configuration values.
|
|
132
|
+
|
|
133
|
+
| Method | Description |
|
|
134
|
+
| ----------------- | --------------------------------------------------- |
|
|
135
|
+
| `get(key)` | Returns the value for the key with full type safety |
|
|
136
|
+
| `getString(key)` | Returns the value converted to a string |
|
|
137
|
+
| `getNumber(key)` | Returns the value converted to a number |
|
|
138
|
+
| `getBoolean(key)` | Returns the value converted to a boolean |
|
|
139
|
+
| `getObject(key)` | Returns the value cast as an object |
|
|
140
|
+
|
|
141
|
+
### AppConfigBuilder
|
|
142
|
+
|
|
143
|
+
Builder for constructing `AppConfig` instances from multiple sources.
|
|
144
|
+
|
|
145
|
+
| Method | Description |
|
|
146
|
+
| ----------------------- | ------------------------------------------------------------ |
|
|
147
|
+
| `addSource(source)` | Adds a configuration source (later sources override earlier) |
|
|
148
|
+
| `addProvider(provider)` | Adds a provider to transform string values |
|
|
149
|
+
| `build<T>()` | Builds and returns the `AppConfig` instance |
|
|
150
|
+
|
|
151
|
+
### Sources
|
|
152
|
+
|
|
153
|
+
#### AppConfigSourceJson
|
|
154
|
+
|
|
155
|
+
Loads configuration from a JSON file.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
// Basic usage (ignores missing file by default)
|
|
159
|
+
const source = new AppConfigSourceJson('./config.json');
|
|
160
|
+
|
|
161
|
+
// Throw error if file is missing
|
|
162
|
+
const source = new AppConfigSourceJson('./config.json', {
|
|
163
|
+
ignoreMissingFile: false,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Custom encoding
|
|
167
|
+
const source = new AppConfigSourceJson('./config.json', {
|
|
168
|
+
encoding: 'utf16le',
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
#### AppConfigSourceYaml
|
|
173
|
+
|
|
174
|
+
Loads configuration from a YAML file. Supports both `.yaml` and `.yml` extensions.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// Basic usage (ignores missing file by default)
|
|
178
|
+
const source = new AppConfigSourceYaml('./config.yaml');
|
|
179
|
+
|
|
180
|
+
// Throw error if file is missing
|
|
181
|
+
const source = new AppConfigSourceYaml('./config.yaml', {
|
|
182
|
+
ignoreMissingFile: false,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Custom encoding
|
|
186
|
+
const source = new AppConfigSourceYaml('./config.yaml', {
|
|
187
|
+
encoding: 'utf16le',
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
YAML files support all standard YAML features including nested objects, arrays, multiline strings, and anchors:
|
|
192
|
+
|
|
193
|
+
```yaml
|
|
194
|
+
# config.yaml
|
|
195
|
+
database:
|
|
196
|
+
host: localhost
|
|
197
|
+
port: 5432
|
|
198
|
+
credentials:
|
|
199
|
+
username: admin
|
|
200
|
+
password: ${env:DB_PASSWORD}
|
|
201
|
+
|
|
202
|
+
features:
|
|
203
|
+
- feature1
|
|
204
|
+
- feature2
|
|
205
|
+
|
|
206
|
+
description: |
|
|
207
|
+
This is a multiline
|
|
208
|
+
string value
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### AppConfigSourceDotenv
|
|
212
|
+
|
|
213
|
+
Loads environment variables from a `.env` file.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
// Load from default .env file
|
|
217
|
+
const source = new AppConfigSourceDotenv();
|
|
218
|
+
|
|
219
|
+
// Load from custom path
|
|
220
|
+
const source = new AppConfigSourceDotenv('./config/.env.local');
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Providers
|
|
224
|
+
|
|
225
|
+
#### AppConfigProviderDotenv
|
|
226
|
+
|
|
227
|
+
Resolves environment variable references in configuration values.
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
// Default pattern: ${env:VAR_NAME}
|
|
231
|
+
const provider = new AppConfigProviderDotenv();
|
|
232
|
+
|
|
233
|
+
// Custom regex pattern
|
|
234
|
+
const provider = new AppConfigProviderDotenv(/\$\{([^}]+)\}/g);
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
The provider automatically attempts to parse resolved values as JSON, so numeric and boolean environment variables are converted to their proper types.
|
|
238
|
+
|
|
239
|
+
#### AppConfigProviderGcpSecrets
|
|
240
|
+
|
|
241
|
+
Resolves GCP Secret Manager references in configuration values.
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
// Default pattern: ${gcp:SECRET_NAME}
|
|
245
|
+
const provider = new AppConfigProviderGcpSecrets('my-project-id');
|
|
246
|
+
|
|
247
|
+
// Custom regex pattern
|
|
248
|
+
const provider = new AppConfigProviderGcpSecrets('my-project-id', /\$\{secret:([^}]+)\}/g);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
| Parameter | Type | Description |
|
|
252
|
+
| ----------- | ------------------ | ---------------------------------------------------------------------- |
|
|
253
|
+
| `projectId` | `string` | The GCP project ID where secrets are stored |
|
|
254
|
+
| `prefix` | `string \| RegExp` | Optional regex pattern to match secret references. Default: `${gcp:*}` |
|
|
255
|
+
|
|
256
|
+
The provider:
|
|
257
|
+
|
|
258
|
+
- Fetches secrets from GCP Secret Manager using the latest version
|
|
259
|
+
- Automatically attempts to parse secret values as JSON
|
|
260
|
+
- Requires valid GCP credentials (uses Application Default Credentials)
|
|
261
|
+
- Is decorated with `@Injectable()` for dependency injection support
|
|
262
|
+
|
|
263
|
+
## Custom Sources and Providers
|
|
264
|
+
|
|
265
|
+
### Creating a Custom Source
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
import { AppConfigSource } from '@maroonedsoftware/appconfig';
|
|
269
|
+
|
|
270
|
+
class MyCustomSource implements AppConfigSource {
|
|
271
|
+
async load(): Promise<Record<string, unknown>> {
|
|
272
|
+
// Load configuration from your custom source
|
|
273
|
+
return { key: 'value' };
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Creating a Custom Provider
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
import { AppConfigProvider, ObjectVisitorMeta } from '@maroonedsoftware/appconfig';
|
|
282
|
+
|
|
283
|
+
class MyCustomProvider implements AppConfigProvider {
|
|
284
|
+
canParse(value: string): boolean {
|
|
285
|
+
return value.startsWith('custom:');
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async parse(value: string, meta: ObjectVisitorMeta): Promise<void> {
|
|
289
|
+
const transformed = value.replace('custom:', '');
|
|
290
|
+
(meta.owner as Record<string, unknown>)[meta.propertyPath] = transformed;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Configuration Merging
|
|
296
|
+
|
|
297
|
+
When using multiple sources, configurations are deep-merged in the order they are added. Later sources override earlier ones:
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
const config = await new AppConfigBuilder()
|
|
301
|
+
.addSource(new AppConfigSourceJson('./defaults.json')) // Base config
|
|
302
|
+
.addSource(new AppConfigSourceYaml('./config.yaml')) // Overrides defaults
|
|
303
|
+
.addSource(new AppConfigSourceJson('./local.json')) // Overrides both
|
|
304
|
+
.addSource(new AppConfigSourceDotenv()) // Overrides all
|
|
305
|
+
.build();
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## License
|
|
309
|
+
|
|
310
|
+
MIT
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AppConfig } from './app.config.js';
|
|
2
|
+
import { AppConfigProvider } from './app.config.provider.js';
|
|
3
|
+
import { AppConfigSource } from './app.config.source.js';
|
|
4
|
+
/**
|
|
5
|
+
* Builder for constructing AppConfig instances from multiple sources with value transformation.
|
|
6
|
+
*
|
|
7
|
+
* The builder allows you to:
|
|
8
|
+
* - Load configuration from multiple sources (files, environment variables, etc.)
|
|
9
|
+
* - Merge configurations with later sources overriding earlier ones
|
|
10
|
+
* - Transform string values using providers (e.g., resolving environment variable references)
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const config = await new AppConfigBuilder()
|
|
15
|
+
* .addSource(new AppConfigSourceJson('./config.json'))
|
|
16
|
+
* .addSource(new AppConfigSourceDotenv())
|
|
17
|
+
* .addProvider(new AppConfigProviderDotenv())
|
|
18
|
+
* .build();
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare class AppConfigBuilder {
|
|
22
|
+
private readonly sources;
|
|
23
|
+
private readonly providers;
|
|
24
|
+
/**
|
|
25
|
+
* Adds a configuration source to the builder.
|
|
26
|
+
*
|
|
27
|
+
* Sources are loaded in the order they are added, and later sources override earlier ones
|
|
28
|
+
* when merging configurations.
|
|
29
|
+
*
|
|
30
|
+
* @param source - The configuration source to add.
|
|
31
|
+
* @returns The builder instance for method chaining.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* builder
|
|
36
|
+
* .addSource(new AppConfigSourceJson('./default.json'))
|
|
37
|
+
* .addSource(new AppConfigSourceJson('./local.json'));
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
addSource(source: AppConfigSource): this;
|
|
41
|
+
/**
|
|
42
|
+
* Adds a provider to transform string values during configuration building.
|
|
43
|
+
*
|
|
44
|
+
* Providers are applied to all string values found in the merged configuration.
|
|
45
|
+
* The first provider that can parse a value will be used to transform it.
|
|
46
|
+
*
|
|
47
|
+
* @param provider - The provider to add.
|
|
48
|
+
* @returns The builder instance for method chaining.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* builder.addProvider(new AppConfigProviderDotenv());
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
addProvider(provider: AppConfigProvider): this;
|
|
56
|
+
/**
|
|
57
|
+
* Builds the AppConfig instance by loading all sources, merging them, and applying providers.
|
|
58
|
+
*
|
|
59
|
+
* The build process:
|
|
60
|
+
* 1. Loads all sources in parallel
|
|
61
|
+
* 2. Merges configurations (later sources override earlier ones)
|
|
62
|
+
* 3. Traverses the merged configuration and applies providers to string values
|
|
63
|
+
* 4. Returns the final AppConfig instance
|
|
64
|
+
*
|
|
65
|
+
* @template T - The type of the configuration object. Defaults to `Record<string, unknown>`.
|
|
66
|
+
* @returns A promise that resolves to the built AppConfig instance.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const config = await builder.build<MyConfigType>();
|
|
71
|
+
* const value = config.get('someKey');
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
build<T = Record<string, unknown>>(): Promise<AppConfig<T>>;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=app.config.builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.config.builder.d.ts","sourceRoot":"","sources":["../src/app.config.builder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAGzD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAyB;IACjD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA2B;IAErD;;;;;;;;;;;;;;;OAeG;IACH,SAAS,CAAC,MAAM,EAAE,eAAe;IAKjC;;;;;;;;;;;;;OAaG;IACH,WAAW,CAAC,QAAQ,EAAE,iBAAiB;IAKvC;;;;;;;;;;;;;;;;;OAiBG;IACG,KAAK,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;CAmBlE"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration container that provides type-safe access to configuration values.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the configuration object. Defaults to `Record<string, unknown>`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const config = new AppConfig({
|
|
9
|
+
* database: { host: 'localhost', port: 5432 },
|
|
10
|
+
* api: { timeout: 5000 }
|
|
11
|
+
* });
|
|
12
|
+
*
|
|
13
|
+
* const host = config.get('database').host; // Type-safe access
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare class AppConfig<T = Record<string, unknown>> {
|
|
17
|
+
private readonly config;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new AppConfig instance with the provided configuration.
|
|
20
|
+
*
|
|
21
|
+
* @param config - The configuration object to wrap.
|
|
22
|
+
*/
|
|
23
|
+
constructor(config: T);
|
|
24
|
+
/**
|
|
25
|
+
* Retrieves a configuration value by key.
|
|
26
|
+
*
|
|
27
|
+
* @template K - The key type, must be a key of T.
|
|
28
|
+
* @param key - The configuration key to retrieve.
|
|
29
|
+
* @returns The configuration value for the given key.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const config = new AppConfig({ port: 3000, host: 'localhost' });
|
|
34
|
+
* const port = config.get('port'); // Returns 3000, typed as number
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
get<K extends keyof T>(key: K): T[K];
|
|
38
|
+
/**
|
|
39
|
+
* Retrieves a configuration value as a string.
|
|
40
|
+
*
|
|
41
|
+
* The value is converted to a string using `String()`. This is useful when
|
|
42
|
+
* you need to ensure a value is a string regardless of its original type.
|
|
43
|
+
*
|
|
44
|
+
* @template K - The key type, must be a key of T.
|
|
45
|
+
* @param key - The configuration key to retrieve.
|
|
46
|
+
* @returns The configuration value converted to a string.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const config = new AppConfig({ port: 3000, enabled: true });
|
|
51
|
+
* const portStr = config.getString('port'); // Returns "3000"
|
|
52
|
+
* const enabledStr = config.getString('enabled'); // Returns "true"
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
getString<K extends keyof T>(key: K): string;
|
|
56
|
+
/**
|
|
57
|
+
* Retrieves a configuration value as a number.
|
|
58
|
+
*
|
|
59
|
+
* The value is converted to a number using `Number()`. This is useful when
|
|
60
|
+
* you need to ensure a value is a number regardless of its original type.
|
|
61
|
+
* Note: Invalid conversions will result in `NaN`.
|
|
62
|
+
*
|
|
63
|
+
* @template K - The key type, must be a key of T.
|
|
64
|
+
* @param key - The configuration key to retrieve.
|
|
65
|
+
* @returns The configuration value converted to a number.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const config = new AppConfig({ port: '3000', timeout: '5000' });
|
|
70
|
+
* const port = config.getNumber('port'); // Returns 3000
|
|
71
|
+
* const timeout = config.getNumber('timeout'); // Returns 5000
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
getNumber<K extends keyof T>(key: K): number;
|
|
75
|
+
/**
|
|
76
|
+
* Retrieves a configuration value as a boolean.
|
|
77
|
+
*
|
|
78
|
+
* The value is converted to a boolean using `Boolean()`. This is useful when
|
|
79
|
+
* you need to ensure a value is a boolean regardless of its original type.
|
|
80
|
+
* Note: Only falsy values (false, 0, '', null, undefined, NaN) become false.
|
|
81
|
+
*
|
|
82
|
+
* @template K - The key type, must be a key of T.
|
|
83
|
+
* @param key - The configuration key to retrieve.
|
|
84
|
+
* @returns The configuration value converted to a boolean.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const config = new AppConfig({ enabled: 'true', debug: 1 });
|
|
89
|
+
* const enabled = config.getBoolean('enabled'); // Returns true
|
|
90
|
+
* const debug = config.getBoolean('debug'); // Returns true
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
getBoolean<K extends keyof T>(key: K): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Retrieves a configuration value as an object.
|
|
96
|
+
*
|
|
97
|
+
* The value is cast to an object type. This is useful when you know a value
|
|
98
|
+
* is an object and want to access it with object methods.
|
|
99
|
+
*
|
|
100
|
+
* @template K - The key type, must be a key of T.
|
|
101
|
+
* @param key - The configuration key to retrieve.
|
|
102
|
+
* @returns The configuration value cast as an object.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const config = new AppConfig({
|
|
107
|
+
* database: { host: 'localhost', port: 5432 }
|
|
108
|
+
* });
|
|
109
|
+
* const db = config.getObject('database'); // Returns { host: 'localhost', port: 5432 }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
getObject<K extends keyof T>(key: K): object;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=app.config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.config.d.ts","sourceRoot":"","sources":["../src/app.config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,qBAAa,SAAS,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAMpC,OAAO,CAAC,QAAQ,CAAC,MAAM;IALnC;;;;OAIG;gBAC0B,MAAM,EAAE,CAAC;IAEtC;;;;;;;;;;;;OAYG;IACH,GAAG,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAIpC;;;;;;;;;;;;;;;;OAgBG;IACH,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,MAAM;IAI5C;;;;;;;;;;;;;;;;;OAiBG;IACH,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,MAAM;IAI5C;;;;;;;;;;;;;;;;;OAiBG;IACH,UAAU,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO;IAI9C;;;;;;;;;;;;;;;;;OAiBG;IACH,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,MAAM;CAG7C"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ObjectVisitorMeta } from './object.visitor.js';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for providers that transform string values in configuration.
|
|
4
|
+
*
|
|
5
|
+
* Providers are used by AppConfigBuilder to transform string values found in the
|
|
6
|
+
* configuration. For example, a provider might resolve environment variable references
|
|
7
|
+
* or perform other string transformations.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* class MyProvider implements AppConfigProvider {
|
|
12
|
+
* canParse(value: string): boolean {
|
|
13
|
+
* return value.startsWith('custom:');
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* async parse(value: string, meta: ObjectVisitorMeta): Promise<void> {
|
|
17
|
+
* // Transform the value
|
|
18
|
+
* meta.owner[meta.propertyPath] = transformedValue;
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export interface AppConfigProvider {
|
|
24
|
+
/**
|
|
25
|
+
* Determines whether this provider can parse the given string value.
|
|
26
|
+
*
|
|
27
|
+
* @param value - The string value to check.
|
|
28
|
+
* @returns `true` if this provider can parse the value, `false` otherwise.
|
|
29
|
+
*/
|
|
30
|
+
canParse(value: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Parses and transforms the given string value.
|
|
33
|
+
*
|
|
34
|
+
* The provider should update the value in the configuration object using
|
|
35
|
+
* the provided metadata.
|
|
36
|
+
*
|
|
37
|
+
* @param value - The string value to parse and transform.
|
|
38
|
+
* @param meta - Metadata about the value's location in the configuration object.
|
|
39
|
+
* @returns A promise that resolves when the transformation is complete.
|
|
40
|
+
*/
|
|
41
|
+
parse(value: string, meta: ObjectVisitorMeta): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=app.config.provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.config.provider.d.ts","sourceRoot":"","sources":["../src/app.config.provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAEjC;;;;;;;;;OASG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9D"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for configuration sources that can load configuration data.
|
|
3
|
+
*
|
|
4
|
+
* Sources are used by AppConfigBuilder to load configuration from various places
|
|
5
|
+
* such as files, environment variables, remote APIs, etc.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* class MySource implements AppConfigSource {
|
|
10
|
+
* async load(): Promise<Record<string, unknown>> {
|
|
11
|
+
* // Load configuration from somewhere
|
|
12
|
+
* return { key: 'value' };
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export interface AppConfigSource {
|
|
18
|
+
/**
|
|
19
|
+
* Loads configuration data from the source.
|
|
20
|
+
*
|
|
21
|
+
* @returns A promise that resolves to the configuration object.
|
|
22
|
+
*/
|
|
23
|
+
load(): Promise<Record<string, unknown>>;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=app.config.source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.config.source.d.ts","sourceRoot":"","sources":["../src/app.config.source.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC1C"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for configuring AppConfigSourceFile behavior.
|
|
3
|
+
*/
|
|
4
|
+
export interface AppConfigSourceFileOptions {
|
|
5
|
+
/**
|
|
6
|
+
* If `true`, returns an empty object when the file doesn't exist instead of throwing.
|
|
7
|
+
* Defaults to `true`.
|
|
8
|
+
*/
|
|
9
|
+
ignoreMissingFile?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* The file encoding to use when reading the file.
|
|
12
|
+
* Defaults to `'utf8'`.
|
|
13
|
+
*/
|
|
14
|
+
encoding?: BufferEncoding;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=app.config.source.options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.config.source.options.d.ts","sourceRoot":"","sources":["../src/app.config.source.options.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;OAGG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC3B"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempts to parse a string as JSON, returning the original string if parsing fails.
|
|
3
|
+
*
|
|
4
|
+
* @param text - The text to parse.
|
|
5
|
+
* @returns The parsed JSON value, or the original text if parsing fails.
|
|
6
|
+
*/
|
|
7
|
+
export declare function tryParseJson(text: string): unknown;
|
|
8
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAMlD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { AppConfig } from './app.config.js';
|
|
2
|
+
export { AppConfigBuilder } from './app.config.builder.js';
|
|
3
|
+
export type { AppConfigProvider } from './app.config.provider.js';
|
|
4
|
+
export type { AppConfigSource } from './app.config.source.js';
|
|
5
|
+
export { AppConfigSourceDotenv } from './sources/app.config.source.dotenv.js';
|
|
6
|
+
export { AppConfigSourceJson } from './sources/app.config.source.json.js';
|
|
7
|
+
export { AppConfigSourceYaml } from './sources/app.config.source.yaml.js';
|
|
8
|
+
export { AppConfigProviderDotenv } from './providers/app.config.provider.dotenv.js';
|
|
9
|
+
export { AppConfigProviderGcpSecrets } from './providers/app.config.provider.gcp.secrets.js';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,YAAY,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,YAAY,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAC9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,2CAA2C,CAAC;AACpF,OAAO,EAAE,2BAA2B,EAAE,MAAM,gDAAgD,CAAC"}
|