update config.ts with variables to implement rateLiiting, apiVersioning and client userAgent
This commit is contained in:
@ -2,6 +2,11 @@ export interface IConfig {
|
|||||||
port: number;
|
port: number;
|
||||||
mongodbUri: string;
|
mongodbUri: string;
|
||||||
nodeEnv: string;
|
nodeEnv: string;
|
||||||
|
apiVersion: string;
|
||||||
|
rateLimitWindowMs: number;
|
||||||
|
rateLimitMaxRequests: number;
|
||||||
|
requestTimeoutMs: number;
|
||||||
|
userAgent: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Config implements IConfig {
|
class Config implements IConfig {
|
||||||
@ -10,12 +15,21 @@ class Config implements IConfig {
|
|||||||
public readonly port: number;
|
public readonly port: number;
|
||||||
public readonly mongodbUri: string;
|
public readonly mongodbUri: string;
|
||||||
public readonly nodeEnv: 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() {
|
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.mongodbUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/dailytrends';
|
||||||
this.nodeEnv = process.env.NODE_ENV || 'development';
|
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();
|
this.validateConfig();
|
||||||
}
|
}
|
||||||
@ -31,6 +45,22 @@ class Config implements IConfig {
|
|||||||
if (!this.mongodbUri) {
|
if (!this.mongodbUri) {
|
||||||
throw new Error('MONGODB_URI is required');
|
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 {
|
public isDevelopment(): boolean {
|
||||||
|
Reference in New Issue
Block a user