@geekmidas/envkit 0.0.5 → 0.0.6
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/EnvironmentParser.d.mts +1 -1
- package/dist/__tests__/sst.spec.cjs +305 -0
- package/dist/__tests__/sst.spec.d.cts +1 -0
- package/dist/__tests__/sst.spec.d.mts +1 -0
- package/dist/__tests__/sst.spec.mjs +304 -0
- package/dist/index.d.mts +1 -1
- package/dist/sst-BSxwaAdz.cjs +146 -0
- package/dist/sst-CQhO0S6y.mjs +128 -0
- package/dist/sst.cjs +4 -130
- package/dist/sst.mjs +1 -126
- package/package.json +1 -1
- package/src/__tests__/sst.spec.ts +415 -0
- /package/dist/{EnvironmentParser-BD6NmvPR.d.mts → EnvironmentParser-C-arQEHQ.d.mts} +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const lodash_snakecase = require_chunk.__toESM(require("lodash.snakecase"));
|
|
3
|
+
|
|
4
|
+
//#region src/sst.ts
|
|
5
|
+
/**
|
|
6
|
+
* Converts a string to environment variable case format (UPPER_SNAKE_CASE).
|
|
7
|
+
* Numbers following underscores are preserved without the underscore.
|
|
8
|
+
*
|
|
9
|
+
* @param name - The string to convert
|
|
10
|
+
* @returns The converted string in environment variable format
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
14
|
+
* environmentCase('api_v2') // 'APIV2'
|
|
15
|
+
*/
|
|
16
|
+
function environmentCase(name) {
|
|
17
|
+
return (0, lodash_snakecase.default)(name).toUpperCase().replace(/_\d+/g, (r) => {
|
|
18
|
+
return r.replace("_", "");
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Enumeration of supported SST (Serverless Stack Toolkit) resource types.
|
|
23
|
+
* Used to identify and process different AWS and SST resources.
|
|
24
|
+
*/
|
|
25
|
+
let ResourceType = /* @__PURE__ */ function(ResourceType$1) {
|
|
26
|
+
ResourceType$1["ApiGatewayV2"] = "sst.aws.ApiGatewayV2";
|
|
27
|
+
ResourceType$1["Postgres"] = "sst.aws.Postgres";
|
|
28
|
+
ResourceType$1["Function"] = "sst.aws.Function";
|
|
29
|
+
ResourceType$1["Bucket"] = "sst.aws.Bucket";
|
|
30
|
+
ResourceType$1["Vpc"] = "sst.aws.Vpc";
|
|
31
|
+
ResourceType$1["Secret"] = "sst.sst.Secret";
|
|
32
|
+
ResourceType$1["SSTSecret"] = "sst:sst:Secret";
|
|
33
|
+
ResourceType$1["SSTFunction"] = "sst:sst:Function";
|
|
34
|
+
ResourceType$1["SSTApiGatewayV2"] = "sst:aws:ApiGatewayV2";
|
|
35
|
+
ResourceType$1["SSTPostgres"] = "sst:aws:Postgres";
|
|
36
|
+
ResourceType$1["SSTBucket"] = "sst:aws:Bucket";
|
|
37
|
+
return ResourceType$1;
|
|
38
|
+
}({});
|
|
39
|
+
/**
|
|
40
|
+
* Processes a Secret resource into environment variables.
|
|
41
|
+
*
|
|
42
|
+
* @param name - The resource name
|
|
43
|
+
* @param value - The Secret resource
|
|
44
|
+
* @returns Object with environment variable mappings
|
|
45
|
+
*/
|
|
46
|
+
const secret = (name, value) => ({ [environmentCase(name)]: value.value });
|
|
47
|
+
/**
|
|
48
|
+
* Processes a Postgres database resource into environment variables.
|
|
49
|
+
* Creates multiple environment variables for database connection details.
|
|
50
|
+
*
|
|
51
|
+
* @param key - The resource key
|
|
52
|
+
* @param value - The Postgres resource
|
|
53
|
+
* @returns Object with database connection environment variables
|
|
54
|
+
*/
|
|
55
|
+
const postgres = (key, value) => {
|
|
56
|
+
const prefix = `${environmentCase(key)}`;
|
|
57
|
+
return {
|
|
58
|
+
[`${prefix}_NAME`]: value.database,
|
|
59
|
+
[`${prefix}_HOST`]: value.host,
|
|
60
|
+
[`${prefix}_PASSWORD`]: value.password,
|
|
61
|
+
[`${prefix}_PORT`]: value.port,
|
|
62
|
+
[`${prefix}_USERNAME`]: value.username
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Processes a Bucket resource into environment variables.
|
|
67
|
+
*
|
|
68
|
+
* @param name - The resource name
|
|
69
|
+
* @param value - The Bucket resource
|
|
70
|
+
* @returns Object with bucket name environment variable
|
|
71
|
+
*/
|
|
72
|
+
const bucket = (name, value) => {
|
|
73
|
+
const prefix = `${environmentCase(name)}`;
|
|
74
|
+
return { [`${prefix}_NAME`]: value.name };
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* No-operation processor for resources that don't require environment variables.
|
|
78
|
+
*
|
|
79
|
+
* @param name - The resource name (unused)
|
|
80
|
+
* @param value - The resource value (unused)
|
|
81
|
+
* @returns Empty object
|
|
82
|
+
*/
|
|
83
|
+
const noop = (name, value) => ({});
|
|
84
|
+
/**
|
|
85
|
+
* Map of resource types to their corresponding processor functions.
|
|
86
|
+
* Each processor converts resource data into environment variables.
|
|
87
|
+
*/
|
|
88
|
+
const processors = {
|
|
89
|
+
[ResourceType.ApiGatewayV2]: noop,
|
|
90
|
+
[ResourceType.Function]: noop,
|
|
91
|
+
[ResourceType.Vpc]: noop,
|
|
92
|
+
[ResourceType.Secret]: secret,
|
|
93
|
+
[ResourceType.Postgres]: postgres,
|
|
94
|
+
[ResourceType.Bucket]: bucket,
|
|
95
|
+
[ResourceType.SSTSecret]: secret,
|
|
96
|
+
[ResourceType.SSTBucket]: bucket,
|
|
97
|
+
[ResourceType.SSTFunction]: noop,
|
|
98
|
+
[ResourceType.SSTPostgres]: postgres,
|
|
99
|
+
[ResourceType.SSTApiGatewayV2]: noop
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Normalizes SST resources and plain strings into environment variables.
|
|
103
|
+
* Processes resources based on their type and converts names to environment case.
|
|
104
|
+
*
|
|
105
|
+
* @param record - Object containing resources and/or string values
|
|
106
|
+
* @returns Normalized environment variables object
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* normalizeResourceEnv({
|
|
110
|
+
* apiUrl: 'https://api.example.com',
|
|
111
|
+
* database: { type: ResourceType.Postgres, ... }
|
|
112
|
+
* })
|
|
113
|
+
*/
|
|
114
|
+
function normalizeResourceEnv(record) {
|
|
115
|
+
const env = {};
|
|
116
|
+
for (const [k, value] of Object.entries(record)) {
|
|
117
|
+
if (typeof value === "string") {
|
|
118
|
+
env[environmentCase(k)] = value;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const processor = processors[value.type];
|
|
122
|
+
if (processor) Object.assign(env, processor(k, value));
|
|
123
|
+
else console.warn(`No processor found for resource type: `, { value });
|
|
124
|
+
}
|
|
125
|
+
return env;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
Object.defineProperty(exports, 'ResourceType', {
|
|
130
|
+
enumerable: true,
|
|
131
|
+
get: function () {
|
|
132
|
+
return ResourceType;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
Object.defineProperty(exports, 'environmentCase', {
|
|
136
|
+
enumerable: true,
|
|
137
|
+
get: function () {
|
|
138
|
+
return environmentCase;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
Object.defineProperty(exports, 'normalizeResourceEnv', {
|
|
142
|
+
enumerable: true,
|
|
143
|
+
get: function () {
|
|
144
|
+
return normalizeResourceEnv;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
@@ -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/dist/sst.cjs
CHANGED
|
@@ -1,131 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
const lodash_snakecase = require_chunk.__toESM(require("lodash.snakecase"));
|
|
1
|
+
const require_sst = require('./sst-BSxwaAdz.cjs');
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* Numbers following underscores are preserved without the underscore.
|
|
8
|
-
*
|
|
9
|
-
* @param name - The string to convert
|
|
10
|
-
* @returns The converted string in environment variable format
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
14
|
-
* environmentCase('api_v2') // 'APIV2'
|
|
15
|
-
*/
|
|
16
|
-
function environmentCase(name) {
|
|
17
|
-
return (0, lodash_snakecase.default)(name).toUpperCase().replace(/_\d+/g, (r) => {
|
|
18
|
-
return r.replace("_", "");
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Enumeration of supported SST (Serverless Stack Toolkit) resource types.
|
|
23
|
-
* Used to identify and process different AWS and SST resources.
|
|
24
|
-
*/
|
|
25
|
-
let ResourceType = /* @__PURE__ */ function(ResourceType$1) {
|
|
26
|
-
ResourceType$1["ApiGatewayV2"] = "sst.aws.ApiGatewayV2";
|
|
27
|
-
ResourceType$1["Postgres"] = "sst.aws.Postgres";
|
|
28
|
-
ResourceType$1["Function"] = "sst.aws.Function";
|
|
29
|
-
ResourceType$1["Bucket"] = "sst.aws.Bucket";
|
|
30
|
-
ResourceType$1["Vpc"] = "sst.aws.Vpc";
|
|
31
|
-
ResourceType$1["Secret"] = "sst.sst.Secret";
|
|
32
|
-
ResourceType$1["SSTSecret"] = "sst:sst:Secret";
|
|
33
|
-
ResourceType$1["SSTFunction"] = "sst:sst:Function";
|
|
34
|
-
ResourceType$1["SSTApiGatewayV2"] = "sst:aws:ApiGatewayV2";
|
|
35
|
-
ResourceType$1["SSTPostgres"] = "sst:aws:Postgres";
|
|
36
|
-
ResourceType$1["SSTBucket"] = "sst:aws:Bucket";
|
|
37
|
-
return ResourceType$1;
|
|
38
|
-
}({});
|
|
39
|
-
/**
|
|
40
|
-
* Processes a Secret resource into environment variables.
|
|
41
|
-
*
|
|
42
|
-
* @param name - The resource name
|
|
43
|
-
* @param value - The Secret resource
|
|
44
|
-
* @returns Object with environment variable mappings
|
|
45
|
-
*/
|
|
46
|
-
const secret = (name, value) => ({ [environmentCase(name)]: value.value });
|
|
47
|
-
/**
|
|
48
|
-
* Processes a Postgres database resource into environment variables.
|
|
49
|
-
* Creates multiple environment variables for database connection details.
|
|
50
|
-
*
|
|
51
|
-
* @param key - The resource key
|
|
52
|
-
* @param value - The Postgres resource
|
|
53
|
-
* @returns Object with database connection environment variables
|
|
54
|
-
*/
|
|
55
|
-
const postgres = (key, value) => {
|
|
56
|
-
const prefix = `${environmentCase(key)}`;
|
|
57
|
-
return {
|
|
58
|
-
[`${prefix}_NAME`]: value.database,
|
|
59
|
-
[`${prefix}_HOST`]: value.host,
|
|
60
|
-
[`${prefix}_PASSWORD`]: value.password,
|
|
61
|
-
[`${prefix}_PORT`]: value.port,
|
|
62
|
-
[`${prefix}_USERNAME`]: value.username
|
|
63
|
-
};
|
|
64
|
-
};
|
|
65
|
-
/**
|
|
66
|
-
* Processes a Bucket resource into environment variables.
|
|
67
|
-
*
|
|
68
|
-
* @param name - The resource name
|
|
69
|
-
* @param value - The Bucket resource
|
|
70
|
-
* @returns Object with bucket name environment variable
|
|
71
|
-
*/
|
|
72
|
-
const bucket = (name, value) => {
|
|
73
|
-
const prefix = `${environmentCase(name)}`;
|
|
74
|
-
return { [`${prefix}_NAME`]: value.name };
|
|
75
|
-
};
|
|
76
|
-
/**
|
|
77
|
-
* No-operation processor for resources that don't require environment variables.
|
|
78
|
-
*
|
|
79
|
-
* @param name - The resource name (unused)
|
|
80
|
-
* @param value - The resource value (unused)
|
|
81
|
-
* @returns Empty object
|
|
82
|
-
*/
|
|
83
|
-
const noop = (name, value) => ({});
|
|
84
|
-
/**
|
|
85
|
-
* Map of resource types to their corresponding processor functions.
|
|
86
|
-
* Each processor converts resource data into environment variables.
|
|
87
|
-
*/
|
|
88
|
-
const processors = {
|
|
89
|
-
[ResourceType.ApiGatewayV2]: noop,
|
|
90
|
-
[ResourceType.Function]: noop,
|
|
91
|
-
[ResourceType.Vpc]: noop,
|
|
92
|
-
[ResourceType.Secret]: secret,
|
|
93
|
-
[ResourceType.Postgres]: postgres,
|
|
94
|
-
[ResourceType.Bucket]: bucket,
|
|
95
|
-
[ResourceType.SSTSecret]: secret,
|
|
96
|
-
[ResourceType.SSTBucket]: bucket,
|
|
97
|
-
[ResourceType.SSTFunction]: noop,
|
|
98
|
-
[ResourceType.SSTPostgres]: postgres,
|
|
99
|
-
[ResourceType.SSTApiGatewayV2]: noop
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* Normalizes SST resources and plain strings into environment variables.
|
|
103
|
-
* Processes resources based on their type and converts names to environment case.
|
|
104
|
-
*
|
|
105
|
-
* @param record - Object containing resources and/or string values
|
|
106
|
-
* @returns Normalized environment variables object
|
|
107
|
-
*
|
|
108
|
-
* @example
|
|
109
|
-
* normalizeResourceEnv({
|
|
110
|
-
* apiUrl: 'https://api.example.com',
|
|
111
|
-
* database: { type: ResourceType.Postgres, ... }
|
|
112
|
-
* })
|
|
113
|
-
*/
|
|
114
|
-
function normalizeResourceEnv(record) {
|
|
115
|
-
const env = {};
|
|
116
|
-
for (const [k, value] of Object.entries(record)) {
|
|
117
|
-
if (typeof value === "string") {
|
|
118
|
-
env[environmentCase(k)] = value;
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
const processor = processors[value.type];
|
|
122
|
-
if (processor) Object.assign(env, processor(k, value));
|
|
123
|
-
else console.warn(`No processor found for resource type: `, { value });
|
|
124
|
-
}
|
|
125
|
-
return env;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
//#endregion
|
|
129
|
-
exports.ResourceType = ResourceType;
|
|
130
|
-
exports.environmentCase = environmentCase;
|
|
131
|
-
exports.normalizeResourceEnv = normalizeResourceEnv;
|
|
3
|
+
exports.ResourceType = require_sst.ResourceType;
|
|
4
|
+
exports.environmentCase = require_sst.environmentCase;
|
|
5
|
+
exports.normalizeResourceEnv = require_sst.normalizeResourceEnv;
|
package/dist/sst.mjs
CHANGED
|
@@ -1,128 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ResourceType, environmentCase, normalizeResourceEnv } from "./sst-CQhO0S6y.mjs";
|
|
2
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
3
|
export { ResourceType, environmentCase, normalizeResourceEnv };
|