Make `route` property on `RouteConfig` optional for redirect routes
I think this is technically a feature request. It's kind of that grey area. While one could argue the interface has a bug in its design, the code that consumes the interface works correctly despite it, even if you violate the interface. Let me explain.
Library Version: 1.1.1 Browser: All Language: Typescript
Current behavior:
The RouteConfig interface always requires a consumer to specify a route property on it. This is not surprising as it makes sense the vast majority of the time. However, it does not make sense when utilizing it with the mapUnknownRoutes() function. In that situation, the whole reason the route is being consumed is because the route doesn't exist. So if I want to set up a redirect back to my "home" route if a specific route does not exist, I have to configure the route like so:
public configureRouter(config: RouterConfiguration, router: Router) {
config.map([
{
moduleId: 'subjects/home',
name: 'subject-home',
route: ''
},
{
moduleId: 'subjects/tab',
name: 'subject-tab',
route: ':slug'
}
]);
config.mapUnknownRoutes({ route: 'required-because-of-interface', redirect: '' });
return config;
}
What is the expected behavior?
Assuming all of the above code is the same except for the call to mapUnknownRoutes(), I would expect the following to work:
config.mapUnknownRoutes({ redirect: '' });
This removes the superfluous (and ignored, as far as I can tell) route property.
What is the motivation / use case for changing the behavior?
The slugs we generate for some URLs are based upon user controlled content, so if the user changes the underlying name of a value, a cached URL will no longer be valid. In those situations, we want to redirect the users back to the equivalent of a "home" route. When I looked on how to do this, I found this answer on StackOverflow. When I tried the accepted answer, I found it did not compile due to typescript requiring the route property. Enter this issue.
Perhaps it makes sense to create separate interfaces for regular RouteConfig and another for redirect RouteConfig (where corresponding properties for the use-case are required). Smth like
export type RouteConfig = RegularRouteConfig | RedirectRouteConfig;
Then type-safety would be guaranteed for both of the use-cases and the Aurelia code-base would need almost no changes (except the two types).
Sure, that also solves the problem.