convex-env 2.0.0 → 2.2.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
@@ -14,8 +14,9 @@ Validators currently supported:
14
14
  - v.string()
15
15
  - v.number()
16
16
  - v.boolean()
17
+ - v.union() + v.literal() (strings only)
17
18
 
18
- You can use `v.optional()` on _any_ supported validator, see [example](#usage) below
19
+ You can use `v.optional()` on _any_ supported validator, see [examples](#usage) below
19
20
 
20
21
  <span style="color: red;"><strong>IMPORTANT</strong></span>: The <code>env</code> object from <code>createEnv</code> should only be used in the Convex runtime, the values on it will not be accessible client-side.
21
22
 
@@ -50,6 +51,7 @@ import { createEnv } from "convex-env";
50
51
  import { v } from "convex/values";
51
52
 
52
53
  export const env = createEnv({
54
+ ENVIRONMENT: v.union(v.literal("development"), v.literal("production")),
53
55
  CONVEX_SITE_URL: v.string(),
54
56
  BETTER_AUTH_SECRET: v.string(),
55
57
  GOOGLE_CLIENT_ID: v.string(),
@@ -93,6 +95,7 @@ export const env = createEnv({
93
95
  MAX_REQUESTS_PER_USER: v.number(),
94
96
  DEBUG_MODE: v.optional(v.boolean()),
95
97
  },
98
+ // optional, defaults to process.env
96
99
  values: {
97
100
  CONVEX_SITE_URL: process.env.CONVEX_SITE_URL,
98
101
  MAX_REQUESTS_PER_USER: process.env.MAX_REQUESTS_PER_USER,
@@ -117,12 +120,6 @@ export const schema = {
117
120
 
118
121
  export const env = createEnv({
119
122
  schema,
120
- // optional, defaults to process.env
121
- values: {
122
- CONVEX_SITE_URL: process.env.CONVEX_SITE_URL,
123
- MAX_REQUESTS_PER_USER: process.env.MAX_REQUESTS_PER_USER,
124
- DEBUG_MODE: process.env.DEBUG_MODE,
125
- },
126
123
  options: {
127
124
  skipValidation: true,
128
125
  },
package/dist/index.d.mts CHANGED
@@ -1,7 +1,10 @@
1
- import { Infer, VBoolean, VFloat64, VOptional, VString } from "convex/values";
1
+ import { Infer, VBoolean, VFloat64, VLiteral, VOptional, VString, VUnion } from "convex/values";
2
2
 
3
3
  //#region src/types.d.ts
4
- type AllowedPrimitiveValidators = VString | VFloat64 | VBoolean;
4
+ type AllowedBaseValidators = VString | VFloat64 | VBoolean;
5
+ type AllowedLiteralValidators = VLiteral<string>;
6
+ type AllowedUnionValidators = VUnion<any, [AllowedLiteralValidators, AllowedLiteralValidators, ...AllowedLiteralValidators[]]>;
7
+ type AllowedPrimitiveValidators = AllowedBaseValidators | AllowedLiteralValidators | AllowedUnionValidators;
5
8
  type AllowedOptionalValidators = VOptional<AllowedPrimitiveValidators>;
6
9
  type AllowedValidators = AllowedPrimitiveValidators | AllowedOptionalValidators;
7
10
  type InferredOuput<V extends AllowedValidators> = Infer<V>;
@@ -25,6 +28,7 @@ type CreateEnvOptions = {
25
28
  * FOO: v.string(),
26
29
  * BAR: v.number(),
27
30
  * BAZ: v.boolean(),
31
+ * FIZZ: v.union(v.literal("development"), v.literal("production")),
28
32
  * });
29
33
  *
30
34
  * @example
@@ -33,11 +37,13 @@ type CreateEnvOptions = {
33
37
  * FOO: v.string(),
34
38
  * BAR: v.number(),
35
39
  * BAZ: v.optional(v.boolean()),
40
+ * FIZZ: v.union(v.literal("development"), v.literal("production")),
36
41
  * },
37
42
  * values: {
38
43
  * FOO: process.env.FOO,
39
44
  * BAR: "42",
40
45
  * BAZ: "true",
46
+ * FIZZ: "development",
41
47
  * },
42
48
  * options: {
43
49
  * skipValidation: true,
@@ -49,7 +55,10 @@ declare const createEnv: <Schema extends Record<string, AllowedValidators>>(args
49
55
  schema: Schema;
50
56
  values?: Values<Schema>;
51
57
  options?: CreateEnvOptions;
52
- }) => { [K in keyof Schema]: InferredOuput<Schema[K]> };
58
+ }) => { [K in keyof Schema]: InferredOuput<Schema[K]> } & {
59
+ CONVEX_SITE_URL: string;
60
+ CONVEX_CLOUD_URL: string;
61
+ };
53
62
  /**
54
63
  *
55
64
  * @description You may want to verify the existence and type of the environment variables separately from the creation of the env object. If so, use this function in convex.config.ts, and use the skipValidation option when calling createEnv.
package/dist/index.mjs CHANGED
@@ -1,13 +1,14 @@
1
1
  import { validate } from "convex-helpers/validators";
2
- import { v } from "convex/values";
3
2
 
4
3
  //#region src/transform.ts
5
4
  const transformed = (value, validator) => {
6
5
  if (value === void 0) return void 0;
7
6
  if (value.trim() === "") throw new Error("Value is empty");
8
- if (validator.kind === v.string().kind) return value;
9
- if (validator.kind === v.number().kind) return validateAndTransformNumber(value);
10
- if (validator.kind === v.boolean().kind) return validateAndTransformBoolean(value);
7
+ if (validator.kind === "union") return value;
8
+ if (validator.kind === "literal") return value;
9
+ if (validator.kind === "string") return value;
10
+ if (validator.kind === "float64") return validateAndTransformNumber(value);
11
+ if (validator.kind === "boolean") return validateAndTransformBoolean(value);
11
12
  throw new Error("Validator is not supported");
12
13
  };
13
14
  const validateAndTransformNumber = (value) => {
@@ -37,6 +38,7 @@ const validateAndTransformBoolean = (value) => {
37
38
  * FOO: v.string(),
38
39
  * BAR: v.number(),
39
40
  * BAZ: v.boolean(),
41
+ * FIZZ: v.union(v.literal("development"), v.literal("production")),
40
42
  * });
41
43
  *
42
44
  * @example
@@ -45,11 +47,13 @@ const validateAndTransformBoolean = (value) => {
45
47
  * FOO: v.string(),
46
48
  * BAR: v.number(),
47
49
  * BAZ: v.optional(v.boolean()),
50
+ * FIZZ: v.union(v.literal("development"), v.literal("production")),
48
51
  * },
49
52
  * values: {
50
53
  * FOO: process.env.FOO,
51
54
  * BAR: "42",
52
55
  * BAZ: "true",
56
+ * FIZZ: "development",
53
57
  * },
54
58
  * options: {
55
59
  * skipValidation: true,
@@ -72,6 +76,7 @@ const createEnv = (args) => {
72
76
  const values = inputValues ?? process.env;
73
77
  return Object.keys(schema).map((key) => {
74
78
  try {
79
+ if (key === "CONVEX_SITE_URL" || key === "CONVEX_CLOUD_URL") throw new Error("Cannot override CONVEX_SITE_URL or CONVEX_CLOUD_URL");
75
80
  const validator = schema[key];
76
81
  const envValue = values[key];
77
82
  if (validator.isOptional === "required" && envValue === void 0 && options?.skipValidation !== true) throw new Error("Variable is required but not found in env");
@@ -85,7 +90,10 @@ const createEnv = (args) => {
85
90
  }).reduce((acc, [key, value]) => {
86
91
  acc[key] = value;
87
92
  return acc;
88
- }, {});
93
+ }, {
94
+ CONVEX_SITE_URL: process.env.CONVEX_SITE_URL,
95
+ CONVEX_CLOUD_URL: process.env.CONVEX_CLOUD_URL
96
+ });
89
97
  };
90
98
  /**
91
99
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "convex-env",
3
3
  "type": "module",
4
- "version": "2.0.0",
4
+ "version": "2.2.0",
5
5
  "license": "MIT",
6
6
  "description": "Type-safe access to environment variables in Convex",
7
7
  "exports": {