Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strengthen typings #148

Merged
merged 3 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions env-var.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,24 @@ interface IOptionalVariable<Exs extends Extensions = {}> extends VariableAccesso

export class EnvVarError extends Error {}

interface IEnv<PresentVariable, OptionalVariable> {
interface IEnv<OptionalVariable, Container> {
/**
* Returns an object containing all current environment variables
*/
get (): {[varName: string]: string},
get (): Container,

/**
* Gets an environment variable that is possibly not set to a value
*/
get (varName: string): OptionalVariable;
get (varName: keyof Container): OptionalVariable;

/**
* Returns a new env-var instance, where the given object is used for the environment variable mapping.
* Use this when writing unit tests or in environments outside node.js.
*/
from<T extends Extensions>(values: NodeJS.ProcessEnv, extensions?: T, logger?: LoggerFn): IEnv<
IPresentVariable<T> & ExtenderType<T>,
IOptionalVariable<T> & ExtenderTypeOptional<T>
from<V, T extends Extensions>(values: V, extensions?: T, logger?: LoggerFn): IEnv<
IOptionalVariable<T> & ExtenderTypeOptional<T>,
V
>;

accessors: PublicAccessors
Expand All @@ -299,14 +299,16 @@ type ExtenderTypeOptional<T extends Extensions> = { [P in keyof T]: (...args: Re
export type Extensions = {
[key: string]: ExtensionFn<any>
}

export type LoggerFn = (varname: string, str: string) => void
export type RaiseErrorFn = (error: string) => void
export type ExtensionFn<T> = (value: string, ...args: any[]) => T
export function get(): {[varName: string]: string}
export function get(varName: string): IOptionalVariable;
export function from<T extends Extensions>(values: NodeJS.ProcessEnv, extensions?: T, logger?: LoggerFn): IEnv<
IPresentVariable<T> & ExtenderType<T>,
IOptionalVariable<T> & ExtenderTypeOptional<T>
>;

export const accessors: PublicAccessors
export const EnvVarError: EnvVarError

export function logger (varname: string, str: string): void
export let accessors: PublicAccessors

type IDefaultEnv = IEnv<IOptionalVariable, NodeJS.ProcessEnv>
export const get: IDefaultEnv['get']
export const from: IDefaultEnv['from']
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"mocha-lcov-reporter": "~1.3.0",
"nyc": "~15.1.0",
"standard": "~14.3.4",
"typescript": "~3.8.3"
"typescript": "~3.9.0"
},
"engines": {
"node": ">=10"
Expand Down
2 changes: 2 additions & 0 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('typescript tests', () => {
// env-var instance with no vars
const e = env.from({})

// @ts-expect-error `PATH` is not in container object
expect(e.get('PATH').asString()).to.equal(undefined)
})
})
Expand All @@ -28,6 +29,7 @@ describe('typescript tests', () => {
const e = env.from({})

expect(() => {
// @ts-expect-error `A_MISSING_VARIABLE` is not in container object
e.get('A_MISSING_VARIABLE').required().asString()
}).to.throw('env-var: "A_MISSING_VARIABLE" is a required variable, but it was not set')
})
Expand Down