convex-env 2.4.1 → 2.5.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/index.mjs +74 -28
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -22,6 +22,16 @@ const validateAndTransformBoolean = (value) => {
|
|
|
22
22
|
throw new Error("Value is not a valid boolean");
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/utils.ts
|
|
27
|
+
function formatEnvErrors(issues) {
|
|
28
|
+
return [
|
|
29
|
+
"❌ Invalid environment variables:",
|
|
30
|
+
"",
|
|
31
|
+
...issues.map(({ variable, reason }) => ` ${variable}: ${reason}`)
|
|
32
|
+
].join("\n");
|
|
33
|
+
}
|
|
34
|
+
|
|
25
35
|
//#endregion
|
|
26
36
|
//#region src/env.ts
|
|
27
37
|
/**
|
|
@@ -96,26 +106,47 @@ const createEnv = (args) => {
|
|
|
96
106
|
options = args.options;
|
|
97
107
|
} else schema = args;
|
|
98
108
|
const values = inputValues ?? process.env;
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
const issues = [];
|
|
110
|
+
const entries = [];
|
|
111
|
+
for (const key of Object.keys(schema)) try {
|
|
112
|
+
if (key === "CONVEX_SITE_URL" || key === "CONVEX_CLOUD_URL") {
|
|
113
|
+
issues.push({
|
|
114
|
+
variable: key,
|
|
115
|
+
reason: "Cannot override CONVEX_SITE_URL or CONVEX_CLOUD_URL"
|
|
116
|
+
});
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const validator = schema[key];
|
|
120
|
+
const envValue = values[key];
|
|
121
|
+
if (validator.isOptional === "required" && envValue === void 0 && options?.skipValidation !== true) {
|
|
122
|
+
issues.push({
|
|
123
|
+
variable: key,
|
|
124
|
+
reason: "Variable is required but not found in env"
|
|
125
|
+
});
|
|
126
|
+
continue;
|
|
111
127
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
128
|
+
const transformedValue = transformed(envValue, validator);
|
|
129
|
+
if (validate(validator, transformedValue) === false && options?.skipValidation !== true) {
|
|
130
|
+
issues.push({
|
|
131
|
+
variable: key,
|
|
132
|
+
reason: `Variable failed to validate as type: ${validator.kind}`
|
|
133
|
+
});
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
entries.push([key, transformedValue]);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
139
|
+
issues.push({
|
|
140
|
+
variable: key,
|
|
141
|
+
reason: errorMessage
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
if (issues.length > 0) throw new Error(formatEnvErrors(issues));
|
|
145
|
+
return {
|
|
116
146
|
CONVEX_SITE_URL: process.env.CONVEX_SITE_URL,
|
|
117
|
-
CONVEX_CLOUD_URL: process.env.CONVEX_CLOUD_URL
|
|
118
|
-
|
|
147
|
+
CONVEX_CLOUD_URL: process.env.CONVEX_CLOUD_URL,
|
|
148
|
+
...Object.fromEntries(entries)
|
|
149
|
+
};
|
|
119
150
|
};
|
|
120
151
|
/**
|
|
121
152
|
*
|
|
@@ -153,17 +184,32 @@ const verifyEnv = (args) => {
|
|
|
153
184
|
inputValues = args.values;
|
|
154
185
|
} else schema = args;
|
|
155
186
|
const values = inputValues ?? process.env;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
187
|
+
const issues = [];
|
|
188
|
+
for (const key of Object.keys(schema)) try {
|
|
189
|
+
const validator = schema[key];
|
|
190
|
+
const envValue = values[key];
|
|
191
|
+
if (validator.isOptional === "required" && envValue === void 0) {
|
|
192
|
+
issues.push({
|
|
193
|
+
variable: key,
|
|
194
|
+
reason: "Variable is required but not found in env"
|
|
195
|
+
});
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (validate(validator, transformed(envValue, validator)) === false) {
|
|
199
|
+
issues.push({
|
|
200
|
+
variable: key,
|
|
201
|
+
reason: `Variable failed to validate as type: ${validator.kind}`
|
|
202
|
+
});
|
|
203
|
+
continue;
|
|
165
204
|
}
|
|
166
|
-
})
|
|
205
|
+
} catch (error) {
|
|
206
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
207
|
+
issues.push({
|
|
208
|
+
variable: key,
|
|
209
|
+
reason: errorMessage
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
if (issues.length > 0) throw new Error(formatEnvErrors(issues));
|
|
167
213
|
};
|
|
168
214
|
|
|
169
215
|
//#endregion
|