diff --git a/.env.example b/.env.example index e69de29..6331415 100644 --- a/.env.example +++ b/.env.example @@ -0,0 +1,4 @@ +NODE_ENV= +PORT= +MONGO_URI= +JWT_SECRET= \ No newline at end of file diff --git a/package.json b/package.json index 6890c4a..d292577 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "order-service", "version": "1.0.0", "description": "", + "type": "module", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" diff --git a/src/config/env.ts b/src/config/env.ts index e69de29..a3aa3c4 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -0,0 +1,25 @@ +import dotenv from "dotenv"; + +dotenv.config(); + +function requireEnv(key: string): string { + const value = process.env[key]; + + if (!value) { + throw new Error(`Environment variable ${key} is required but not set.`); + } + + return value; +} + +export const env = { + nodeEnv: process.env.NODE_ENV ?? "development", + port: Number(process.env.PORT) ?? 3000, + + mongoUri: requireEnv("MONGO_URI"), + + jwtSecret: { + secret: requireEnv("JWT_SECRET"), + expiresIn: process.env.JWT_EXPIRES_IN ?? "1h", + }, +};