Skip to content

Commit

Permalink
Fix typescript typings again (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeyBurkman authored and evanshortiss committed Dec 20, 2016
1 parent 79c110b commit 262c100
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 9 deletions.
135 changes: 128 additions & 7 deletions env-var.d.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,154 @@

export interface IPresentVariable {
interface IPresentVariable {
/**
* Attempt to parse the variable to a float. Throws an exception if parsing fails.
*/
asFloat: () => number;

/**
* Attempt to parse the variable to an integer. Throws an exception if parsing fails.
* This is a strict check, meaning that if the process.env value is 1.2, an exception will be raised rather than rounding up/down.
*/
asInt: () => number;

/**
* Performs the same task as asInt(), but also verifies that the number is positive (greater than or equal to zero).
*/
asPositiveInt: () => number;

/**
* Performs the same task as asInt(), but also verifies that the number is negative (less than zero).
*/
asNegativeInt: () => number;

/**
* Return the variable value as a String. Throws an exception if value is not a String.
* It's highly unlikely that a variable will not be a String since all process.env entries you set in bash are Strings by default.
*/
asString: () => string;

/**
* Attempt to parse the variable to a JSON Object or Array. Throws an exception if parsing fails.
*/
asJson: () => Object|Array<any>;

/**
* The same as asJson but checks that the data is a JSON Array, e.g [1,2].
*/
asJsonArray: () => Array<any>;

/**
* The same as asJson but checks that the data is a JSON Object, e.g {a: 1}.
*/
asJsonObject: () => Object;

/**
* Reads an environment variable as a string, then splits it on each occurence of the specified delimiter.
* By default a comma is used as the delimiter. For example a var set to "1,2,3" would become ['1', '2', '3'].
*/
asArray: (delimiter?: string) => Array<string>;

/**
* Attempt to parse the variable to a Boolean. Throws an exception if parsing fails.
* The var must be set to either "true", "false" (upper or lowercase), 0 or 1 to succeed.
*/
asBool: () => boolean;

/**
* Attempt to parse the variable to a Boolean. Throws an exception if parsing fails.
* The var must be set to either "true" or "false" (upper or lowercase) to succeed.
*/
asStrictBool: () => boolean;
}

export interface IOptionalVariable {
interface IOptionalVariable {
/**
* Ensure the variable is set on process.env, if not an exception will be thrown.
*/
required: () => IPresentVariable;

/**
* Attempt to parse the variable to a float. Throws an exception if parsing fails.
*/
asFloat: () => number|undefined;

/**
* Attempt to parse the variable to an integer. Throws an exception if parsing fails.
* This is a strict check, meaning that if the process.env value is 1.2, an exception will be raised rather than rounding up/down.
*/
asInt: () => number|undefined;

/**
* Performs the same task as asInt(), but also verifies that the number is positive (greater than or equal to zero).
*/
asPositiveInt: () => number|undefined;

/**
* Performs the same task as asInt(), but also verifies that the number is negative (less than zero).
*/
asNegativeInt: () => number|undefined;

/**
* Return the variable value as a String. Throws an exception if value is not a String.
* It's highly unlikely that a variable will not be a String since all process.env entries you set in bash are Strings by default.
*/
asString: () => string|undefined;

/**
* Attempt to parse the variable to a JSON Object or Array. Throws an exception if parsing fails.
*/
asJson: () => Object|Array<any>|undefined;

/**
* The same as asJson but checks that the data is a JSON Array, e.g [1,2].
*/
asJsonArray: () => Array<any>|undefined;

/**
* The same as asJson but checks that the data is a JSON Object, e.g {a: 1}.
*/
asJsonObject: () => Object|undefined;

/**
* Reads an environment variable as a string, then splits it on each occurence of the specified delimiter.
* By default a comma is used as the delimiter. For example a var set to "1,2,3" would become ['1', '2', '3'].
*/
asArray: (delimiter?: string) => Array<string>|undefined;

/**
* Attempt to parse the variable to a Boolean. Throws an exception if parsing fails.
* The var must be set to either "true", "false" (upper or lowercase), 0 or 1 to succeed.
*/
asBool: () => boolean|undefined;

/**
* Attempt to parse the variable to a Boolean. Throws an exception if parsing fails.
* The var must be set to either "true" or "false" (upper or lowercase) to succeed.
*/
asStrictBool: () => boolean|undefined;
}

export interface IEnv {
(): Object,
interface IEnv {
/**
* Returns an object containing all current environment variables
*/
(): {[varName: string]: string},

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

/**
* Gets an environment variable, using the default value if it is not already set
*/
(varName: string, defaultValue: string): IPresentVariable;
mock(mockVars: Object): IEnv;

/**
* Returns a mock env-var instance, where the given object is used for the environment variable mapping. Use this when writing unit tests.
*/
mock(mockVars: {[varName: string]: string}): IEnv;
}

export const env: IEnv;
export default env;
declare const env: IEnv;
export = env;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "env-var",
"version": "2.4.1",
"description": "solution for loading and sanatizing environment variables in node.js",
"version": "2.4.2",
"description": "Solution for loading and sanatizing environment variables in node.js",
"main": "lib/index.js",
"scripts": {
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
Expand Down

0 comments on commit 262c100

Please sign in to comment.