@1matrix/config-loader 0.1.1 → 0.1.2
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/README.md +97 -75
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @1matrix/config-loader
|
|
2
2
|
|
|
3
3
|
Hot-reloadable public configuration management for Applications with GitHub webhook integration.
|
|
4
4
|
|
|
@@ -17,7 +17,7 @@ Hot-reloadable public configuration management for Applications with GitHub webh
|
|
|
17
17
|
## Installation
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
|
-
pnpm add @
|
|
20
|
+
pnpm add @1matrix/config-loader
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
## Quick Start
|
|
@@ -25,15 +25,15 @@ pnpm add @onematrix/config-loader
|
|
|
25
25
|
### 1. Initialize ConfigLoader
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
|
-
import { ConfigLoader } from
|
|
28
|
+
import { ConfigLoader } from "@1matrix/config-loader";
|
|
29
29
|
|
|
30
30
|
const config = new ConfigLoader({
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
repository: "OneMatrixL1/public-configs",
|
|
32
|
+
branch: "main",
|
|
33
|
+
githubToken: process.env.GITHUB_TOKEN,
|
|
34
|
+
webhookSecret: process.env.GITHUB_WEBHOOK_SECRET,
|
|
35
|
+
cacheFile: "./config-cache.json",
|
|
36
|
+
pollingInterval: 5 * 60 * 1000, // 5 minutes
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
await config.initialize();
|
|
@@ -43,14 +43,14 @@ await config.initialize();
|
|
|
43
43
|
|
|
44
44
|
```typescript
|
|
45
45
|
// Get specific value
|
|
46
|
-
const apiKeyName = config.get(
|
|
46
|
+
const apiKeyName = config.get("api-keys.a129f786...");
|
|
47
47
|
|
|
48
48
|
// Get with type inference
|
|
49
|
-
const handlers = config.get<string[]>(
|
|
49
|
+
const handlers = config.get<string[]>("intent-handlers", []);
|
|
50
50
|
|
|
51
51
|
// Check if exists
|
|
52
|
-
if (config.has(
|
|
53
|
-
|
|
52
|
+
if (config.has("api-keys.some-hash")) {
|
|
53
|
+
// ...
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
// Get all configs
|
|
@@ -61,35 +61,35 @@ const allConfigs = config.getAll();
|
|
|
61
61
|
|
|
62
62
|
```typescript
|
|
63
63
|
// Listen to all updates
|
|
64
|
-
config.on(
|
|
65
|
-
|
|
64
|
+
config.on("update", (newConfigs, changedKeys) => {
|
|
65
|
+
console.log("Updated configs:", changedKeys);
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
// Listen to specific config updates
|
|
69
|
-
config.on(
|
|
70
|
-
|
|
69
|
+
config.on("update:api-keys", (newApiKeys) => {
|
|
70
|
+
console.log("API keys changed");
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
// Listen to validation errors
|
|
74
|
-
config.on(
|
|
75
|
-
|
|
74
|
+
config.on("validation-error", (configName, errors) => {
|
|
75
|
+
console.error("Validation failed:", configName, errors);
|
|
76
76
|
});
|
|
77
77
|
```
|
|
78
78
|
|
|
79
79
|
### 4. Set Up Webhook (Express)
|
|
80
80
|
|
|
81
81
|
```typescript
|
|
82
|
-
import express from
|
|
82
|
+
import express from "express";
|
|
83
83
|
|
|
84
84
|
const app = express();
|
|
85
85
|
|
|
86
86
|
app.post(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
"/webhook/config-update",
|
|
88
|
+
express.json(),
|
|
89
|
+
config.createWebhookHandler({
|
|
90
|
+
onUpdate: (configs) => console.log("Updated via webhook"),
|
|
91
|
+
onError: (err) => console.error(err),
|
|
92
|
+
})
|
|
93
93
|
);
|
|
94
94
|
```
|
|
95
95
|
|
|
@@ -116,48 +116,50 @@ public-configs/
|
|
|
116
116
|
### Example Configurations
|
|
117
117
|
|
|
118
118
|
**`dev/api-keys.json`**
|
|
119
|
+
|
|
119
120
|
```json
|
|
120
121
|
{
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
"a129f7860d47c0630e6d06c153fe36711f25a31bfb4304dc6d2a79e609da0e96": "VNIDC",
|
|
123
|
+
"b234c8971e58d1741f7e17d264gf47822g36b42cgc5415ed7e3b8a710eb1fa07": "SERVICE_2"
|
|
123
124
|
}
|
|
124
125
|
```
|
|
125
126
|
|
|
126
127
|
**`dev/roles.json`**
|
|
128
|
+
|
|
127
129
|
```json
|
|
128
130
|
{
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
"0x194f5b1755562966302Ef0BbF4349c842c60FC42"
|
|
135
|
-
]
|
|
131
|
+
"explorer.viewer": [
|
|
132
|
+
"0xE61383556642AF1Bd7c5756b13f19A63Dc8601df",
|
|
133
|
+
"0x7d5538fEe2CE89dA936ec29cC48386b6E7548FaB"
|
|
134
|
+
],
|
|
135
|
+
"admin": ["0x194f5b1755562966302Ef0BbF4349c842c60FC42"]
|
|
136
136
|
}
|
|
137
137
|
```
|
|
138
138
|
|
|
139
139
|
**`dev/intent-handlers.json`**
|
|
140
|
+
|
|
140
141
|
```json
|
|
141
142
|
[
|
|
142
|
-
|
|
143
|
-
|
|
143
|
+
"0x84f915BcbD5C1134BCb93a0f50D9D36E6D3b508c",
|
|
144
|
+
"0x626b1E2458A9307E73A570c291bCd467216cc1D7"
|
|
144
145
|
]
|
|
145
146
|
```
|
|
146
147
|
|
|
147
148
|
### Example Schemas
|
|
148
149
|
|
|
149
150
|
**`schemas/api-keys.schema.json`**
|
|
151
|
+
|
|
150
152
|
```json
|
|
151
153
|
{
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
154
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
155
|
+
"type": "object",
|
|
156
|
+
"patternProperties": {
|
|
157
|
+
"^[a-f0-9]{64}$": {
|
|
158
|
+
"type": "string",
|
|
159
|
+
"minLength": 1
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
"additionalProperties": false
|
|
161
163
|
}
|
|
162
164
|
```
|
|
163
165
|
|
|
@@ -167,78 +169,98 @@ public-configs/
|
|
|
167
169
|
|
|
168
170
|
```typescript
|
|
169
171
|
interface ConfigLoaderOptions {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
172
|
+
repository: string; // GitHub repository (owner/repo)
|
|
173
|
+
branch: string; // Branch to fetch from
|
|
174
|
+
githubToken?: string; // GitHub personal access token
|
|
175
|
+
webhookSecret?: string; // GitHub webhook secret
|
|
176
|
+
envMappings?: Record<string, string>; // NODE_ENV to directory mappings
|
|
177
|
+
defaultEnv?: string; // Default environment (default: 'dev')
|
|
178
|
+
pollingInterval?: number; // Polling interval in ms (default: 300000)
|
|
179
|
+
cacheFile?: string; // Cache file path (default: './config-cache.json')
|
|
180
|
+
usePackageSchemas?: boolean; // Use built-in schemas (default: true)
|
|
179
181
|
}
|
|
180
182
|
```
|
|
181
183
|
|
|
182
184
|
### Methods
|
|
183
185
|
|
|
184
186
|
#### `async initialize(): Promise<void>`
|
|
187
|
+
|
|
185
188
|
Initialize the config loader. Attempts to load from GitHub, falls back to cache if unavailable.
|
|
186
189
|
|
|
187
190
|
#### `async refresh(): Promise<void>`
|
|
191
|
+
|
|
188
192
|
Manually refresh configurations from GitHub.
|
|
189
193
|
|
|
190
194
|
#### `get<T>(path: string, defaultValue?: T): T | undefined`
|
|
195
|
+
|
|
191
196
|
Get a configuration value by dot-notation path.
|
|
192
197
|
|
|
193
198
|
#### `has(path: string): boolean`
|
|
199
|
+
|
|
194
200
|
Check if a configuration path exists.
|
|
195
201
|
|
|
196
202
|
#### `getAll(): Record<string, any>`
|
|
203
|
+
|
|
197
204
|
Get all configurations.
|
|
198
205
|
|
|
199
206
|
#### `getCurrentEnvironment(): string`
|
|
207
|
+
|
|
200
208
|
Get the current environment name.
|
|
201
209
|
|
|
202
210
|
#### `getConfigNames(): string[]`
|
|
211
|
+
|
|
203
212
|
Get names of all loaded configurations.
|
|
204
213
|
|
|
205
214
|
#### `createWebhookHandler(options?: WebhookHandlerOptions): ExpressMiddleware`
|
|
215
|
+
|
|
206
216
|
Create an Express middleware for handling GitHub webhooks.
|
|
207
217
|
|
|
208
218
|
#### `destroy(): void`
|
|
219
|
+
|
|
209
220
|
Clean up resources (stop polling, remove listeners).
|
|
210
221
|
|
|
211
222
|
### Events
|
|
212
223
|
|
|
213
224
|
#### `'update'`
|
|
225
|
+
|
|
214
226
|
Emitted when configurations are updated.
|
|
227
|
+
|
|
215
228
|
```typescript
|
|
216
|
-
config.on(
|
|
217
|
-
|
|
218
|
-
|
|
229
|
+
config.on(
|
|
230
|
+
"update",
|
|
231
|
+
(newConfigs: Record<string, any>, changedKeys: string[]) => {
|
|
232
|
+
// Handle update
|
|
233
|
+
}
|
|
234
|
+
);
|
|
219
235
|
```
|
|
220
236
|
|
|
221
237
|
#### `'update:${configName}'`
|
|
238
|
+
|
|
222
239
|
Emitted when a specific configuration is updated.
|
|
240
|
+
|
|
223
241
|
```typescript
|
|
224
|
-
config.on(
|
|
225
|
-
|
|
242
|
+
config.on("update:api-keys", (newApiKeys: any) => {
|
|
243
|
+
// Handle API keys update
|
|
226
244
|
});
|
|
227
245
|
```
|
|
228
246
|
|
|
229
247
|
#### `'validation-error'`
|
|
248
|
+
|
|
230
249
|
Emitted when configuration validation fails.
|
|
250
|
+
|
|
231
251
|
```typescript
|
|
232
|
-
config.on(
|
|
233
|
-
|
|
252
|
+
config.on("validation-error", (configName: string, errors: any[]) => {
|
|
253
|
+
// Handle validation error
|
|
234
254
|
});
|
|
235
255
|
```
|
|
236
256
|
|
|
237
257
|
#### `'error'`
|
|
258
|
+
|
|
238
259
|
Emitted when an error occurs.
|
|
260
|
+
|
|
239
261
|
```typescript
|
|
240
|
-
config.on(
|
|
241
|
-
|
|
262
|
+
config.on("error", (error: Error) => {
|
|
263
|
+
// Handle error
|
|
242
264
|
});
|
|
243
265
|
```
|
|
244
266
|
|
|
@@ -277,17 +299,17 @@ Map `NODE_ENV` values to configuration directories:
|
|
|
277
299
|
|
|
278
300
|
```typescript
|
|
279
301
|
const config = new ConfigLoader({
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
302
|
+
// ...
|
|
303
|
+
envMappings: {
|
|
304
|
+
dev: "dev",
|
|
305
|
+
development: "dev",
|
|
306
|
+
develop: "develop",
|
|
307
|
+
stg: "stg",
|
|
308
|
+
staging: "staging",
|
|
309
|
+
prod: "prod",
|
|
310
|
+
production: "production",
|
|
311
|
+
},
|
|
312
|
+
defaultEnv: "dev",
|
|
291
313
|
});
|
|
292
314
|
```
|
|
293
315
|
|
package/package.json
CHANGED