|
| 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() |
0 commit comments