Skip to content

Commit

Permalink
Migrate to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
suphero committed Nov 10, 2023
1 parent b008a6b commit 7ce0829
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
- name: Install dependencies
run: npm install

- name: Build
run: npm run build

- name: Extract version from package.json
id: package_version
run: echo "::set-output name=VERSION::$(node -p "require('./package.json').version")"
Expand Down
38 changes: 37 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
node_modules/
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.swp

pids
logs
results
tmp

# Build
public/css/main.css

# Coverage reports
coverage

# API keys and secrets
.env

# Dependency directory
node_modules
bower_components

# Editors
.idea
*.iml

# OS metadata
.DS_Store
Thumbs.db

# Ignore built ts files
dist/**/*
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
129 changes: 128 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "envflex",
"version": "1.0.0",
"version": "1.0.1",
"description": "Enhanced environment variable loader for Node.js",
"main": "index.js",
"keywords": [
Expand All @@ -12,13 +12,18 @@
"author": "Think Thread",
"license": "MIT",
"scripts": {
"build": "tsc",
"test": "jest"
},
"dependencies": {
"dotenv": "^16.3.1"
},
"devDependencies": {
"jest": "^29.7.0"
"@types/jest": "^29.5.8",
"@types/node": "^20.9.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
},
"publishConfig": {
"access": "public"
Expand Down
37 changes: 23 additions & 14 deletions index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
const dotenv = require('dotenv');
import dotenv from 'dotenv';
dotenv.config();

class EnvFlex {
static _castToString(value) {
export default class EnvFlex {
static _castToString(value: string) {
return String(value).trim();
}

static _castToNumber(value) {
static _castToNumber(value: string) {
const number = Number(value.trim());
if (isNaN(number)) {
throw new Error(`EnvFlex: Value "${value}" is not a valid number.`);
}
return number;
}

static _castToBoolean(value) {
return ['true', '1', 'yes'].includes(value.trim().toLowerCase());
static _castToBoolean(value: string) {
return ["true", "1", "yes"].includes(value.trim().toLowerCase());
}

static getString(key, defaultValue = '') {
static getString(key: string, defaultValue = "") {
const value = process.env[key];
return value === undefined ? defaultValue : EnvFlex._castToString(value);
}

static getNumber(key, defaultValue = 0) {
static getNumber(key: string, defaultValue = 0) {
const value = process.env[key];
return value === undefined ? defaultValue : EnvFlex._castToNumber(value);
}

static getBoolean(key, defaultValue = false) {
static getBoolean(key: string, defaultValue = false) {
const value = process.env[key];
return value === undefined ? defaultValue : EnvFlex._castToBoolean(value);
}

static getStringArray(key, defaultValue = [], delimiter = ',') {
static getStringArray(key: string, defaultValue = [], delimiter = ",") {
const value = process.env[key];
if (value === undefined) {
return defaultValue;
Expand All @@ -42,7 +42,7 @@ class EnvFlex {
return value.split(delimiter).map(EnvFlex._castToString);
}

static getNumberArray(key, defaultValue = [], delimiter = ',') {
static getNumberArray(key: string, defaultValue = [], delimiter = ",") {
const value = process.env[key];
if (value === undefined) {
return defaultValue;
Expand All @@ -51,14 +51,23 @@ class EnvFlex {
return value.split(delimiter).map(EnvFlex._castToNumber);
}

static getBooleanArray(key, defaultValue = [], delimiter = ',') {
static getBooleanArray(key: string, defaultValue = [], delimiter = ",") {
const value = process.env[key];
if (value === undefined) {
return defaultValue;
}

return value.split(delimiter).map(EnvFlex._castToBoolean);
}
}

module.exports = EnvFlex;
static require(key: string) {
const value = process.env[key];
if (value === undefined) {
throw new Error(
`EnvFlex: Required environment variable "${key}" is not set.`
);
}

return value;
}
}
2 changes: 1 addition & 1 deletion test/envflex.test.js → test/envflex.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const EnvFlex = require('../index');
import EnvFlex from '../src/index';

describe('Value', () => {
beforeAll(() => {
Expand Down
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}

0 comments on commit 7ce0829

Please sign in to comment.