Skip to content

Commit e7f4dad

Browse files
authored
feat: esm only (#6)
BREAKING CHANGE: drop Node.js < 22.17.0 support
1 parent ecb8066 commit e7f4dad

7 files changed

Lines changed: 48 additions & 54 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ on:
55
branches: [master]
66
pull_request:
77
branches: [master]
8+
merge_group:
89

910
jobs:
1011
Job:
1112
name: Node.js
1213
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1314
with:
1415
os: 'ubuntu-latest, macos-latest, windows-latest'
15-
version: '20, 22, 24'
16+
version: '22, 24'
1617
secrets:
1718
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/pkg.pr.new.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- run: corepack enable
1313
- uses: actions/setup-node@v4
1414
with:
15-
node-version: 20
15+
node-version: 22
1616

1717
- name: Install dependencies
1818
run: npm install

.oxlintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"import/no-default-export": "allow",
7979
"import/unambiguous": "error",
8080
"import/group-exports": "allow",
81+
"import/consistent-type-specifier-style": "allow",
8182

8283
// promise
8384
"promise/no-return-wrap": "error",

package.json

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,26 @@
1717
},
1818
"homepage": "https://github.com/node-modules/read-env-value#readme",
1919
"engines": {
20-
"node": ">= 20.0.0"
20+
"node": ">= 22.17.0"
2121
},
2222
"devDependencies": {
23-
"@arethetypeswrong/cli": "^0.18.1",
24-
"@eggjs/tsconfig": "2",
25-
"@types/node": "22",
26-
"@vitest/coverage-v8": "3",
23+
"@eggjs/tsconfig": "3",
24+
"@types/node": "24",
25+
"c8": "10",
2726
"husky": "9",
2827
"mm": "4",
29-
"oxlint": "^0.16.10",
28+
"oxlint": "1",
3029
"prettier": "3",
31-
"tshy": "3",
32-
"tshy-after": "1",
33-
"typescript": "5",
34-
"vitest": "3"
30+
"typescript": "5"
3531
},
3632
"scripts": {
3733
"lint": "oxlint",
3834
"pretest": "npm run lint -- --fix",
39-
"test": "vitest run",
35+
"test": "node --test --experimental-strip-types",
4036
"preci": "npm run lint",
41-
"ci": "vitest run --coverage",
37+
"ci": "c8 -r html -r lcov -r text npm test",
4238
"postci": "npm run prepublishOnly",
43-
"prepublishOnly": "tshy && tshy-after && attw --pack",
39+
"prepublishOnly": "tsc -b --clean && tsc",
4440
"prepare": "husky"
4541
},
4642
"lint-staged": {
@@ -50,31 +46,11 @@
5046
"oxlint --fix"
5147
]
5248
},
53-
"type": "module",
54-
"tshy": {
55-
"exports": {
56-
".": "./src/index.ts",
57-
"./package.json": "./package.json"
58-
}
59-
},
60-
"exports": {
61-
".": {
62-
"import": {
63-
"types": "./dist/esm/index.d.ts",
64-
"default": "./dist/esm/index.js"
65-
},
66-
"require": {
67-
"types": "./dist/commonjs/index.d.ts",
68-
"default": "./dist/commonjs/index.js"
69-
}
70-
},
71-
"./package.json": "./package.json"
72-
},
7349
"files": [
7450
"dist",
7551
"src"
7652
],
77-
"types": "./dist/commonjs/index.d.ts",
78-
"main": "./dist/commonjs/index.js",
79-
"module": "./dist/esm/index.js"
53+
"type": "module",
54+
"exports": "./dist/index.js",
55+
"types": "./dist/index.d.ts"
8056
}

src/index.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
export type ValueType = 'string' | 'boolean' | 'number';
22
export type DefaultValue = string | boolean | number | undefined;
33

4-
interface TypeMap<D> {
5-
string: D extends undefined ? string | undefined : string;
6-
boolean: D extends undefined ? boolean | undefined : boolean;
7-
number: D extends undefined ? number | undefined : number;
4+
interface TypeMap<Default> {
5+
string: Default extends undefined ? string | undefined : string;
6+
boolean: Default extends undefined ? boolean | undefined : boolean;
7+
number: Default extends undefined ? number | undefined : number;
88
}
99

10-
type EnvReturn<V extends ValueType, D extends DefaultValue> = TypeMap<D>[V];
10+
type EnvReturn<
11+
Value extends ValueType,
12+
Default extends DefaultValue,
13+
> = TypeMap<Default>[Value];
1114

1215
export class EnvError extends Error {
1316
code: string;
@@ -19,21 +22,21 @@ export class EnvError extends Error {
1922
}
2023
}
2124

22-
export function env<V extends ValueType, D extends DefaultValue>(
25+
export function env<Value extends ValueType, Default extends DefaultValue>(
2326
key: string,
24-
valueType: V,
25-
defaultValue?: D
26-
): EnvReturn<V, D> {
27+
valueType: Value,
28+
defaultValue?: Default
29+
): EnvReturn<Value, Default> {
2730
let value = process.env[key];
2831
if (typeof value === 'string') {
2932
value = value.trim();
3033
}
3134
if (undefined === value || value === '') {
32-
return defaultValue as EnvReturn<V, D>;
35+
return defaultValue as EnvReturn<Value, Default>;
3336
}
3437

3538
if (valueType === 'string') {
36-
return value as EnvReturn<V, D>;
39+
return value as EnvReturn<Value, Default>;
3740
}
3841

3942
if (valueType === 'boolean') {
@@ -49,7 +52,7 @@ export function env<V extends ValueType, D extends DefaultValue>(
4952
'ERR_ENV_INVALID_BOOLEAN_VALUE'
5053
);
5154
}
52-
return booleanValue as EnvReturn<V, D>;
55+
return booleanValue as EnvReturn<Value, Default>;
5356
}
5457

5558
if (valueType === 'number') {
@@ -60,7 +63,7 @@ export function env<V extends ValueType, D extends DefaultValue>(
6063
'ERR_ENV_INVALID_NUMBER_VALUE'
6164
);
6265
}
63-
return numberValue as EnvReturn<V, D>;
66+
return numberValue as EnvReturn<Value, Default>;
6467
}
6568

6669
throw new EnvError(

test/index.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assert from 'node:assert/strict';
2+
import { test, beforeEach } from 'node:test';
23

3-
import { test, beforeEach } from 'vitest';
44
import { mock, restore } from 'mm';
55

6-
import { env, type ValueType } from '../src/index.js';
6+
import { env, EnvError, type ValueType } from '../src/index.ts';
77

88
beforeEach(restore);
99

@@ -142,3 +142,10 @@ test('should work with number value', () => {
142142
mock(process.env, 'TEST_ENV_NUMBER', '0');
143143
assert.equal(env('TEST_ENV_NUMBER', 'number', 10), 0);
144144
});
145+
146+
test('should create EnvError without code', () => {
147+
const err = new EnvError('test');
148+
assert.equal(err.code, 'READ_ENV_VALUE_ERROR');
149+
assert.equal(err.message, 'test');
150+
assert.equal(err.name, 'ReadEnvValueError');
151+
});

tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
{
2-
"extends": "@eggjs/tsconfig"
2+
"extends": "@eggjs/tsconfig",
3+
"compilerOptions": {
4+
"rootDir": "./src",
5+
"outDir": "./dist"
6+
},
7+
"include": ["src"],
8+
"exclude": ["test"]
39
}

0 commit comments

Comments
 (0)