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

feat(vinternalrouter): implement route wildcard #68

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Check out the [demo application](https://github.com/YoeriNijs/vienna-demo-app).
- [Json](#json)
- [Routes](#routes)
- [Nested routes](#nested-routes)
- [Route wildcards](#route-wildcards)
- [Route data](#route-data)
- [Route params](#route-params)
- [Query params](#query-params)
Expand Down Expand Up @@ -597,6 +598,36 @@ export class Application {}

```

### Route wildcards

The Vienna router supports wildcards. Just pass the `*`-sign to add a wildcard. Note that the first route wins. This means
that if you have two routes that match, the router picks the first one. It does not matter whether one of them is a wildcard.

```

@VApplication({
declarations: [
AboutComponent,
PageNotFoundComponent
],
routes: [
{
path: '/about',
component: AboutComponent
},
{
path: '*',
component: PageNotFoundComponent
}
]
})
export class Application {}

```

Please note: the example above is just to demonstrate how you can use the wildcard. Of course, if you to render a component
for a page that is not found, you can also implement the `routeNotFoundStrategy`.

### Route data

Optional key-value based map to specify some custom values for a specific route.
Expand Down
161 changes: 161 additions & 0 deletions src/core/router/__tests__/v-internal-router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,166 @@ describe('VInternalRouter', () => {
]
}).catch(e => expect(e).toEqual(new VInvalidRouteStrategyException('Invalid route strategy: \'none\'')));
});

it('should navigate with route param', done => {
eventBus.subscribe(VInternalEventName.NAVIGATED, (r: VRoute) => {
expect(r.path).toEqual('/:id');
done();
});
setup('/blog/:id', {
routes: [
{
path: '/blog',
component: jest.fn(),
guards: [],
children: [
{
path: '/:id',
component: jest.fn(),
guards: [],
children: []
}
]
}
]
});
});

it('should navigate with query param', done => {
eventBus.subscribe(VInternalEventName.NAVIGATED, (r: VRoute) => {
expect(r.path).toEqual('/page');
done();
});
setup('/page?message=Hello%20there', {
routes: [
{
path: '/page',
component: jest.fn(),
guards: [],
children: []
}
]
});
});

it('should navigate with query param and wildcard', done => {
eventBus.subscribe(VInternalEventName.NAVIGATED, (r: VRoute) => {
expect(r.path).toEqual('/');
done();
});
setup('/*?message=Hello%20there', {
routes: [
{
path: '/',
component: jest.fn(),
guards: [],
children: []
}
]
});
});

it('should navigate with query param and wildcard', done => {
eventBus.subscribe(VInternalEventName.NAVIGATED, (r: VRoute) => {
expect(r.path).toEqual('/');
done();
});
setup('*?message=Hello%20there', {
routes: [
{
path: '/',
component: jest.fn(),
guards: [],
children: []
}
]
});
});

it('should navigate with wildcard and children', done => {
eventBus.subscribe(VInternalEventName.NAVIGATED, (r: VRoute) => {
expect(r.path).toEqual('/relevant');
done();
});
setup('/*/relevant', {
routes: [
{
path: '/*',
component: jest.fn(),
guards: [],
children: [
{
path: '/relevant',
component: jest.fn(),
guards: [],
children: []
}
]
}
]
});
});

it('should navigate with wildcard and children without slash', done => {
eventBus.subscribe(VInternalEventName.NAVIGATED, (r: VRoute) => {
expect(r.path).toEqual('/relevant');
done();
});
setup('/*/relevant', {
routes: [
{
path: '*',
component: jest.fn(),
guards: [],
children: [
{
path: '/relevant',
component: jest.fn(),
guards: [],
children: []
}
]
}
]
});
});

it('should navigate to first route before wildcard', done => {
eventBus.subscribe(VInternalEventName.NAVIGATED, (r: VRoute) => {
expect(r.path).toEqual('/about');
done();
});
setup('/about', {
routes: [
{
path: '/about',
component: jest.fn()
},
{
path: '*',
component: jest.fn()
}
]
});
});

it('should navigate to wildcard first', done => {
eventBus.subscribe(VInternalEventName.NAVIGATED, (r: VRoute) => {
expect(r.path).toEqual('*');
done();
});
setup('/about', {
routes: [
{
path: '*',
component: jest.fn()
},
{
path: '/about',
component: jest.fn()
}
]
});
});
});
});
9 changes: 7 additions & 2 deletions src/core/router/v-internal-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,19 @@ export class VInternalRouter {
const lowerUrl = url.toLowerCase();
const paramIndex = url.indexOf('?');
if (paramIndex === -1) {
return lowerPath === lowerUrl;
return lowerPath === lowerUrl || this.isWildCard(lowerPath);
} else {
return lowerPath === lowerUrl.substring(0, paramIndex);
const pathBeforeParam = lowerUrl.substring(0, paramIndex);
return lowerPath === pathBeforeParam || this.isWildCard(pathBeforeParam);
}
});
return exactRoute ? exactRoute : null;
}

private isWildCard(value: string): boolean {
return value === '*' || value === '/*';
}

private dispatchNavigationAction(route: VRoute): void {
const eventBus: VInternalEventbus = this.options.eventBus;
eventBus.unsubscribe(VInternalEventName.ROUTE_DATA);
Expand Down
Loading