primatejs / primate

Web framework focused on flexibility and developer freedom
https://primatejs.com
MIT License
211 stars 9 forks source link

[routing] Map named capture groups to `request.path.<name>` #1

Closed terrablue closed 2 years ago

terrablue commented 2 years ago

Currently, it is possible to access the parts of a pathname via the request.path array:

router.get(`/namespace/action/([a-z0-9]*)`, request => {
   const [,, _id] = request.path;
});

It would be more convenient to populate request.path with the results of matching named capture groups, such as that the name of the capture group is available as a property on request.path.

router.get(`/namespace/action/(?<_id>[a-z0-9]*)`, request => {
   // still works
   const [,, _id] = request.path;
   // but also available as
   const {"_id": also_id} = request.path;
});
terrablue commented 2 years ago

done as of 916b134