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.
Files changed (2) hide show
  1. package/dist/index.mjs +74 -28
  2. 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
- return Object.keys(schema).map((key) => {
100
- try {
101
- if (key === "CONVEX_SITE_URL" || key === "CONVEX_CLOUD_URL") throw new Error("Cannot override CONVEX_SITE_URL or CONVEX_CLOUD_URL");
102
- const validator = schema[key];
103
- const envValue = values[key];
104
- if (validator.isOptional === "required" && envValue === void 0 && options?.skipValidation !== true) throw new Error("Variable is required but not found in env");
105
- const transformedValue = transformed(envValue, validator);
106
- if (validate(validator, transformedValue) === false && options?.skipValidation !== true) throw new Error(`Variable failed to validated as type: ${validator.kind}`);
107
- return [key, transformedValue];
108
- } catch (error) {
109
- const errorMessage = error instanceof Error ? error.message : String(error);
110
- throw new Error(`Error creating environment variable ${key}: ${errorMessage}`);
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
- }).reduce((acc, [key, value]) => {
113
- acc[key] = value;
114
- return acc;
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
- Object.keys(schema).map((key) => {
157
- try {
158
- const validator = schema[key];
159
- const envValue = values[key];
160
- if (validator.isOptional === "required" && envValue === void 0) throw new Error("Variable is required but not found in env");
161
- if (validate(validator, transformed(envValue, validator)) === false) throw new Error(`Variable failed validation`);
162
- } catch (error) {
163
- const errorMessage = error instanceof Error ? error.message : String(error);
164
- throw new Error(`Error verifying environment variable ${key}: ${errorMessage}`);
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
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "convex-env",
3
3
  "type": "module",
4
- "version": "2.4.1",
4
+ "version": "2.5.0",
5
5
  "license": "MIT",
6
6
  "description": "Type-safe access to environment variables in Convex",
7
7
  "exports": {