env-verifier
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,21 +0,15 @@ | ||
export interface MappedConfig { | ||
[key: string]: any | string | undefined | MappedConfig | Secret; | ||
export declare type MappedConfig<T> = { | ||
[P in keyof T]: T[P] extends SecretKey ? Secret : T[P] extends TransformTuple<infer U> ? U | undefined : T[P] extends InsertValue<infer U> ? U : T[P] extends string ? string | undefined : MappedConfig<T[P]>; | ||
}; | ||
export declare type TransformTuple<T> = [string, (_: string) => T]; | ||
export declare type ConfigWithEnvKeys<T> = { | ||
[P in keyof T]: T[P] extends InsertValue<infer U> ? InsertValue<U> : T[P] extends SecretKey ? SecretKey : T[P] extends string ? string : T[P] extends TransformTuple<infer U> ? TransformTuple<U> : T[P] extends ConfigWithEnvKeys<T[P]> ? ConfigWithEnvKeys<T[P]> : never; | ||
}; | ||
export declare type VerifiedConfig<T> = { | ||
[P in keyof T]: T[P] extends SecretKey ? Secret : T[P] extends TransformTuple<infer U> ? U : T[P] extends InsertValue<infer U> ? U : T[P] extends string ? string : VerifiedConfig<T[P]>; | ||
}; | ||
declare class InsertValue<T> { | ||
value: T; | ||
constructor(value: T); | ||
} | ||
export interface TransformFn { | ||
(envValue: string): any; | ||
} | ||
export declare type TransformTuple = [string, TransformFn]; | ||
export interface ConfigWithEnvKeys { | ||
[key: string]: string | InsertValue | TransformTuple | SecretKey | ConfigWithEnvKeys; | ||
} | ||
export interface NotASecretObject { | ||
[key: string]: string | InsertValue | TransformTuple | ConfigWithEnvKeys; | ||
} | ||
export interface VerifiedConfig { | ||
[key: string]: any | string | VerifiedConfig; | ||
} | ||
declare class InsertValue { | ||
value: any; | ||
constructor(value: any); | ||
} | ||
declare class SecretKey { | ||
@@ -32,13 +26,13 @@ secret: string; | ||
} | ||
export declare function verify(config: ConfigWithEnvKeys, env?: { | ||
export declare function verify<T>(config: ConfigWithEnvKeys<T>, env?: { | ||
[key: string]: string | undefined; | ||
}): { | ||
config: MappedConfig; | ||
config: MappedConfig<T>; | ||
errors: string[]; | ||
}; | ||
export declare function strictVerify(config: ConfigWithEnvKeys, env?: { | ||
export declare function strictVerify<T>(config: ConfigWithEnvKeys<T>, env?: { | ||
[key: string]: string | undefined; | ||
}): VerifiedConfig; | ||
export declare function insert(value: any): InsertValue; | ||
}): VerifiedConfig<T>; | ||
export declare function insert<T>(value: any): InsertValue<T>; | ||
export declare function secret(envKey: string): SecretKey; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.secret = exports.insert = exports.strictVerify = exports.verify = exports.Secret = void 0; | ||
class InsertValue { | ||
@@ -14,8 +15,8 @@ constructor(value) { | ||
class Secret { | ||
constructor(secret) { | ||
this.reveal = () => secret; | ||
} | ||
toJSON() { | ||
return '[secret]'; | ||
} | ||
constructor(secret) { | ||
this.reveal = () => secret; | ||
} | ||
} | ||
@@ -42,3 +43,3 @@ exports.Secret = Secret; | ||
const value = config[key]; | ||
const subPath = path.length === 0 ? key : `${path}.${key}`; | ||
const subPath = path.length === 0 ? key + '' : `${path}.${key}`; | ||
const getEnvValueOrError = getEnvValueOrErrorCurried(env, subPath); | ||
@@ -48,6 +49,12 @@ if (value instanceof SecretKey) { | ||
const [secretValue, errors] = getEnvValueOrError(secretKey); | ||
return [{ [key]: getSecretObject(secretValue) }, errors]; | ||
return [ | ||
{ [key]: getSecretObject(secretValue) }, | ||
errors | ||
]; | ||
} | ||
if (value instanceof InsertValue) { | ||
return [{ [key]: value.value }, []]; | ||
return [ | ||
{ [key]: value.value }, | ||
[] | ||
]; | ||
} | ||
@@ -58,7 +65,13 @@ if (Array.isArray(value)) { | ||
const transforedValue = envValue && transformFn(envValue); | ||
return [{ [key]: transforedValue }, errors]; | ||
return [ | ||
{ [key]: transforedValue }, | ||
errors | ||
]; | ||
} | ||
if (typeof value === 'string') { | ||
const [envValue, errors] = getEnvValueOrError(value); | ||
return [{ [key]: envValue }, errors]; | ||
return [ | ||
{ [key]: envValue }, | ||
errors | ||
]; | ||
} | ||
@@ -70,7 +83,10 @@ const { errors, config: subConfig } = recursiveVerify({ | ||
}); | ||
return [{ [key]: subConfig }, errors]; | ||
return [ | ||
{ [key]: subConfig }, | ||
errors | ||
]; | ||
}; | ||
const reduceConf = (acc, [config, errors]) => { | ||
return { | ||
config: Object.assign({}, acc.config, config), | ||
config: Object.assign(Object.assign({}, acc.config), config), | ||
errors: acc.errors.concat(errors) | ||
@@ -77,0 +93,0 @@ }; |
{ | ||
"name": "env-verifier", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "\"Make sure you have all your env variables!\"", | ||
@@ -17,2 +17,5 @@ "main": "dist/index.js", | ||
], | ||
"files": [ | ||
"dist" | ||
], | ||
"author": "Snugbear", | ||
@@ -22,9 +25,9 @@ "license": "MIT", | ||
"@types/node": "^12.7.1", | ||
"jest": "^24.8.0", | ||
"onchange": "^6.0.0", | ||
"@types/jest": "^24.0.17", | ||
"prettier": "1.18.2", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.5.3" | ||
"jest": "^26.4.0", | ||
"onchange": "^7.0.2", | ||
"@types/jest": "^26.0.10", | ||
"prettier": "2.0.5", | ||
"ts-jest": "^26.2.0", | ||
"typescript": "^3.9.7" | ||
} | ||
} |
@@ -68,3 +68,2 @@ # env-verifier | ||
- [Function Signatures](#functionSignatures) | ||
- [Arbitrary Value Insertion](#arbitraryValueInsertion) | ||
@@ -74,44 +73,4 @@ - [Secret Insertion](#secretInsertion) | ||
- [Variable Transformation (TransformTuple)](#variableTransformation) | ||
- [Dynamic Typings](#dynamicTypings) | ||
#### <a name="functionSignatures"><a/> Function signatures | ||
```typescript | ||
interface TransformFn { | ||
(envValue: string): any | ||
} | ||
//see below | ||
// [envKeyName, TransformFn] | ||
type TransformTuple = [string, TransformFn] | ||
interface ConfigWithEnvKeys { | ||
[key: string]: string | InsertValue | SecretKey | TransformTuple | ConfigWithEnvKeys | ||
} | ||
interface MappedConfig { | ||
[key: string]: any | string | undefined | Secret | Config | ||
} | ||
interface VerifiedConfig { | ||
[key: string]: any | string | Secret | VerifiedConfig | ||
} | ||
interface Env { | ||
[key: string]: string | undefined | ||
} | ||
class Secret { | ||
reveal(): string | ||
} | ||
function secret(envKey: string): SecretKey | ||
function insert(value: any): InsertValue | ||
function verify(config: ConfigWithEnvKeys, env: Env = process.env): { config: MappedConfig, errors: string[] } | ||
function strictVerify(config: ConfigWithEnvKeys, env: Env = process.env): VerifiedConfig | ||
``` | ||
#### <a name="arbitraryValueInsertion"><a/> Arbitrary Value Insertion | ||
@@ -227,2 +186,56 @@ | ||
#### <a name="dynamicTypings"><a/> Dynamic Typings | ||
`env-verifier` tries to give typescript typings for the config object that it returns, but needs a little help to get the correct types | ||
If you are using TypeScript, you can do the following: | ||
```typescript | ||
const config: { | ||
a: 'A', | ||
b: insert([1, 2]) | ||
c: { | ||
d: ['A', (envValue) => ([envValue])] | ||
} | ||
} | ||
const verifiedConfig = strictVerify(config) | ||
// typings: | ||
// typeof verifiedConfig = { | ||
// a: VerifiedConfig<unknown> | ||
// b: VerifiedConfig<unknown> | ||
// c: VerifiedConfig<unknown> | ||
// } | ||
// add typeof config object | ||
const verifiedConfig = strictVerify<typeof config>(config) | ||
// better typings: | ||
// typeof verifiedConfig = { | ||
// a: string, | ||
// b: number[], | ||
// c: { | ||
// d: (string | (envVerify: any) => any) | ||
// } | ||
// } | ||
// cast TransformTuple types correctly | ||
const config = { | ||
a: 'A', | ||
b: insert([1, 2]) | ||
c: { | ||
d: ['A', (envValue) => ([envValue])] as TransformTuple<string> | ||
} | ||
} | ||
const verifiedConfig = strictVerify<typeof config>(config) | ||
// best typings: | ||
// typeof verifiedConfig = { | ||
// a: string, | ||
// b: number[], | ||
// c: { | ||
// d: string | ||
// } | ||
// } | ||
``` | ||
### Prerequisites | ||
@@ -229,0 +242,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
283
0
23963
6
156