@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 CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  > Environment variable parsing using Dotenv & Typebox
4
4
 
5
- ## Warning ⚠️
5
+ ## Compatibility
6
6
 
7
- From v2 this library is shipped in ESM. If your project is setup in CJS you must use v1.
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 envSchema = Type.Object({
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(myEnvSchema);
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"]}
@@ -0,0 +1,6 @@
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.d.ts CHANGED
@@ -1,3 +1,6 @@
1
- import 'dotenv/config';
2
- import { type Static, type TSchema } from '@sinclair/typebox';
3
- export declare const envious: <T extends TSchema>(schema: T) => Static<T>;
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
- export const envious = (schema) => {
5
- const parsed = Value.Convert(schema, process.env);
6
- const errors = [...Value.Errors(schema, parsed)];
7
- if (errors.length) {
8
- const computedErrorMessages = {};
9
- errors.forEach(({ path, message }) => {
10
- const envVarName = path.replace(/^\//, '');
11
- if (!computedErrorMessages[envVarName]) {
12
- computedErrorMessages[envVarName] = [];
13
- }
14
- computedErrorMessages[envVarName].push(message);
15
- });
16
- const errorTextParts = [
17
- 'Invalid environment variables',
18
- ...Object.entries(computedErrorMessages).map(([varName, messages]) => ` ${varName} : ${messages.join(', ')}`)
19
- ];
20
- throw new Error(errorTextParts.join(Os.EOL));
21
- }
22
- return Value.Cast({
23
- ...schema,
24
- additionalProperties: false
25
- }, parsed);
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,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAC;AACvB,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAEhD,MAAM,CAAC,MAAM,OAAO,GAAG,CAAoB,MAAS,EAAa,EAAE;IAC/D,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAClD,MAAM,MAAM,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACjD,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;QAChB,MAAM,qBAAqB,GAA6B,EAAE,CAAC;QAC3D,MAAM,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE;YACjC,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YAC3C,IAAI,CAAC,qBAAqB,CAAC,UAAU,CAAC,EAAE,CAAC;gBACrC,qBAAqB,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;YAC3C,CAAC;YACD,qBAAqB,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACpD,CAAC,CAAC,CAAC;QACH,MAAM,cAAc,GAAa;YAC7B,+BAA+B;YAC/B,GAAG,MAAM,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAC,GAAG,CACxC,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,EAAE,EAAE,CAAC,KAAK,OAAO,MAAM,QAAQ,CAAC,IAAI,CAAC,I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Object, 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": "2.0.0",
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": "tsc --build --clean tsconfig.json && tsc --project tsconfig.json",
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
@@ -1,3 +0,0 @@
1
- node_modules
2
- dist
3
- .husky
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
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env sh
2
- . "$(dirname -- "$0")/_/husky.sh"
3
-
4
- npm run format
5
- git add -u
package/.prettierignore DELETED
@@ -1,4 +0,0 @@
1
- node_modules
2
- dist
3
- .husky
4
- README.md
package/.prettierrc DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "trailingComma": "none",
3
- "jsxSingleQuote": false,
4
- "singleQuote": true,
5
- "tabWidth": 4,
6
- "useTabs": false
7
- }
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
- }