I am writing an Express JS application and I need to calculate a route given a controller function or route path.
For example:
let myUrl = functionToGetRoutePath(ThingsController.getThing); // would return https://host/api/v1/things/{id}
// Or
let myUrl = functionToGetRoutePath('/things/{id}'); // would return https://host/api/v1/things/{id}
Or, given parameters, it could fill in the URL parameters, for example:
let myUrl = functionToGetRoutePath(ThingsController.getThing, { id: 1 }); // would return https://host/api/v1/things/1
// Or
let myUrl = functionToGetRoutePath('/things/{id}', { id: 1 }); // would return https://host/api/v1/things/1
Coming from .NET, this is pretty easy using UrlHelper:
new UrlHelper(Request).Link("Things Route Name", new { id = 1 }); // returns https://host/api/v1/things/1