@geekmidas/envkit 0.0.2 → 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.
package/dist/sst.mjs ADDED
@@ -0,0 +1,128 @@
1
+ import snakecase from "lodash.snakecase";
2
+
3
+ //#region src/sst.ts
4
+ /**
5
+ * Converts a string to environment variable case format (UPPER_SNAKE_CASE).
6
+ * Numbers following underscores are preserved without the underscore.
7
+ *
8
+ * @param name - The string to convert
9
+ * @returns The converted string in environment variable format
10
+ *
11
+ * @example
12
+ * environmentCase('myVariable') // 'MY_VARIABLE'
13
+ * environmentCase('api_v2') // 'APIV2'
14
+ */
15
+ function environmentCase(name) {
16
+ return snakecase(name).toUpperCase().replace(/_\d+/g, (r) => {
17
+ return r.replace("_", "");
18
+ });
19
+ }
20
+ /**
21
+ * Enumeration of supported SST (Serverless Stack Toolkit) resource types.
22
+ * Used to identify and process different AWS and SST resources.
23
+ */
24
+ let ResourceType = /* @__PURE__ */ function(ResourceType$1) {
25
+ ResourceType$1["ApiGatewayV2"] = "sst.aws.ApiGatewayV2";
26
+ ResourceType$1["Postgres"] = "sst.aws.Postgres";
27
+ ResourceType$1["Function"] = "sst.aws.Function";
28
+ ResourceType$1["Bucket"] = "sst.aws.Bucket";
29
+ ResourceType$1["Vpc"] = "sst.aws.Vpc";
30
+ ResourceType$1["Secret"] = "sst.sst.Secret";
31
+ ResourceType$1["SSTSecret"] = "sst:sst:Secret";
32
+ ResourceType$1["SSTFunction"] = "sst:sst:Function";
33
+ ResourceType$1["SSTApiGatewayV2"] = "sst:aws:ApiGatewayV2";
34
+ ResourceType$1["SSTPostgres"] = "sst:aws:Postgres";
35
+ ResourceType$1["SSTBucket"] = "sst:aws:Bucket";
36
+ return ResourceType$1;
37
+ }({});
38
+ /**
39
+ * Processes a Secret resource into environment variables.
40
+ *
41
+ * @param name - The resource name
42
+ * @param value - The Secret resource
43
+ * @returns Object with environment variable mappings
44
+ */
45
+ const secret = (name, value) => ({ [environmentCase(name)]: value.value });
46
+ /**
47
+ * Processes a Postgres database resource into environment variables.
48
+ * Creates multiple environment variables for database connection details.
49
+ *
50
+ * @param key - The resource key
51
+ * @param value - The Postgres resource
52
+ * @returns Object with database connection environment variables
53
+ */
54
+ const postgres = (key, value) => {
55
+ const prefix = `${environmentCase(key)}`;
56
+ return {
57
+ [`${prefix}_NAME`]: value.database,
58
+ [`${prefix}_HOST`]: value.host,
59
+ [`${prefix}_PASSWORD`]: value.password,
60
+ [`${prefix}_PORT`]: value.port,
61
+ [`${prefix}_USERNAME`]: value.username
62
+ };
63
+ };
64
+ /**
65
+ * Processes a Bucket resource into environment variables.
66
+ *
67
+ * @param name - The resource name
68
+ * @param value - The Bucket resource
69
+ * @returns Object with bucket name environment variable
70
+ */
71
+ const bucket = (name, value) => {
72
+ const prefix = `${environmentCase(name)}`;
73
+ return { [`${prefix}_NAME`]: value.name };
74
+ };
75
+ /**
76
+ * No-operation processor for resources that don't require environment variables.
77
+ *
78
+ * @param name - The resource name (unused)
79
+ * @param value - The resource value (unused)
80
+ * @returns Empty object
81
+ */
82
+ const noop = (name, value) => ({});
83
+ /**
84
+ * Map of resource types to their corresponding processor functions.
85
+ * Each processor converts resource data into environment variables.
86
+ */
87
+ const processors = {
88
+ [ResourceType.ApiGatewayV2]: noop,
89
+ [ResourceType.Function]: noop,
90
+ [ResourceType.Vpc]: noop,
91
+ [ResourceType.Secret]: secret,
92
+ [ResourceType.Postgres]: postgres,
93
+ [ResourceType.Bucket]: bucket,
94
+ [ResourceType.SSTSecret]: secret,
95
+ [ResourceType.SSTBucket]: bucket,
96
+ [ResourceType.SSTFunction]: noop,
97
+ [ResourceType.SSTPostgres]: postgres,
98
+ [ResourceType.SSTApiGatewayV2]: noop
99
+ };
100
+ /**
101
+ * Normalizes SST resources and plain strings into environment variables.
102
+ * Processes resources based on their type and converts names to environment case.
103
+ *
104
+ * @param record - Object containing resources and/or string values
105
+ * @returns Normalized environment variables object
106
+ *
107
+ * @example
108
+ * normalizeResourceEnv({
109
+ * apiUrl: 'https://api.example.com',
110
+ * database: { type: ResourceType.Postgres, ... }
111
+ * })
112
+ */
113
+ function normalizeResourceEnv(record) {
114
+ const env = {};
115
+ for (const [k, value] of Object.entries(record)) {
116
+ if (typeof value === "string") {
117
+ env[environmentCase(k)] = value;
118
+ continue;
119
+ }
120
+ const processor = processors[value.type];
121
+ if (processor) Object.assign(env, processor(k, value));
122
+ else console.warn(`No processor found for resource type: `, { value });
123
+ }
124
+ return env;
125
+ }
126
+
127
+ //#endregion
128
+ export { ResourceType, environmentCase, normalizeResourceEnv };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geekmidas/envkit",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -8,6 +8,11 @@
8
8
  "import": "./dist/index.mjs",
