From 0d301f5c4955e9a85b07fae23f461f4b42708b94 Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 28 Jul 2025 23:39:11 +0200 Subject: [PATCH] update config.ts with variables to implement rateLiiting, apiVersioning and client userAgent --- src/config/config.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/config/config.ts b/src/config/config.ts index dec4b91..0b0851c 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -2,6 +2,11 @@ export interface IConfig { port: number; mongodbUri: string; nodeEnv: string; + apiVersion: string; + rateLimitWindowMs: number; + rateLimitMaxRequests: number; + requestTimeoutMs: number; + userAgent: string; } class Config implements IConfig { @@ -10,12 +15,21 @@ class Config implements IConfig { public readonly port: number; public readonly mongodbUri: string; public readonly nodeEnv: string; - + public readonly apiVersion: string; + public readonly rateLimitWindowMs: number; + public readonly rateLimitMaxRequests: number; + public readonly requestTimeoutMs: number; + public readonly userAgent: string; private constructor() { - this.port = parseInt(process.env.PORT || '4000', 10); + this.port = parseInt(process.env.PORT || '3000', 10); this.mongodbUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/dailytrends'; this.nodeEnv = process.env.NODE_ENV || 'development'; + this.apiVersion = process.env.API_VERSION || 'v1'; + this.rateLimitWindowMs = parseInt(process.env.RATE_LIMIT_WINDOW_MS || '900000', 10); + this.rateLimitMaxRequests = parseInt(process.env.RATE_LIMIT_MAX_REQUESTS || '100', 10); + this.requestTimeoutMs = parseInt(process.env.REQUEST_TIMEOUT_MS || '10000', 10); + this.userAgent = process.env.USER_AGENT || 'DailyTrends-Bot/1.0'; this.validateConfig(); } @@ -31,6 +45,22 @@ class Config implements IConfig { if (!this.mongodbUri) { throw new Error('MONGODB_URI is required'); } + + if (this.port < 1 || this.port > 65535) { + throw new Error('PORT must be between 1 and 65535'); + } + + if (this.rateLimitWindowMs < 1000) { + throw new Error('RATE_LIMIT_WINDOW_MS must be at least 1000ms'); + } + + if (this.rateLimitMaxRequests < 1) { + throw new Error('RATE_LIMIT_MAX_REQUESTS must be at least 1'); + } + + if (this.requestTimeoutMs < 1000) { + throw new Error('REQUEST_TIMEOUT_MS must be at least 1000ms'); + } } public isDevelopment(): boolean {