Comparing version
@@ -0,0 +0,0 @@ export declare class EnvValidationError extends Error { |
export * from "./errors"; | ||
export * from "./validate"; | ||
export * from "./validators"; |
@@ -1,2 +0,2 @@ | ||
function t(e){return t=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},t(e)}function e(t,n){return e=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},e(t,n)}function n(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}function r(t,o,i){return r=n()?Reflect.construct.bind():function(t,n,r){var o=[null];o.push.apply(o,n);var i=new(Function.bind.apply(t,o));return r&&e(i,r.prototype),i},r.apply(null,arguments)}function o(n){var i="function"==typeof Map?new Map:void 0;return o=function(n){if(null===n||-1===Function.toString.call(n).indexOf("[native code]"))return n;if("function"!=typeof n)throw new TypeError("Super expression must either be null or a function");if(void 0!==i){if(i.has(n))return i.get(n);i.set(n,o)}function o(){return r(n,arguments,t(this).constructor)}return o.prototype=Object.create(n.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),e(o,n)},o(n)}var i=/*#__PURE__*/function(t){var n,r;function o(e){var n,r=e.invalidVariables,i=void 0===r?[]:r,u=e.missingVariables,a=void 0===u?[]:u,c=["Some environment variables cannot be validated"];return i.length>0&&c.push("Invalid variables: "+i.join(", ")),a.length>0&&c.push("Missing variables: "+a.join(", ")),(n=t.call(this,c.join("\n"))||this).invalidVariables=void 0,n.missingVariables=void 0,Object.setPrototypeOf(function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(n),o.prototype),n.name=n.constructor.name,n.invalidVariables=i,n.missingVariables=a,n}return r=t,(n=o).prototype=Object.create(r.prototype),n.prototype.constructor=n,e(n,r),o}(/*#__PURE__*/o(Error));exports.EnvValidationError=i,exports.boolean=function(t){return"true"===t||"false"!==t&&void 0},exports.number=function(t){var e=parseFloat(t);if(!Number.isNaN(e))return e},exports.oneOf=function(){var t=arguments;return function(e){var n=[].slice.call(t).find(function(t){return t===e});if(void 0!==n)return n}},exports.string=function(t){return t},exports.validate=function(t){var e=t.env,n=t.validators,r=t.prefix,o=void 0===r?"":r,u=t.overrides,a=void 0===u?{}:u,c={},f=[],s=[],l=Object.keys(n),p=Object.keys(a).filter(function(t){return-1!==l.indexOf(t)});if(p.forEach(function(t){c[t]=a[t]}),l.forEach(function(t){if(-1===p.indexOf(t)){var r=n[t],i=e[o+t];if(void 0!==i){if(void 0!==r){var u=r(String(i));void 0!==u?c[t]=u:f.push(t)}}else s.push(t)}}),f.length>0||s.length>0)throw new i({invalidVariables:f,missingVariables:s});return c}; | ||
function t(e){return t=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},t(e)}function e(t,n){return e=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},e(t,n)}function n(t,r,o){return n=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}()?Reflect.construct.bind():function(t,n,r){var o=[null];o.push.apply(o,n);var i=new(Function.bind.apply(t,o));return r&&e(i,r.prototype),i},n.apply(null,arguments)}function r(o){var i="function"==typeof Map?new Map:void 0;return r=function(r){if(null===r||-1===Function.toString.call(r).indexOf("[native code]"))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==i){if(i.has(r))return i.get(r);i.set(r,o)}function o(){return n(r,arguments,t(this).constructor)}return o.prototype=Object.create(r.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),e(o,r)},r(o)}var o=/*#__PURE__*/function(t){var n,r;function o(e){var n,r=e.invalidVariables,i=void 0===r?[]:r,u=e.missingVariables,a=void 0===u?[]:u,c=["Some environment variables cannot be validated"];return i.length>0&&c.push("Invalid variables: "+i.join(", ")),a.length>0&&c.push("Missing variables: "+a.join(", ")),(n=t.call(this,c.join("\n"))||this).invalidVariables=void 0,n.missingVariables=void 0,Object.setPrototypeOf(function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(n),o.prototype),n.name=n.constructor.name,n.invalidVariables=i,n.missingVariables=a,n}return r=t,(n=o).prototype=Object.create(r.prototype),n.prototype.constructor=n,e(n,r),o}(/*#__PURE__*/r(Error));exports.EnvValidationError=o,exports.boolean=function(t){return"true"===t||"false"!==t&&void 0},exports.number=function(t){var e=parseFloat(t);if(!Number.isNaN(e))return e},exports.oneOf=function(){var t=arguments;return function(e){var n=[].slice.call(t).find(function(t){return t===e});if(void 0!==n)return n}},exports.string=function(t){return t},exports.validate=function(t){var e=t.env,n=t.validators,r=t.overrides,i=void 0===r?{}:r,u={},a=[],c=[],f=Object.keys(n),s=Object.keys(i).filter(function(t){return-1!==f.indexOf(t)});if(s.forEach(function(t){u[t]=i[t]}),f.forEach(function(t){if(-1===s.indexOf(t)){var r=n[t],o=e[t];if(void 0!==o){if(void 0!==r){var i=r(String(o));void 0!==i?u[t]=i:a.push(t)}}else c.push(t)}}),a.length>0||c.length>0)throw new o({invalidVariables:a,missingVariables:c});return u}; | ||
//# sourceMappingURL=index.js.map |
import { Validator } from "./validators"; | ||
export declare const validate: <Validators extends Record<string, Validator<unknown>>>({ env, validators, prefix, overrides, }: { | ||
export declare const validate: <Validators extends Record<string, Validator<unknown>>>({ env, validators, overrides, }: { | ||
env: Record<string, string | number | boolean | undefined>; | ||
validators: Validators; | ||
prefix?: string | undefined; | ||
overrides?: { [Key in keyof Validators]?: ReturnType<Validators[Key]> | undefined; } | undefined; | ||
}) => Readonly<{ [Key_1 in keyof Validators]: Exclude<ReturnType<Validators[Key_1]>, undefined>; }>; |
@@ -1,2 +0,2 @@ | ||
export declare type Validator<T> = (value: string) => T | undefined; | ||
export type Validator<T> = (value: string) => T | undefined; | ||
export declare const boolean: Validator<boolean>; | ||
@@ -3,0 +3,0 @@ export declare const number: Validator<number>; |
{ | ||
"name": "valienv", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"license": "MIT", | ||
@@ -31,3 +31,3 @@ "description": "A simple environment variables validator for Node.js and web browsers.", | ||
"dev": "microbundle watch", | ||
"format": "prettier '**/*.{js,md,ts,tsx}' --write", | ||
"format": "prettier '**/*' -u -w", | ||
"lint": "eslint --ext ts,tsx __tests__ src", | ||
@@ -48,12 +48,12 @@ "test": "vitest --run", | ||
"devDependencies": { | ||
"@types/node": "^18.0.6", | ||
"@typescript-eslint/eslint-plugin": "^5.30.7", | ||
"@typescript-eslint/parser": "^5.30.7", | ||
"eslint": "^8.20.0", | ||
"microbundle": "^0.15.0", | ||
"prettier": "^2.7.1", | ||
"prettier-plugin-organize-imports": "^3.0.0", | ||
"typescript": "^4.7.4", | ||
"vitest": "^0.18.1" | ||
"@types/node": "^18.15.10", | ||
"@typescript-eslint/eslint-plugin": "^5.56.0", | ||
"@typescript-eslint/parser": "^5.56.0", | ||
"eslint": "^8.36.0", | ||
"microbundle": "^0.15.1", | ||
"prettier": "^2.8.7", | ||
"prettier-plugin-organize-imports": "^3.2.2", | ||
"typescript": "^5.0.2", | ||
"vitest": "^0.29.7" | ||
} | ||
} |
@@ -17,3 +17,3 @@ # ✓ valienv | ||
## 📘 Basic usage | ||
## 📘 Usage | ||
@@ -54,29 +54,2 @@ This library exports a main function: `validate`.<br> | ||
## 📕 Advanced usage | ||
The `validate` function accepts `prefix` and `overrides` options. | ||
#### prefix | ||
Some bundlers only expose prefixed environment variables to your application (ex: [Create React App](https://create-react-app.dev/docs/adding-custom-environment-variables/), [Vite](https://vitejs.dev/guide/env-and-mode.html)).<br> | ||
The `prefix` option is very useful to remove them. | ||
```ts | ||
import { string, validate } from "valienv"; | ||
// with process.env = { | ||
// REACT_APP_CONTACT_EMAIL: "zoontek@github.com", | ||
// } | ||
export const env = validate({ | ||
env: process.env, | ||
prefix: "REACT_APP_", | ||
validators: { | ||
CONTACT_EMAIL: string, | ||
}, | ||
}); | ||
// -> typeof env = Readonly<{ CONTACT_EMAIL: string }> | ||
``` | ||
#### overrides | ||
@@ -110,3 +83,3 @@ | ||
## Custom validators | ||
### Custom validators | ||
@@ -180,5 +153,5 @@ By default, `valienv` only exports 3 validators: `string`, `number` and `boolean`. It also offers `oneOf`, a helper to create validators for union of string literals. | ||
## Optional values | ||
### Optional values | ||
As it's a common pattern to have some optional environment values, you can wrap every validator with a small helper of your choice: | ||
As it's a common pattern to have some optional environment values, you can write a small helper to wrap every validator with: | ||
@@ -188,3 +161,3 @@ ```ts | ||
// Here's an example with a simple TS discriminating union: | ||
// Here's we are using a simple TS discriminating union: | ||
type OptionalEnvValue<T> = { isSet: true; value: T } | { isSet: false }; | ||
@@ -214,3 +187,3 @@ | ||
But you can also wrap them using a library of your choice: | ||
But you can also wrap them using a library of your choice. Here's an example with [`@swan-io/boxed`](https://github.com/swan-io/boxed): | ||
@@ -245,5 +218,5 @@ ```ts | ||
### Why not handling `NODE_ENV` for us? | ||
#### Why not handling `NODE_ENV` for us? | ||
Frontend bundlers generally **statically replace** `process.env.NODE_ENV` values at build time, allowing minifiers like [`terser`](https://github.com/terser/terser) to eliminate dead code from production build. Aliasing `NODE_ENV` would prevent such optimisations.<br /> | ||
But if you are working with Node.js, feel free to use `oneOf` on `NODE_ENV` if you want. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26012
-3.3%46
-2.13%216
-11.11%