Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add /client/env API route for sending client env vars as JSON #3505

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions shared/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,20 @@ export interface PrivateFormErrorDto extends ErrorDto {
isPageFound: true
formTitle: string
}

// List of env vars expected from the server that client uses
export type ClientEnvVars = {
isGeneralMaintenance: string
isLoginBanner: string
siteBannerContent: string
adminBannerContent: string
logoBucketUrl: string // S3 bucket
formsgSdkMode: 'staging' | 'production' | 'development' | 'test'
captchaPublicKey: string // Recaptcha
sentryConfigUrl: string // Sentry.IO
isSPMaintenance: string // Singpass maintenance message
isCPMaintenance: string // Corppass maintenance message
myInfoBannerContent: string // MyInfo maintenance message
GATrackingID: string | null
spcpCookieDomain: string // Cookie domain used for removing spcp cookies
}
13 changes: 13 additions & 0 deletions src/app/modules/frontend/frontend.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import ejs from 'ejs'
import { StatusCodes } from 'http-status-codes'

import { ClientEnvVars } from '../../../../shared/types/core'
import { createLoggerWithLabel } from '../../config/logger'
import { createReqMeta } from '../../utils/request'
import { ControllerHandler } from '../core/core.types'

import { validateGenerateRedirectParams } from './frontend.middlewares'
import { getClientEnvVars } from './frontend.service'

const logger = createLoggerWithLabel(module)

Expand Down Expand Up @@ -77,6 +79,17 @@ export const addEnvVarData: ControllerHandler<unknown, { message: string }> = (
}
}

/**
* Handler for GET /frontend/env endpoint.
* @returns the environment variables needed to hydrate the frontend.
*/
export const handleGetEnvironment: ControllerHandler<never, ClientEnvVars> = (
_req,
res,
) => {
return res.json(getClientEnvVars())
}

/**
* Handler for GET /frontend/redirect endpoint.
* @param req - Express request object
Expand Down
4 changes: 4 additions & 0 deletions src/app/modules/frontend/frontend.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as FrontendServerController from './frontend.controller'
export const FrontendRouter = Router()

/**
* @deprecated use routes in src/app/routes/api/v3/client/client.routes.ts instead
* Generate the templated Javascript code for the frontend to initialise Google Tag Manager
* Code depends on whether googleAnalyticsFeature.isEnabled
* @route GET /frontend/datalayer
Expand All @@ -17,6 +18,7 @@ FrontendRouter.get(
)

/**
* @deprecated use routes in src/app/routes/api/v3/client/client.routes.ts instead
* Generate the templated Javascript code with environment variables for the frontend
* @route GET /frontend/environment
* @return 200 when code generation is successful
Expand All @@ -25,6 +27,7 @@ FrontendRouter.get(
FrontendRouter.get('/environment', FrontendServerController.addEnvVarData)

/**
* @deprecated use routes in src/app/routes/api/v3/client/client.routes.ts instead
* Generate a json of current activated features
* @route GET /frontend/features
* @return json with featureManager.states
Expand All @@ -34,6 +37,7 @@ FrontendRouter.get('/environment', FrontendServerController.addEnvVarData)
FrontendRouter.get('/features', FrontendServerController.showFeaturesStates)

/**
* @deprecated use routes in src/app/routes/api/v3/client/client.routes.ts instead
* Generate the javascript code to redirect to the correct url
* @route GET /frontend/redirect
* @return 200 when redirect code is successful
Expand Down
24 changes: 24 additions & 0 deletions src/app/modules/frontend/frontend.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ClientEnvVars } from '../../../../shared/types/core'
import config from '../../config/config'
import { captchaConfig } from '../../config/features/captcha.config'
import { googleAnalyticsConfig } from '../../config/features/google-analytics.config'
import { sentryConfig } from '../../config/features/sentry.config'
import { spcpMyInfoConfig } from '../../config/features/spcp-myinfo.config'

export const getClientEnvVars = (): ClientEnvVars => {
return {
isGeneralMaintenance: config.isGeneralMaintenance,
isLoginBanner: config.isLoginBanner,
siteBannerContent: config.siteBannerContent,
adminBannerContent: config.adminBannerContent,
logoBucketUrl: config.aws.logoBucketUrl, // S3 bucket
formsgSdkMode: config.formsgSdkMode,
captchaPublicKey: captchaConfig.captchaPublicKey, // Recaptcha
sentryConfigUrl: sentryConfig.sentryConfigUrl, // Sentry.IO
isSPMaintenance: spcpMyInfoConfig.isSPMaintenance, // Singpass maintenance message
isCPMaintenance: spcpMyInfoConfig.isCPMaintenance, // Corppass maintenance message
myInfoBannerContent: spcpMyInfoConfig.myInfoBannerContent, // MyInfo maintenance message
GATrackingID: googleAnalyticsConfig.GATrackingID,
spcpCookieDomain: spcpMyInfoConfig.spcpCookieDomain, // Cookie domain used for removing spcp cookies
}
}
7 changes: 7 additions & 0 deletions src/app/routes/api/v3/client/client.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ ClientRouter.get(
*/
ClientRouter.get('/environment', FrontendServerController.addEnvVarData)

/**
* Retrieve the environment variables for the frontend.
* @route GET /api/v3/client/env
* @return 200 with environment variables needed for the client
*/
ClientRouter.get('/env', FrontendServerController.handleGetEnvironment)

/**
* Generate a json of current activated features
* @route GET /api/v3/client/features
Expand Down