herbsjs / herbs2rest

Create a REST API based on HerbsJS
MIT License
2 stars 13 forks source link

Override the default request logic #13

Open dalssoft opened 3 years ago

dalssoft commented 3 years ago

Having a default request logic (how the req info is dealt for each type of route) is useful but forcing it is bad. There should be a way to override it for certain use cases.

Suggested UX:

const myRequest = (req) => { return Object.assign({}, req.query, req.params) }

const controllerList = [
  {
    name: 'lists',
    idParameter: 'listId',
    getAll: require('../usecases/getLists'),
    getById: { usecase: require('../usecases/getLists'), request: myRequest }, // my request
    ...
  }
]
italojs commented 3 years ago

I think we need only specify the request, because in my request I will build my own logic to call my usecase:

const controllerList = [
  {
    name: 'lists',
    idParameter: 'listId',
    getAll: require('../usecases/getLists'),
    getById:  (req, res, next) => { 
        // [...] controller logic
        const result = usecase.run()
        res.json(result)
       }
    ...
  }
]
dalssoft commented 3 years ago

the issue is specific on how to deal with variable coming from query string, body and params.

that is the current implementation: https://github.com/herbsjs/herbs2rest/blob/885a83b4644c3e43efb9bad9d3ea5d0d39bcdbc6/src/helpers/req2request.js#L9

but overriding it should be possible.