Skip to content

Commit

Permalink
refactor(frontend-api): duplicate frontend client endpoints to new /a…
Browse files Browse the repository at this point in the history
…pi/v3 router (#1574)

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

- duplicate frontend client endpoints functionality and update them to use
the new api v3 routes
- update v3 router to use new endpoints
- update frontend api calls to use new endpoints

* refactor(frontend-api): use updated frontend handlers

* refactor(frontend-api): clean up routes and update comments

* fix(frontend-api): fix /features route and clean up routes
  • Loading branch information
orbitalsqwib authored Apr 13, 2021
1 parent 5ca787d commit fffd972
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
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

0 comments on commit fffd972

Please sign in to comment.