From abb73f34b13da99f2bcb2c24a1a19a1250ab75d9 Mon Sep 17 00:00:00 2001 From: Khunakorn Johnson Date: Tue, 22 Oct 2024 17:35:55 +0700 Subject: [PATCH] Feat: Use typesafe config --- package.json | 1 + src/infra/configuration/config.ts | 33 ++++++++++++----- .../persistence/typeorm/typeorm.module.ts | 4 ++- yarn.lock | 36 +++++-------------- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 3431b68..97b7673 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "class-validator": "^0.14.1", "dotenv": "^16.4.5", "dotenv-cli": "^7.4.2", + "env-var": "^7.5.0", "module-alias": "^2.2.3", "oxide.ts": "^1.1.0", "pg": "^8.12.0", diff --git a/src/infra/configuration/config.ts b/src/infra/configuration/config.ts index a8280bc..8b264fe 100644 --- a/src/infra/configuration/config.ts +++ b/src/infra/configuration/config.ts @@ -1,14 +1,31 @@ -export default () => ({ +import { get } from 'env-var'; + +export interface AppConfig { app: { - nodeEnv: process.env.NODE_ENV || 'develop', + nodeEnv: string; + prefix: string; + port: number; + }; + database: { + host: string; + username: string; + password: string; + port: number; + name: string; + }; +} + +export default (): AppConfig => ({ + app: { + nodeEnv: get('NODE_ENV').default('develop').asString(), prefix: 'api', - port: parseInt(process.env.APP_PORT, 10) || 3000, + port: get('APP_PORT').default(3000).asPortNumber(), }, database: { - host: process.env.DB_HOST, - username: process.env.DB_USERNAME, - password: process.env.DB_PASSWORD, - port: parseInt(process.env.DB_PORT, 10) || 5432, - name: process.env.DB_NAME, + host: get('DB_HOST').required().asString(), + username: get('DB_USERNAME').required().asString(), + password: get('DB_PASSWORD').required().asString(), + port: get('DB_PORT').default(5432).asPortNumber(), + name: get('DB_NAME').required().asString(), }, }); diff --git a/src/infra/persistence/typeorm/typeorm.module.ts b/src/infra/persistence/typeorm/typeorm.module.ts index 97439c5..42d952c 100644 --- a/src/infra/persistence/typeorm/typeorm.module.ts +++ b/src/infra/persistence/typeorm/typeorm.module.ts @@ -9,6 +9,8 @@ import { IOrderRepository } from '@core/domain/order/repository/order.repository import { IProductRepository } from '@core/domain/product/repository/product.repository'; import { IUserRepository } from '@core/domain/user/repository/user.repository'; +import { AppConfig } from '@infra/configuration/config'; + import { Order, Product, User } from './entity'; import { OrderRepository, @@ -20,7 +22,7 @@ export const DataSourceProvider: Provider = { provide: DataSource, inject: [ConfigService], useFactory: async (configService: ConfigService) => { - const dbConfig = configService.get('database'); + const dbConfig = configService.get('database'); const dataSource = await getInitializedDataSource(dbConfig); return addTransactionalDataSource(dataSource); }, diff --git a/yarn.lock b/yarn.lock index afd3848..d78a9d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2322,6 +2322,11 @@ enhanced-resolve@^5.0.0, enhanced-resolve@^5.17.0, enhanced-resolve@^5.7.0: graceful-fs "^4.2.4" tapable "^2.2.0" +env-var@^7.5.0: + version "7.5.0" + resolved "https://registry.yarnpkg.com/env-var/-/env-var-7.5.0.tgz#5f0c2d999d2997eaf7cf7f1437b3f857b865bf48" + integrity sha512-mKZOzLRN0ETzau2W2QXefbFjo5EF4yWq28OyKb9ICdeNhHJlOE/pHHnz4hdYJ9cNZXcJHo5xN4OT4pzuSHSNvA== + envix@^1.5.0: version "1.5.0" resolved "https://registry.yarnpkg.com/envix/-/envix-1.5.0.tgz#bbb7ceba6f098a272ef2044e606789844d08c1db" @@ -4836,16 +4841,7 @@ string-length@^4.0.1: char-regex "^1.0.2" strip-ansi "^6.0.0" -"string-width-cjs@npm:string-width@^4.2.0": - version "4.2.3" - resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" - integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== - dependencies: - emoji-regex "^8.0.0" - is-fullwidth-code-point "^3.0.0" - strip-ansi "^6.0.1" - -string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: +"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -4877,14 +4873,7 @@ string_decoder@~1.1.1: dependencies: safe-buffer "~5.1.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1": - version "6.0.1" - resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" - integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== - dependencies: - ansi-regex "^5.0.1" - -strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -5410,7 +5399,7 @@ word-wrap@^1.2.5: resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34" integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== @@ -5428,15 +5417,6 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0: string-width "^4.1.0" strip-ansi "^6.0.0" -wrap-ansi@^7.0.0: - version "7.0.0" - resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" - integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== - dependencies: - ansi-styles "^4.0.0" - string-width "^4.1.0" - strip-ansi "^6.0.0" - wrap-ansi@^8.1.0: version "8.1.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"