@geekmidas/envkit 0.1.0 → 0.3.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/dist/{EnvironmentBuilder-DfmYRBm-.mjs → EnvironmentBuilder-BSuHZm0y.mjs} +2 -4
- package/dist/EnvironmentBuilder-BSuHZm0y.mjs.map +1 -0
- package/dist/EnvironmentBuilder-DHfDXJUm.d.mts.map +1 -0
- package/dist/{EnvironmentBuilder-W2wku49g.cjs → EnvironmentBuilder-Djr1VsWM.cjs} +2 -4
- package/dist/EnvironmentBuilder-Djr1VsWM.cjs.map +1 -0
- package/dist/EnvironmentBuilder-Xuf2Dd9u.d.cts.map +1 -0
- package/dist/EnvironmentBuilder.cjs +1 -1
- package/dist/EnvironmentBuilder.mjs +1 -1
- package/dist/EnvironmentParser-Bt246UeP.cjs.map +1 -1
- package/dist/{EnvironmentParser-CVWU1ooT.d.mts → EnvironmentParser-CY8TosTN.d.mts} +2 -1
- package/dist/EnvironmentParser-CY8TosTN.d.mts.map +1 -0
- package/dist/{EnvironmentParser-tV-JjCg7.d.cts → EnvironmentParser-DtOL86NU.d.cts} +2 -1
- package/dist/EnvironmentParser-DtOL86NU.d.cts.map +1 -0
- package/dist/EnvironmentParser-c06agx31.mjs.map +1 -1
- package/dist/EnvironmentParser.d.cts +1 -1
- package/dist/EnvironmentParser.d.mts +1 -1
- package/dist/SnifferEnvironmentParser.cjs.map +1 -1
- package/dist/SnifferEnvironmentParser.d.cts +3 -2
- package/dist/SnifferEnvironmentParser.d.cts.map +1 -0
- package/dist/SnifferEnvironmentParser.d.mts +3 -2
- package/dist/SnifferEnvironmentParser.d.mts.map +1 -0
- package/dist/SnifferEnvironmentParser.mjs.map +1 -1
- package/dist/{SstEnvironmentBuilder-DEa3lTUB.mjs → SstEnvironmentBuilder-BEBFSUYr.mjs} +2 -2
- package/dist/SstEnvironmentBuilder-BEBFSUYr.mjs.map +1 -0
- package/dist/SstEnvironmentBuilder-CjURMGjW.d.mts.map +1 -0
- package/dist/SstEnvironmentBuilder-D4oSo_KX.d.cts.map +1 -0
- package/dist/{SstEnvironmentBuilder-BuFw1hCe.cjs → SstEnvironmentBuilder-wFnN2M5O.cjs} +2 -2
- package/dist/SstEnvironmentBuilder-wFnN2M5O.cjs.map +1 -0
- package/dist/SstEnvironmentBuilder.cjs +2 -2
- package/dist/SstEnvironmentBuilder.mjs +2 -2
- package/dist/credentials.cjs +66 -0
- package/dist/credentials.cjs.map +1 -0
- package/dist/credentials.d.cts +31 -0
- package/dist/credentials.d.cts.map +1 -0
- package/dist/credentials.d.mts +31 -0
- package/dist/credentials.d.mts.map +1 -0
- package/dist/credentials.mjs +62 -0
- package/dist/credentials.mjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/sst.cjs +2 -2
- package/dist/sst.cjs.map +1 -1
- package/dist/sst.d.cts +1 -0
- package/dist/sst.d.cts.map +1 -0
- package/dist/sst.d.mts +1 -0
- package/dist/sst.d.mts.map +1 -0
- package/dist/sst.mjs +2 -2
- package/dist/sst.mjs.map +1 -1
- package/examples/basic-usage.ts +329 -333
- package/package.json +6 -1
- package/src/EnvironmentBuilder.ts +76 -80
- package/src/EnvironmentParser.ts +231 -231
- package/src/SnifferEnvironmentParser.ts +178 -178
- package/src/SstEnvironmentBuilder.ts +127 -127
- package/src/__tests__/ConfigParser.spec.ts +388 -388
- package/src/__tests__/EnvironmentBuilder.spec.ts +245 -265
- package/src/__tests__/EnvironmentParser.spec.ts +828 -828
- package/src/__tests__/SnifferEnvironmentParser.spec.ts +380 -326
- package/src/__tests__/SstEnvironmentBuilder.spec.ts +347 -367
- package/src/__tests__/credentials.integration.spec.ts +239 -0
- package/src/__tests__/credentials.spec.ts +136 -0
- package/src/__tests__/sst.spec.ts +390 -413
- package/src/credentials.ts +99 -0
- package/src/index.ts +11 -11
- package/src/sst.ts +24 -24
- package/sst-env.d.ts +0 -1
- package/tsconfig.json +9 -0
- package/dist/EnvironmentBuilder-DfmYRBm-.mjs.map +0 -1
- package/dist/EnvironmentBuilder-W2wku49g.cjs.map +0 -1
- package/dist/SstEnvironmentBuilder-BuFw1hCe.cjs.map +0 -1
- package/dist/SstEnvironmentBuilder-DEa3lTUB.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/envkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -18,6 +18,11 @@
|
|
|
18
18
|
"types": "./dist/SnifferEnvironmentParser.d.ts",
|
|
19
19
|
"import": "./dist/SnifferEnvironmentParser.mjs",
|
|
20
20
|
"require": "./dist/SnifferEnvironmentParser.cjs"
|
|
21
|
+
},
|
|
22
|
+
"./credentials": {
|
|
23
|
+
"types": "./dist/credentials.d.ts",
|
|
24
|
+
"import": "./dist/credentials.mjs",
|
|
25
|
+
"require": "./dist/credentials.cjs"
|
|
21
26
|
}
|
|
22
27
|
},
|
|
23
28
|
"repository": {
|
|
@@ -12,11 +12,11 @@ import snakecase from 'lodash.snakecase';
|
|
|
12
12
|
* environmentCase('apiV2') // 'APIV2'
|
|
13
13
|
*/
|
|
14
14
|
export function environmentCase(name: string): string {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
return snakecase(name)
|
|
16
|
+
.toUpperCase()
|
|
17
|
+
.replace(/_\d+/g, (r) => {
|
|
18
|
+
return r.replace('_', '');
|
|
19
|
+
});
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
@@ -24,7 +24,7 @@ export function environmentCase(name: string): string {
|
|
|
24
24
|
* Values can be primitives or nested records.
|
|
25
25
|
*/
|
|
26
26
|
export interface EnvRecord {
|
|
27
|
-
|
|
27
|
+
[key: string]: EnvValue;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
@@ -52,11 +52,11 @@ export type Resolvers = Record<string, EnvironmentResolver<any>>;
|
|
|
52
52
|
* Options for configuring the EnvironmentBuilder.
|
|
53
53
|
*/
|
|
54
54
|
export interface EnvironmentBuilderOptions {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Handler called when a value's type doesn't match any registered resolver.
|
|
57
|
+
* Defaults to console.warn.
|
|
58
|
+
*/
|
|
59
|
+
onUnmatchedValue?: (key: string, value: unknown) => void;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/**
|
|
@@ -68,8 +68,8 @@ export type InputValue = string | { type: string; [key: string]: unknown };
|
|
|
68
68
|
* Base type for typed input values with a specific type discriminator.
|
|
69
69
|
*/
|
|
70
70
|
export type TypedInputValue<TType extends string = string> = {
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
type: TType;
|
|
72
|
+
[key: string]: unknown;
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -86,19 +86,19 @@ type OmitType<T> = T extends { type: string } ? Omit<T, 'type'> : never;
|
|
|
86
86
|
* Extracts all unique `type` values from a record (excluding plain strings).
|
|
87
87
|
*/
|
|
88
88
|
type AllTypeValues<TRecord extends Record<string, InputValue>> = {
|
|
89
|
-
|
|
89
|
+
[K in keyof TRecord]: ExtractType<TRecord[K]>;
|
|
90
90
|
}[keyof TRecord];
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
93
|
* For a given type value, finds the corresponding value type (without `type` key).
|
|
94
94
|
*/
|
|
95
95
|
type ValueForType<
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
TRecord extends Record<string, InputValue>,
|
|
97
|
+
TType extends string,
|
|
98
98
|
> = {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
[K in keyof TRecord]: TRecord[K] extends { type: TType }
|
|
100
|
+
? OmitType<TRecord[K]>
|
|
101
|
+
: never;
|
|
102
102
|
}[keyof TRecord];
|
|
103
103
|
|
|
104
104
|
/**
|
|
@@ -106,9 +106,9 @@ type ValueForType<
|
|
|
106
106
|
* Keys are the `type` values, values are resolver functions receiving the value without `type`.
|
|
107
107
|
*/
|
|
108
108
|
export type TypedResolvers<TRecord extends Record<string, InputValue>> = {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
[TType in AllTypeValues<TRecord>]: EnvironmentResolver<
|
|
110
|
+
ValueForType<TRecord, TType>
|
|
111
|
+
>;
|
|
112
112
|
};
|
|
113
113
|
|
|
114
114
|
/**
|
|
@@ -134,63 +134,59 @@ export type TypedResolvers<TRecord extends Record<string, InputValue>> = {
|
|
|
134
134
|
* ```
|
|
135
135
|
*/
|
|
136
136
|
export class EnvironmentBuilder<
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
TRecord extends Record<string, InputValue> = Record<string, InputValue>,
|
|
138
|
+
TResolvers extends Resolvers = TypedResolvers<TRecord>,
|
|
139
139
|
> {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return env;
|
|
195
|
-
}
|
|
140
|
+
private readonly record: TRecord;
|
|
141
|
+
private readonly resolvers: TResolvers;
|
|
142
|
+
private readonly options: Required<EnvironmentBuilderOptions>;
|
|
143
|
+
|
|
144
|
+
constructor(
|
|
145
|
+
record: TRecord,
|
|
146
|
+
resolvers: TResolvers,
|
|
147
|
+
options: EnvironmentBuilderOptions = {},
|
|
148
|
+
) {
|
|
149
|
+
this.record = record;
|
|
150
|
+
this.resolvers = resolvers;
|
|
151
|
+
this.options = {
|
|
152
|
+
onUnmatchedValue: options.onUnmatchedValue ?? ((_key, _value) => {}),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Build environment variables from the input record.
|
|
158
|
+
*
|
|
159
|
+
* - Plain string values are passed through with key transformation
|
|
160
|
+
* - Object values with a `type` property are matched against resolvers
|
|
161
|
+
* - Resolvers receive values without the `type` key
|
|
162
|
+
* - Only root-level keys are transformed to UPPER_SNAKE_CASE
|
|
163
|
+
*
|
|
164
|
+
* @returns A record of environment variables
|
|
165
|
+
*/
|
|
166
|
+
build(): EnvRecord {
|
|
167
|
+
const env: EnvRecord = {};
|
|
168
|
+
|
|
169
|
+
for (const [key, value] of Object.entries(this.record)) {
|
|
170
|
+
// Handle plain string values
|
|
171
|
+
if (typeof value === 'string') {
|
|
172
|
+
env[environmentCase(key)] = value;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Handle objects with type discriminator
|
|
177
|
+
const { type, ...rest } = value;
|
|
178
|
+
const resolver = this.resolvers[type];
|
|
179
|
+
if (resolver) {
|
|
180
|
+
const resolved = resolver(key, rest);
|
|
181
|
+
// Transform only root-level keys from resolver output
|
|
182
|
+
for (const [resolvedKey, resolvedValue] of Object.entries(resolved)) {
|
|
183
|
+
env[environmentCase(resolvedKey)] = resolvedValue;
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
this.options.onUnmatchedValue(key, value);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return env;
|
|
191
|
+
}
|
|
196
192
|
}
|