@aamini/lib 0.0.11 → 0.0.13
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/env.d.mts.map +1 -1
- package/dist/env.mjs +6 -2
- package/dist/env.mjs.map +1 -1
- package/package.json +6 -3
- package/src/env.test.ts +13 -0
- package/src/env.ts +10 -2
package/dist/env.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.d.mts","names":[],"sources":["../src/env.ts"],"mappings":";KAEK,SAAA;EACJ,KAAA,CAAM,KAAA,YAAiB,CAAA;AAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"env.d.mts","names":[],"sources":["../src/env.ts"],"mappings":";KAEK,SAAA;EACJ,KAAA,CAAM,KAAA,YAAiB,CAAA;AAAA;AAAA,iBAOR,SAAA,kBAAA,CAA4B,MAAA,EAAQ,SAAA,CAAU,CAAA,IAAK,CAAA"}
|
package/dist/env.mjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { config } from "dotenv";
|
|
2
2
|
//#region src/env.ts
|
|
3
|
+
function isUsableEnvValue(value) {
|
|
4
|
+
return value !== void 0 && value.trim() !== "";
|
|
5
|
+
}
|
|
3
6
|
function createEnv(schema) {
|
|
4
7
|
const raw = process.env.RAILWAY_ENVIRONMENT_NAME ?? (process.env.NODE_ENV === "production" ? "production" : "development");
|
|
5
8
|
const environmentName = /(?:^|-)pr-\d+$/.test(raw) ? "staging" : raw;
|
|
@@ -18,8 +21,9 @@ function createEnv(schema) {
|
|
|
18
21
|
let parsed;
|
|
19
22
|
function parseEnv() {
|
|
20
23
|
if (parsed !== void 0) return parsed;
|
|
21
|
-
const env = {
|
|
22
|
-
for (const [key, value] of Object.entries(
|
|
24
|
+
const env = {};
|
|
25
|
+
for (const [key, value] of Object.entries(fileEnvironment)) if (isUsableEnvValue(value)) env[key] = value;
|
|
26
|
+
for (const [key, value] of Object.entries(process.env)) if (isUsableEnvValue(value)) env[key] = value;
|
|
23
27
|
parsed = schema.parse(env);
|
|
24
28
|
return parsed;
|
|
25
29
|
}
|
package/dist/env.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.mjs","names":[],"sources":["../src/env.ts"],"sourcesContent":["import { config } from 'dotenv'\n\ntype EnvSchema<T extends object> = {\n\tparse(input: unknown): T\n}\n\nexport function createEnv<T extends object>(schema: EnvSchema<T>): T {\n\tconst raw =\n\t\tprocess.env.RAILWAY_ENVIRONMENT_NAME ??\n\t\t(process.env.NODE_ENV === 'production' ? 'production' : 'development')\n\tconst environmentName = /(?:^|-)pr-\\d+$/.test(raw) ? 'staging' : raw\n\tconst fileEnvironment: NodeJS.ProcessEnv = {}\n\n\tconfig({\n\t\tpath: [\n\t\t\t'.env',\n\t\t\t'.env.local',\n\t\t\t`.env.${environmentName}`,\n\t\t\t`.env.${environmentName}.local`,\n\t\t],\n\t\tquiet: true,\n\t\toverride: true,\n\t\tprocessEnv: fileEnvironment,\n\t})\n\n\tlet parsed: T | undefined\n\n\tfunction parseEnv() {\n\t\tif (parsed !== undefined) return parsed\n\n\t\tconst env: NodeJS.ProcessEnv = {
|
|
1
|
+
{"version":3,"file":"env.mjs","names":[],"sources":["../src/env.ts"],"sourcesContent":["import { config } from 'dotenv'\n\ntype EnvSchema<T extends object> = {\n\tparse(input: unknown): T\n}\n\nfunction isUsableEnvValue(value: string | undefined): value is string {\n\treturn value !== undefined && value.trim() !== ''\n}\n\nexport function createEnv<T extends object>(schema: EnvSchema<T>): T {\n\tconst raw =\n\t\tprocess.env.RAILWAY_ENVIRONMENT_NAME ??\n\t\t(process.env.NODE_ENV === 'production' ? 'production' : 'development')\n\tconst environmentName = /(?:^|-)pr-\\d+$/.test(raw) ? 'staging' : raw\n\tconst fileEnvironment: NodeJS.ProcessEnv = {}\n\n\tconfig({\n\t\tpath: [\n\t\t\t'.env',\n\t\t\t'.env.local',\n\t\t\t`.env.${environmentName}`,\n\t\t\t`.env.${environmentName}.local`,\n\t\t],\n\t\tquiet: true,\n\t\toverride: true,\n\t\tprocessEnv: fileEnvironment,\n\t})\n\n\tlet parsed: T | undefined\n\n\tfunction parseEnv() {\n\t\tif (parsed !== undefined) return parsed\n\n\t\tconst env: NodeJS.ProcessEnv = {}\n\n\t\tfor (const [key, value] of Object.entries(fileEnvironment)) {\n\t\t\tif (isUsableEnvValue(value)) env[key] = value\n\t\t}\n\n\t\tfor (const [key, value] of Object.entries(process.env)) {\n\t\t\tif (isUsableEnvValue(value)) env[key] = value\n\t\t}\n\n\t\tparsed = schema.parse(env)\n\t\treturn parsed\n\t}\n\n\treturn new Proxy({} as Record<PropertyKey, unknown>, {\n\t\tget(_target, property, receiver) {\n\t\t\treturn Reflect.get(parseEnv() as object, property, receiver)\n\t\t},\n\t\thas(_target, property) {\n\t\t\treturn property in (parseEnv() as object)\n\t\t},\n\t\townKeys() {\n\t\t\treturn Reflect.ownKeys(parseEnv() as object)\n\t\t},\n\t\tgetOwnPropertyDescriptor(_target, property) {\n\t\t\treturn Object.getOwnPropertyDescriptor(parseEnv() as object, property)\n\t\t},\n\t}) as T\n}\n"],"mappings":";;AAMA,SAAS,iBAAiB,OAA4C;CACrE,OAAO,UAAU,KAAA,KAAa,MAAM,MAAM,KAAK;;AAGhD,SAAgB,UAA4B,QAAyB;CACpE,MAAM,MACL,QAAQ,IAAI,6BACX,QAAQ,IAAI,aAAa,eAAe,eAAe;CACzD,MAAM,kBAAkB,iBAAiB,KAAK,IAAI,GAAG,YAAY;CACjE,MAAM,kBAAqC,EAAE;CAE7C,OAAO;EACN,MAAM;GACL;GACA;GACA,QAAQ;GACR,QAAQ,gBAAgB;GACxB;EACD,OAAO;EACP,UAAU;EACV,YAAY;EACZ,CAAC;CAEF,IAAI;CAEJ,SAAS,WAAW;EACnB,IAAI,WAAW,KAAA,GAAW,OAAO;EAEjC,MAAM,MAAyB,EAAE;EAEjC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,gBAAgB,EACzD,IAAI,iBAAiB,MAAM,EAAE,IAAI,OAAO;EAGzC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,QAAQ,IAAI,EACrD,IAAI,iBAAiB,MAAM,EAAE,IAAI,OAAO;EAGzC,SAAS,OAAO,MAAM,IAAI;EAC1B,OAAO;;CAGR,OAAO,IAAI,MAAM,EAAE,EAAkC;EACpD,IAAI,SAAS,UAAU,UAAU;GAChC,OAAO,QAAQ,IAAI,UAAU,EAAY,UAAU,SAAS;;EAE7D,IAAI,SAAS,UAAU;GACtB,OAAO,YAAa,UAAU;;EAE/B,UAAU;GACT,OAAO,QAAQ,QAAQ,UAAU,CAAW;;EAE7C,yBAAyB,SAAS,UAAU;GAC3C,OAAO,OAAO,yBAAyB,UAAU,EAAY,SAAS;;EAEvE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aamini/lib",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/
|
|
8
|
+
"url": "https://github.com/aria-amini/toolkit",
|
|
9
9
|
"directory": "packages/lib"
|
|
10
10
|
},
|
|
11
|
-
"
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/aria-amini/toolkit/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/aria-amini/toolkit/tree/main/packages/lib#readme",
|
|
12
15
|
"publishConfig": {
|
|
13
16
|
"access": "public"
|
|
14
17
|
},
|
package/src/env.test.ts
CHANGED
|
@@ -79,4 +79,17 @@ describe.sequential('createEnv', () => {
|
|
|
79
79
|
expect(() => env).not.toThrow()
|
|
80
80
|
expect(() => env.DATABASE_URL).toThrow(/expected string/)
|
|
81
81
|
})
|
|
82
|
+
|
|
83
|
+
it('ignores empty dotenv values', () => {
|
|
84
|
+
delete process.env.DATABASE_URL
|
|
85
|
+
writeFileSync(join(tempDir, '.env.production'), 'DATABASE_URL=\n')
|
|
86
|
+
|
|
87
|
+
const env = createEnv(
|
|
88
|
+
z.object({
|
|
89
|
+
DATABASE_URL: z.string(),
|
|
90
|
+
}),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
expect(() => env.DATABASE_URL).toThrow(/expected string/)
|
|
94
|
+
})
|
|
82
95
|
})
|
package/src/env.ts
CHANGED
|
@@ -4,6 +4,10 @@ type EnvSchema<T extends object> = {
|
|
|
4
4
|
parse(input: unknown): T
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
function isUsableEnvValue(value: string | undefined): value is string {
|
|
8
|
+
return value !== undefined && value.trim() !== ''
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
export function createEnv<T extends object>(schema: EnvSchema<T>): T {
|
|
8
12
|
const raw =
|
|
9
13
|
process.env.RAILWAY_ENVIRONMENT_NAME ??
|
|
@@ -28,10 +32,14 @@ export function createEnv<T extends object>(schema: EnvSchema<T>): T {
|
|
|
28
32
|
function parseEnv() {
|
|
29
33
|
if (parsed !== undefined) return parsed
|
|
30
34
|
|
|
31
|
-
const env: NodeJS.ProcessEnv = {
|
|
35
|
+
const env: NodeJS.ProcessEnv = {}
|
|
36
|
+
|
|
37
|
+
for (const [key, value] of Object.entries(fileEnvironment)) {
|
|
38
|
+
if (isUsableEnvValue(value)) env[key] = value
|
|
39
|
+
}
|
|
32
40
|
|
|
33
41
|
for (const [key, value] of Object.entries(process.env)) {
|
|
34
|
-
if (value
|
|
42
|
+
if (isUsableEnvValue(value)) env[key] = value
|
|
35
43
|
}
|
|
36
44
|
|
|
37
45
|
parsed = schema.parse(env)
|