Let's say that I have the following URL path:
/api/resource/:id
Given the context object {id: 1}
the path will be transformed to /api/resource/1
.
Now I want to build a more complex path template:
/api/resource/:id(/relationships/:name)?
Here the relationships
member is optional so that the both contexts can match:
{id: 1, name: 'rel1'}
will build the string/api/resource/1/relationships/rel1
.{id: 1}
should still build the string/api/resource/1
.
I would like to be able to build such strings using a limited set of the regular expressions magic.
How to implement such a template engine?