Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,11 @@ | ||
# [1.2.0](https://gitlab.com/tdolsen/getenv.ts/compare/v1.1.0...v1.2.0) (2021-03-23) | ||
### Features | ||
* adds tests for case insensitive boolean check, and fixes typo in .releaserc ([67eda39](https://gitlab.com/tdolsen/getenv.ts/commit/67eda39)) | ||
* lower cases all string values for boolean conversion, supporting values as 'True' and 'TRUE' ([210a8b6](https://gitlab.com/tdolsen/getenv.ts/commit/210a8b6)) | ||
* moves commitlint config to JS file, so ignore rules can be added ([d88392a](https://gitlab.com/tdolsen/getenv.ts/commit/d88392a)) | ||
* updates dependencies ([3219bb4](https://gitlab.com/tdolsen/getenv.ts/commit/3219bb4)) | ||
# [1.1.0](https://gitlab.com/tdolsen/getenv.ts/compare/v1.0.0...v1.1.0) (2018-09-21) | ||
@@ -2,0 +12,0 @@ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toString = exports.toFloat = exports.toBoolean = void 0; | ||
function toBoolean(value) { | ||
if (typeof value === "string") | ||
value = value.toLowerCase(); | ||
return typeof value === "boolean" | ||
@@ -5,0 +8,0 @@ ? value |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.invalidTypeError = void 0; | ||
function invalidTypeError(env, type) { | ||
@@ -4,0 +5,0 @@ return new TypeError("Environment variable \"" + env + "\" is not of type " + type + "."); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getEnv = exports.getEnvString = exports.getEnvInteger = exports.getEnvFloat = exports.getEnvBoolean = exports.getEnvRaw = void 0; | ||
var conversions_1 = require("./conversions"); | ||
@@ -4,0 +5,0 @@ var error_handling_1 = require("./error-handling"); |
@@ -0,3 +1,5 @@ | ||
export * from "./types"; | ||
export * from "./getenv"; | ||
import { GetEnvInterface } from "./types"; | ||
declare const getenv: GetEnvInterface; | ||
export default getenv; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./types"), exports); | ||
__exportStar(require("./getenv"), exports); | ||
var getenv_1 = require("./getenv"); | ||
@@ -4,0 +16,0 @@ // Remap to exported name. |
@@ -5,3 +5,3 @@ module.exports = { | ||
"ts-jest": { | ||
"tsConfigFile": "tsconfig.json" | ||
tsconfig: "tsconfig.json" | ||
} | ||
@@ -8,0 +8,0 @@ }, |
{ | ||
"name": "getenv.ts", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Get and typecast environment variables with TypeScript", | ||
@@ -15,3 +15,3 @@ "author": "Torkild Dyvik Olsen <torkild@tdolsen.net>", | ||
"test": "jest", | ||
"lint": "prettier --write", | ||
"format": "prettier --write", | ||
"compile": "tsc", | ||
@@ -22,3 +22,4 @@ "build": "yarn clean && yarn compile", | ||
"prerelease": "pkg-ok", | ||
"release": "semantic-release" | ||
"release": "semantic-release", | ||
"prepare": "husky install" | ||
}, | ||
@@ -31,22 +32,22 @@ "config": { | ||
"devDependencies": { | ||
"@commitlint/cli": "^7.1.2", | ||
"@commitlint/config-conventional": "^7.1.2", | ||
"@commitlint/prompt": "^7.1.2", | ||
"@semantic-release/changelog": "^3.0.0", | ||
"@semantic-release/git": "^7.0.4", | ||
"@semantic-release/gitlab": "^3.0.2", | ||
"@semantic-release/npm": "^5.0.4", | ||
"@types/jest": "^23.3.2", | ||
"@types/node": "^10.9.4", | ||
"commitizen": "^2.10.1", | ||
"del-cli": "^1.1.0", | ||
"husky": "^1.0.0-rc.14", | ||
"jest": "^23.6.0", | ||
"lint-staged": "^7.2.2", | ||
"pkg-ok": "^2.2.0", | ||
"prettier": "^1.14.2", | ||
"semantic-release": "^15.9.15", | ||
"ts-jest": "^23.1.4", | ||
"typescript": "^3.0.3" | ||
"@commitlint/cli": "^12.0.1", | ||
"@commitlint/config-conventional": "^12.0.1", | ||
"@commitlint/prompt": "^12.0.1", | ||
"@semantic-release/changelog": "^5.0.1", | ||
"@semantic-release/git": "^9.0.0", | ||
"@semantic-release/gitlab": "^6.0.9", | ||
"@semantic-release/npm": "^7.0.10", | ||
"@types/jest": "^26.0.21", | ||
"@types/node": "^14.14.35", | ||
"commitizen": "^4.2.3", | ||
"del-cli": "^3.0.1", | ||
"husky": "^5.2.0", | ||
"jest": "^26.6.3", | ||
"lint-staged": "^10.5.4", | ||
"pkg-ok": "^2.3.1", | ||
"prettier": "^2.2.1", | ||
"semantic-release": "^17.4.2", | ||
"ts-jest": "^26.5.4", | ||
"typescript": "^4.2.3" | ||
} | ||
} |
# getenv.ts | ||
> Helper to get and typecast environment variables with TypeScript. | ||
> Helper to get, validate, cast and provide type safety for environment | ||
> variables with TypeScript. | ||
## :warning: Alpha | ||
This library is still in alpha stages and is subject to change. The intention is | ||
to get the API up to par with [getenv], but a lot is lacking and bugs are | ||
probable. | ||
### To do: | ||
* [ ] `.boolish` | ||
* [ ] `.array` | ||
* [ ] `.multi` | ||
* [ ] `.url` | ||
* [ ] `.(enable/disable)Fallbacks` | ||
* [ ] `.(enable/disable)Errors` | ||
* [ ] Full devops | ||
* [ ] Full test coverage | ||
* [ ] Full documentation | ||
* [ ] [vue-cli] compatability | ||
## Installation | ||
> yarn add getenv.ts | ||
> npm install getenv.ts | ||
@@ -46,9 +27,26 @@ | ||
const host = getenv("HTTP_HOST"); // Same as `getenv.string("HTTP_HOST");` | ||
const port = getenv.int("HTTP_PORT"); | ||
const secure = getenv.bool("HTTP_SECURE"); | ||
const abTestRatio = getenv.float("AB_TEST_RATIO"); | ||
// Get values cast to desired type. | ||
const host: string = getenv.string("HTTP_HOST"); | ||
const port: number = getenv.int("HTTP_PORT"); | ||
const secure: boolean = getenv.bool("HTTP_SECURE"); | ||
const abTestRatio: number = getenv.float("AB_TEST_RATIO"); | ||
// Use default value if variable isn't defined. | ||
const sudoUser = getenv.string("SUDO_USER", "root"); | ||
// Throws `ReferenceError` when variable isn't defined. | ||
try { | ||
const invalid = getenv.string("DOES_NOT_EXIST"); | ||
} catch (error) { | ||
// Will throw... | ||
} | ||
// Throws `TypeError` when variable isn't of the expected type. | ||
try { | ||
const invalid = getenv.int("HTTP_HOST"); | ||
} catch (error) { | ||
// Will throw... | ||
} | ||
``` | ||
[getenv]: https://github.com/ctavan/node-getenv | ||
[vue-cli]: https://github.com/vuejs/vue-cli |
export function toBoolean(value?: any) { | ||
if (typeof value === "string") value = value.toLowerCase(); | ||
return typeof value === "boolean" | ||
? value | ||
: value === "true" | ||
? true | ||
: value === "false" | ||
? false | ||
: undefined; | ||
? true | ||
: value === "false" | ||
? false | ||
: undefined; | ||
} | ||
@@ -10,0 +11,0 @@ |
@@ -12,2 +12,3 @@ import getenv from "."; | ||
FALSE: "false", | ||
TRUE: "True", | ||
FLOAT: "1.1", | ||
@@ -26,2 +27,3 @@ INT: "1", | ||
expect(getenv("FALSE")).toBe("false"); | ||
expect(getenv("TRUE")).toBe("True"); | ||
expect(getenv("FLOAT")).toBe("1.1"); | ||
@@ -57,2 +59,3 @@ expect(getenv("INT")).toBe("1"); | ||
expect(getenv.bool("FALSE")).toBe(false); | ||
expect(getenv.bool("TRUE")).toBe(true); | ||
}); | ||
@@ -59,0 +62,0 @@ |
@@ -0,1 +1,4 @@ | ||
export * from "./types"; | ||
export * from "./getenv"; | ||
import { GetEnvInterface } from "./types"; | ||
@@ -2,0 +5,0 @@ import { |
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
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
470
25289
52