Skip to content

Commit 60ee342

Browse files
author
Renoir Boulanger
committed
Optionally show examples menu by using SHOW_EXAMPLES environment.
1 parent 45cdf14 commit 60ee342

File tree

7 files changed

+98
-78
lines changed

7 files changed

+98
-78
lines changed

.vscode/launch.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"sourceMaps": true,
1515
"env": {
1616
"NODE_ENV": "development",
17-
"DEBUG": "nuxt:*,koa:*"
17+
"DEBUG": "nuxt:*,koa:*",
18+
"SHOW_EXAMPLES": "true"
1819
}
1920
},
2021
{

server/api/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const Koa = require('koa') // Koa framework
22
const xmlify = require('xmlify') // JS object to XML
33
const yaml = require('js-yaml') // JS object to YAML
4-
const consts = require('../utils/consts')
54
const auth = require('./routes-auth')
65
const examples = require('./routes-examples')
7-
const menu = require('./routes-menu')
6+
const ui = require('./routes-ui')
87

98
const app = new Koa() // API app
109

@@ -82,9 +81,9 @@ app.use(async function handleErrors (ctx, next) {
8281
// public (unsecured) modules first
8382

8483
app.use(auth)
85-
app.use(menu)
84+
app.use(ui)
8685

87-
if (consts.SHOW_EXAMPLES === true) {
86+
if (process.env.SHOW_EXAMPLES === 'true') {
8887
app.use(examples)
8988
}
9089

server/api/routes-auth.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const translator = translatorFactory('en')
6363
* HttpOnly Cookie is made for this.
6464
*/
6565
router.post('/auth/login', async (ctx) => {
66-
console.log('POST /auth/login', {...ctx.request.body})
66+
// console.log('POST /auth/login', {...ctx.request.body})
6767
const user = ctx.request.body
6868
if (!user || !user.userName || !user.password) {
6969
ctx.throw(401, translator.translate('auth.login.required.missing'))
@@ -202,6 +202,8 @@ router.get('/auth/validate', async (ctx) => {
202202

203203
try {
204204
userData = await getUserData(jwt)
205+
// This is assuming your UserData object has a status property, and when the credentials we've given
206+
// are still valid (i.e. not expired) as per that backend, it returns exactly the String "valid"
205207
authenticated = userData.status === 'valid' || false
206208
} catch (e) {
207209
// Nothing to do, body.authenticated defaults to false. Which would be what we want.

server/api/routes-menu.js

-71
This file was deleted.

server/api/routes-ui.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2+
/* Route to handle /ui */
3+
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
4+
const koaRouter = require('koa-router')
5+
const consts = require('../utils/consts')
6+
const menu = require('../models/menu')
7+
8+
const router = koaRouter({
9+
prefix: consts.BASE_API
10+
})
11+
12+
const includingExamples = (assertion = true) => item => {
13+
const assertIncludeExample = assertion === true
14+
// Defaults to true. Anything could be an example.
15+
// So we have to be careful at what we might end up with in production build.
16+
let isExample = true
17+
const idFieldExists = Reflect.has(item, 'id')
18+
// Make it in example range if id is missing
19+
const idFieldFirstDigits = idFieldExists ? item.id.split('-')[0] : 1000
20+
const idFieldInteger = Number.parseInt(idFieldFirstDigits)
21+
if (idFieldExists && typeof idFieldInteger === 'number') {
22+
// Assuming id 9999-99 IS NOT an example
23+
// Assuming id 1000-00 IS an example
24+
isExample = idFieldInteger > 999
25+
}
26+
return isExample === false || isExample === assertIncludeExample
27+
}
28+
29+
router.get('/ui/menu', async (ctx, next) => {
30+
ctx.assert(ctx.session.jwt, 401, 'Require authentication')
31+
32+
const body = menu.filter(i => includingExamples(process.env.SHOW_EXAMPLES === 'true')(i))
33+
34+
ctx.status = 200
35+
ctx.body = body
36+
})
37+
38+
module.exports = router.routes()

server/models/menu.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const items = [
2+
{
3+
id: '1',
4+
name: 'nav.home',
5+
url: '/',
6+
icon: 'el-icon-menu'
7+
},
8+
{
9+
id: '3',
10+
name: 'nav.about',
11+
url: '/about',
12+
icon: 'el-icon-menu'
13+
},
14+
// Assuming id > 999 are examples.
15+
{
16+
id: '1000',
17+
name: 'nav.kitchenSink',
18+
icon: 'el-icon-goods',
19+
children: [
20+
{
21+
id: '1000-1',
22+
name: 'nav.demo',
23+
url: '/examples',
24+
icon: 'el-icon-share'
25+
},
26+
{
27+
id: '1000-2',
28+
name: 'nav.list',
29+
url: '/examples/activity',
30+
icon: 'el-icon-view'
31+
},
32+
{
33+
id: '1000-3',
34+
name: 'nav.create',
35+
url: '/examples/activity/create',
36+
icon: 'el-icon-message'
37+
},
38+
{
39+
id: '1000-4',
40+
name: 'nav.charts',
41+
url: '/examples/charts',
42+
icon: 'el-icon-picture'
43+
}
44+
]
45+
}
46+
]
47+
48+
module.exports = [...items]

server/utils/helpers.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const handleTokenExp = exp => {
4747
* to point to your own API.
4848
*/
4949
const createRequest = async (method, url, requestConfig) => {
50-
// #TODO Make this only return a request configuration object, and factor axios out from this.
50+
// #TODO #refactorCreateRequestBackIntoKoa Make this only return a request configuration object, and factor axios out from this.
5151
const baseURL = process.env.LB_ADDR || consts.LB_ADDR
5252
const verb = method.toUpperCase()
5353
console.log(`createRequest ${verb} ${baseURL}${url}, requestConfig`, requestConfig)
@@ -87,8 +87,11 @@ const getUserData = async (token) => {
8787
* Would create a request like this;
8888
*
8989
* GET /platform/uaano/oauth/validate?Token=111.222.333&userinfo=PreferredLanguage,TimeZone
90+
*
91+
* rel: #refactorCreateRequestBackIntoKoa
9092
*/
9193
const response = await createRequest('GET', consts.ENDPOINT_BACKEND_VALIDATE, { params })
94+
// console.log(`getUserData response`, {...response})
9295

9396
const body = {
9497
status: response.Status

0 commit comments

Comments
 (0)