@zpress/config 0.4.0 → 0.5.1
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 +8 -397
- package/dist/index.d.ts +587 -0
- package/dist/index.mjs +10 -1
- package/package.json +4 -4
- package/schemas/schema.json +298 -286
package/README.md
CHANGED
|
@@ -2,411 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
Configuration loading, validation, and schema generation for zpress.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
<span class="zp-badge">
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
[](https://github.com/joggrdocs/zpress/actions/workflows/ci.yml)
|
|
8
|
+
[](https://www.npmjs.com/package/@zpress/config)
|
|
9
|
+
[](https://github.com/joggrdocs/zpress/blob/main/LICENSE)
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
</span>
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
- **Type-safe config definitions** with `defineConfig` helper
|
|
13
|
-
- **Runtime validation** using Zod schemas
|
|
14
|
-
- **JSON Schema generation** for IDE autocomplete and validation
|
|
15
|
-
- **Result-based error handling** with typed error tuples
|
|
16
|
-
- **Theme integration** via `@zpress/theme` package
|
|
17
|
-
- **c12-powered loader** for flexible config resolution
|
|
18
|
-
|
|
19
|
-
## Installation
|
|
20
|
-
|
|
21
|
-
This package is typically installed as a dependency of `@zpress/core`:
|
|
13
|
+
## Install
|
|
22
14
|
|
|
23
15
|
```bash
|
|
24
16
|
npm install @zpress/config
|
|
25
17
|
```
|
|
26
18
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
### Define Config (TypeScript)
|
|
30
|
-
|
|
31
|
-
```typescript
|
|
32
|
-
// zpress.config.ts
|
|
33
|
-
import { defineConfig } from '@zpress/config'
|
|
34
|
-
|
|
35
|
-
export default defineConfig({
|
|
36
|
-
title: 'My Documentation',
|
|
37
|
-
description: 'Built with zpress',
|
|
38
|
-
theme: {
|
|
39
|
-
name: 'midnight',
|
|
40
|
-
colorMode: 'dark',
|
|
41
|
-
},
|
|
42
|
-
sections: [
|
|
43
|
-
{
|
|
44
|
-
text: 'Getting Started',
|
|
45
|
-
from: 'docs/getting-started',
|
|
46
|
-
},
|
|
47
|
-
],
|
|
48
|
-
})
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### JSON Config with Schema
|
|
52
|
-
|
|
53
|
-
```json
|
|
54
|
-
{
|
|
55
|
-
"$schema": "https://raw.githubusercontent.com/joggrdocs/zpress/v0.3.0/packages/config/schemas/schema.json",
|
|
56
|
-
"title": "My Documentation",
|
|
57
|
-
"theme": {
|
|
58
|
-
"name": "arcade",
|
|
59
|
-
"colorMode": "dark"
|
|
60
|
-
},
|
|
61
|
-
"sections": [
|
|
62
|
-
{
|
|
63
|
-
"text": "Guide",
|
|
64
|
-
"from": "docs"
|
|
65
|
-
}
|
|
66
|
-
]
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### YAML Config with Schema
|
|
71
|
-
|
|
72
|
-
```yaml
|
|
73
|
-
# yaml-language-server: $schema=https://raw.githubusercontent.com/joggrdocs/zpress/v0.3.0/packages/config/schemas/schema.json
|
|
74
|
-
|
|
75
|
-
title: My Documentation
|
|
76
|
-
theme:
|
|
77
|
-
name: base
|
|
78
|
-
colorMode: toggle
|
|
79
|
-
sections:
|
|
80
|
-
- text: Documentation
|
|
81
|
-
from: docs
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### Load Config Programmatically
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
import { loadConfig } from '@zpress/config'
|
|
88
|
-
|
|
89
|
-
const [error, config] = await loadConfig('/path/to/project')
|
|
90
|
-
if (error) {
|
|
91
|
-
console.error('Config load failed:', error.message)
|
|
92
|
-
process.exit(1)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
console.log('Loaded config:', config.title)
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### Load Config with Options
|
|
99
|
-
|
|
100
|
-
```typescript
|
|
101
|
-
import { loadConfig } from '@zpress/config'
|
|
102
|
-
|
|
103
|
-
const [error, config] = await loadConfig({
|
|
104
|
-
cwd: '/path/to/project',
|
|
105
|
-
configFile: 'custom.config.ts',
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
if (error) {
|
|
109
|
-
// Handle error based on type
|
|
110
|
-
if (error.type === 'not_found') {
|
|
111
|
-
console.error('No config file found')
|
|
112
|
-
} else if (error.type === 'validation_failed') {
|
|
113
|
-
console.error('Config validation errors:')
|
|
114
|
-
error.errors?.forEach((err) => {
|
|
115
|
-
console.error(` ${err.path.join('.')}: ${err.message}`)
|
|
116
|
-
})
|
|
117
|
-
}
|
|
118
|
-
process.exit(1)
|
|
119
|
-
}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
### Validate Config
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
import { validateConfig } from '@zpress/config'
|
|
126
|
-
|
|
127
|
-
const config = {
|
|
128
|
-
title: 'My Docs',
|
|
129
|
-
sections: [{ text: 'Guide', from: 'docs' }],
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const [error, validatedConfig] = validateConfig(config)
|
|
133
|
-
if (error) {
|
|
134
|
-
console.error('Validation failed:', error.message)
|
|
135
|
-
process.exit(1)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// validatedConfig is fully typed and validated
|
|
139
|
-
console.log(validatedConfig.title)
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
## Multi-Format Support
|
|
143
|
-
|
|
144
|
-
The config loader uses [c12](https://github.com/unjs/c12) to support multiple file formats:
|
|
145
|
-
|
|
146
|
-
- `zpress.config.ts` - TypeScript (recommended)
|
|
147
|
-
- `zpress.config.js` - JavaScript ESM
|
|
148
|
-
- `zpress.config.mjs` - JavaScript ESM
|
|
149
|
-
- `zpress.config.json` - JSON
|
|
150
|
-
- `zpress.config.jsonc` - JSON with comments
|
|
151
|
-
- `zpress.config.yml` - YAML
|
|
152
|
-
- `zpress.config.yaml` - YAML
|
|
153
|
-
|
|
154
|
-
Files are resolved in the order above. The first matching file is loaded.
|
|
155
|
-
|
|
156
|
-
## JSON Schema
|
|
157
|
-
|
|
158
|
-
The package generates a JSON Schema file that provides IDE autocomplete and validation for `.json` and `.yaml` config files.
|
|
159
|
-
|
|
160
|
-
### Using the Schema
|
|
161
|
-
|
|
162
|
-
#### JSON
|
|
163
|
-
|
|
164
|
-
Add `$schema` property to your `zpress.config.json`:
|
|
165
|
-
|
|
166
|
-
```json
|
|
167
|
-
{
|
|
168
|
-
"$schema": "https://raw.githubusercontent.com/joggrdocs/zpress/v0.3.0/packages/config/schemas/schema.json"
|
|
169
|
-
}
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
#### YAML
|
|
173
|
-
|
|
174
|
-
Add a modeline comment to your `zpress.config.yaml`:
|
|
175
|
-
|
|
176
|
-
```yaml
|
|
177
|
-
# yaml-language-server: $schema=https://raw.githubusercontent.com/joggrdocs/zpress/v0.3.0/packages/config/schemas/schema.json
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
### Versioned Schemas
|
|
181
|
-
|
|
182
|
-
You can reference a specific version using GitHub tags:
|
|
183
|
-
|
|
184
|
-
```json
|
|
185
|
-
{
|
|
186
|
-
"$schema": "https://raw.githubusercontent.com/joggrdocs/zpress/v1.0.0/packages/config/schemas/schema.json"
|
|
187
|
-
}
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
## API Reference
|
|
191
|
-
|
|
192
|
-
### Types
|
|
193
|
-
|
|
194
|
-
#### Core Config Types
|
|
195
|
-
|
|
196
|
-
- `ZpressConfig` - Complete zpress configuration object
|
|
197
|
-
- `ThemeConfig` - Theme configuration (name, colorMode, switcher, colors)
|
|
198
|
-
- `ThemeName` - Built-in theme names with custom theme support
|
|
199
|
-
- `IconColor` - Built-in icon colors with custom color support
|
|
200
|
-
- `ColorMode` - `'dark' | 'light' | 'toggle'`
|
|
201
|
-
- `ThemeColors` - Color override configuration
|
|
202
|
-
|
|
203
|
-
#### Content Types
|
|
204
|
-
|
|
205
|
-
- `Entry` - Sidebar entry (can be nested)
|
|
206
|
-
- `NavItem` - Navigation bar item (can be nested)
|
|
207
|
-
- `WorkspaceItem` - Workspace/app/package definition
|
|
208
|
-
- `WorkspaceGroup` - Grouped workspace items
|
|
209
|
-
- `Feature` - Feature card for landing pages
|
|
210
|
-
- `CardConfig` - Card display configuration
|
|
211
|
-
- `Frontmatter` - Page frontmatter options
|
|
212
|
-
|
|
213
|
-
#### System Types
|
|
214
|
-
|
|
215
|
-
- `Paths` - Directory paths configuration
|
|
216
|
-
- `ResolvedPage` - Resolved page metadata
|
|
217
|
-
- `ResolvedSection` - Resolved section with pages
|
|
218
|
-
|
|
219
|
-
#### Error Types
|
|
220
|
-
|
|
221
|
-
- `ConfigError` - Error object with `_tag`, `type`, and `message`
|
|
222
|
-
- `ConfigErrorType` - Union of error types: `'not_found' | 'parse_error' | 'validation_failed' | 'empty_sections'`
|
|
223
|
-
- `ConfigResult<T>` - Result tuple: `readonly [ConfigError, null] | readonly [null, T]`
|
|
224
|
-
|
|
225
|
-
### Functions
|
|
226
|
-
|
|
227
|
-
#### `defineConfig(config)`
|
|
228
|
-
|
|
229
|
-
Type helper for config files. Provides type checking and autocomplete.
|
|
230
|
-
|
|
231
|
-
```typescript
|
|
232
|
-
function defineConfig(config: ZpressConfig): ZpressConfig
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
**Parameters:**
|
|
236
|
-
|
|
237
|
-
- `config` - Zpress configuration object
|
|
238
|
-
|
|
239
|
-
**Returns:** The same config object (typed)
|
|
240
|
-
|
|
241
|
-
**Example:**
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
export default defineConfig({
|
|
245
|
-
title: 'My Docs',
|
|
246
|
-
sections: [{ text: 'Guide', from: 'docs' }],
|
|
247
|
-
})
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
#### `loadConfig(dirOrOptions?)`
|
|
251
|
-
|
|
252
|
-
Load and validate zpress config from a directory.
|
|
253
|
-
|
|
254
|
-
```typescript
|
|
255
|
-
function loadConfig(dirOrOptions?: string | LoadConfigOptions): Promise<ConfigResult<ZpressConfig>>
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
**Parameters:**
|
|
259
|
-
|
|
260
|
-
- `dirOrOptions` - Directory path (string) or options object:
|
|
261
|
-
- `cwd?: string` - Working directory to search for config
|
|
262
|
-
- `configFile?: string` - Specific config file name
|
|
263
|
-
|
|
264
|
-
**Returns:** Result tuple with error or validated config
|
|
265
|
-
|
|
266
|
-
**Example:**
|
|
267
|
-
|
|
268
|
-
```typescript
|
|
269
|
-
const [error, config] = await loadConfig('/path/to/project')
|
|
270
|
-
if (error) {
|
|
271
|
-
console.error(error.message)
|
|
272
|
-
process.exit(1)
|
|
273
|
-
}
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
#### `validateConfig(config)`
|
|
277
|
-
|
|
278
|
-
Validate a config object against the Zod schema.
|
|
279
|
-
|
|
280
|
-
```typescript
|
|
281
|
-
function validateConfig(config: unknown): ConfigResult<ZpressConfig>
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
**Parameters:**
|
|
285
|
-
|
|
286
|
-
- `config` - Config object to validate
|
|
287
|
-
|
|
288
|
-
**Returns:** Result tuple with validation error or validated config
|
|
289
|
-
|
|
290
|
-
**Example:**
|
|
291
|
-
|
|
292
|
-
```typescript
|
|
293
|
-
const [error, validatedConfig] = validateConfig(rawConfig)
|
|
294
|
-
```
|
|
295
|
-
|
|
296
|
-
#### `configError(type, message)`
|
|
297
|
-
|
|
298
|
-
Create a ConfigError object.
|
|
299
|
-
|
|
300
|
-
```typescript
|
|
301
|
-
function configError(type: ConfigErrorType, message: string): ConfigError
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
#### `configErrorFromZod(zodError)`
|
|
305
|
-
|
|
306
|
-
Convert Zod validation error to ConfigError.
|
|
307
|
-
|
|
308
|
-
```typescript
|
|
309
|
-
function configErrorFromZod(zodError: z.ZodError): ConfigError
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
### Constants
|
|
313
|
-
|
|
314
|
-
Re-exported from `@zpress/theme`:
|
|
315
|
-
|
|
316
|
-
- `THEME_NAMES` - Array of built-in theme names: `['base', 'midnight', 'arcade']`
|
|
317
|
-
- `COLOR_MODES` - Array of valid color modes: `['dark', 'light', 'toggle']`
|
|
318
|
-
- `ICON_COLORS` - Array of built-in icon colors: `['purple', 'blue', 'green', 'amber', 'cyan', 'red', 'pink', 'slate']`
|
|
319
|
-
|
|
320
|
-
### Type Guards
|
|
321
|
-
|
|
322
|
-
Re-exported from `@zpress/theme`:
|
|
323
|
-
|
|
324
|
-
- `isBuiltInTheme(name: string): name is BuiltInThemeName` - Check if theme is built-in
|
|
325
|
-
- `isBuiltInIconColor(color: string): color is BuiltInIconColor` - Check if icon color is built-in
|
|
326
|
-
|
|
327
|
-
### Utilities
|
|
328
|
-
|
|
329
|
-
Re-exported from `@zpress/theme`:
|
|
330
|
-
|
|
331
|
-
- `resolveDefaultColorMode(theme: BuiltInThemeName): ColorMode` - Get default color mode for a built-in theme
|
|
332
|
-
|
|
333
|
-
### Schemas
|
|
334
|
-
|
|
335
|
-
Zod schemas for validation:
|
|
336
|
-
|
|
337
|
-
- `zpressConfigSchema` - Main config schema
|
|
338
|
-
- `pathsSchema` - Paths configuration schema
|
|
339
|
-
|
|
340
|
-
## Error Handling
|
|
341
|
-
|
|
342
|
-
All fallible operations return Result tuples instead of throwing exceptions.
|
|
343
|
-
|
|
344
|
-
### Result Type
|
|
345
|
-
|
|
346
|
-
```typescript
|
|
347
|
-
type Result<T, E> = readonly [E, null] | readonly [null, T]
|
|
348
|
-
type ConfigResult<T> = Result<T, ConfigError>
|
|
349
|
-
```
|
|
350
|
-
|
|
351
|
-
### Error Structure
|
|
352
|
-
|
|
353
|
-
```typescript
|
|
354
|
-
interface ConfigError {
|
|
355
|
-
readonly _tag: 'ConfigError'
|
|
356
|
-
readonly type: ConfigErrorType
|
|
357
|
-
readonly message: string
|
|
358
|
-
readonly errors?: ReadonlyArray<{
|
|
359
|
-
readonly path: ReadonlyArray<string | number>
|
|
360
|
-
readonly message: string
|
|
361
|
-
}>
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
type ConfigErrorType =
|
|
365
|
-
| 'not_found'
|
|
366
|
-
| 'parse_error'
|
|
367
|
-
| 'validation_failed'
|
|
368
|
-
| 'empty_sections'
|
|
369
|
-
| 'missing_field'
|
|
370
|
-
| 'invalid_entry'
|
|
371
|
-
| 'invalid_section'
|
|
372
|
-
| 'invalid_field'
|
|
373
|
-
| 'invalid_icon'
|
|
374
|
-
| 'invalid_theme'
|
|
375
|
-
| 'duplicate_prefix'
|
|
376
|
-
| 'unknown'
|
|
377
|
-
```
|
|
378
|
-
|
|
379
|
-
### Handling Errors
|
|
380
|
-
|
|
381
|
-
```typescript
|
|
382
|
-
const [error, config] = await loadConfig()
|
|
383
|
-
|
|
384
|
-
if (error) {
|
|
385
|
-
// Handle different error types
|
|
386
|
-
switch (error.type) {
|
|
387
|
-
case 'not_found':
|
|
388
|
-
console.error('Config file not found')
|
|
389
|
-
break
|
|
390
|
-
case 'parse_error':
|
|
391
|
-
console.error('Failed to parse config:', error.message)
|
|
392
|
-
break
|
|
393
|
-
case 'validation_failed':
|
|
394
|
-
console.error('Validation failed:')
|
|
395
|
-
error.errors?.forEach((err) => {
|
|
396
|
-
console.error(` ${err.path.join('.')}: ${err.message}`)
|
|
397
|
-
})
|
|
398
|
-
break
|
|
399
|
-
case 'empty_sections':
|
|
400
|
-
console.error('Config must have at least one section')
|
|
401
|
-
break
|
|
402
|
-
}
|
|
403
|
-
process.exit(1)
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// Config is validated and fully typed
|
|
407
|
-
console.log(config.title)
|
|
408
|
-
```
|
|
19
|
+
> Most users should install [`@zpress/kit`](https://www.npmjs.com/package/@zpress/kit) instead.
|
|
409
20
|
|
|
410
21
|
## License
|
|
411
22
|
|
|
412
|
-
MIT
|
|
23
|
+
[MIT](https://github.com/joggrdocs/zpress/blob/main/LICENSE) - Joggr, Inc.
|