Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add express style parameter regular expression handling #1433

Closed
stevehipwell opened this issue Aug 4, 2017 · 3 comments
Closed

Add express style parameter regular expression handling #1433

stevehipwell opened this issue Aug 4, 2017 · 3 comments
Assignees
Labels

Comments

@stevehipwell
Copy link

Feature Request

Adding Express style parameter regular expression handling, such as /test/:id([a-f\d]{24}) and /test/:num(\d+), to Restify would make working with paramaters in Restify a much nicer experience. In addition it would enable Restify to handle the shared middleware scenarios that it cannot currently support due to parameters being unnamed and an array when regular expressions are used.

Use Case

A nested scenario.

const idMiddleware = (req, res, next) => {
  const obj = service.getObjById(req.params.id);
});

const numMiddleware = (req, res, next) => {
  const obj = service.getObjByNum(req.params.num);
});

server.get("/test:id([a-f\d]{24})", idMiddleware);
server.get("/test:num(\d+)", numMiddleware);

server.get("/parent:parentId/test:id([a-f\d]{24})", idMiddleware);
server.get("/parent:parentId/test:num(\d+)", numMiddleware);

A custom middleware scenario.

function getMiddleware(parmasPath, objPath) {
  return (req, res, next) => {
    const obj = service.getObjById(req.params[paramsPath]);
    const value = obj[objPath];
  });
}

server.get("/test:id([a-f\d]{24})", getMiddleware("id", "id"));
server.get("/test:num(\d+)", getMiddleware("num", "numericId"));

Example API

You can use regular expressions to validate parameters by including the regular expression in brackets after the parameter name. The pattern /test:id(\d+) will match the url /test/8 and res.params.id will be 8; however the url /test/hello will not match.

Are you willing and able to implement this?

I'm willing to help implement this if I can be pointed in the right direction.

@retrohacker
Copy link
Member

Yes! This would be amazing ❤️

We have a feature request asking for this here #1292

I believe the current proposal is to use path-to-regexp

@stevehipwell
Copy link
Author

@retrohacker - Is there some developer documentation?

@hekike
Copy link
Member

hekike commented Apr 10, 2018

Restify 7.x router is wildcard based, please see migration guide.

@hekike hekike closed this as completed Apr 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants