Skip to content

Commit

Permalink
fix: 🐛 port string to number
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 13, 2024
1 parent 66c0681 commit bb73162
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
16 changes: 8 additions & 8 deletions libs/config/src/lib/applications.config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,7 +20,7 @@ describe('Config Tests', () => {
expect(config).toEqual({
protocol: 'https',
host: 'gateway.example.com',
port: '4444',
port: 4444,
});

delete process.env['PROTOCOL'];
Expand All @@ -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';
Expand All @@ -49,7 +49,7 @@ describe('Config Tests', () => {
expect(config).toEqual({
protocol: 'https',
host: 'user.example.com',
port: '5555',
port: 5555,
name: 'user',
});

Expand Down
51 changes: 34 additions & 17 deletions libs/config/src/lib/applications.config.ts
Original file line number Diff line number Diff line change
@@ -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',
}),
);

0 comments on commit bb73162

Please sign in to comment.