moleculer-web icon indicating copy to clipboard operation
moleculer-web copied to clipboard

feat(compileRest): allow to reuse the rest settings, to generate rest path from parameters

Open thib3113 opened this issue 2 years ago • 1 comments

This PR allow to call a endpoint to generate the rest url .

allow to do :

const svc = {
 name: "users",
 settings: {
  rest: '/users'
 },
 actions: {
  action: {
    rest: 'GET /:id'
  }
 }
}

await ctx.call('gateway.getRest', {action: 'my.action', params: {id:123}}); // => return /users/123

It can be usefull if you want to setup hypermedias, or just generate a link


I'm not sure about the naming, so I reuse the naming from path-to-regexp "compile" .

Not sure about the error throw if missing parameters, but it seems that path-regexp throw an error if a parameter is missing (I also tried with parameter validate to false) .


State of the PR :

  • [ ] write tests
  • [ ] write documentation

thib3113 avatar Sep 28 '23 09:09 thib3113

Good idea, please add relevant tests as well

icebob avatar Oct 01 '23 09:10 icebob