@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/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>;
@@ -1,99 +0,0 @@
1
- import get from "lodash.get";
2
- import set from "lodash.set";
3
- import { z } from "zod/v4";
4
-
5
- //#region src/EnvironmentParser.ts
6
- var ConfigParser = class {
7
- constructor(config) {
8
- this.config = config;
9
- }
10
- /**
11
- * Parses the config object and validates it against the Zod schemas
12
- * @returns The parsed config object
13
- */
14
- parse() {
15
- const errors = [];
16
- const parseDeep = (config, path = []) => {
17
- const result = {};
18
- if (config && typeof config !== "object") return config;
19
- for (const key in config) {
20
- const schema = config[key];
21
- const currentPath = [...path, key];
22
- if (schema instanceof z.ZodType) {
23
- const parsed = schema.safeParse(void 0);
24
- if (parsed.success) set(result, key, parsed.data);
25
- else errors.push(...parsed.error.issues.map((issue) => ({
26
- ...issue,
27
- path: [...currentPath, ...issue.path]
28
- })));
29
- } else if (schema) set(result, key, parseDeep(schema, currentPath));
30
- }
31
- return result;
32
- };
33
- const parsedConfig = parseDeep(this.config);
34
- if (errors.length > 0) throw new z.ZodError(errors);
35
- return parsedConfig;
36
- }
37
- };
38
- var EnvironmentParser = class {
39
- constructor(config) {
40
- this.config = config;
41
- }
42
- getZodGetter = (name) => {
43
- return new Proxy({ ...z }, { get: (target, prop) => {
44
- const func = target[prop];
45
- if (typeof func === "function") return (...args) => {
46
- const schema = func(...args);
47
- const originalParse = schema.parse;
48
- const originalSafeParse = schema.safeParse;
49
- schema.parse = () => {
50
- const value = get(this.config, name);
51
- try {
52
- return originalParse.call(schema, value);
53
- } catch (error) {
54
- if (error instanceof z.ZodError) {
55
- const modifiedIssues = error.issues.map((issue) => ({
56
- ...issue,
57
- message: `Environment variable "${name}": ${issue.message}`,
58
- path: [name, ...issue.path]
59
- }));
60
- throw new z.ZodError(modifiedIssues);
61
- }
62
- throw error;
63
- }
64
- };
65
- schema.safeParse = () => {
66
- const value = get(this.config, name);
67
- const result = originalSafeParse.call(schema, value);
68
- if (!result.success) {
69
- const modifiedIssues = result.error.issues.map((issue) => ({
70
- ...issue,
71
- message: `Environment variable "${name}": ${issue.message}`,
72
- path: [name, ...issue.path]
73
- }));
74
- return {
75
- success: false,
76
- error: new z.ZodError(modifiedIssues)
77
- };
78
- }
79
- return result;
80
- };
81
- return schema;
82
- };
83
- return func;
84
- } });
85
- };
86
- /**
87
- * Creates a new JordConfigParser object that can be used to parse the config object
88
- *
89
- * @param builder - A function that takes a getter function and returns a config object
90
- * @returns A JordConfigParser object that can be used to parse the config object
91
- */
92
- create(builder) {
93
- const config = builder(this.getZodGetter);
94
- return new ConfigParser(config);
95
- }
96
- };
97
-
98
- //#endregion
99
- export { ConfigParser, EnvironmentParser };
@@ -1,133 +0,0 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
- key = keys[i];
11
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
- get: ((k) => from[k]).bind(null, key),
13
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
- });
15
- }
16
- return to;
17
- };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
- value: mod,
20
- enumerable: true
21
- }) : target, mod));
22
-
23
- //#endregion
24
- const lodash_get = __toESM(require("lodash.get"));
25
- const lodash_set = __toESM(require("lodash.set"));
26
- const zod_v4 = __toESM(require("zod/v4"));
27
-
28
- //#region src/EnvironmentParser.ts
29
- var ConfigParser = class {
30
- constructor(config) {
31
- this.config = config;
32
- }
33
- /**
34
- * Parses the config object and validates it against the Zod schemas
35
- * @returns The parsed config object
36
- */
37
- parse() {
38
- const errors = [];
39
- const parseDeep = (config, path = []) => {
40
- const result = {};
41
- if (config && typeof config !== "object") return config;
42
- for (const key in config) {
43
- const schema = config[key];
44
- const currentPath = [...path, key];
45
- if (schema instanceof zod_v4.z.ZodType) {
46
- const parsed = schema.safeParse(void 0);
47
- if (parsed.success) (0, lodash_set.default)(result, key, parsed.data);
48
- else errors.push(...parsed.error.issues.map((issue) => ({
49
- ...issue,
50
- path: [...currentPath, ...issue.path]
51
- })));
52
- } else if (schema) (0, lodash_set.default)(result, key, parseDeep(schema, currentPath));
53
- }
54
- return result;
55
- };
56
- const parsedConfig = parseDeep(this.config);
57
- if (errors.length > 0) throw new zod_v4.z.ZodError(errors);
58
- return parsedConfig;
59
- }
60
- };
61
- var EnvironmentParser = class {
62
- constructor(config) {
63
- this.config = config;
64
- }
65
- getZodGetter = (name) => {
66
- return new Proxy({ ...zod_v4.z }, { get: (target, prop) => {
67
- const func = target[prop];
68
- if (typeof func === "function") return (...args) => {
69
- const schema = func(...args);
70
- const originalParse = schema.parse;
71
- const originalSafeParse = schema.safeParse;
72
- schema.parse = () => {
73
- const value = (0, lodash_get.default)(this.config, name);
74
- try {
75
- return originalParse.call(schema, value);
76
- } catch (error) {
77
- if (error instanceof zod_v4.z.ZodError) {
78
- const modifiedIssues = error.issues.map((issue) => ({
79
- ...issue,
80
- message: `Environment variable "${name}": ${issue.message}`,
81
- path: [name, ...issue.path]
82
- }));
83
- throw new zod_v4.z.ZodError(modifiedIssues);
84
- }
85
- throw error;
86
- }
87
- };
88
- schema.safeParse = () => {
89
- const value = (0, lodash_get.default)(this.config, name);
90
- const result = originalSafeParse.call(schema, value);
91
- if (!result.success) {
92
- const modifiedIssues = result.error.issues.map((issue) => ({
93
- ...issue,
94
- message: `Environment variable "${name}": ${issue.message}`,
95
- path: [name, ...issue.path]
96
- }));
97
- return {
98
- success: false,
99
- error: new zod_v4.z.ZodError(modifiedIssues)
100
- };
101
- }
102
- return result;
103
- };
104
- return schema;
105
- };
106
- return func;
107
- } });
108
- };
109
- /**
110
- * Creates a new JordConfigParser object that can be used to parse the config object
111
- *
112
- * @param builder - A function that takes a getter function and returns a config object
113
- * @returns A JordConfigParser object that can be used to parse the config object
114
- */
115
- create(builder) {
116
- const config = builder(this.getZodGetter);
117
- return new ConfigParser(config);
118
- }
119
- };
120
-
121
- //#endregion
122
- Object.defineProperty(exports, 'ConfigParser', {
123
- enumerable: true,
124
- get: function () {
125
- return ConfigParser;
126
- }
127
- });
128
- Object.defineProperty(exports, 'EnvironmentParser', {
129
- enumerable: true,
130
- get: function () {
131
- return EnvironmentParser;
132
- }
133
- });