@pitininja/envious 2.0.0 → 3.0.0-beta.1
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 +16 -6
- package/dist/index.cjs +47 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.js +37 -23
- package/dist/index.js.map +1 -1
- package/package.json +18 -4
- package/.eslintignore +0 -3
- package/.eslintrc +0 -29
- package/.husky/pre-commit +0 -5
- package/.prettierignore +0 -4
- package/.prettierrc +0 -7
- package/src/index.ts +0 -33
- package/tsconfig.json +0 -19
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
> Environment variable parsing using Dotenv & Typebox
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Compatibility
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Since v3 this library is compatible with both ESM and CJS.
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
@@ -21,7 +21,7 @@ npm i @pitininja/envious
|
|
|
21
21
|
import { Type } from '@sinclair/typebox';
|
|
22
22
|
import { envious } from '@pitininja/envious';
|
|
23
23
|
|
|
24
|
-
export const
|
|
24
|
+
export const schema = Type.Object({
|
|
25
25
|
STRING_VAR: Type.String(),
|
|
26
26
|
NUMBER_VAR: Type.Integer(),
|
|
27
27
|
BOOLEAN_VAR: Type.Boolean(),
|
|
@@ -29,8 +29,18 @@ export const envSchema = Type.Object({
|
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
Parse your environment variables
|
|
33
|
-
If the environment variables don't match the schema, an error will be thrown
|
|
32
|
+
Parse your environment variables.
|
|
33
|
+
If the environment variables don't match the schema, an error will be thrown.
|
|
34
34
|
*/
|
|
35
|
-
const env = envious(
|
|
35
|
+
const env = envious(schema);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
You can provide default values as second parameter of the envious function.
|
|
39
|
+
*/
|
|
40
|
+
const envWithDefaults = envious(schema, {
|
|
41
|
+
STRING_VAR: 'Example',
|
|
42
|
+
NUMBER_VAR: 123,
|
|
43
|
+
BOOLEAN_VAR: true,
|
|
44
|
+
OPTIONAL_VAR: 'Example'
|
|
45
|
+
});
|
|
36
46
|
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('dotenv/config');
|
|
4
|
+
var value = require('@sinclair/typebox/value');
|
|
5
|
+
var Os = require('os');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var Os__default = /*#__PURE__*/_interopDefault(Os);
|
|
10
|
+
|
|
11
|
+
// src/index.ts
|
|
12
|
+
var envious = (schema, defaultValues) => {
|
|
13
|
+
const parsed = value.Value.Convert(schema, process.env);
|
|
14
|
+
const errors = [...value.Value.Errors(schema, parsed)];
|
|
15
|
+
if (errors.length) {
|
|
16
|
+
const computedErrorMessages = {};
|
|
17
|
+
errors.forEach(({ path, message }) => {
|
|
18
|
+
const envVarName = path.replace(/^\//, "");
|
|
19
|
+
if (!computedErrorMessages[envVarName]) {
|
|
20
|
+
computedErrorMessages[envVarName] = [];
|
|
21
|
+
}
|
|
22
|
+
computedErrorMessages[envVarName].push(message);
|
|
23
|
+
});
|
|
24
|
+
const errorTextParts = [
|
|
25
|
+
"Invalid environment variables",
|
|
26
|
+
...Object.entries(computedErrorMessages).map(
|
|
27
|
+
([varName, messages]) => ` ${varName} : ${messages.join(", ")}`
|
|
28
|
+
)
|
|
29
|
+
];
|
|
30
|
+
throw new Error(errorTextParts.join(Os__default.default.EOL));
|
|
31
|
+
}
|
|
32
|
+
const env = value.Value.Cast(
|
|
33
|
+
{
|
|
34
|
+
...schema,
|
|
35
|
+
additionalProperties: false
|
|
36
|
+
},
|
|
37
|
+
parsed
|
|
38
|
+
);
|
|
39
|
+
return {
|
|
40
|
+
...defaultValues,
|
|
41
|
+
...env
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
exports.envious = envious;
|
|
46
|
+
//# sourceMappingURL=out.js.map
|
|
47
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO;AAEP,SAAS,aAAa;AACtB,OAAO,QAAQ;AAER,IAAM,UAAU,CACnB,QACA,kBACY;AACZ,QAAM,SAAS,MAAM,QAAQ,QAAQ,QAAQ,GAAG;AAChD,QAAM,SAAS,CAAC,GAAG,MAAM,OAAO,QAAQ,MAAM,CAAC;AAC/C,MAAI,OAAO,QAAQ;AACf,UAAM,wBAAkD,CAAC;AACzD,WAAO,QAAQ,CAAC,EAAE,MAAM,QAAQ,MAAM;AAClC,YAAM,aAAa,KAAK,QAAQ,OAAO,EAAE;AACzC,UAAI,CAAC,sBAAsB,UAAU,GAAG;AACpC,8BAAsB,UAAU,IAAI,CAAC;AAAA,MACzC;AACA,4BAAsB,UAAU,EAAE,KAAK,OAAO;AAAA,IAClD,CAAC;AACD,UAAM,iBAA2B;AAAA,MAC7B;AAAA,MACA,GAAG,OAAO,QAAQ,qBAAqB,EAAE;AAAA,QACrC,CAAC,CAAC,SAAS,QAAQ,MAAM,KAAK,OAAO,MAAM,SAAS,KAAK,IAAI,CAAC;AAAA,MAClE;AAAA,IACJ;AACA,UAAM,IAAI,MAAM,eAAe,KAAK,GAAG,GAAG,CAAC;AAAA,EAC/C;AACA,QAAM,MAAM,MAAM;AAAA,IACd;AAAA,MACI,GAAG;AAAA,MACH,sBAAsB;AAAA,IAC1B;AAAA,IACA;AAAA,EACJ;AACA,SAAO;AAAA,IACH,GAAG;AAAA,IACH,GAAG;AAAA,EACP;AACJ","sourcesContent":["import 'dotenv/config';\nimport { type TObject, type Static } from '@sinclair/typebox';\nimport { Value } from '@sinclair/typebox/value';\nimport Os from 'os';\n\nexport const envious = <T extends TObject>(\n schema: T,\n defaultValues?: Static<T>\n): Static<T> => {\n const parsed = Value.Convert(schema, process.env);\n const errors = [...Value.Errors(schema, parsed)];\n if (errors.length) {\n const computedErrorMessages: Record<string, string[]> = {};\n errors.forEach(({ path, message }) => {\n const envVarName = path.replace(/^\\//, '');\n if (!computedErrorMessages[envVarName]) {\n computedErrorMessages[envVarName] = [];\n }\n computedErrorMessages[envVarName].push(message);\n });\n const errorTextParts: string[] = [\n 'Invalid environment variables',\n ...Object.entries(computedErrorMessages).map(\n ([varName, messages]) => ` ${varName} : ${messages.join(', ')}`\n )\n ];\n throw new Error(errorTextParts.join(Os.EOL));\n }\n const env = Value.Cast(\n {\n ...schema,\n additionalProperties: false\n },\n parsed\n );\n return {\n ...defaultValues,\n ...env\n };\n};\n"]}
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import * as _sinclair_typebox from '@sinclair/typebox';
|
|
2
|
+
import { TObject, Static } from '@sinclair/typebox';
|
|
3
|
+
|
|
4
|
+
declare const envious: <T extends TObject<_sinclair_typebox.TProperties>>(schema: T, defaultValues?: Static<T>) => Static<T>;
|
|
5
|
+
|
|
6
|
+
export { envious };
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,41 @@
|
|
|
1
1
|
import 'dotenv/config';
|
|
2
|
-
import Os from 'os';
|
|
3
2
|
import { Value } from '@sinclair/typebox/value';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
3
|
+
import Os from 'os';
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
var envious = (schema, defaultValues) => {
|
|
7
|
+
const parsed = Value.Convert(schema, process.env);
|
|
8
|
+
const errors = [...Value.Errors(schema, parsed)];
|
|
9
|
+
if (errors.length) {
|
|
10
|
+
const computedErrorMessages = {};
|
|
11
|
+
errors.forEach(({ path, message }) => {
|
|
12
|
+
const envVarName = path.replace(/^\//, "");
|
|
13
|
+
if (!computedErrorMessages[envVarName]) {
|
|
14
|
+
computedErrorMessages[envVarName] = [];
|
|
15
|
+
}
|
|
16
|
+
computedErrorMessages[envVarName].push(message);
|
|
17
|
+
});
|
|
18
|
+
const errorTextParts = [
|
|
19
|
+
"Invalid environment variables",
|
|
20
|
+
...Object.entries(computedErrorMessages).map(
|
|
21
|
+
([varName, messages]) => ` ${varName} : ${messages.join(", ")}`
|
|
22
|
+
)
|
|
23
|
+
];
|
|
24
|
+
throw new Error(errorTextParts.join(Os.EOL));
|
|
25
|
+
}
|
|
26
|
+
const env = Value.Cast(
|
|
27
|
+
{
|
|
28
|
+
...schema,
|
|
29
|
+
additionalProperties: false
|
|
30
|
+
},
|
|
31
|
+
parsed
|
|
32
|
+
);
|
|
33
|
+
return {
|
|
34
|
+
...defaultValues,
|
|
35
|
+
...env
|
|
36
|
+
};
|
|
26
37
|
};
|
|
38
|
+
|
|
39
|
+
export { envious };
|
|
40
|
+
//# sourceMappingURL=out.js.map
|
|
27
41
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO;AAEP,SAAS,aAAa;AACtB,OAAO,QAAQ;AAER,IAAM,UAAU,CACnB,QACA,kBACY;AACZ,QAAM,SAAS,MAAM,QAAQ,QAAQ,QAAQ,GAAG;AAChD,QAAM,SAAS,CAAC,GAAG,MAAM,OAAO,QAAQ,MAAM,CAAC;AAC/C,MAAI,OAAO,QAAQ;AACf,UAAM,wBAAkD,CAAC;AACzD,WAAO,QAAQ,CAAC,EAAE,MAAM,QAAQ,MAAM;AAClC,YAAM,aAAa,KAAK,QAAQ,OAAO,EAAE;AACzC,UAAI,CAAC,sBAAsB,UAAU,GAAG;AACpC,8BAAsB,UAAU,IAAI,CAAC;AAAA,MACzC;AACA,4BAAsB,UAAU,EAAE,KAAK,OAAO;AAAA,IAClD,CAAC;AACD,UAAM,iBAA2B;AAAA,MAC7B;AAAA,MACA,GAAG,OAAO,QAAQ,qBAAqB,EAAE;AAAA,QACrC,CAAC,CAAC,SAAS,QAAQ,MAAM,KAAK,OAAO,MAAM,SAAS,KAAK,IAAI,CAAC;AAAA,MAClE;AAAA,IACJ;AACA,UAAM,IAAI,MAAM,eAAe,KAAK,GAAG,GAAG,CAAC;AAAA,EAC/C;AACA,QAAM,MAAM,MAAM;AAAA,IACd;AAAA,MACI,GAAG;AAAA,MACH,sBAAsB;AAAA,IAC1B;AAAA,IACA;AAAA,EACJ;AACA,SAAO;AAAA,IACH,GAAG;AAAA,IACH,GAAG;AAAA,EACP;AACJ","sourcesContent":["import 'dotenv/config';\nimport { type TObject, type Static } from '@sinclair/typebox';\nimport { Value } from '@sinclair/typebox/value';\nimport Os from 'os';\n\nexport const envious = <T extends TObject>(\n schema: T,\n defaultValues?: Static<T>\n): Static<T> => {\n const parsed = Value.Convert(schema, process.env);\n const errors = [...Value.Errors(schema, parsed)];\n if (errors.length) {\n const computedErrorMessages: Record<string, string[]> = {};\n errors.forEach(({ path, message }) => {\n const envVarName = path.replace(/^\\//, '');\n if (!computedErrorMessages[envVarName]) {\n computedErrorMessages[envVarName] = [];\n }\n computedErrorMessages[envVarName].push(message);\n });\n const errorTextParts: string[] = [\n 'Invalid environment variables',\n ...Object.entries(computedErrorMessages).map(\n ([varName, messages]) => ` ${varName} : ${messages.join(', ')}`\n )\n ];\n throw new Error(errorTextParts.join(Os.EOL));\n }\n const env = Value.Cast(\n {\n ...schema,\n additionalProperties: false\n },\n parsed\n );\n return {\n ...defaultValues,\n ...env\n };\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pitininja/envious",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta.1",
|
|
4
4
|
"license": "AGPL-3.0-or-later",
|
|
5
5
|
"homepage": "https://github.com/pitininja/envious",
|
|
6
6
|
"repository": {
|
|
@@ -8,14 +8,27 @@
|
|
|
8
8
|
"url": "https://github.com/pitininja/envious"
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
|
+
"files": [
|
|
12
|
+
"./dist",
|
|
13
|
+
"./LICENSE",
|
|
14
|
+
"./package.json",
|
|
15
|
+
"./package-lock.json",
|
|
16
|
+
"./README.md"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
"./package.json": "./package.json",
|
|
20
|
+
".": {
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"require": "./dist/index.cjs",
|
|
23
|
+
"types": "./dist/index.d.ts"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
11
26
|
"scripts": {
|
|
12
27
|
"prepare": "[ -d '.husky' ] && husky || true",
|
|
13
|
-
"build": "
|
|
28
|
+
"build": "tsup",
|
|
14
29
|
"lint": "npx eslint . --ext=.js,.ts --max-warnings=0 && npx prettier --check . && npx tsc --noEmit",
|
|
15
30
|
"format": "npx eslint . --fix --ext=.js,.ts --max-warnings=0 && npx prettier --write . && npx tsc --noEmit"
|
|
16
31
|
},
|
|
17
|
-
"types": "./dist/index.d.ts",
|
|
18
|
-
"main": "./dist/index.js",
|
|
19
32
|
"dependencies": {
|
|
20
33
|
"@sinclair/typebox": "^0.32.20",
|
|
21
34
|
"dotenv": "^16.4.5"
|
|
@@ -32,6 +45,7 @@
|
|
|
32
45
|
"eslint-plugin-import": "^2.29.1",
|
|
33
46
|
"husky": "^9.0.11",
|
|
34
47
|
"prettier": "^3.2.5",
|
|
48
|
+
"tsup": "^8.0.2",
|
|
35
49
|
"typescript": "^5.4.4"
|
|
36
50
|
}
|
|
37
51
|
}
|
package/.eslintignore
DELETED
package/.eslintrc
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"root": true,
|
|
3
|
-
"env": {
|
|
4
|
-
"node": true
|
|
5
|
-
},
|
|
6
|
-
"parser": "@typescript-eslint/parser",
|
|
7
|
-
"parserOptions": {
|
|
8
|
-
"project": "tsconfig.json"
|
|
9
|
-
},
|
|
10
|
-
"extends": ["airbnb-base", "airbnb-typescript/base", "prettier"],
|
|
11
|
-
"rules": {
|
|
12
|
-
"@typescript-eslint/lines-between-class-members": [
|
|
13
|
-
"error",
|
|
14
|
-
"always",
|
|
15
|
-
{
|
|
16
|
-
"exceptAfterSingleLine": true
|
|
17
|
-
}
|
|
18
|
-
],
|
|
19
|
-
"import/prefer-default-export": "off",
|
|
20
|
-
"lines-between-class-members": "off",
|
|
21
|
-
"no-param-reassign": [
|
|
22
|
-
"error",
|
|
23
|
-
{
|
|
24
|
-
"props": false
|
|
25
|
-
}
|
|
26
|
-
],
|
|
27
|
-
"radix": "off"
|
|
28
|
-
}
|
|
29
|
-
}
|
package/.husky/pre-commit
DELETED
package/.prettierignore
DELETED
package/.prettierrc
DELETED
package/src/index.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import 'dotenv/config';
|
|
2
|
-
import Os from 'os';
|
|
3
|
-
import { type Static, type TSchema } from '@sinclair/typebox';
|
|
4
|
-
import { Value } from '@sinclair/typebox/value';
|
|
5
|
-
|
|
6
|
-
export const envious = <T extends TSchema>(schema: T): Static<T> => {
|
|
7
|
-
const parsed = Value.Convert(schema, process.env);
|
|
8
|
-
const errors = [...Value.Errors(schema, parsed)];
|
|
9
|
-
if (errors.length) {
|
|
10
|
-
const computedErrorMessages: Record<string, string[]> = {};
|
|
11
|
-
errors.forEach(({ path, message }) => {
|
|
12
|
-
const envVarName = path.replace(/^\//, '');
|
|
13
|
-
if (!computedErrorMessages[envVarName]) {
|
|
14
|
-
computedErrorMessages[envVarName] = [];
|
|
15
|
-
}
|
|
16
|
-
computedErrorMessages[envVarName].push(message);
|
|
17
|
-
});
|
|
18
|
-
const errorTextParts: string[] = [
|
|
19
|
-
'Invalid environment variables',
|
|
20
|
-
...Object.entries(computedErrorMessages).map(
|
|
21
|
-
([varName, messages]) => ` ${varName} : ${messages.join(', ')}`
|
|
22
|
-
)
|
|
23
|
-
];
|
|
24
|
-
throw new Error(errorTextParts.join(Os.EOL));
|
|
25
|
-
}
|
|
26
|
-
return Value.Cast(
|
|
27
|
-
{
|
|
28
|
-
...schema,
|
|
29
|
-
additionalProperties: false
|
|
30
|
-
},
|
|
31
|
-
parsed
|
|
32
|
-
);
|
|
33
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "@tsconfig/recommended/tsconfig.json",
|
|
3
|
-
"ts-node": {
|
|
4
|
-
"transpileOnly": true,
|
|
5
|
-
"files": true
|
|
6
|
-
},
|
|
7
|
-
"compilerOptions": {
|
|
8
|
-
"outDir": "dist",
|
|
9
|
-
"target": "ESNext",
|
|
10
|
-
"module": "NodeNext",
|
|
11
|
-
"moduleResolution": "NodeNext",
|
|
12
|
-
"declaration": true,
|
|
13
|
-
"sourceMap": true,
|
|
14
|
-
"strict": true,
|
|
15
|
-
"esModuleInterop": true
|
|
16
|
-
},
|
|
17
|
-
"include": ["src"],
|
|
18
|
-
"exclude": ["node_modules"]
|
|
19
|
-
}
|