UNPKG

jsm-core

Version:
359 lines (358 loc) 13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const dotenv_1 = __importDefault(require("dotenv")); // must be loaded before other config files const envFound = dotenv_1.default.config(); const node_1 = require("jsm-utilities/dist/node"); const db_config_1 = __importDefault(require("./db.config")); const services_config_1 = require("./services.config"); const cache_config_1 = __importDefault(require("./cache.config")); const date_constants_1 = require("../../constants/date.constants"); // Set the NODE_ENV to 'development' by default process.env.NODE_ENV = process.env.NODE_ENV || "development"; if (envFound.error) { // This error should crash whole process // throw new exceptions.InternalServerError("Couldn't find .env file"); } const SERVICE_NAME = (0, node_1.getEnv)('SERVICE_NAME', '').toLowerCase(); const SERVICES_API_PREFIX = (0, node_1.getEnv)('SYSTEM_API_PREFIX', '/api/v1'); let internalGateway = (0, node_1.getEnv)('INTERNAL_GATEWAY_URL', `http://localhost:${services_config_1.SERVICE_PORTS.DEFAULT}`); if (!internalGateway.endsWith('/')) { internalGateway += '/'; } const serviceHosts = services_config_1.SERVICES.reduce((prev, service) => (Object.assign(Object.assign({}, prev), { [service]: (0, node_1.getEnv)(`SERVICE_HOST_${service.toUpperCase()}`, 'localhost') })), {}); const serviceApiUrls = services_config_1.SERVICES.reduce((prev, service) => (Object.assign(Object.assign({}, prev), { [service]: `${internalGateway}${service}${SERVICES_API_PREFIX}` })), {}); const config = { /** * Environement */ environement: process.env.NODE_ENV === "production" ? "production" : "development", get isDev() { return process.env.NODE_ENV !== "production"; }, get isProd() { return process.env.NODE_ENV === "production"; }, /** * Development mode */ dev: { master: { email: (0, node_1.getEnv)('DEV_MASTER_EMAIL', ''), password: (0, node_1.getEnv)('DEV_MASTER_PASSWORD', ''), }, faker: { userPassword: (0, node_1.getEnv)('DEV_FAKER_USER_PASSWORD', '123456'), }, debugProduction: (0, node_1.getEnv)('DEBUG_PRODUCTION', false), }, /** * Configuration for logging. */ logging: { /** * Whether to log duplicate errors or not. */ log_duplicate_errors: (0, node_1.getEnv)('LOG_DUPLICATE_ERRORS', false), /** * The log level. */ level: (0, node_1.getEnv)('LOG_LEVEL', 'silly'), }, /** * Configuration for Agenda.js. */ agenda: { /** * The name of the database collection used by Agenda.js. */ dbCollection: (0, node_1.getEnv)('AGENDA_DB_COLLECTION'), /** * The pool time for Agenda.js. */ pooltime: (0, node_1.getEnv)('AGENDA_POOL_TIME'), /** * The concurrency for Agenda.js. */ concurrency: (0, node_1.getEnv)('AGENDA_CONCURRENCY', 3), /** * Configuration for Agendash. */ agendash: { /** * Whether Agendash is enabled or not. */ enabled: false, /** * The username for Agendash. */ user: "agendash", /** * The password for Agendash. */ password: "123456", }, }, /** * Configuration for amqplib. */ amqplib: { /** * The host for amqplib. */ host: (0, node_1.getEnv)('AMQPLIB_HOST', 'localhost'), /** * The port for amqplib. */ port: (0, node_1.getEnv)('AMQPLIB_PORT', 5672), /** * The retry delay for amqplib. */ retryDelay: (0, node_1.getEnv)('AMQPLIB_RETRY_DELAY', 5000), /** * The maximum retries for amqplib. */ maxRetries: (0, node_1.getEnv)('AMQPLIB_MAX_RETRIES', 5), user: (0, node_1.getEnv)('AMQPLIB_USER'), password: (0, node_1.getEnv)('AMQPLIB_PASS'), }, /** * Configuration for email services. */ emails: { /** * Configuration for Mailgun. */ mailgun: { /** * The API key for Mailgun. */ apiKey: (0, node_1.getEnv)('MAILGUN_API_KEY'), /** * The API username for Mailgun. */ apiUsername: (0, node_1.getEnv)('MAILGUN_USERNAME'), /** * The domain for Mailgun. */ domain: (0, node_1.getEnv)('MAILGUN_DOMAIN'), }, /** * Configuration for Nodemailer. */ nodemailer: { /** * The service for Nodemailer. */ service: 'Gmail', /** * The user for Nodemailer. */ user: (0, node_1.getEnv)('NODEMAILER_USER'), /** * The password for Nodemailer. */ pass: (0, node_1.getEnv)('NODEMAILER_PASSWORD'), }, }, /** * Configuration for security-related settings. */ security: { /** * The secret used for internal service communication. */ internalServiceSecret: (0, node_1.getEnv)('INTERNAL_SERVICE_SECRET', ''), /** * The secret used for external service communication. */ externalServiceSecret: (0, node_1.getEnv)('EXTERNAL_SERVICE_SECRET', ''), /** * The secret used for frontend service communication. */ frontendServiceSecret: (0, node_1.getEnv)('FRONTEND_SERVICE_SECRET', ''), /** * JWT Configuration */ jwt: { /** * The secret used for JSON Web Token (JWT) generation and verification. */ secret: (0, node_1.getEnv)('JWT_SECRET', 'secret'), /** * The algorithm used for JSON Web Token (JWT) generation and verification. */ algorithm: (0, node_1.getEnv)('JWT_ALGO'), /** * @description The expiration for JSON Web Token (JWT). * - The time range must be specified with a number followed by a unit of time (e.g. "5 minutes", "3 hours", "1 day") * - Valid units of time are "second", "minute", "hour", "day", "week", "month", and "year" * - Valid units of time can be abbreviated to "s", "m", "h", "d", "w", "M", and "y" * - If no unit of time is specified, the default unit is "day" * - If the unit of time is plural (e.g. "days"), it must be pluralized using an "s" * - If the unit of time is abbreviated, it must be followed by an "s" * - The time range string must be in the format "[number] [unit of time]s?" * - For example, "5 minutes" is valid, but "5 minute" is not valid, and "5 m" is not valid either * - If the unit of time is plural (e.g. "days"), it must be pluralized using an "s" * - This function return {false} if the time range string is not in the correct format */ tokenExpiration: (0, node_1.getEnv)('JWT_EXPIRES_IN', '1d'), /** * @description The expiration for JSON Web Refresh Token (JWT). * - The time range must be specified with a number followed by a unit of time (e.g. "5 minutes", "3 hours", "1 day") * - Valid units of time are "second", "minute", "hour", "day", "week", "month", and "year" * - Valid units of time can be abbreviated to "s", "m", "h", "d", "w", "M", and "y" * - If no unit of time is specified, the default unit is "day" * - If the unit of time is plural (e.g. "days"), it must be pluralized using an "s" * - If the unit of time is abbreviated, it must be followed by an "s" * - The time range string must be in the format "[number] [unit of time]s?" * - For example, "5 minutes" is valid, but "5 minute" is not valid, and "5 m" is not valid either * - If the unit of time is plural (e.g. "days"), it must be pluralized using an "s" * - This function return {false} if the time range string is not in the correct format */ refreshTokenExpiration: (0, node_1.getEnv)('JWT_REFRESH_EXPIRES_IN', '7d'), }, }, /** * Configuration for logging. */ activityLog: { /** * The secret key for logging. */ secretKey: (0, node_1.getEnv)('INTEGRATION_LUP_LOG_SECRET', ''), /** * The URL for logging. */ url: `${(0, node_1.getEnv)('INTERNAL_GATEWAY_URL', `http://localhost:${services_config_1.SERVICE_PORTS.DEFAULT}`)}/activity/api/logs`, }, /** * Configuration for HTTP. */ http: { /** * The body size limit for HTTP. */ bodySizeLimit: (0, node_1.getEnv)('SYSTEM_HTTP_BODY_SIZE_LIMIT', '10MB'), services: { /** * @description Configuration for services with '/api/v2' path. */ api: serviceApiUrls, hosts: serviceHosts, }, /** * Configuration for the API. */ api: { /** * The prefix for the API. */ prefix: SERVICES_API_PREFIX, }, /** * SDK Configuration */ sdk: { baseURL: (0, node_1.getEnv)('SDK_BASE_URL', `http://localhost:${services_config_1.SERVICE_PORTS.DEFAULT}`), appId: (0, node_1.getEnv)('SDK_APP_ID', ''), appSecret: (0, node_1.getEnv)('SDK_APP_SECRET', ''), debug: (0, node_1.getEnv)('SDK_DEBUG', false), }, upload: { /** * The maximum file size for uploads. * Default is 16MB. */ maxFileSize: (0, node_1.getEnv)('SYSTEM_HTTP_MAX_FILE_SIZE', 16777216), }, }, /** * Configuration for settings. */ settings: { preferences: { /** * The default language for the application. */ defaultLanguage: (0, node_1.getEnv)('SYSTEM_DEFAULT_LANGUAGE', 'en'), }, /** * Configuration for tracking. */ tracking: { /** * The suffix length for tracking. */ suffixLength: (0, node_1.getEnv)('SYSTEM_TRACKING_SUFFIX_LENGTH', 6), }, /** * Configuration for items listing. */ listing: { /** * The default count for items listing. */ defaultCount: (0, node_1.getEnv)('SYSTEM_LISTING_DEFAULT_COUNT', 24), }, }, /** * Configuration for the current service. */ currentService: { /** * The name of the current service. */ name: SERVICE_NAME, /** * The port of the current service. */ port: (0, node_1.getEnv)('PORT', 3000), /** * The database for the current service. */ db: (0, db_config_1.default)(), }, cacheManager: { redis: { /** * The URL for the Redis cache manager. */ url: (0, node_1.getEnv)('CACHE_MANAGER_REDIS_URL', 'redis://localhost:6379'), /** * The port for the Redis cache manager. */ port: (0, node_1.getEnv)('CACHE_MANAGER_REDIS_PORT', 6379), /** * */ user: (0, node_1.getEnv)('CACHE_MANAGER_REDIS_USER', null) || null, /** * The password for the Redis cache manager. */ password: (0, node_1.getEnv)('CACHE_MANAGER_REDIS_PASSWORD', null) || null, }, keyPrefix: (0, node_1.getEnv)('CACHE_MANAGER_KEY_PREFIX', 'LUP_V2:'), /** * @description All values in seconds */ cacheDuration: cache_config_1.default, getDuration(key) { let result = cache_config_1.default.dev || date_constants_1.SECONDS_IN_A_MINUTE; if (process.env.NODE_ENV !== 'production') return result; let obj = cache_config_1.default; for (const k of key.split('.')) { obj = obj[k] && typeof obj[k] === 'object' ? obj[k] : obj; } result = typeof obj === 'number' ? obj : result; return result; }, }, }; exports.default = config;