From bb73162dac6189b571c93bc61007d99d346c42a4 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Wed, 13 Nov 2024 15:21:56 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20port=20string=20to=20numb?= =?UTF-8?q?er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/lib/applications.config.spec.ts | 16 +++--- libs/config/src/lib/applications.config.ts | 51 ++++++++++++------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/libs/config/src/lib/applications.config.spec.ts b/libs/config/src/lib/applications.config.spec.ts index 5628a4b..d7f9532 100644 --- a/libs/config/src/lib/applications.config.spec.ts +++ b/libs/config/src/lib/applications.config.spec.ts @@ -2,16 +2,16 @@ import { gatewayConfig, userAppConfig } from './applications.config'; describe('Config Tests', () => { describe('gatewayConfig', () => { - it('should return default values when environment variables are not set', () => { + it('should return default gateway config when no environment variables are set', () => { const config = gatewayConfig(); expect(config).toEqual({ protocol: 'http', host: 'localhost', - port: '3333', + port: 3333, }); }); - it('should return environment variable values when they are set', () => { + it('should return gateway config with environment variables', () => { process.env['PROTOCOL'] = 'https'; process.env['GATEWAY_HOST'] = 'gateway.example.com'; process.env['GATEWAY_PORT'] = '4444'; @@ -20,7 +20,7 @@ describe('Config Tests', () => { expect(config).toEqual({ protocol: 'https', host: 'gateway.example.com', - port: '4444', + port: 4444, }); delete process.env['PROTOCOL']; @@ -30,17 +30,17 @@ describe('Config Tests', () => { }); describe('userAppConfig', () => { - it('should return default values when environment variables are not set', () => { + it('should return default user app config when no environment variables are set', () => { const config = userAppConfig(); expect(config).toEqual({ protocol: 'http', host: 'localhost', - port: '15001', + port: 15001, name: 'user', }); }); - it('should return environment variable values when they are set', () => { + it('should return user app config with environment variables', () => { process.env['PROTOCOL'] = 'https'; process.env['USER_HOST'] = 'user.example.com'; process.env['USER_PORT'] = '5555'; @@ -49,7 +49,7 @@ describe('Config Tests', () => { expect(config).toEqual({ protocol: 'https', host: 'user.example.com', - port: '5555', + port: 5555, name: 'user', }); diff --git a/libs/config/src/lib/applications.config.ts b/libs/config/src/lib/applications.config.ts index 7aafa77..8c0c2ea 100644 --- a/libs/config/src/lib/applications.config.ts +++ b/libs/config/src/lib/applications.config.ts @@ -1,22 +1,39 @@ import { registerAs } from '@nestjs/config'; -const DEFAULT_PROTOCOL = 'http'; -const DEFAULT_HOST = 'localhost'; +interface ServiceConfig { + protocol: string; + host: string; + port: number; + name?: string; +} + +const DEFAULT_PROTOCOL = 'http' as const; +const DEFAULT_HOST = 'localhost' as const; const DEFAULT_PORT = { - user: '15001', - task: '15002', - gateway: '3333', -}; + user: 15001, + task: 15002, + gateway: 3333, +} as const; -export const gatewayConfig = registerAs('gateway', () => ({ - protocol: process.env['PROTOCOL'] ?? DEFAULT_PROTOCOL, - host: process.env['GATEWAY_HOST'] ?? DEFAULT_HOST, - port: process.env['GATEWAY_PORT'] ?? DEFAULT_PORT.gateway, -})); +export const gatewayConfig = registerAs( + 'gateway', + (): ServiceConfig => ({ + protocol: process.env['PROTOCOL'] ?? DEFAULT_PROTOCOL, + host: process.env['GATEWAY_HOST'] ?? DEFAULT_HOST, + port: process.env['GATEWAY_PORT'] + ? Number(process.env['GATEWAY_PORT']) + : DEFAULT_PORT.gateway, + }), +); -export const userAppConfig = registerAs('userApp', () => ({ - protocol: process.env['PROTOCOL'] ?? DEFAULT_PROTOCOL, - host: process.env['USER_HOST'] ?? DEFAULT_HOST, - port: process.env['USER_PORT'] ?? DEFAULT_PORT.user, - name: 'user', -})); +export const userAppConfig = registerAs( + 'userApp', + (): ServiceConfig => ({ + protocol: process.env['PROTOCOL'] ?? DEFAULT_PROTOCOL, + host: process.env['USER_HOST'] ?? DEFAULT_HOST, + port: process.env['USER_PORT'] + ? Number(process.env['USER_PORT']) + : DEFAULT_PORT.user, + name: 'user', + }), +);