@pitininja/envious 1.1.0 → 3.0.0-beta
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 +75 -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 +38 -25
- package/dist/index.js.map +1 -1
- package/package.json +16 -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,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
envious: () => envious
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var import_config = require("dotenv/config");
|
|
37
|
+
var import_value = require("@sinclair/typebox/value");
|
|
38
|
+
var import_os = __toESM(require("os"), 1);
|
|
39
|
+
var envious = (schema, defaultValues) => {
|
|
40
|
+
const parsed = import_value.Value.Convert(schema, process.env);
|
|
41
|
+
const errors = [...import_value.Value.Errors(schema, parsed)];
|
|
42
|
+
if (errors.length) {
|
|
43
|
+
const computedErrorMessages = {};
|
|
44
|
+
errors.forEach(({ path, message }) => {
|
|
45
|
+
const envVarName = path.replace(/^\//, "");
|
|
46
|
+
if (!computedErrorMessages[envVarName]) {
|
|
47
|
+
computedErrorMessages[envVarName] = [];
|
|
48
|
+
}
|
|
49
|
+
computedErrorMessages[envVarName].push(message);
|
|
50
|
+
});
|
|
51
|
+
const errorTextParts = [
|
|
52
|
+
"Invalid environment variables",
|
|
53
|
+
...Object.entries(computedErrorMessages).map(
|
|
54
|
+
([varName, messages]) => ` ${varName} : ${messages.join(", ")}`
|
|
55
|
+
)
|
|
56
|
+
];
|
|
57
|
+
throw new Error(errorTextParts.join(import_os.default.EOL));
|
|
58
|
+
}
|
|
59
|
+
const env = import_value.Value.Cast(
|
|
60
|
+
{
|
|
61
|
+
...schema,
|
|
62
|
+
additionalProperties: false
|
|
63
|
+
},
|
|
64
|
+
parsed
|
|
65
|
+
);
|
|
66
|
+
return {
|
|
67
|
+
...defaultValues,
|
|
68
|
+
...env
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
envious
|
|
74
|
+
});
|
|
75
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAO;AAEP,mBAAsB;AACtB,gBAAe;AAER,IAAM,UAAU,CACnB,QACA,kBACY;AACZ,QAAM,SAAS,mBAAM,QAAQ,QAAQ,QAAQ,GAAG;AAChD,QAAM,SAAS,CAAC,GAAG,mBAAM,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,UAAAA,QAAG,GAAG,CAAC;AAAA,EAC/C;AACA,QAAM,MAAM,mBAAM;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;","names":["Os"]}
|
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,40 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import { Value } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import "dotenv/config";
|
|
3
|
+
import { Value } from "@sinclair/typebox/value";
|
|
4
|
+
import Os from "os";
|
|
5
|
+
var envious = (schema, defaultValues) => {
|
|
6
|
+
const parsed = Value.Convert(schema, process.env);
|
|
7
|
+
const errors = [...Value.Errors(schema, parsed)];
|
|
8
|
+
if (errors.length) {
|
|
9
|
+
const computedErrorMessages = {};
|
|
10
|
+
errors.forEach(({ path, message }) => {
|
|
11
|
+
const envVarName = path.replace(/^\//, "");
|
|
12
|
+
if (!computedErrorMessages[envVarName]) {
|
|
13
|
+
computedErrorMessages[envVarName] = [];
|
|
14
|
+
}
|
|
15
|
+
computedErrorMessages[envVarName].push(message);
|
|
16
|
+
});
|
|
17
|
+
const errorTextParts = [
|
|
18
|
+
"Invalid environment variables",
|
|
19
|
+
...Object.entries(computedErrorMessages).map(
|
|
20
|
+
([varName, messages]) => ` ${varName} : ${messages.join(", ")}`
|
|
21
|
+
)
|
|
22
|
+
];
|
|
23
|
+
throw new Error(errorTextParts.join(Os.EOL));
|
|
24
|
+
}
|
|
25
|
+
const env = Value.Cast(
|
|
26
|
+
{
|
|
27
|
+
...schema,
|
|
28
|
+
additionalProperties: false
|
|
29
|
+
},
|
|
30
|
+
parsed
|
|
31
|
+
);
|
|
32
|
+
return {
|
|
33
|
+
...defaultValues,
|
|
34
|
+
...env
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export {
|
|
38
|
+
envious
|
|
26
39
|
};
|
|
27
40
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"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"],"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;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pitininja/envious",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta",
|
|
4
4
|
"license": "AGPL-3.0-or-later",
|
|
5
5
|
"homepage": "https://github.com/pitininja/envious",
|
|
6
6
|
"repository": {
|
|
@@ -8,14 +8,25 @@
|
|
|
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
|
+
"default": "./dist/index.js",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"require": "./dist/index.cjs",
|
|
22
|
+
"types": "./dist/index.d.ts"
|
|
23
|
+
},
|
|
11
24
|
"scripts": {
|
|
12
25
|
"prepare": "[ -d '.husky' ] && husky || true",
|
|
13
|
-
"build": "
|
|
26
|
+
"build": "tsup",
|
|
14
27
|
"lint": "npx eslint . --ext=.js,.ts --max-warnings=0 && npx prettier --check . && npx tsc --noEmit",
|
|
15
28
|
"format": "npx eslint . --fix --ext=.js,.ts --max-warnings=0 && npx prettier --write . && npx tsc --noEmit"
|
|
16
29
|
},
|
|
17
|
-
"types": "./dist/index.d.ts",
|
|
18
|
-
"main": "./dist/index.js",
|
|
19
30
|
"dependencies": {
|
|
20
31
|
"@sinclair/typebox": "^0.32.20",
|
|
21
32
|
"dotenv": "^16.4.5"
|
|
@@ -32,6 +43,7 @@
|
|
|
32
43
|
"eslint-plugin-import": "^2.29.1",
|
|
33
44
|
"husky": "^9.0.11",
|
|
34
45
|
"prettier": "^3.2.5",
|
|
46
|
+
"tsup": "^8.0.2",
|
|
35
47
|
"typescript": "^5.4.4"
|
|
36
48
|
}
|
|
37
49
|
}
|
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
|
-
}
|