convex-env 1.0.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/LICENSE +21 -0
- package/README.md +108 -0
- package/dist/index.d.mts +41 -0
- package/dist/index.mjs +76 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shawn Rodgers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://b9sa6juqtj.ufs.sh/f/WmevHvqCEmRaetBLb0NWmSZIsPhkweq8TtVxHraXLjAdgyEC" alt="Convex Env Logo" style="max-width: 500px; width: 100%;">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h2 align="center">Convex Env</h2>
|
|
6
|
+
<p align="center">Typesafe access to environment variables in Convex</p>
|
|
7
|
+
|
|
8
|
+
### Overview
|
|
9
|
+
|
|
10
|
+
If you've ever used [t3-env](https://github.com/t3-oss/t3-env), its basically that, but native to [Convex](https://www.convex.dev).
|
|
11
|
+
|
|
12
|
+
Validators currently supported:
|
|
13
|
+
|
|
14
|
+
- v.string()
|
|
15
|
+
- v.number()
|
|
16
|
+
- v.boolean()
|
|
17
|
+
|
|
18
|
+
you can use `v.optional()` on _any supported validator_, see [example](#usage) below
|
|
19
|
+
|
|
20
|
+
<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
|
+
### Installation
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
ni convex-env
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
pnpm i convex-env
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
bun i convex-env
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
npm i convex-env
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
yarn add convex-env
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Usage
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
// convex.env.ts
|
|
48
|
+
|
|
49
|
+
import { createEnv } from "convex-env";
|
|
50
|
+
import { v } from "convex/values";
|
|
51
|
+
|
|
52
|
+
export const env = createEnv({
|
|
53
|
+
CONVEX_SITE_URL: v.string(),
|
|
54
|
+
BETTER_AUTH_SECRET: v.string(),
|
|
55
|
+
GOOGLE_CLIENT_ID: v.string(),
|
|
56
|
+
GOOGLE_CLIENT_SECRET: v.string(),
|
|
57
|
+
MAX_REQUESTS_PER_USER: v.number(),
|
|
58
|
+
DEBUG_MODE: v.optional(v.boolean()),
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Then access them anywhere in Convex.
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// auth.ts
|
|
66
|
+
|
|
67
|
+
import { env } from "./convex.env";
|
|
68
|
+
|
|
69
|
+
export const createAuth = (ctx: GenericCtx<DataModel>) => {
|
|
70
|
+
return betterAuth({
|
|
71
|
+
baseURL: env.CONVEX_SITE_URL,
|
|
72
|
+
socialProviders: {
|
|
73
|
+
google: {
|
|
74
|
+
clientId: env.GOOGLE_CLIENT_ID,
|
|
75
|
+
clientSecret: env.GOOGLE_CLIENT_SECRET,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
You can also pass values manually
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// convex.env.ts
|
|
86
|
+
|
|
87
|
+
import { createEnv } from "convex-env";
|
|
88
|
+
import { v } from "convex/values";
|
|
89
|
+
|
|
90
|
+
export const env = createEnv(
|
|
91
|
+
{
|
|
92
|
+
CONVEX_SITE_URL: v.string(),
|
|
93
|
+
BETTER_AUTH_SECRET: v.string(),
|
|
94
|
+
GOOGLE_CLIENT_ID: v.string(),
|
|
95
|
+
GOOGLE_CLIENT_SECRET: v.string(),
|
|
96
|
+
MAX_REQUESTS_PER_USER: v.number(),
|
|
97
|
+
DEBUG_MODE: v.optional(v.boolean()),
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
CONVEX_SITE_URL: process.env.CONVEX_SITE_URL,
|
|
101
|
+
BETTER_AUTH_SECRET: process.env.BETTER_AUTH_SECRET,
|
|
102
|
+
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
|
|
103
|
+
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
|
|
104
|
+
MAX_REQUESTS_PER_USER: process.env.MAX_REQUESTS_PER_USER,
|
|
105
|
+
DEBUG_MODE: process.env.DEBUG_MODE,
|
|
106
|
+
}
|
|
107
|
+
);
|
|
108
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Infer, VBoolean, VFloat64, VOptional, VString } from "convex/values";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type AllowedPrimitiveValidators = VString | VFloat64 | VBoolean;
|
|
5
|
+
type AllowedOptionalValidators = VOptional<AllowedPrimitiveValidators>;
|
|
6
|
+
type AllowedValidators = AllowedPrimitiveValidators | AllowedOptionalValidators;
|
|
7
|
+
type InferredOuput<V extends AllowedValidators> = Infer<V>;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/index.d.ts
|
|
10
|
+
/**
|
|
11
|
+
* WARNING: The object returned by `createEnv` should only be accessed within the Convex runtime.
|
|
12
|
+
* The values on the returned object will NOT be accessible client side.
|
|
13
|
+
*
|
|
14
|
+
* @param entries - The names of the environment variables and their validators
|
|
15
|
+
* @param inputEnv (Optional) pass in a record of the values to use, defaults to process.env if not provided
|
|
16
|
+
* @returns An object with the same keys as the entries, but with the validated typesafe values
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const env = createEnv({
|
|
22
|
+
* FOO: v.string(),
|
|
23
|
+
* BAR: v.number(),
|
|
24
|
+
* BAZ: v.boolean(),
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* const env = createEnv({
|
|
29
|
+
* FOO: v.string(),
|
|
30
|
+
* BAR: v.number(),
|
|
31
|
+
* BAZ: v.boolean(),
|
|
32
|
+
* }, {
|
|
33
|
+
* FOO: process.env.FOO,
|
|
34
|
+
* BAR: process.env.BAR,
|
|
35
|
+
* BAZ: process.env.BAZ,
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
declare const createEnv: <T extends Record<string, AllowedValidators>>(entries: T, inputEnv?: Partial<Record<keyof T, string | undefined>>) => { [K in keyof T]: InferredOuput<T[K]> };
|
|
40
|
+
//#endregion
|
|
41
|
+
export { createEnv };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { validate } from "convex-helpers/validators";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
//#region src/transform.ts
|
|
5
|
+
const transformed = (value, validator) => {
|
|
6
|
+
if (value === void 0) return void 0;
|
|
7
|
+
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);
|
|
11
|
+
throw new Error("Validator is not supported");
|
|
12
|
+
};
|
|
13
|
+
const validateAndTransformNumber = (value) => {
|
|
14
|
+
if (Number.isNaN(Number(value))) throw new Error("Value is not a number");
|
|
15
|
+
if (Number.isFinite(Number(value)) === false) throw new Error("Value is not a finite number");
|
|
16
|
+
return Number(value);
|
|
17
|
+
};
|
|
18
|
+
const validateAndTransformBoolean = (value) => {
|
|
19
|
+
if (value.trim().toLowerCase() === "true") return true;
|
|
20
|
+
if (value.trim().toLowerCase() === "false") return false;
|
|
21
|
+
throw new Error("Value is not a valid boolean");
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/index.ts
|
|
26
|
+
/**
|
|
27
|
+
* WARNING: The object returned by `createEnv` should only be accessed within the Convex runtime.
|
|
28
|
+
* The values on the returned object will NOT be accessible client side.
|
|
29
|
+
*
|
|
30
|
+
* @param entries - The names of the environment variables and their validators
|
|
31
|
+
* @param inputEnv (Optional) pass in a record of the values to use, defaults to process.env if not provided
|
|
32
|
+
* @returns An object with the same keys as the entries, but with the validated typesafe values
|
|
33
|
+
*
|
|
34
|
+
* @public
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const env = createEnv({
|
|
38
|
+
* FOO: v.string(),
|
|
39
|
+
* BAR: v.number(),
|
|
40
|
+
* BAZ: v.boolean(),
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const env = createEnv({
|
|
45
|
+
* FOO: v.string(),
|
|
46
|
+
* BAR: v.number(),
|
|
47
|
+
* BAZ: v.boolean(),
|
|
48
|
+
* }, {
|
|
49
|
+
* FOO: process.env.FOO,
|
|
50
|
+
* BAR: process.env.BAR,
|
|
51
|
+
* BAZ: process.env.BAZ,
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
const createEnv = (entries, inputEnv) => {
|
|
56
|
+
const env = inputEnv ?? process.env;
|
|
57
|
+
return Object.keys(entries).map((key) => {
|
|
58
|
+
try {
|
|
59
|
+
const validator = entries[key];
|
|
60
|
+
const envValue = env[key];
|
|
61
|
+
if (validator.isOptional === "required" && envValue === void 0) throw new Error("Variable is required but not found in env");
|
|
62
|
+
const transformedValue = transformed(envValue, validator);
|
|
63
|
+
if (validate(validator, transformedValue) === false) throw new Error(`Variable failed validation`);
|
|
64
|
+
return [key, transformedValue];
|
|
65
|
+
} catch (error) {
|
|
66
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
67
|
+
throw new Error(`Error creating environment variable ${key}: ${errorMessage}`);
|
|
68
|
+
}
|
|
69
|
+
}).reduce((acc, [key, value]) => {
|
|
70
|
+
acc[key] = value;
|
|
71
|
+
return acc;
|
|
72
|
+
}, {});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
export { createEnv };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "convex-env",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Typesafe access to environment variables in Convex",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/index.mjs",
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
10
|
+
"main": "./dist/index.mjs",
|
|
11
|
+
"module": "./dist/index.mjs",
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsdown",
|
|
18
|
+
"dev": "tsdown --watch",
|
|
19
|
+
"lint": "oxlint --type-aware --type-check",
|
|
20
|
+
"lint:fix": "oxlint --type-aware --type-check --fix && oxfmt",
|
|
21
|
+
"fmt": "oxfmt",
|
|
22
|
+
"test": "vitest",
|
|
23
|
+
"release": "changeset publish"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@changesets/changelog-github": "^0.5.2",
|
|
27
|
+
"@changesets/cli": "^2.29.8",
|
|
28
|
+
"@types/node": "^25.0.3",
|
|
29
|
+
"oxfmt": "^0.20.0",
|
|
30
|
+
"oxlint": "^1.35.0",
|
|
31
|
+
"oxlint-tsgolint": "^0.10.0",
|
|
32
|
+
"tsdown": "^0.18.1",
|
|
33
|
+
"typescript": "^5.9.3",
|
|
34
|
+
"vitest": "^4.0.16"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"convex-helpers": "^0.1.108"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"convex": ">=1.20.0"
|
|
41
|
+
}
|
|
42
|
+
}
|