@geekmidas/envkit 0.0.3 → 0.0.4

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.
@@ -2,7 +2,18 @@ import get from 'lodash.get';
2
2
  import set from 'lodash.set';
3
3
  import { z } from 'zod/v4';
4
4
 
5
+ /**
6
+ * Parses and validates configuration objects against Zod schemas.
7
+ * Handles nested configurations and aggregates validation errors.
8
+ *
9
+ * @template TResponse - The shape of the configuration object
10
+ */
5
11
  export class ConfigParser<TResponse extends EmptyObject> {
12
+ /**
13
+ * Creates a new ConfigParser instance.
14
+ *
15
+ * @param config - The configuration object to parse
16
+ */
6
17
  constructor(private readonly config: TResponse) {}
7
18
  /**
8
19
  * Parses the config object and validates it against the Zod schemas
@@ -56,9 +67,41 @@ export class ConfigParser<TResponse extends EmptyObject> {
56
67
  }
57
68
  }
58
69
 
70
+ /**
71
+ * Parses environment variables with type-safe validation using Zod schemas.
72
+ * Provides a fluent API for defining environment variable schemas with automatic
73
+ * error context enrichment.
74
+ *
75
+ * @template T - The type of the configuration object (typically process.env)
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const config = new EnvironmentParser(process.env)
80
+ * .create((get) => ({
81
+ * port: get('PORT').string().transform(Number).default(3000),
82
+ * database: {
83
+ * url: get('DATABASE_URL').string().url()
84
+ * }
85
+ * }))
86
+ * .parse();
87
+ * ```
88
+ */
59
89
  export class EnvironmentParser<T extends EmptyObject> {
90
+ /**
91
+ * Creates a new EnvironmentParser instance.
92
+ *
93
+ * @param config - The configuration object to parse (typically process.env)
94
+ */
60
95
  constructor(private readonly config: T) {}
61
96
 
97
+ /**
98
+ * Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages
99
+ * with environment variable context.
100
+ *
101
+ * @param schema - The Zod schema to wrap
102
+ * @param name - The environment variable name for error context
103
+ * @returns A wrapped Zod schema with enhanced error reporting
104
+ */
62
105
  private wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {
63
106
  // Create a proxy that intercepts all method calls on the schema
64
107
  return new Proxy(schema, {
@@ -126,6 +169,13 @@ export class EnvironmentParser<T extends EmptyObject> {
126
169
  });
127
170
  };
128
171
 
172
+ /**
173
+ * Creates a proxied version of the Zod object that wraps all schema creators
174
+ * to provide enhanced error messages with environment variable context.
175
+ *
176
+ * @param name - The environment variable name
177
+ * @returns A proxied Zod object with wrapped schema creators
178
+ */
129
179
  private getZodGetter = (name: string) => {
130
180
  // Return an object that has all Zod schemas but with our wrapper
131
181
  return new Proxy(
@@ -148,7 +198,8 @@ export class EnvironmentParser<T extends EmptyObject> {
148
198
  if (value && typeof value === 'object') {
149
199
  return new Proxy(value, {
150
200
  get: (nestedTarget, nestedProp) => {
151
- const nestedValue = nestedTarget[nestedProp as keyof typeof nestedTarget];
201
+ const nestedValue =
202
+ nestedTarget[nestedProp as keyof typeof nestedTarget];
152
203
  if (typeof nestedValue === 'function') {
153
204
  return (...args: any[]) => {
154
205
  const schema = nestedValue(...args);
@@ -167,10 +218,10 @@ export class EnvironmentParser<T extends EmptyObject> {
167
218
  };
168
219
 
169
220
  /**
170
- * Creates a new JordConfigParser object that can be used to parse the config object
221
+ * Creates a new ConfigParser object that can be used to parse the config object
171
222
  *
172
223
  * @param builder - A function that takes a getter function and returns a config object
173
- * @returns A JordConfigParser object that can be used to parse the config object
224
+ * @returns A ConfigParser object that can be used to parse the config object
174
225
  */
175
226
  create<TReturn extends EmptyObject>(
176
227
  builder: (get: EnvFetcher) => TReturn,
@@ -180,6 +231,12 @@ export class EnvironmentParser<T extends EmptyObject> {
180
231
  }
181
232
  }
182
233
 
234
+ /**
235
+ * Infers the TypeScript type of a configuration object based on its Zod schemas.
236
+ * Recursively processes nested objects and extracts types from Zod schemas.
237
+ *
238
+ * @template T - The configuration object type
239
+ */
183
240
  export type InferConfig<T extends EmptyObject> = {
184
241
  [K in keyof T]: T[K] extends z.ZodSchema
185
242
  ? z.infer<T[K]>
@@ -188,12 +245,32 @@ export type InferConfig<T extends EmptyObject> = {
188
245
  : T[K];
189
246
  };
190
247
 
248
+ /**
249
+ * Function type for fetching environment variables with Zod validation.
250
+ * Returns a Zod object scoped to a specific environment variable.
251
+ *
252
+ * @template TPath - The environment variable path type
253
+ * @param name - The environment variable name
254
+ * @returns A Zod object for defining the schema
255
+ */
191
256
  export type EnvFetcher<TPath extends string = string> = (
192
257
  name: TPath,
193
258
  ) => typeof z;
194
259
 
260
+ /**
261
+ * Function type for building environment configuration objects.
262
+ * Takes an EnvFetcher and returns a configuration object with Zod schemas.
263
+ *
264
+ * @template TResponse - The response configuration object type
265
+ * @param get - The environment variable fetcher function
266
+ * @returns The configuration object with Zod schemas
267
+ */
195
268
  export type EnvironmentBuilder<TResponse extends EmptyObject> = (
196
269
  get: EnvFetcher,
197
270
  ) => TResponse;
198
271
 
272
+ /**
273
+ * Type alias for a generic object with unknown values.
274
+ * Used as a constraint for configuration objects.
275
+ */
199
276
  export type EmptyObject = Record<string | number | symbol, unknown>;
package/src/sst.ts ADDED
@@ -0,0 +1,221 @@
1
+ import snakecase from 'lodash.snakecase';
2
+
3
+ /**
4
+ * Converts a string to environment variable case format (UPPER_SNAKE_CASE).
5
+ * Numbers following underscores are preserved without the underscore.
6
+ *
7
+ * @param name - The string to convert
8
+ * @returns The converted string in environment variable format
9
+ *
10
+ * @example
11
+ * environmentCase('myVariable') // 'MY_VARIABLE'
12
+ * environmentCase('api_v2') // 'APIV2'
13
+ */
14
+ export function environmentCase(name: string) {
15
+ return snakecase(name)
16
+ .toUpperCase()
17
+ .replace(/_\d+/g, (r) => {
18
+ return r.replace('_', '');
19
+ });
20
+ }
21
+
22
+ /**
23
+ * Enumeration of supported SST (Serverless Stack Toolkit) resource types.
24
+ * Used to identify and process different AWS and SST resources.
25
+ */
26
+ export enum ResourceType {
27
+ ApiGatewayV2 = 'sst.aws.ApiGatewayV2',
28
+ Postgres = 'sst.aws.Postgres',
29
+ Function = 'sst.aws.Function',
30
+ Bucket = 'sst.aws.Bucket',
31
+ Vpc = 'sst.aws.Vpc',
32
+ Secret = 'sst.sst.Secret',
33
+ SSTSecret = 'sst:sst:Secret',
34
+ SSTFunction = 'sst:sst:Function',
35
+ SSTApiGatewayV2 = 'sst:aws:ApiGatewayV2',
36
+ SSTPostgres = 'sst:aws:Postgres',
37
+ SSTBucket = 'sst:aws:Bucket',
38
+ }
39
+
40
+ /**
41
+ * Processes a Secret resource into environment variables.
42
+ *
43
+ * @param name - The resource name
44
+ * @param value - The Secret resource
45
+ * @returns Object with environment variable mappings
46
+ */
47
+ const secret = (name: string, value: Secret) => ({
48
+ [environmentCase(name)]: value.value,
49
+ });
50
+ /**
51
+ * Processes a Postgres database resource into environment variables.
52
+ * Creates multiple environment variables for database connection details.
53
+ *
54
+ * @param key - The resource key
55
+ * @param value - The Postgres resource
56
+ * @returns Object with database connection environment variables
57
+ */
58
+ const postgres = (key: string, value: Postgres) => {
59
+ const prefix = `${environmentCase(key)}`;
60
+ return {
61
+ [`${prefix}_NAME`]: value.database,
62
+ [`${prefix}_HOST`]: value.host,
63
+ [`${prefix}_PASSWORD`]: value.password,
64
+ [`${prefix}_PORT`]: value.port,
65
+ [`${prefix}_USERNAME`]: value.username,
66
+ };
67
+ };
68
+
69
+ /**
70
+ * Processes a Bucket resource into environment variables.
71
+ *
72
+ * @param name - The resource name
73
+ * @param value - The Bucket resource
74
+ * @returns Object with bucket name environment variable
75
+ */
76
+ const bucket = (name: string, value: Bucket) => {
77
+ const prefix = `${environmentCase(name)}`;
78
+ return {
79
+ [`${prefix}_NAME`]: value.name,
80
+ };
81
+ };
82
+
83
+ /**
84
+ * No-operation processor for resources that don't require environment variables.
85
+ *
86
+ * @param name - The resource name (unused)
87
+ * @param value - The resource value (unused)
88
+ * @returns Empty object
89
+ */
90
+ const noop = (name: string, value: any) => ({});
91
+
92
+ /**
93
+ * Map of resource types to their corresponding processor functions.
94
+ * Each processor converts resource data into environment variables.
95
+ */
96
+ const processors: Record<ResourceType, ResourceProcessor<any>> = {
97
+ [ResourceType.ApiGatewayV2]: noop,
98
+ [ResourceType.Function]: noop,
99
+ [ResourceType.Vpc]: noop,
100
+ [ResourceType.Secret]: secret,
101
+ [ResourceType.Postgres]: postgres,
102
+ [ResourceType.Bucket]: bucket,
103
+
104
+ [ResourceType.SSTSecret]: secret,
105
+ [ResourceType.SSTBucket]: bucket,
106
+ [ResourceType.SSTFunction]: noop,
107
+ [ResourceType.SSTPostgres]: postgres,
108
+ [ResourceType.SSTApiGatewayV2]: noop,
109
+ };
110
+
111
+ /**
112
+ * Normalizes SST resources and plain strings into environment variables.
113
+ * Processes resources based on their type and converts names to environment case.
114
+ *
115
+ * @param record - Object containing resources and/or string values
116
+ * @returns Normalized environment variables object
117
+ *
118
+ * @example
119
+ * normalizeResourceEnv({
120
+ * apiUrl: 'https://api.example.com',
121
+ * database: { type: ResourceType.Postgres, ... }
122
+ * })
123
+ */
124
+ export function normalizeResourceEnv(
125
+ record: Record<string, Resource | string>,
126
+ ): Record<string, string> {
127
+ const env: Record<string, string> = {};
128
+ for (const [k, value] of Object.entries(record)) {
129
+ if (typeof value === 'string') {
130
+ env[environmentCase(k)] = value;
131
+ continue;
132
+ }
133
+
134
+ const processor = processors[value.type];
135
+ if (processor) {
136
+ Object.assign(env, processor(k, value));
137
+ } else {
138
+ console.warn(`No processor found for resource type: `, { value });
139
+ }
140
+ }
141
+
142
+ return env;
143
+ }
144
+
145
+ /**
146
+ * AWS API Gateway V2 resource type.
147
+ * Represents an HTTP/WebSocket API.
148
+ */
149
+ export type ApiGatewayV2 = {
150
+ type: ResourceType.ApiGatewayV2;
151
+ url: string;
152
+ };
153
+
154
+ /**
155
+ * PostgreSQL database resource type.
156
+ * Contains all connection details needed to connect to the database.
157
+ */
158
+ export type Postgres = {
159
+ database: string;
160
+ host: string;
161
+ password: string;
162
+ port: number;
163
+ type: ResourceType.Postgres;
164
+ username: string;
165
+ };
166
+
167
+ /**
168
+ * AWS Lambda Function resource type.
169
+ */
170
+ export type Function = {
171
+ name: string;
172
+ type: ResourceType.Function;
173
+ };
174
+
175
+ /**
176
+ * AWS S3 Bucket resource type.
177
+ */
178
+ export type Bucket = {
179
+ name: string;
180
+ type: ResourceType.Bucket;
181
+ };
182
+
183
+ /**
184
+ * AWS VPC (Virtual Private Cloud) resource type.
185
+ */
186
+ export type Vpc = {
187
+ bastion: string;
188
+ type: ResourceType.Vpc;
189
+ };
190
+
191
+ /**
192
+ * Secret resource type for storing sensitive values.
193
+ */
194
+ export type Secret = {
195
+ type: ResourceType.Secret;
196
+ value: string;
197
+ };
198
+
199
+ /**
200
+ * Union type of all supported SST resource types.
201
+ */
202
+ export type Resource =
203
+ | ApiGatewayV2
204
+ | Postgres
205
+ | Function
206
+ | Bucket
207
+ | Vpc
208
+ | Secret;
209
+
210
+ /**
211
+ * Function type for processing a specific resource type into environment variables.
212
+ *
213
+ * @template K - The specific resource type
214
+ * @param name - The resource name
215
+ * @param value - The resource value
216
+ * @returns Object mapping environment variable names to values
217
+ */
218
+ export type ResourceProcessor<K extends Resource> = (
219
+ name: string,
220
+ value: K,
221
+ ) => Record<string, string | number>;