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

refactor(frontend-api): duplicate frontend client endpoints to new /api/v3 router #1574

Merged
merged 6 commits into from
Apr 13, 2021
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
52 changes: 52 additions & 0 deletions src/app/routes/api/v3/client/client.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { celebrate, Joi, Segments } from 'celebrate'
import { Router } from 'express'

import * as FrontendServerController from '../../../../modules/frontend/frontend.controller'
import { GoogleAnalyticsFactory } from '../../../../modules/frontend/google-analytics.factory'

export const ClientRouter = Router()

/**
* Generate the templated Javascript code for the frontend to initialise Google Tag Manager
* Code depends on whether googleAnalyticsFeature.isEnabled
* @route GET /api/v3/client/analytics/google
* @return 200 when code generation is successful
* @return 400 when code generation fails
*/
ClientRouter.get(
'/analytics/google',
GoogleAnalyticsFactory.addGoogleAnalyticsData,
)

/**
* Generate the templated Javascript code with environment variables for the frontend
* @route GET /api/v3/client/environment
* @return 200 when code generation is successful
* @return 400 when code generation fails
*/
ClientRouter.get('/environment', FrontendServerController.addEnvVarData)

/**
* Generate a json of current activated features
* @route GET /api/v3/client/features
* @return json with featureManager.states
*/
ClientRouter.get('/features', FrontendServerController.showFeaturesStates)

/**
* Generate the javascript code to redirect to the correct url
* @route GET /api/v3/client/redirect
* @return 200 when redirect code is successful
* @return 400 when redirect code fails
*/
ClientRouter.get(
'/redirect',
celebrate({
[Segments.QUERY]: {
redirectPath: Joi.string()
.regex(/^[a-fA-F0-9]{24}(\/(preview|template|use-template))?/)
.required(),
},
}),
FrontendServerController.generateRedirectUrl,
)
1 change: 1 addition & 0 deletions src/app/routes/api/v3/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ClientRouter } from './client.routes'
2 changes: 2 additions & 0 deletions src/app/routes/api/v3/v3.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AdminRouter } from './admin'
import { AnalyticsRouter } from './analytics'
import { AuthRouter } from './auth'
import { BillingsRouter } from './billings'
import { ClientRouter } from './client'
import { NotificationsRouter } from './notifications'
import { UserRouter } from './user'

Expand All @@ -12,6 +13,7 @@ export const V3Router = Router()
V3Router.use('/admin', AdminRouter)
V3Router.use('/user', UserRouter)
V3Router.use('/auth', AuthRouter)
V3Router.use('/client', ClientRouter)
V3Router.use('/notifications', NotificationsRouter)
V3Router.use('/billings', BillingsRouter)
V3Router.use('/analytics', AnalyticsRouter)
6 changes: 3 additions & 3 deletions src/app/views/index.server.view.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
async
src="https://www.googletagmanager.com/gtag/js?id=<%= GATrackingID%>"
></script>
<script src="/frontend/datalayer"></script>
<script src="/api/v3/client/analytics/google"></script>
<% } %>
<!--Embedding custom variables in window-->
<script src="/frontend/environment"></script>
<script src="/api/v3/client/environment"></script>

<%if (redirectPath) { %>
<script src="/frontend/redirect?redirectPath=<%= redirectPath%>"></script>
<script src="/api/v3/client/redirect?redirectPath=<%= redirectPath%>"></script>
<% } %>

<title><%= title %></title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function featureFactory($resource) {
if (this.states) {
return this.states
} else {
this.states = await $resource('/frontend/features').get().$promise
this.states = await $resource('/api/v3/client/features').get().$promise
return this.states
}
},
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function enterEmail(t, email) {
* @returns boolean
*/
async function getFeatureState(feature) {
const featureStates = await axios.get(`${appUrl}/frontend/features`)
const featureStates = await axios.get(`${appUrl}/api/v3/client/features`)
return featureStates.data[feature]
}

Expand Down