dagger-env 0.2.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 +223 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jacob Hands
|
|
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,223 @@
|
|
|
1
|
+
# dagger-env
|
|
2
|
+
|
|
3
|
+
A type-safe, reusable environment configuration abstraction for Dagger modules with full Zod v4 validation.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔒 **Type-safe**: Full TypeScript support with Zod v4 validation
|
|
8
|
+
- 🔄 **Reusable**: Create multiple environment configurations for different projects
|
|
9
|
+
- 🎯 **Consistent**: Standardized API across all Dagger modules
|
|
10
|
+
- 🛡️ **Validated**: Runtime validation of arguments, environment variables, and secrets
|
|
11
|
+
- 📦 **Modular**: Secret presets and derived environment variables
|
|
12
|
+
- 🚀 **Easy to use**: Simple configuration-based setup
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install dagger-env zod
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { z } from 'zod/v4'
|
|
24
|
+
import { createDaggerEnv } from 'dagger-env'
|
|
25
|
+
|
|
26
|
+
// Define your environment configuration
|
|
27
|
+
const myDaggerEnv = createDaggerEnv({
|
|
28
|
+
args: z.object({
|
|
29
|
+
push: z.string().optional(),
|
|
30
|
+
environment: z.enum(['dev', 'staging', 'prod']).optional(),
|
|
31
|
+
}),
|
|
32
|
+
env: z.object({
|
|
33
|
+
CI: z.string().optional(),
|
|
34
|
+
NODE_ENV: z.string().optional(),
|
|
35
|
+
}),
|
|
36
|
+
secrets: z.object({
|
|
37
|
+
API_TOKEN: z.string(),
|
|
38
|
+
DATABASE_URL: z.string(),
|
|
39
|
+
REDIS_URL: z.string(),
|
|
40
|
+
}),
|
|
41
|
+
secretPresets: {
|
|
42
|
+
api: ['API_TOKEN', 'DATABASE_URL'],
|
|
43
|
+
cache: ['REDIS_URL'],
|
|
44
|
+
} as const,
|
|
45
|
+
derivedEnvVars: {
|
|
46
|
+
API_TOKEN: {
|
|
47
|
+
API_BASE_URL: 'https://api.example.com',
|
|
48
|
+
API_VERSION: 'v1',
|
|
49
|
+
},
|
|
50
|
+
DATABASE_URL: {
|
|
51
|
+
DB_POOL_SIZE: '10',
|
|
52
|
+
},
|
|
53
|
+
} as const,
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
// Use in your Dagger module
|
|
57
|
+
import { dag, Container, object, func, Secret } from '@dagger.io/dagger'
|
|
58
|
+
|
|
59
|
+
@object()
|
|
60
|
+
export class MyModule {
|
|
61
|
+
@func()
|
|
62
|
+
async build(options: Secret): Promise<Container> {
|
|
63
|
+
const opts = await myDaggerEnv.parseDaggerOptions(options)
|
|
64
|
+
const withEnv = await myDaggerEnv.getWithEnv(options, ['api'], ['REDIS_URL'])
|
|
65
|
+
|
|
66
|
+
return withEnv(dag.container().from('node:18')).withExec(['npm', 'run', 'build']).sync()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## API Reference
|
|
72
|
+
|
|
73
|
+
### Environment Configuration
|
|
74
|
+
|
|
75
|
+
#### `createDaggerEnv(config)`
|
|
76
|
+
|
|
77
|
+
Creates a new DaggerEnv instance with the provided configuration.
|
|
78
|
+
|
|
79
|
+
**Parameters:**
|
|
80
|
+
|
|
81
|
+
- `config.args`: Zod schema for command-line arguments
|
|
82
|
+
- `config.env`: Zod schema for environment variables
|
|
83
|
+
- `config.secrets`: Zod schema for secrets
|
|
84
|
+
- `config.secretPresets`: Object mapping preset names to arrays of secret names
|
|
85
|
+
- `config.derivedEnvVars`: Object mapping secret names to derived environment variables
|
|
86
|
+
|
|
87
|
+
**Returns:** `DaggerEnv<T>` instance
|
|
88
|
+
|
|
89
|
+
### `daggerEnv.parseDaggerOptions(options: Secret)`
|
|
90
|
+
|
|
91
|
+
Parses and validates dagger options from a Secret containing JSON.
|
|
92
|
+
|
|
93
|
+
**Parameters:**
|
|
94
|
+
|
|
95
|
+
- `options`: Dagger Secret containing JSON options
|
|
96
|
+
|
|
97
|
+
**Returns:** `Promise<DaggerOptionsFromConfig<T>>` - Parsed and typed options object
|
|
98
|
+
|
|
99
|
+
### `daggerEnv.getWithEnv(options, secretPresets, secretNames?)`
|
|
100
|
+
|
|
101
|
+
Creates a function that applies environment variables and secrets to a container.
|
|
102
|
+
|
|
103
|
+
**Parameters:**
|
|
104
|
+
|
|
105
|
+
- `options`: Secret or parsed options object
|
|
106
|
+
- `secretPresets`: Array of preset names to include (e.g., `['api', 'cache']`)
|
|
107
|
+
- `secretNames`: Optional array of additional individual secret names
|
|
108
|
+
|
|
109
|
+
**Returns:** `Promise<(con: Container) => Container>` - Function that applies env vars and secrets
|
|
110
|
+
|
|
111
|
+
### `daggerEnv.getOptionsSchema()`
|
|
112
|
+
|
|
113
|
+
Returns the Zod schema for the complete options object.
|
|
114
|
+
|
|
115
|
+
**Returns:** `ZodObject` - The combined schema for args, env, and secrets
|
|
116
|
+
|
|
117
|
+
### `daggerEnv.getSecretPresets()`
|
|
118
|
+
|
|
119
|
+
Returns array of available secret preset names.
|
|
120
|
+
|
|
121
|
+
**Returns:** `Array<string>` - Available preset names
|
|
122
|
+
|
|
123
|
+
### `daggerEnv.getPresetSecrets(preset)`
|
|
124
|
+
|
|
125
|
+
Returns array of secret names for a specific preset.
|
|
126
|
+
|
|
127
|
+
**Parameters:**
|
|
128
|
+
|
|
129
|
+
- `preset`: Name of the preset
|
|
130
|
+
|
|
131
|
+
**Returns:** `readonly string[]` - Secret names in the preset
|
|
132
|
+
|
|
133
|
+
## Configuration Examples
|
|
134
|
+
|
|
135
|
+
### Simple API Service
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
const apiServiceEnv = createDaggerEnv({
|
|
139
|
+
args: z.object({
|
|
140
|
+
push: z.string().optional(),
|
|
141
|
+
}),
|
|
142
|
+
env: z.object({
|
|
143
|
+
CI: z.string().optional(),
|
|
144
|
+
}),
|
|
145
|
+
secrets: z.object({
|
|
146
|
+
API_TOKEN: z.string(),
|
|
147
|
+
DATABASE_URL: z.string(),
|
|
148
|
+
}),
|
|
149
|
+
secretPresets: {
|
|
150
|
+
api: ['API_TOKEN', 'DATABASE_URL'],
|
|
151
|
+
} as const,
|
|
152
|
+
derivedEnvVars: {
|
|
153
|
+
API_TOKEN: {
|
|
154
|
+
API_BASE_URL: 'https://api.example.com',
|
|
155
|
+
},
|
|
156
|
+
} as const,
|
|
157
|
+
})
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Multi-Environment Setup
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
const multiEnvDaggerEnv = createDaggerEnv({
|
|
164
|
+
args: z.object({
|
|
165
|
+
environment: z.enum(['dev', 'staging', 'prod']),
|
|
166
|
+
push: z.string().optional(),
|
|
167
|
+
}),
|
|
168
|
+
env: z.object({
|
|
169
|
+
CI: z.string().optional(),
|
|
170
|
+
}),
|
|
171
|
+
secrets: z.object({
|
|
172
|
+
DEV_API_KEY: z.string(),
|
|
173
|
+
STAGING_API_KEY: z.string(),
|
|
174
|
+
PROD_API_KEY: z.string(),
|
|
175
|
+
}),
|
|
176
|
+
secretPresets: {
|
|
177
|
+
dev: ['DEV_API_KEY'],
|
|
178
|
+
staging: ['STAGING_API_KEY'],
|
|
179
|
+
prod: ['PROD_API_KEY'],
|
|
180
|
+
} as const,
|
|
181
|
+
derivedEnvVars: {
|
|
182
|
+
DEV_API_KEY: { API_URL: 'https://dev-api.example.com' },
|
|
183
|
+
STAGING_API_KEY: { API_URL: 'https://staging-api.example.com' },
|
|
184
|
+
PROD_API_KEY: { API_URL: 'https://api.example.com' },
|
|
185
|
+
} as const,
|
|
186
|
+
})
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Type Extraction
|
|
190
|
+
|
|
191
|
+
Extract TypeScript types from your configuration:
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
import { z } from 'zod'
|
|
195
|
+
|
|
196
|
+
// Extract output type (after parsing/validation)
|
|
197
|
+
type MyOptions = z.output<ReturnType<typeof myDaggerEnv.getOptionsSchema>>
|
|
198
|
+
|
|
199
|
+
// Extract input type (before parsing/validation)
|
|
200
|
+
type MyInput = z.input<ReturnType<typeof myDaggerEnv.getOptionsSchema>>
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Best Practices
|
|
204
|
+
|
|
205
|
+
1. **Use `as const`** for `secretPresets` and `derivedEnvVars` to ensure proper typing
|
|
206
|
+
2. **Group related secrets** into logical presets (e.g., `api`, `database`, `cache`)
|
|
207
|
+
3. **Validate early** by calling `parseDaggerOptions()` at the start of functions
|
|
208
|
+
4. **Reuse configurations** across multiple Dagger modules in the same project
|
|
209
|
+
5. **Document your schemas** with JSDoc comments for better developer experience
|
|
210
|
+
|
|
211
|
+
## Requirements
|
|
212
|
+
|
|
213
|
+
- Node.js 18+
|
|
214
|
+
- Dagger SDK
|
|
215
|
+
- Zod v4+
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
MIT
|
|
220
|
+
|
|
221
|
+
## Contributing
|
|
222
|
+
|
|
223
|
+
Contributions are welcome! Please read our contributing guidelines and submit pull requests to our GitHub repository.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dagger-env",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "A type-safe, reusable environment configuration abstraction for Dagger modules.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"dagger",
|
|
8
|
+
"env"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/monorepo-rocks/monorepo-rocks/tree/main/packages/dagger-env",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/monorepo-rocks/monorepo-rocks.git",
|
|
14
|
+
"directory": "packages/dagger-env"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Jacob Hands",
|
|
19
|
+
"url": "https://github.com/jahands"
|
|
20
|
+
},
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
|
+
"module": "./dist/index.js",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@dagger.io/dagger": "0.18.9",
|
|
38
|
+
"typescript": "5.5.4",
|
|
39
|
+
"vitest": "3.1.4",
|
|
40
|
+
"zod": "3.25.57",
|
|
41
|
+
"zx": "8.5.4",
|
|
42
|
+
"@repo/tools": "0.2.3",
|
|
43
|
+
"@repo/typescript-config": "0.1.2"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "runx build tsc ./src/index.ts",
|
|
47
|
+
"check:exports": "runx check --exports",
|
|
48
|
+
"check:lint": "run-eslint",
|
|
49
|
+
"check:types": "run-tsc",
|
|
50
|
+
"test": "run-vitest"
|
|
51
|
+
}
|
|
52
|
+
}
|