diff --git a/CHANGELOG.md b/CHANGELOG.md index 489aef4..9b7a3d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [0.7.0](https://github.com/allohamora/config-manager/compare/0.6.0...0.7.0) (2023-07-27) + +### Features + +- add rest to envRecord ([2d57b5d](https://github.com/allohamora/config-manager/commit/2d57b5d4e9ce2555cafd5b302ce0b4195c1b4a83)) +- improve defaultFor with rest return value ([304530d](https://github.com/allohamora/config-manager/commit/304530d2106ac0575fe06cc8a532be48e441d924)) +- improve orThrow methods typing ([d7f7590](https://github.com/allohamora/config-manager/commit/d7f7590e3ee04eef31edeb028752105056982346)) + ## [0.6.0](https://github.com/allohamora/config-manager/compare/0.5.2...0.6.0) (2023-04-13) ### ⚠ BREAKING CHANGES diff --git a/__tests__/unit/env.manager.spec.ts b/__tests__/unit/env.manager.spec.ts index c8f3199..bbda987 100644 --- a/__tests__/unit/env.manager.spec.ts +++ b/__tests__/unit/env.manager.spec.ts @@ -47,6 +47,21 @@ describe('EnvManager', () => { expect(env.pickFor({ test: 'SPACE_STRING' }).value()).toEqual(undefined); expect(env.pickFor({ test: 'OPTIONAL' }).value()).toEqual(undefined); }); + + it('returns default values', () => { + expect(env.pickFor({ production: 'INTEGER', rest: 'EMAIL' }).value()).toBe(envStub.EMAIL); + expect(env.pickFor({ production: 'EMAIL', rest: 'INTEGER' }).value()).toBe(envStub.INTEGER); + }); + + it('returns environment values instead of default', () => { + expect(env.pickFor({ test: 'INTEGER', rest: 'EMAIL' }).value()).toBe(envStub.INTEGER); + expect(env.pickFor({ test: 'EMAIL', rest: 'INTEGER' }).value()).toBe(envStub.EMAIL); + }); + + it('returns undefined', () => { + expect(env.pickFor({ production: 'INTEGER', rest: '__TEST__' }).value()).toBe(undefined); + expect(env.pickFor({ production: 'EMAIL', rest: '__TEST__' }).value()).toBe(undefined); + }); }); describe('pickForOrThrow', () => { @@ -55,6 +70,16 @@ describe('EnvManager', () => { expect(env.pickForOrThrow({ test: 'INTEGER' }).value()).toBe(envStub.INTEGER); }); + it('returns default values', () => { + expect(env.pickForOrThrow({ production: 'INTEGER', rest: 'EMAIL' }).value()).toBe(envStub.EMAIL); + expect(env.pickForOrThrow({ production: 'EMAIL', rest: 'INTEGER' }).value()).toBe(envStub.INTEGER); + }); + + it('returns environment values instead of default', () => { + expect(env.pickForOrThrow({ test: 'INTEGER', rest: 'EMAIL' }).value()).toBe(envStub.INTEGER); + expect(env.pickForOrThrow({ test: 'EMAIL', rest: 'INTEGER' }).value()).toBe(envStub.EMAIL); + }); + it('throws errors', () => { expect(() => env.pickForOrThrow({ test: 'EMPTY_STRING' })).toThrowError(); expect(() => env.pickForOrThrow({ test: 'SPACE_STRING' })).toThrowError(); @@ -62,6 +87,9 @@ describe('EnvManager', () => { expect(() => env.pickForOrThrow({ production: 'EMAIL' })).toThrowError(); expect(() => env.pickForOrThrow({ production: 'production' })).toThrowError(); + + expect(() => env.pickForOrThrow({ production: 'EMAIL', rest: '__TEST__' })).toThrowError(); + expect(() => env.pickForOrThrow({ production: 'EMAIL', rest: 'rest' })).toThrowError(); }); }); @@ -77,6 +105,21 @@ describe('EnvManager', () => { expect(env.getFor({ test: 'SPACE_STRING' })).toEqual(envStub.SPACE_STRING); expect(env.getFor({ test: 'OPTIONAL' })).toEqual(undefined); }); + + it('returns default values', () => { + expect(env.getFor({ production: 'INTEGER', rest: 'EMAIL' })).toBe(envStub.EMAIL); + expect(env.getFor({ production: 'EMAIL', rest: 'INTEGER' })).toBe(envStub.INTEGER); + }); + + it('returns environment values instead of default', () => { + expect(env.getFor({ test: 'INTEGER', rest: 'EMAIL' })).toBe(envStub.INTEGER); + expect(env.getFor({ test: 'EMAIL', rest: 'INTEGER' })).toBe(envStub.EMAIL); + }); + + it('returns undefined', () => { + expect(env.getFor({ production: 'INTEGER', rest: '__TEST__' })).toBe(undefined); + expect(env.getFor({ production: 'EMAIL', rest: '__TEST__' })).toBe(undefined); + }); }); describe('getForOrThrow', () => { @@ -87,10 +130,22 @@ describe('EnvManager', () => { expect(env.getForOrThrow({ test: 'SPACE_STRING' })).toEqual(envStub.SPACE_STRING); }); + it('returns default values', () => { + expect(env.getForOrThrow({ production: 'INTEGER', rest: 'EMAIL' })).toBe(envStub.EMAIL); + expect(env.getForOrThrow({ production: 'EMAIL', rest: 'INTEGER' })).toBe(envStub.INTEGER); + }); + + it('returns environment values instead of default', () => { + expect(env.getForOrThrow({ test: 'INTEGER', rest: 'EMAIL' })).toBe(envStub.INTEGER); + expect(env.getForOrThrow({ test: 'EMAIL', rest: 'INTEGER' })).toBe(envStub.EMAIL); + }); + it('throws errors', () => { expect(() => env.getForOrThrow({ test: 'OPTIONAL' })).toThrowError(); + expect(() => env.getForOrThrow({ test: 'OPTIONAL', rest: '__TEST__' })).toThrowError(); expect(() => env.getForOrThrow({ production: 'EMAIL' })).toThrowError(); expect(() => env.getForOrThrow({ production: 'production' })).toThrowError(); + expect(() => env.getForOrThrow({ production: 'production', rest: 'rest' })).toThrowError(); }); }); }); diff --git a/__tests__/unit/env.picker.spec.ts b/__tests__/unit/env.picker.spec.ts index 89c1df6..1f462d5 100644 --- a/__tests__/unit/env.picker.spec.ts +++ b/__tests__/unit/env.picker.spec.ts @@ -20,6 +20,17 @@ describe('EnvPicker', () => { expect(new EnvPicker(null as string | null, 'test').defaultFor({ test: '123' }).value()).toBe('123'); }); + it('sets rest values', () => { + expect( + new EnvPicker(undefined as string | undefined, 'test' as string) + .defaultFor({ production: '123', rest: '321' }) + .value(), + ).toBe('321'); + expect( + new EnvPicker(null as string | null, 'test' as string).defaultFor({ production: '123', rest: '321' }).value(), + ).toBe('321'); + }); + it('doesn`t set default value for environment', () => { expect( new EnvPicker(undefined as string | undefined, 'test' as string).defaultFor({ production: '123' }).value(), @@ -29,7 +40,7 @@ describe('EnvPicker', () => { ).toBe(undefined); expect(new EnvPicker(false as boolean | undefined, 'test').defaultFor({ test: true }).value()).toBe(false); - expect(new EnvPicker('', 'test').defaultFor({ test: '123' }).value()).toBe(''); + expect(new EnvPicker('' as string | undefined, 'test').defaultFor({ test: '123' }).value()).toBe(''); }); }); @@ -37,6 +48,9 @@ describe('EnvPicker', () => { it('maps state', () => { expect(new EnvPicker('123', 'test').map(Number).value()).toBe(123); expect(new EnvPicker(undefined as string | undefined, 'test').default('123').map(Number).value()).toBe(123); + expect( + new EnvPicker(undefined as string | undefined, 'test').defaultFor({ rest: '123' }).map(Number).value(), + ).toBe(123); expect(new EnvPicker('false', 'test').default('123').map(JSON.parse).value()).toBe(false); expect(new EnvPicker(undefined as string | undefined, 'test').map((state) => !!state).value()).toBe(false); }); diff --git a/package-lock.json b/package-lock.json index 890c56e..1219211 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@allohamora/config-manager", - "version": "0.6.0", + "version": "0.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@allohamora/config-manager", - "version": "0.6.0", + "version": "0.7.0", "license": "MIT", "devDependencies": { "@commitlint/cli": "^17.5.1", @@ -95,9 +95,9 @@ } }, "node_modules/@babel/core/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -166,9 +166,9 @@ } }, "node_modules/@babel/helper-compilation-targets/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -554,13 +554,13 @@ } }, "node_modules/@commitlint/is-ignored": { - "version": "17.4.4", - "resolved": "https://registry.npmjs.org/@commitlint/is-ignored/-/is-ignored-17.4.4.tgz", - "integrity": "sha512-Y3eo1SFJ2JQDik4rWkBC4tlRIxlXEFrRWxcyrzb1PUT2k3kZ/XGNuCDfk/u0bU2/yS0tOA/mTjFsV+C4qyACHw==", + "version": "17.6.7", + "resolved": "https://registry.npmjs.org/@commitlint/is-ignored/-/is-ignored-17.6.7.tgz", + "integrity": "sha512-vqyNRqtbq72P2JadaoWiuoLtXIs9SaAWDqdtef6G2zsoXqKFc7vqj1f+thzVgosXG3X/5K9jNp+iYijmvOfc/g==", "dev": true, "dependencies": { "@commitlint/types": "^17.4.4", - "semver": "7.3.8" + "semver": "7.5.2" }, "engines": { "node": ">=v14" @@ -2601,9 +2601,9 @@ } }, "node_modules/conventional-changelog-core/node_modules/semver": { - "version": "5.7.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", - "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", + "version": "5.7.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz", + "integrity": "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==", "dev": true, "bin": { "semver": "bin/semver" @@ -2703,9 +2703,9 @@ } }, "node_modules/conventional-changelog-writer/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -3804,9 +3804,9 @@ } }, "node_modules/git-semver-tags/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -5068,10 +5068,16 @@ "dev": true }, "node_modules/nanoid": { - "version": "3.3.4", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz", - "integrity": "sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==", + "version": "3.3.6", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.6.tgz", + "integrity": "sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==", "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], "bin": { "nanoid": "bin/nanoid.cjs" }, @@ -5381,9 +5387,9 @@ } }, "node_modules/postcss": { - "version": "8.4.21", - "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.21.tgz", - "integrity": "sha512-tP7u/Sn/dVxK2NnruI4H9BG+x+Wxz6oeZ1cJ8P6G/PZY0IKk4k/63TDsQf2kQq3+qoJeLm2kIBUNlZe3zgb4Zg==", + "version": "8.4.27", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.27.tgz", + "integrity": "sha512-gY/ACJtJPSmUFPDCHtX78+01fHa64FaU4zaaWfuh1MhGJISufJAH4cun6k/8fwsHYeK4UQmENQK+tRLCFJE8JQ==", "dev": true, "funding": [ { @@ -5393,10 +5399,14 @@ { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" } ], "dependencies": { - "nanoid": "^3.3.4", + "nanoid": "^3.3.6", "picocolors": "^1.0.0", "source-map-js": "^1.0.2" }, @@ -5650,9 +5660,9 @@ } }, "node_modules/read-pkg/node_modules/semver": { - "version": "5.7.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", - "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", + "version": "5.7.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz", + "integrity": "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==", "dev": true, "bin": { "semver": "bin/semver" @@ -5795,9 +5805,9 @@ } }, "node_modules/rollup": { - "version": "3.20.2", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-3.20.2.tgz", - "integrity": "sha512-3zwkBQl7Ai7MFYQE0y1MeQ15+9jsi7XxfrqwTb/9EK8D9C9+//EBR4M+CuA1KODRaNbFez/lWxA5vhEGZp4MUg==", + "version": "3.26.3", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-3.26.3.tgz", + "integrity": "sha512-7Tin0C8l86TkpcMtXvQu6saWH93nhG3dGQ1/+l5V2TDMceTxO7kDiK6GzbfLWNNxqJXm591PcEZUozZm51ogwQ==", "dev": true, "bin": { "rollup": "dist/bin/rollup" @@ -5897,9 +5907,9 @@ "dev": true }, "node_modules/semver": { - "version": "7.3.8", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz", - "integrity": "sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==", + "version": "7.5.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.2.tgz", + "integrity": "sha512-SoftuTROv/cRjCze/scjGyiDtcUyxw1rgYQSZY7XTmtR5hX+dm76iDbTH8TkLPHCQmlbQVSSbNZCPM2hb0knnQ==", "dev": true, "dependencies": { "lru-cache": "^6.0.0" @@ -6741,15 +6751,14 @@ } }, "node_modules/vite": { - "version": "4.1.4", - "resolved": "https://registry.npmjs.org/vite/-/vite-4.1.4.tgz", - "integrity": "sha512-3knk/HsbSTKEin43zHu7jTwYWv81f8kgAL99G5NWBcA1LKvtvcVAC4JjBH1arBunO9kQka+1oGbrMKOjk4ZrBg==", + "version": "4.4.7", + "resolved": "https://registry.npmjs.org/vite/-/vite-4.4.7.tgz", + "integrity": "sha512-6pYf9QJ1mHylfVh39HpuSfMPojPSKVxZvnclX1K1FyZ1PXDOcLBibdq5t1qxJSnL63ca8Wf4zts6mD8u8oc9Fw==", "dev": true, "dependencies": { - "esbuild": "^0.16.14", - "postcss": "^8.4.21", - "resolve": "^1.22.1", - "rollup": "^3.10.0" + "esbuild": "^0.18.10", + "postcss": "^8.4.26", + "rollup": "^3.25.2" }, "bin": { "vite": "bin/vite.js" @@ -6757,12 +6766,16 @@ "engines": { "node": "^14.18.0 || >=16.0.0" }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, "optionalDependencies": { "fsevents": "~2.3.2" }, "peerDependencies": { "@types/node": ">= 14", "less": "*", + "lightningcss": "^1.21.0", "sass": "*", "stylus": "*", "sugarss": "*", @@ -6775,6 +6788,9 @@ "less": { "optional": true }, + "lightningcss": { + "optional": true + }, "sass": { "optional": true }, @@ -6832,9 +6848,9 @@ } }, "node_modules/vite/node_modules/@esbuild/android-arm": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.16.17.tgz", - "integrity": "sha512-N9x1CMXVhtWEAMS7pNNONyA14f71VPQN9Cnavj1XQh6T7bskqiLLrSca4O0Vr8Wdcga943eThxnVp3JLnBMYtw==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.18.17.tgz", + "integrity": "sha512-wHsmJG/dnL3OkpAcwbgoBTTMHVi4Uyou3F5mf58ZtmUyIKfcdA7TROav/6tCzET4A3QW2Q2FC+eFneMU+iyOxg==", "cpu": [ "arm" ], @@ -6848,9 +6864,9 @@ } }, "node_modules/vite/node_modules/@esbuild/android-arm64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.16.17.tgz", - "integrity": "sha512-MIGl6p5sc3RDTLLkYL1MyL8BMRN4tLMRCn+yRJJmEDvYZ2M7tmAf80hx1kbNEUX2KJ50RRtxZ4JHLvCfuB6kBg==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.18.17.tgz", + "integrity": "sha512-9np+YYdNDed5+Jgr1TdWBsozZ85U1Oa3xW0c7TWqH0y2aGghXtZsuT8nYRbzOMcl0bXZXjOGbksoTtVOlWrRZg==", "cpu": [ "arm64" ], @@ -6864,9 +6880,9 @@ } }, "node_modules/vite/node_modules/@esbuild/android-x64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.16.17.tgz", - "integrity": "sha512-a3kTv3m0Ghh4z1DaFEuEDfz3OLONKuFvI4Xqczqx4BqLyuFaFkuaG4j2MtA6fuWEFeC5x9IvqnX7drmRq/fyAQ==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.18.17.tgz", + "integrity": "sha512-O+FeWB/+xya0aLg23hHEM2E3hbfwZzjqumKMSIqcHbNvDa+dza2D0yLuymRBQQnC34CWrsJUXyH2MG5VnLd6uw==", "cpu": [ "x64" ], @@ -6880,9 +6896,9 @@ } }, "node_modules/vite/node_modules/@esbuild/darwin-arm64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.16.17.tgz", - "integrity": "sha512-/2agbUEfmxWHi9ARTX6OQ/KgXnOWfsNlTeLcoV7HSuSTv63E4DqtAc+2XqGw1KHxKMHGZgbVCZge7HXWX9Vn+w==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.18.17.tgz", + "integrity": "sha512-M9uJ9VSB1oli2BE/dJs3zVr9kcCBBsE883prage1NWz6pBS++1oNn/7soPNS3+1DGj0FrkSvnED4Bmlu1VAE9g==", "cpu": [ "arm64" ], @@ -6896,9 +6912,9 @@ } }, "node_modules/vite/node_modules/@esbuild/darwin-x64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.16.17.tgz", - "integrity": "sha512-2By45OBHulkd9Svy5IOCZt376Aa2oOkiE9QWUK9fe6Tb+WDr8hXL3dpqi+DeLiMed8tVXspzsTAvd0jUl96wmg==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.18.17.tgz", + "integrity": "sha512-XDre+J5YeIJDMfp3n0279DFNrGCXlxOuGsWIkRb1NThMZ0BsrWXoTg23Jer7fEXQ9Ye5QjrvXpxnhzl3bHtk0g==", "cpu": [ "x64" ], @@ -6912,9 +6928,9 @@ } }, "node_modules/vite/node_modules/@esbuild/freebsd-arm64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.16.17.tgz", - "integrity": "sha512-mt+cxZe1tVx489VTb4mBAOo2aKSnJ33L9fr25JXpqQqzbUIw/yzIzi+NHwAXK2qYV1lEFp4OoVeThGjUbmWmdw==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.18.17.tgz", + "integrity": "sha512-cjTzGa3QlNfERa0+ptykyxs5A6FEUQQF0MuilYXYBGdBxD3vxJcKnzDlhDCa1VAJCmAxed6mYhA2KaJIbtiNuQ==", "cpu": [ "arm64" ], @@ -6928,9 +6944,9 @@ } }, "node_modules/vite/node_modules/@esbuild/freebsd-x64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.16.17.tgz", - "integrity": "sha512-8ScTdNJl5idAKjH8zGAsN7RuWcyHG3BAvMNpKOBaqqR7EbUhhVHOqXRdL7oZvz8WNHL2pr5+eIT5c65kA6NHug==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.18.17.tgz", + "integrity": "sha512-sOxEvR8d7V7Kw8QqzxWc7bFfnWnGdaFBut1dRUYtu+EIRXefBc/eIsiUiShnW0hM3FmQ5Zf27suDuHsKgZ5QrA==", "cpu": [ "x64" ], @@ -6944,9 +6960,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-arm": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.16.17.tgz", - "integrity": "sha512-iihzrWbD4gIT7j3caMzKb/RsFFHCwqqbrbH9SqUSRrdXkXaygSZCZg1FybsZz57Ju7N/SHEgPyaR0LZ8Zbe9gQ==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.18.17.tgz", + "integrity": "sha512-2d3Lw6wkwgSLC2fIvXKoMNGVaeY8qdN0IC3rfuVxJp89CRfA3e3VqWifGDfuakPmp90+ZirmTfye1n4ncjv2lg==", "cpu": [ "arm" ], @@ -6960,9 +6976,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-arm64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.16.17.tgz", - "integrity": "sha512-7S8gJnSlqKGVJunnMCrXHU9Q8Q/tQIxk/xL8BqAP64wchPCTzuM6W3Ra8cIa1HIflAvDnNOt2jaL17vaW+1V0g==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.18.17.tgz", + "integrity": "sha512-c9w3tE7qA3CYWjT+M3BMbwMt+0JYOp3vCMKgVBrCl1nwjAlOMYzEo+gG7QaZ9AtqZFj5MbUc885wuBBmu6aADQ==", "cpu": [ "arm64" ], @@ -6976,9 +6992,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-ia32": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.16.17.tgz", - "integrity": "sha512-kiX69+wcPAdgl3Lonh1VI7MBr16nktEvOfViszBSxygRQqSpzv7BffMKRPMFwzeJGPxcio0pdD3kYQGpqQ2SSg==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.18.17.tgz", + "integrity": "sha512-1DS9F966pn5pPnqXYz16dQqWIB0dmDfAQZd6jSSpiT9eX1NzKh07J6VKR3AoXXXEk6CqZMojiVDSZi1SlmKVdg==", "cpu": [ "ia32" ], @@ -6992,9 +7008,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-loong64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.16.17.tgz", - "integrity": "sha512-dTzNnQwembNDhd654cA4QhbS9uDdXC3TKqMJjgOWsC0yNCbpzfWoXdZvp0mY7HU6nzk5E0zpRGGx3qoQg8T2DQ==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.18.17.tgz", + "integrity": "sha512-EvLsxCk6ZF0fpCB6w6eOI2Fc8KW5N6sHlIovNe8uOFObL2O+Mr0bflPHyHwLT6rwMg9r77WOAWb2FqCQrVnwFg==", "cpu": [ "loong64" ], @@ -7008,9 +7024,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-mips64el": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.16.17.tgz", - "integrity": "sha512-ezbDkp2nDl0PfIUn0CsQ30kxfcLTlcx4Foz2kYv8qdC6ia2oX5Q3E/8m6lq84Dj/6b0FrkgD582fJMIfHhJfSw==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.18.17.tgz", + "integrity": "sha512-e0bIdHA5p6l+lwqTE36NAW5hHtw2tNRmHlGBygZC14QObsA3bD4C6sXLJjvnDIjSKhW1/0S3eDy+QmX/uZWEYQ==", "cpu": [ "mips64el" ], @@ -7024,9 +7040,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-ppc64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.16.17.tgz", - "integrity": "sha512-dzS678gYD1lJsW73zrFhDApLVdM3cUF2MvAa1D8K8KtcSKdLBPP4zZSLy6LFZ0jYqQdQ29bjAHJDgz0rVbLB3g==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.18.17.tgz", + "integrity": "sha512-BAAilJ0M5O2uMxHYGjFKn4nJKF6fNCdP1E0o5t5fvMYYzeIqy2JdAP88Az5LHt9qBoUa4tDaRpfWt21ep5/WqQ==", "cpu": [ "ppc64" ], @@ -7040,9 +7056,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-riscv64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.16.17.tgz", - "integrity": "sha512-ylNlVsxuFjZK8DQtNUwiMskh6nT0vI7kYl/4fZgV1llP5d6+HIeL/vmmm3jpuoo8+NuXjQVZxmKuhDApK0/cKw==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.18.17.tgz", + "integrity": "sha512-Wh/HW2MPnC3b8BqRSIme/9Zhab36PPH+3zam5pqGRH4pE+4xTrVLx2+XdGp6fVS3L2x+DrsIcsbMleex8fbE6g==", "cpu": [ "riscv64" ], @@ -7056,9 +7072,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-s390x": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.16.17.tgz", - "integrity": "sha512-gzy7nUTO4UA4oZ2wAMXPNBGTzZFP7mss3aKR2hH+/4UUkCOyqmjXiKpzGrY2TlEUhbbejzXVKKGazYcQTZWA/w==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.18.17.tgz", + "integrity": "sha512-j/34jAl3ul3PNcK3pfI0NSlBANduT2UO5kZ7FCaK33XFv3chDhICLY8wJJWIhiQ+YNdQ9dxqQctRg2bvrMlYgg==", "cpu": [ "s390x" ], @@ -7072,9 +7088,9 @@ } }, "node_modules/vite/node_modules/@esbuild/linux-x64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.16.17.tgz", - "integrity": "sha512-mdPjPxfnmoqhgpiEArqi4egmBAMYvaObgn4poorpUaqmvzzbvqbowRllQ+ZgzGVMGKaPkqUmPDOOFQRUFDmeUw==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.18.17.tgz", + "integrity": "sha512-QM50vJ/y+8I60qEmFxMoxIx4de03pGo2HwxdBeFd4nMh364X6TIBZ6VQ5UQmPbQWUVWHWws5MmJXlHAXvJEmpQ==", "cpu": [ "x64" ], @@ -7088,9 +7104,9 @@ } }, "node_modules/vite/node_modules/@esbuild/netbsd-x64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.16.17.tgz", - "integrity": "sha512-/PzmzD/zyAeTUsduZa32bn0ORug+Jd1EGGAUJvqfeixoEISYpGnAezN6lnJoskauoai0Jrs+XSyvDhppCPoKOA==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.18.17.tgz", + "integrity": "sha512-/jGlhWR7Sj9JPZHzXyyMZ1RFMkNPjC6QIAan0sDOtIo2TYk3tZn5UDrkE0XgsTQCxWTTOcMPf9p6Rh2hXtl5TQ==", "cpu": [ "x64" ], @@ -7104,9 +7120,9 @@ } }, "node_modules/vite/node_modules/@esbuild/openbsd-x64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.16.17.tgz", - "integrity": "sha512-2yaWJhvxGEz2RiftSk0UObqJa/b+rIAjnODJgv2GbGGpRwAfpgzyrg1WLK8rqA24mfZa9GvpjLcBBg8JHkoodg==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.18.17.tgz", + "integrity": "sha512-rSEeYaGgyGGf4qZM2NonMhMOP/5EHp4u9ehFiBrg7stH6BYEEjlkVREuDEcQ0LfIl53OXLxNbfuIj7mr5m29TA==", "cpu": [ "x64" ], @@ -7120,9 +7136,9 @@ } }, "node_modules/vite/node_modules/@esbuild/sunos-x64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.16.17.tgz", - "integrity": "sha512-xtVUiev38tN0R3g8VhRfN7Zl42YCJvyBhRKw1RJjwE1d2emWTVToPLNEQj/5Qxc6lVFATDiy6LjVHYhIPrLxzw==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.18.17.tgz", + "integrity": "sha512-Y7ZBbkLqlSgn4+zot4KUNYst0bFoO68tRgI6mY2FIM+b7ZbyNVtNbDP5y8qlu4/knZZ73fgJDlXID+ohY5zt5g==", "cpu": [ "x64" ], @@ -7136,9 +7152,9 @@ } }, "node_modules/vite/node_modules/@esbuild/win32-arm64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.16.17.tgz", - "integrity": "sha512-ga8+JqBDHY4b6fQAmOgtJJue36scANy4l/rL97W+0wYmijhxKetzZdKOJI7olaBaMhWt8Pac2McJdZLxXWUEQw==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.18.17.tgz", + "integrity": "sha512-bwPmTJsEQcbZk26oYpc4c/8PvTY3J5/QK8jM19DVlEsAB41M39aWovWoHtNm78sd6ip6prilxeHosPADXtEJFw==", "cpu": [ "arm64" ], @@ -7152,9 +7168,9 @@ } }, "node_modules/vite/node_modules/@esbuild/win32-ia32": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.16.17.tgz", - "integrity": "sha512-WnsKaf46uSSF/sZhwnqE4L/F89AYNMiD4YtEcYekBt9Q7nj0DiId2XH2Ng2PHM54qi5oPrQ8luuzGszqi/veig==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.18.17.tgz", + "integrity": "sha512-H/XaPtPKli2MhW+3CQueo6Ni3Avggi6hP/YvgkEe1aSaxw+AeO8MFjq8DlgfTd9Iz4Yih3QCZI6YLMoyccnPRg==", "cpu": [ "ia32" ], @@ -7168,9 +7184,9 @@ } }, "node_modules/vite/node_modules/@esbuild/win32-x64": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.16.17.tgz", - "integrity": "sha512-y+EHuSchhL7FjHgvQL/0fnnFmO4T1bhvWANX6gcnqTjtnKWbTvUMCpGnv2+t+31d7RzyEAYAd4u2fnIhHL6N/Q==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.18.17.tgz", + "integrity": "sha512-fGEb8f2BSA3CW7riJVurug65ACLuQAzKq0SSqkY2b2yHHH0MzDfbLyKIGzHwOI/gkHcxM/leuSW6D5w/LMNitA==", "cpu": [ "x64" ], @@ -7184,9 +7200,9 @@ } }, "node_modules/vite/node_modules/esbuild": { - "version": "0.16.17", - "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.16.17.tgz", - "integrity": "sha512-G8LEkV0XzDMNwXKgM0Jwu3nY3lSTwSGY6XbxM9cr9+s0T/qSV1q1JVPBGzm3dcjhCic9+emZDmMffkwgPeOeLg==", + "version": "0.18.17", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.18.17.tgz", + "integrity": "sha512-1GJtYnUxsJreHYA0Y+iQz2UEykonY66HNWOb0yXYZi9/kNrORUEHVg87eQsCtqh59PEJ5YVZJO98JHznMJSWjg==", "dev": true, "hasInstallScript": true, "bin": { @@ -7196,28 +7212,28 @@ "node": ">=12" }, "optionalDependencies": { - "@esbuild/android-arm": "0.16.17", - "@esbuild/android-arm64": "0.16.17", - "@esbuild/android-x64": "0.16.17", - "@esbuild/darwin-arm64": "0.16.17", - "@esbuild/darwin-x64": "0.16.17", - "@esbuild/freebsd-arm64": "0.16.17", - "@esbuild/freebsd-x64": "0.16.17", - "@esbuild/linux-arm": "0.16.17", - "@esbuild/linux-arm64": "0.16.17", - "@esbuild/linux-ia32": "0.16.17", - "@esbuild/linux-loong64": "0.16.17", - "@esbuild/linux-mips64el": "0.16.17", - "@esbuild/linux-ppc64": "0.16.17", - "@esbuild/linux-riscv64": "0.16.17", - "@esbuild/linux-s390x": "0.16.17", - "@esbuild/linux-x64": "0.16.17", - "@esbuild/netbsd-x64": "0.16.17", - "@esbuild/openbsd-x64": "0.16.17", - "@esbuild/sunos-x64": "0.16.17", - "@esbuild/win32-arm64": "0.16.17", - "@esbuild/win32-ia32": "0.16.17", - "@esbuild/win32-x64": "0.16.17" + "@esbuild/android-arm": "0.18.17", + "@esbuild/android-arm64": "0.18.17", + "@esbuild/android-x64": "0.18.17", + "@esbuild/darwin-arm64": "0.18.17", + "@esbuild/darwin-x64": "0.18.17", + "@esbuild/freebsd-arm64": "0.18.17", + "@esbuild/freebsd-x64": "0.18.17", + "@esbuild/linux-arm": "0.18.17", + "@esbuild/linux-arm64": "0.18.17", + "@esbuild/linux-ia32": "0.18.17", + "@esbuild/linux-loong64": "0.18.17", + "@esbuild/linux-mips64el": "0.18.17", + "@esbuild/linux-ppc64": "0.18.17", + "@esbuild/linux-riscv64": "0.18.17", + "@esbuild/linux-s390x": "0.18.17", + "@esbuild/linux-x64": "0.18.17", + "@esbuild/netbsd-x64": "0.18.17", + "@esbuild/openbsd-x64": "0.18.17", + "@esbuild/sunos-x64": "0.18.17", + "@esbuild/win32-arm64": "0.18.17", + "@esbuild/win32-ia32": "0.18.17", + "@esbuild/win32-x64": "0.18.17" } }, "node_modules/vitest": { @@ -7329,9 +7345,9 @@ } }, "node_modules/word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", + "integrity": "sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==", "dev": true, "engines": { "node": ">=0.10.0" @@ -7391,9 +7407,9 @@ "dev": true }, "node_modules/yaml": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.2.1.tgz", - "integrity": "sha512-e0WHiYql7+9wr4cWMx3TVQrNwejKaEe7/rHNmQmqRjazfOP5W8PB6Jpebb5o6fIapbz9o9+2ipcaTM2ZwDI6lw==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.1.tgz", + "integrity": "sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ==", "dev": true, "engines": { "node": ">= 14" diff --git a/package.json b/package.json index 2ae5469..a34e0d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@allohamora/config-manager", - "version": "0.6.0", + "version": "0.7.0", "description": "config manager", "main": "./dist/index.cjs", "exports": { diff --git a/src/config/config.manager.ts b/src/config/config.manager.ts index f67a237..336218c 100644 --- a/src/config/config.manager.ts +++ b/src/config/config.manager.ts @@ -1,12 +1,12 @@ import { Manager } from 'src/utils/manager.utils.js'; import { flatten, Flatten } from '../utils/flatten.utils.js'; -interface Options { - getConfig: () => C; +interface Options { + getConfig: () => Config; } -export class ConfigManager> extends Manager> { - constructor({ getConfig }: Options) { +export class ConfigManager> extends Manager> { + constructor({ getConfig }: Options) { super({ getSource: () => flatten(getConfig()) }); } } diff --git a/src/env/env.manager.ts b/src/env/env.manager.ts index 1a1940e..b9a9e99 100644 --- a/src/env/env.manager.ts +++ b/src/env/env.manager.ts @@ -1,25 +1,25 @@ import { Manager } from 'src/utils/manager.utils.js'; -import { BaseConfig, AtLeastOne, EnvPicker } from './env.picker.js'; +import { BaseConfig, EnvPicker, EnvRecord } from './env.picker.js'; -interface Options { - getEnv?: () => C; - getNodeEnv?: () => E; +interface Options { + getEnv?: () => Env; + getNodeEnv?: () => NodeEnv; } -export class EnvManager extends Manager { +export class EnvManager extends Manager { private nodeEnv: string; - private cache = new Map>(); + private cache = new Map>(); constructor({ - getEnv = () => process.env as C, - getNodeEnv = () => (process.env.NODE_ENV ?? 'development') as E, - }: Options = {}) { + getEnv = () => process.env as Config, + getNodeEnv = () => (process.env.NODE_ENV ?? 'development') as NodeEnv, + }: Options = {}) { super({ getSource: getEnv }); this.nodeEnv = getNodeEnv(); } - private getEnvValue(key: K) { + private getEnvValue(key: Key) { const value = this.source[key]; if (value === null || value === undefined || value.trim() === '') { @@ -29,63 +29,66 @@ export class EnvManager ext return value; } - private getKeyOrThrow(envRecord: AtLeastOne>): K { - const key = envRecord[this.nodeEnv as E] as string | undefined; + private getKeyOrThrow(envRecord: EnvRecord) { + const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest; if (!key) { throw new Error(`key is not found empty in nodeEnv: ${this.nodeEnv}`); } - return key as K; + return key; } - public pick(key: K): EnvPicker { + public pick(key: Key) { if (!this.cache.has(key)) { - this.cache.set(key, new EnvPicker(this.getEnvValue(key), this.nodeEnv) as EnvPicker); + this.cache.set( + key, + new EnvPicker(this.getEnvValue(key), this.nodeEnv) as EnvPicker, + ); } - return this.cache.get(key) as EnvPicker; + return this.cache.get(key) as EnvPicker; } - public pickOrThrow(key: K): EnvPicker { + public pickOrThrow(key: Key) { const value = this.source[key]; if (value === null || value === undefined || value.trim() === '') { throw new Error(`key: ${key.toString()} is empty`); } - return this.pick(key) as EnvPicker; + return this.pick(key) as EnvPicker>; } - public pickFor(envRecord: AtLeastOne>): EnvPicker { - const key = envRecord[this.nodeEnv as E] as string | undefined; + public pickFor(envRecord: EnvRecord) { + const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest; if (!key) { - return new EnvPicker(undefined, this.nodeEnv) as EnvPicker; + return new EnvPicker(undefined, this.nodeEnv) as EnvPicker; } - return this.pick(key) as EnvPicker; + return this.pick(key) as EnvPicker; } - public pickForOrThrow(envRecord: AtLeastOne>): EnvPicker { + public pickForOrThrow(envRecord: EnvRecord) { const key = this.getKeyOrThrow(envRecord); - return this.pickOrThrow(key as K); + return this.pickOrThrow(key); } - public getFor(envRecord: AtLeastOne>) { - const key = envRecord[this.nodeEnv as E] as string | undefined; + public getFor(envRecord: EnvRecord) { + const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest; if (!key) { return; } - return this.get(key as K); + return this.get(key); } - public getForOrThrow(envRecord: AtLeastOne>) { - const key = this.getKeyOrThrow(envRecord); + public getForOrThrow(envRecord: EnvRecord) { + const key: Key | undefined = this.getKeyOrThrow(envRecord); - return this.getOrThrow(key as K); + return this.getOrThrow(key); } } diff --git a/src/env/env.picker.ts b/src/env/env.picker.ts index e2b0b15..6f9e011 100644 --- a/src/env/env.picker.ts +++ b/src/env/env.picker.ts @@ -1,53 +1,56 @@ -export type WrappedInEnvPickers = { - [K in keyof T]: EnvPicker; +export type WrappedInEnvPickers = { + [Key in keyof Config]: EnvPicker; }; -type MoveOptional = T extends null - ? T extends undefined - ? R | null | undefined - : R | null - : T extends undefined - ? R | undefined - : R; +type MoveOptional = Type extends null + ? Type extends undefined + ? Value | null | undefined + : Value | null + : Type extends undefined + ? Value | undefined + : Value; export type BaseConfig = Record; +export type EnvRecord = Partial>; -// original https://stackoverflow.com/a/59987826/15681288 -export type AtLeastOne = { [K in keyof T]: Pick }[keyof T]; - -export const wrapInEnvPickers = (config: C, nodeEnv: E) => { +export const wrapInEnvPickers = ( + config: Config, + nodeEnv: NodeEnv, +) => { return Object.keys(config).reduce((result, key) => { return { ...result, [key]: new EnvPicker(config[key], nodeEnv) }; - }, {} as WrappedInEnvPickers); + }, {} as WrappedInEnvPickers); }; -export class EnvPicker { - constructor(private state: S, private nodeEnv: E) {} +export class EnvPicker { + constructor(private state: State, private nodeEnv: NodeEnv) {} - public default(newState: NS): EnvPicker> { + public default(newState: State) { this.state ??= newState; - return this as unknown as EnvPicker>; + return this as unknown as EnvPicker>; } - public defaultFor(envRecord: AtLeastOne>) { - this.state ??= envRecord[this.nodeEnv] as S; + public defaultFor>(envRecord: E) { + this.state ??= (envRecord[this.nodeEnv] ?? envRecord.rest) as State; - return this; + return this as unknown as E extends { rest: State } + ? EnvPicker> + : EnvPicker; } - public map(mapper: (state: S) => R) { - this.state = mapper(this.state) as unknown as S; + public map(mapper: (state: State) => Result) { + this.state = mapper(this.state) as unknown as State; - return this as unknown as EnvPicker; + return this as unknown as EnvPicker; } - public mapIfExists(mapper: (state: NonNullable) => R) { + public mapIfExists(mapper: (state: NonNullable) => Result) { if (this.state !== null && this.state !== undefined) { - this.state = mapper(this.state as NonNullable) as unknown as S; + this.state = mapper(this.state as NonNullable) as unknown as State; } - return this as unknown as EnvPicker>; + return this as unknown as EnvPicker>; } public value() { diff --git a/src/utils/flatten.utils.ts b/src/utils/flatten.utils.ts index 9a52c1d..2e6b55c 100644 --- a/src/utils/flatten.utils.ts +++ b/src/utils/flatten.utils.ts @@ -1,22 +1,22 @@ type SupportedValues = bigint | boolean | null | number | string | symbol | undefined | Record; // original https://github.com/ghoullier/awesome-template-literal-types#dot-notation-string-type-safe -export type PathImpl = K extends string - ? T[K] extends Record - ? K | `${K}.${PathImpl}` - : K +export type PathImpl = Key extends string + ? Obj[Key] extends Record + ? Key | `${Key}.${PathImpl}` + : Key : never; export type Path = PathImpl | keyof T; -export type PathValue> = P extends `${infer K}.${infer Rest}` - ? K extends keyof T - ? Rest extends Path - ? PathValue +export type PathValue> = PathKey extends `${infer Key}.${infer Rest}` + ? Key extends keyof Obj + ? Rest extends Path + ? PathValue : never : never - : P extends keyof T - ? T[P] + : PathKey extends keyof Obj + ? Obj[PathKey] : never; export type Flatten = { diff --git a/src/utils/manager.utils.ts b/src/utils/manager.utils.ts index 3a80d16..abb4550 100644 --- a/src/utils/manager.utils.ts +++ b/src/utils/manager.utils.ts @@ -1,19 +1,19 @@ -interface Options { - getSource: () => C; +interface Options { + getSource: () => Source; } -export class Manager> { - protected source: C; +export class Manager> { + protected source: Source; - constructor({ getSource }: Options) { + constructor({ getSource }: Options) { this.source = getSource(); } - public get(key: K): C[K] { + public get(key: Key) { return this.source[key]; } - public getOrThrow(key: K): NonNullable { + public getOrThrow(key: Key) { const value = this.source[key]; if (value === null || value === undefined) {