9
9
  "require": "./dist/index.cjs",
10
10
  "types": "./src/index.ts"
11
+ },
12
+ "./sst": {
13
+ "import": "./dist/sst.mjs",
14
+ "require": "./dist/sst.cjs",
15
+ "types": "./src/sst.ts"
11
16
  }
12
17
  },
13
18
  "publishConfig": {
@@ -17,10 +22,12 @@
17
22
  "dependencies": {
18
23
  "zod": "~3.25.67",
19
24
  "lodash.set": "~4.3.2",
20
- "lodash.get": "~4.4.2"
25
+ "lodash.get": "~4.4.2",
26
+ "lodash.snakecase": "~4.1.1"
21
27
  },
22
28
  "devDependencies": {
23
29
  "@types/lodash.set": "~4.3.9",
24
- "@types/lodash.get": "~4.4.9"
30
+ "@types/lodash.get": "~4.4.9",
31
+ "@types/lodash.snakecase": "~4.1.9"
25
32
  }
26
33
  }
@@ -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,115 @@ 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
+ */
105
+ private wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {
106
+ // Create a proxy that intercepts all method calls on the schema
107
+ return new Proxy(schema, {
108
+ get: (target, prop) => {
109
+ if (prop === 'parse') {
110
+ return () => {
111
+ const value = get(this.config, name);
112
+ try {
113
+ return target.parse(value);
114
+ } catch (error) {
115
+ if (error instanceof z.ZodError) {
116
+ // Modify the error to include the environment variable name
117
+ const modifiedIssues = error.issues.map((issue) => ({
118
+ ...issue,
119
+ message: `Environment variable "${name}": ${issue.message}`,
120
+ path: [name, ...issue.path],
121
+ }));
122
+ throw new z.ZodError(modifiedIssues);
123
+ }
124
+ throw error;
125
+ }
126
+ };
127
+ }
128
+
129
+ if (prop === 'safeParse') {
130
+ return () => {
131
+ const value = get(this.config, name);
132
+ const result = target.safeParse(value);
133
+
134
+ if (!result.success) {
135
+ // Modify the error to include the environment variable name
136
+ const modifiedIssues = result.error.issues.map(
137
+ (issue: z.core.$ZodIssue) => ({
138
+ ...issue,
139
+ message: `Environment variable "${name}": ${issue.message}`,
140
+ path: [name, ...issue.path],
141
+ }),
142
+ );
143
+ return {
144
+ success: false as const,
145
+ error: new z.ZodError(modifiedIssues),
146
+ };
147
+ }
148
+
149
+ return result;
150
+ };
151
+ }
152
+
153
+ // For any method that returns a new schema (like transform, optional, etc.),
154
+ // wrap the result as well
155
+ const originalProp = target[prop as keyof typeof target];
156
+ if (typeof originalProp === 'function') {
157
+ return (...args: any[]) => {
158
+ const result = originalProp.apply(target, args);
159
+ // If the result is a ZodType, wrap it too
160
+ if (result && typeof result === 'object' && 'parse' in result) {
161
+ return this.wrapSchema(result, name);
162
+ }
163
+ return result;
164
+ };
165
+ }
166
+
167
+ return originalProp;
168
+ },
169
+ });
170
+ };
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
+ */
62
179
  private getZodGetter = (name: string) => {
63
180
  // Return an object that has all Zod schemas but with our wrapper
64
181
  return new Proxy(
@@ -67,70 +184,44 @@ export class EnvironmentParser<T extends EmptyObject> {
67
184
  get: (target, prop) => {
68
185
  // deno-lint-ignore ban-ts-comment
69
186
  // @ts-ignore
70
- const func = target[prop];
187
+ const value = target[prop];
71
188
 
72
- if (typeof func === 'function') {
189
+ if (typeof value === 'function') {
73
190
  // Return a wrapper around each Zod schema creator
74
191
  return (...args: any[]) => {
75
- const schema = func(...args);
76
- // Add a custom parse method that gets the value from config
77
- const originalParse = schema.parse;
78
- const originalSafeParse = schema.safeParse;
79
-
80
- schema.parse = () => {
81
- const value = get(this.config, name);
82
- try {
83
- return originalParse.call(schema, value);
84
- } catch (error) {
85
- if (error instanceof z.ZodError) {
86
- // Modify the error to include the environment variable name
87
- const modifiedIssues = error.issues.map((issue) => ({
88
- ...issue,
89
- message: `Environment variable "${name}": ${issue.message}`,
90
- path: [name, ...issue.path],
91
- }));
92
- throw new z.ZodError(modifiedIssues);
93
- }
94
- throw error;
95
- }
96
- };
192
+ const schema = value(...args);
193
+ return this.wrapSchema(schema, name);
194
+ };
195
+ }
97
196
 
98
- schema.safeParse = () => {
99
- const value = get(this.config, name);
100
- const result = originalSafeParse.call(schema, value);
101
-
102
- if (!result.success) {
103
- // Modify the error to include the environment variable name
104
- const modifiedIssues = result.error.issues.map(
105
- (issue: z.core.$ZodIssue) => ({
106
- ...issue,
107
- message: `Environment variable "${name}": ${issue.message}`,
108
- path: [name, ...issue.path],
109
- }),
110
- );
111
- return {
112
- success: false as const,
113
- error: new z.ZodError(modifiedIssues),
197
+ // Handle objects like z.coerce
198
+ if (value && typeof value === 'object') {
199
+ return new Proxy(value, {
200
+ get: (nestedTarget, nestedProp) => {
201
+ const nestedValue =
202
+ nestedTarget[nestedProp as keyof typeof nestedTarget];
203
+ if (typeof nestedValue === 'function') {
204
+ return (...args: any[]) => {
205
+ const schema = nestedValue(...args);
206
+ return this.wrapSchema(schema, name);
114
207
  };
115
208
  }
116
-
117
- return result;
118
- };
119
-
120
- return schema;
121
- };
209
+ return nestedValue;
210
+ },
211
+ });
122
212
  }
123
- return func;
213
+
214
+ return value;
124
215
  },
125
216
  },
126
217
  );
127
218
  };
128
219
 
129
220
  /**
130
- * 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
131
222
  *
132
223
  * @param builder - A function that takes a getter function and returns a config object
133
- * @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
134
225
  */
135
226
  create<TReturn extends EmptyObject>(
136
227
  builder: (get: EnvFetcher) => TReturn,
@@ -140,6 +231,12 @@ export class EnvironmentParser<T extends EmptyObject> {
140
231
  }
141
232
  }
142
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
+ */
143
240
  export type InferConfig<T extends EmptyObject> = {
144
241
  [K in keyof T]: T[K] extends z.ZodSchema
145
242
  ? z.infer<T[K]>
@@ -148,12 +245,32 @@ export type InferConfig<T extends EmptyObject> = {
148
245
  : T[K];
149
246
  };
150
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
+ */
151
256
  export type EnvFetcher<TPath extends string = string> = (
152
257
  name: TPath,
153
258
  ) => typeof z;
154
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
+ */
155
268
  export type EnvironmentBuilder<TResponse extends EmptyObject> = (
156
269
  get: EnvFetcher,
157
270
  ) => TResponse;
158
271
 
272
+ /**
273
+ * Type alias for a generic object with unknown values.
274
+ * Used as a constraint for configuration objects.
275
+ */
159
276
  export type EmptyObject = Record<string | number | symbol, unknown>;