srv.before() handler
This handler is called before the srv.on() handler. It is usually used for adding user’s input validation, for example:
this.before (['CREATE', 'UPDATE'],'Record', (req)=>{
const record = req.data
if (record.spentTime <= 0) throw 'Spent time must be positive number'
})
There are another four things that you need to know about .before()
handler:
- If you have more than one
.before()
handler, then they will be executed in order of their registration. - If your handler executes asynchronous task, you need to return a promise.
- Execution of the
.on()
handlers starts, when all promises will be resolved. - If your
.before()
handlers are running in asynchronous way, you can’t react on side-effects.
srv.on() handler
These handlers are run in a chain and every handler can terminate it. .on()
handler helps you to create custom handlers replacing standard ones.
Please find below some examples for handling requests:
srv.on('READ','Records', async (req)=> req.reply(await cds.tx(req).run(...))
srv.on('READ','Records', (req)=> req.reply(...))
srv.on('READ','Records', (req)=> cds.tx(req).run(...))
srv.on('READ','Records', (req)=> [ ... ])
srv.on('READ','Records', ()=> SELECT.from(Records))
srv.on('READ','Records', (req,next)=>{
if (...) return SELECT.from(Records)
else return next()
})
srv.after() handler
The .after()
handler is used to modify the response. It runs on the results, which are returned by the standard or custom .on()
handler.
There are a few things you should know about this handler:
- Otherwise than the
.on()
handler, the.after()
handler has two parameters -(result, req)
. - You should use
req
parameter, if you need to reflect on an inbound request - This method is used only to modify the result. You can’t replace the result, e.g. you can’t do the following:
this.after('READ','Records', (records)=>{ return ...something... })
That would be nice if you could make your code a little bit more expressive.