moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
295 stars 118 forks source link

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

Open thib3113 opened 1 year ago

thib3113 commented 1 year ago

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 :

icebob commented 1 year ago

Good idea, please add relevant tests as well