convex-env 2.4.0 → 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/README.md CHANGED
@@ -141,13 +141,13 @@ I wrote out some presets for commonly used pairs of variables. Below is a comple
141
141
  - everything supported by [better-auth](https://www.better-auth.com)
142
142
  - environment
143
143
  - "development", "preview", or "production"
144
+ - [aws](https://aws.amazon.com/sdk-for-javascript/)
144
145
  - [clerk](https://docs.convex.dev/auth/clerk)
145
146
  - [workOS](https://docs.convex.dev/auth/authkit/),
146
147
  - [betterAuth](https://labs.convex.dev/better-auth)
147
148
  - [auth0](https://docs.convex.dev/auth/auth0)
148
149
  - [resend](https://www.convex.dev/components/resend)
149
150
  - [r2](https://www.convex.dev/components/cloudflare-r2)
150
- - [aws](https://aws.amazon.com/sdk-for-javascript/)
151
151
  - [stripe](https://www.convex.dev/components/stripe)
152
152
  - [autumn](https://www.convex.dev/components/autumn)
153
153
  - [dodo](https://www.convex.dev/components/dodopayments)
package/dist/index.d.mts CHANGED
@@ -44,11 +44,11 @@ type Prettify<T> = { [K in keyof T]: T[K] } & {};
44
44
  * import { betterAuth, oAuth, r2 } from "convex-env/presets";
45
45
  *
46
46
  * const env = createEnv({
47
- * ...betterAuth,
48
- * ...oAuth.google,
49
- * ...oAuth.apple,
50
- * ...r2,
51
47
  * schema: {
48
+ * ...oAuth.apple,
49
+ * ...oAuth.google,
50
+ * ...betterAuth,
51
+ * ...r2,
52
52
  * OPENAI_API_KEY: v.string(),
53
53
  * FREE_REQUESTS_PER_USER: v.number(),
54
54
  * DEBUG_MODE: v.optional(v.boolean()),
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
  /**
@@ -53,11 +63,11 @@ const validateAndTransformBoolean = (value) => {
53
63
  * import { betterAuth, oAuth, r2 } from "convex-env/presets";
54
64
  *
55
65
  * const env = createEnv({
56
- * ...betterAuth,
57
- * ...oAuth.google,
58
- * ...oAuth.apple,
59
- * ...r2,
60
66
  * schema: {
67
+ * ...oAuth.apple,
68
+ * ...oAuth.google,
69
+ * ...betterAuth,
70
+ * ...r2,
61
71
  * OPENAI_API_KEY: v.string(),
62
72
  * FREE_REQUESTS_PER_USER: v.number(),
63
73
  * DEBUG_MODE: v.optional(v.boolean()),
@@ -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;
127
+ }
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;
111
135
  }
112
- }).reduce((acc, [key, value]) => {
113
- acc[key] = value;
114
- return acc;
115
- }, {
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.0",
4
+ "version": "2.5.0",
5
5
  "license": "MIT",
6
6
  "description": "Type-safe access to environment variables in Convex",
7
7
  "exports": {