import { Amplify, Auth } from 'aws-amplify'; export type EnvironmentStage = 'dev' | 'prod' export type EnvironmentTarget = 'local' | 'cloud' | 'off' export type EnvironmentEndpoint = { dev: string, prod: string, local: string } export type EnvironmentConfig = { 'stage': EnvironmentStage, 'region': string, 'modules': { [moduleName: string]: { [serviceName: string]: EnvironmentTarget } }, 'auth': { 'user-pool': { 'id': string, 'web-client-id': string, 'identity-id': string, }, 'domain': string, 'scope': string[], 'attributes': { 'username': string[], 'signup': string[], }, 'provider': string[], 'federation-target': string, 'mfa-configuration': 'ON' | 'OFF', 'response-type': string, 'sign-in': EnvironmentEndpoint, 'sign-out': EnvironmentEndpoint, }, 'API': { [moduleName: string]: { [serviceName: string]: { 'name': string, 'endpoints': EnvironmentEndpoint } } } 'APP SYNC': { 'stages': { dev: string, prod: string, }, 'endpoint': string, 'authentication-type': string, }, 'STORAGE': { 'stages': { dev: string, prod: string, }, 'bucket': string, } } const isLocalhost = Boolean( window.location.hostname === "localhost" || window.location.hostname === "[::1]" || window.location.hostname.match( /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ ) ) let app_environment: EnvironmentConfig = null; export function configure(environment: EnvironmentConfig) { app_environment = environment; const apiEnpoints = () => { const result: any[] = []; Object.keys(environment.API).forEach(moduleName => { const module = environment.API[moduleName]; Object.keys(module).forEach(serviceName => { const service = module[serviceName]; const target = environment.modules[moduleName] ? environment.modules[moduleName][serviceName] : 'cloud'; if (target !== 'off') { result.push({ name: service.name, endpoint: target === 'cloud' ? isLocalhost || environment.stage === 'dev' ? service.endpoints.dev : service.endpoints.prod : service.endpoints.local, custom_header: async () => { return { Authorization: `Bearer ${(await Auth.currentSession()).getIdToken().getJwtToken()}` } } }) } }) }) return result; } Amplify.configure({ aws_project_region: environment.region, aws_cognito_region: environment.region, aws_user_pools_id: environment.auth['user-pool'].id, aws_user_pools_web_client_id: environment.auth['user-pool']['web-client-id'], Auth: { region: environment.region, userPoolId: environment.auth['user-pool'].id, identityPoolId: environment.auth['user-pool']['identity-id'], userPoolWebClientId: environment.auth['user-pool']['web-client-id'], }, oauth: { domain: environment.auth.domain, scope: environment.auth.scope, redirectSignIn: isLocalhost ? environment.auth['sign-in'].local : isLocalhost || environment.stage === 'dev' ? environment.auth['sign-in'].dev : environment.auth['sign-in'].prod, redirectSignOut: isLocalhost ? environment.auth['sign-out'].local : isLocalhost || environment.stage === 'dev' ? environment.auth['sign-out'].dev : environment.auth['sign-out'].prod, responseType: environment.auth['response-type'] }, federationTarget: environment.auth['federation-target'], aws_cognito_username_attributes: environment.auth.attributes.username, aws_cognito_social_providers: environment.auth.provider, aws_cognito_signup_attributes: environment.auth.attributes.signup, aws_cognito_mfa_configuration: environment.auth['mfa-configuration'], aws_appsync_region: environment.region, aws_appsync_graphqlEndpoint: environment['APP SYNC'].endpoint, aws_appsync_authenticationType: environment['APP SYNC']['authentication-type'], API: { endpoints: apiEnpoints() }, Storage: { AWSS3: { region: environment.region, bucket: environment.STORAGE.bucket, identityPoolId: environment.auth['user-pool']['identity-id'], } } }) } export class App { public static get IsLocalhost(): boolean { return isLocalhost; } public static get Environment(): EnvironmentConfig { return app_environment; } public static get EnvironmentStage(): string { return !isLocalhost && app_environment && app_environment.stage ? app_environment.stage : 'dev'; } public static get Stage(): string { const env = App.Environment; const stage = isLocalhost || !env ? 'dev' : env.stage const appsync = env && env['APP SYNC']; if (appsync && appsync.stages && appsync.stages[stage]) { return appsync.stages[stage]; } const storage = env && env.STORAGE; if (env && storage && storage.stages && storage.stages[stage]) { return storage.stages[stage]; } return stage; } }