Skip to content

Commit

Permalink
fix(router): added async handler for skill routes
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Nov 9, 2018
1 parent 71a5772 commit ffcde57
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
54 changes: 32 additions & 22 deletions src/bp/core/routers/modules.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Logger } from 'botpress/sdk'
import { Router } from 'express'

import { ModuleLoader } from '../module-loader'
import { SkillService } from '../services/dialog/skill/service'

import { CustomRouter } from '.'
import { asyncMiddleware, error as sendError, success as sendSuccess } from './util'
export class ModulesRouter implements CustomRouter {
public readonly router: Router
private asyncMiddleware!: Function

constructor(private moduleLoader: ModuleLoader, private skillService: SkillService) {
constructor(logger: Logger, private moduleLoader: ModuleLoader, private skillService: SkillService) {
this.asyncMiddleware = asyncMiddleware({ logger })
this.router = Router({ mergeParams: true })
this.setupRoutes()
}
Expand All @@ -17,26 +21,32 @@ export class ModulesRouter implements CustomRouter {
res.json(this.moduleLoader.getLoadedModules())
})

this.router.get('/skills', async (req, res, next) => {
try {
res.send(await this.moduleLoader.getAllSkills())
} catch (err) {
next(err)
}
})

this.router.post('/:moduleName/skill/:skillId/generateFlow', async (req, res) => {
const flowGenerator = await this.moduleLoader.getFlowGenerator(req.params.moduleName, req.params.skillId)

if (!flowGenerator) {
return res.status(404).send('Invalid module name or flow name')
}

try {
res.send(await this.skillService.finalizeFlow(flowGenerator(req.body)))
} catch (err) {
res.status(400).send(`Error while trying to generate the flow: ${err}`)
}
})
this.router.get(
'/skills',
this.asyncMiddleware(async (req, res, next) => {
try {
res.send(await this.moduleLoader.getAllSkills())
} catch (err) {
next(err)
}
})
)

this.router.post(
'/:moduleName/skill/:skillId/generateFlow',
this.asyncMiddleware(async (req, res) => {
const flowGenerator = await this.moduleLoader.getFlowGenerator(req.params.moduleName, req.params.skillId)

if (!flowGenerator) {
return res.status(404).send('Invalid module name or flow name')
}

try {
res.send(await this.skillService.finalizeFlow(flowGenerator(req.body)))
} catch (err) {
res.status(400).send(`Error while trying to generate the flow: ${err}`)
}
})
)
}
}
2 changes: 1 addition & 1 deletion src/bp/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class HTTPServer {

this.httpServer = createServer(this.app)

this.modulesRouter = new ModulesRouter(moduleLoader, skillService)
this.modulesRouter = new ModulesRouter(this.logger, moduleLoader, skillService)
this.authRouter = new AuthRouter(this.logger, this.authService, this.adminService)
this.adminRouter = new AdminRouter(this.logger, this.authService, this.adminService, licenseService)
this.shortlinksRouter = new ShortLinksRouter()
Expand Down

0 comments on commit ffcde57

Please sign in to comment.