Thursday 26 July 2018

HAPI - Route Handlers and passing additional data besides (request,h)

This is a snippet from my api.js that holds my api routes.Above this code is all the static route handlers. Auth/Login/EtcWhat you see below is the routes added dynamically based on my models.js which holds on the object classes that have api interfaces.I need to some how pass from the API call to the handler the class model to my db.js which holds the handler. Even just passing the name of the model I could resolve it by using models[model] in the db.js. Unfortunately this is seeming harder or more convoluted than I want.pre [] seems to happen at the time the route is called not when the api call is built so this causes issues with the model that gets passed into request.pre.model. The API call says it's path is /_api/Merge/Person but when it executes pre[] it doesn't pass the Person model. It passes the last model in the array which could be something like "Organization" and not "Person".for (var model in models) { routes.push({ method: 'POST', path: '/_api/Merge/'+model, options: { pre: [ { method: function () { return models[model]; }, assign: 'model' } ], handler: db.mergeObject.handler, description: 'Add/Update Object ' + model, tags: ['api'], validate: { payload: models[model].schema() } } }) routes.push({ method: 'DELETE', path: '/_api/Delete/' + model, options: { pre: [ { method: function () { return models[model]; }, assign: 'model'} ], handler: db.mergeObject.handler, description: 'Add/Update Object ' + model, tags: ['api'], validate: { payload: { key: joi.string().required() } } } }) } I'm not sure how to hard code a value in my routes. I thought I could use option.notes and store the object name but I felt like that was kind of lame and there could be a more sophisticated way of doing it.

Submitted July 27, 2018 at 12:56AM by Thriven

No comments:

Post a Comment