@tmlmobilidade/lib
Version:
159 lines (158 loc) • 6.4 kB
JavaScript
/* * */
import { getCurrentEnvironment } from '@tmlmobilidade/types';
/* * */
const DEFAULT_NON_DEV_CONFIG = {
api_port: 5050,
cookie_domain: '.sae.carrismetropolitana.pt',
cors_origin: new RegExp(`https://.*\\.sae\\.carrismetropolitana\\.pt$`),
frontend_port: 3000,
};
const APP_CONFIGS = {
alerts: {
development: {
api_port: 52001,
api_url: 'http://localhost:52001',
cookie_domain: 'localhost',
cors_origin: true,
frontend_port: 51001,
frontend_url: 'http://localhost:51001',
},
production: {
api_url: 'https://alerts.sae.carrismetropolitana.pt/api',
frontend_url: 'https://alerts.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
staging: {
api_url: 'https://staging.alerts.sae.carrismetropolitana.pt/api',
frontend_url: 'https://staging.alerts.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
},
auth: {
development: {
api_port: 52000,
api_url: 'http://localhost:52000',
cookie_domain: 'localhost',
cors_origin: true,
frontend_port: 51000,
frontend_url: 'http://localhost:51000',
},
production: {
api_url: 'https://auth.sae.carrismetropolitana.pt/api',
frontend_url: 'https://auth.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
staging: {
api_url: 'https://auth.sae.carrismetropolitana.pt/api',
frontend_url: 'https://auth.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
},
controller: {
development: {
api_port: 52002,
api_url: 'http://localhost:52002',
cookie_domain: 'localhost',
cors_origin: true,
frontend_port: 51002,
frontend_url: 'http://localhost:51002',
},
production: {
api_url: 'https://controller.sae.carrismetropolitana.pt/api',
frontend_url: 'https://controller.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
staging: {
api_url: 'https://staging.controller.sae.carrismetropolitana.pt/api',
frontend_url: 'https://staging.controller.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
},
locations: {
development: {
api_port: 52005,
api_url: 'http://localhost:52005',
cookie_domain: 'localhost',
cors_origin: true,
frontend_port: 51005,
frontend_url: 'http://localhost:51005',
},
production: {
api_url: 'https://locations.sae.carrismetropolitana.pt/api',
frontend_url: 'https://locations.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
staging: {
api_url: 'https://staging.locations.sae.carrismetropolitana.pt/api',
frontend_url: 'https://staging.locations.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
},
plans: {
development: {
api_port: 52004,
api_url: 'http://localhost:52004',
cookie_domain: 'localhost',
cors_origin: true,
frontend_port: 51004,
frontend_url: 'http://localhost:51004',
},
production: {
api_url: 'https://plans.sae.carrismetropolitana.pt/api',
frontend_url: 'https://plans.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
staging: {
api_url: 'https://staging.plans.sae.carrismetropolitana.pt/api',
frontend_url: 'https://staging.plans.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
},
stops: {
development: {
api_port: 52003,
api_url: 'http://localhost:52003',
cookie_domain: 'localhost',
cors_origin: true,
frontend_port: 51003,
frontend_url: 'http://localhost:51003',
},
production: {
api_url: 'https://stops.sae.carrismetropolitana.pt/api',
frontend_url: 'https://stops.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
staging: {
api_url: 'https://staging.stops.sae.carrismetropolitana.pt/api',
frontend_url: 'https://staging.stops.sae.carrismetropolitana.pt',
...DEFAULT_NON_DEV_CONFIG,
},
},
};
/* * */
/**
* Retrieves the value of a specific property from the app configuration for a given app and environment.
* @param app The app ID.
* @param property The property of the app configuration to retrieve (e.g., 'api_url', 'frontend_url').
* @param environment The environment to get the property for. If not provided, it will use the ENVIRONMENT environment variable.
* @returns The value of the specified property for the given app and environment.
*/
export function getAppConfig(app, property, environment) {
// Get the desired app object
const appObject = APP_CONFIGS[app];
if (!appObject)
throw new Error(`[@core/lib] App Config Object for "${app}" app not found. Available apps: ${Object.keys(APP_CONFIGS).join(', ')}`);
// Extract the current app environment either from the parameter
// or automatically from the set environment variable.
const currentEnvironment = environment || getCurrentEnvironment();
// Get the config group for the current environment
const configGroupForEnvironment = appObject[currentEnvironment];
if (!configGroupForEnvironment)
throw new Error(`[@core/lib] AppConfig group for app "${app}" in environment "${currentEnvironment}" environment not found. Available environments: ${Object.keys(appObject).join(', ')}`);
// Get the property value from the config group
const propertyValue = configGroupForEnvironment[property];
if (propertyValue === undefined)
throw new Error(`[@core/lib] Property "${property}" for app "${app}" in environment "${currentEnvironment}" not found. Available properties: ${Object.keys(configGroupForEnvironment).join(', ')}`);
// Return the value
return propertyValue;
}