Skip to content

Commit

Permalink
fix: issue with extender types (#120)
Browse files Browse the repository at this point in the history
* BREAKING CHANGE: change get() and required() function behaviours

* chore: fix coverage

* Update env-var.d.ts

Co-Authored-By: Phoenix <todofixthis@users.noreply.github.com>

* Update lib/variable.js

Co-Authored-By: Phoenix <todofixthis@users.noreply.github.com>

* address PR feedback

* update README

* add feature list to readme

* more emojis, because why the fuck not

* emoji strategy change

* readme improvements

* rejig things

* remove benefits section

* change tagline

* line break

* line break

* add error message for 5.x get usage. update types

* update readme

* types fix for default()

* fix record and array generics

* chore: bump version and changelog

* fix: issue with extender types

Co-authored-by: Phoenix <todofixthis@users.noreply.github.com>
  • Loading branch information
evanshortiss and todofixthis authored Apr 23, 2020
1 parent 0eda3ba commit 08097bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 6.1.1 (22/04/20)
* Fix TS error with *ExtenderTypeOptional* and *ExtenderType* typings (#119)

## 6.1.0 (20/04/20)
* Fix TS error with *extraAccessor* typings (#114)
* Add support for generic types in *asEnum* (#116)
Expand Down
24 changes: 12 additions & 12 deletions env-var.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@

import { Url } from 'url';

interface IPresentVariable<Extensions = {}> {
interface IPresentVariable<Exs extends Extensions = {}> {
/**
* Converts a bas64 environment variable to ut8
*/
convertFromBase64: () => IPresentVariable<Extensions> & Extensions
convertFromBase64: () => IPresentVariable<Exs> & Exs

/**
* Provide an example value that can be used in error output if the variable
* is not set, or is set to an invalid value
*/
example: (example: string) => IPresentVariable<Extensions> & Extensions
example: (example: string) => IPresentVariable<Exs> & Exs

/**
* Set a default value for this variable. This will be used if a value is not
* set in the process environment
*/
default: (value: string|number|Record<string, any>|Array<any>) => IPresentVariable<Extensions> & Extensions;
default: (value: string|number|Record<string, any>|Array<any>) => IPresentVariable<Exs> & Exs;

/**
* Ensures the variable is set on process.env. If it's not set an exception
* will be thrown. Can pass false to bypass the check.
*/
required: (isRequired?: boolean) => IPresentVariable & ExtenderType<Extensions>;
required: (isRequired?: boolean) => IPresentVariable & ExtenderType<Exs>;

/**
* Converts a number to an integer and verifies it's in port ranges 0-65535
Expand Down Expand Up @@ -120,29 +120,29 @@ interface IPresentVariable<Extensions = {}> {
asEnum: <T extends string>(validValues: T[]) => T;
}

interface IOptionalVariable<Extensions = {}> {
interface IOptionalVariable<Exs extends Extensions = {}> {
/**
* Decodes a base64-encoded environment variable
*/
convertFromBase64: () => IOptionalVariable<Extensions> & Extensions;
convertFromBase64: () => IOptionalVariable<Exs> & Exs;

/**
* Provide an example value that can be used in error output if the variable
* is not set, or is set to an invalid value
*/
example: (value: string) => IOptionalVariable<Extensions> & Extensions;
example: (value: string) => IOptionalVariable<Exs> & Exs;

/**
* Set a default value for this variable. This will be used if a value is not
* set in the process environment
*/
default: (value: string|number|Record<string, any>|Array<any>) => IPresentVariable<Extensions> & Extensions;
default: (value: string|number|Record<string, any>|Array<any>) => IPresentVariable<Exs> & Exs;

/**
* Ensures the variable is set on process.env. If it's not set an exception will be thrown.
* Can pass false to bypass the check
*/
required: (isRequired?: boolean) => IPresentVariable & ExtenderType<Extensions>;
required: (isRequired?: boolean) => IPresentVariable & ExtenderType<Exs>;

/**
* Converts a number to an integer and verifies it's in port ranges 0-65535
Expand Down Expand Up @@ -267,8 +267,8 @@ interface IEnv<PresentVariable, OptionalVariable> {
}

// Used internally only to support extension fns
type ExtenderType<T> = { [P in keyof T]: (...args: any[]) => ReturnType<T[P]> }
type ExtenderTypeOptional<T> = { [P in keyof T]: (...args: any[]) => ReturnType<T[P]>|undefined }
type ExtenderType<T extends Extensions> = { [P in keyof T]: (...args: any[]) => ReturnType<T[P]> }
type ExtenderTypeOptional<T extends Extensions> = { [P in keyof T]: (...args: any[]) => ReturnType<T[P]>|undefined }

export type Extensions = {
[key: string]: ExtensionFn<any>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "env-var",
"version": "6.1.0",
"version": "6.1.1",
"description": "Verification, sanatization, and type coercion for environment variables in Node.js",
"main": "env-var.js",
"typings": "env-var.d.ts",
Expand Down Expand Up @@ -63,7 +63,7 @@
"mocha-lcov-reporter": "~1.3.0",
"nyc": "~15.0.0",
"standard": "~14.3.0",
"typescript": "~3.7.5"
"typescript": "~3.8.3"
},
"engines": {
"node": ">=8"
Expand Down

0 comments on commit 08097bc

Please sign in to comment.