Skip to content

Route patterns matching #23

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

Merged
merged 3 commits into from
Jul 13, 2022
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
5 changes: 5 additions & 0 deletions .changeset/lovely-buttons-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chialab/synapse": patch
---

Route patterns matching
11 changes: 9 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { RequestInit, RequestMethod } from './Router/Request';
import { Component, window, property, state, observe, listen } from '@chialab/dna';
import { Request } from './Router/Request';
import { Response } from './Router/Response';
import { Router } from './Router/Router';
import { Router, DEFAULT_ORIGIN } from './Router/Router';
import { Page } from './Components/Page';

enum NavigationDirection {
Expand All @@ -22,7 +22,14 @@ export class App extends Component {
type: String,
})
get origin(): string {
return this.getInnerPropertyValue('origin') || window.location?.origin;
if (this.getInnerPropertyValue('origin')) {
return this.getInnerPropertyValue('origin');
}
if (window.location.origin && window.location.origin !== 'null') {
return window.location.origin;
}

return DEFAULT_ORIGIN;
}
set origin(value: string) {
this.setInnerPropertyValue('origin', value);
Expand Down
3 changes: 2 additions & 1 deletion src/Router/Pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export class Pattern {
* @param path The path to check.
* @return False if does not match, grouped values if it does.
*/
matches(path: string): RequestParams|false {
matches(path: string): RequestParams | false {
path = path.split('?')[0];
if (this.cache[path]) {
return this.cache[path];
}
Expand Down
5 changes: 5 additions & 0 deletions src/Router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export interface PopStateData {
previous: State;
}

/**
* Default router origin.
*/
export const DEFAULT_ORIGIN = 'http://local';

/**
* Trim slashes from the start and end of a string.
* @param token The string to trim.
Expand Down
5 changes: 5 additions & 0 deletions test/App.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ describe('App', () => {

afterEach(() => {
DOM.removeChild(document.body, wrapper);
if (typeof window._jsdom !== 'undefined') {
window._jsdom.reconfigure({
url: 'about:blank',
});
}
});

it('should initialize a router', () => {
Expand Down
47 changes: 47 additions & 0 deletions test/Router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,51 @@ describe('Router', () => {
expect(response.data.href).to.be.equal('http://local/base');
expect(history.entries[0].url).to.be.equal('http://local/base');
});

describe('patterns', () => {
let router;
before(() => {
router = new Router({
base: '/base',
}, [
{
pattern: '/params/:action/:id',
},
{
pattern: '/params/:id',
},
{
pattern: '/params',
},
]);
});

it('should match empty params', async () => {
const { request: { params } } = await router.navigate('/params');

expect(Object.keys(params)).to.have.lengthOf(0);
});

it('should match single param', async () => {
const { request: { params } } = await router.navigate('/params/123');

expect(Object.keys(params)).to.have.lengthOf(1);
expect(params.id).to.be.equal('123');
});

it('should match single param with query string', async () => {
const { request: { params } } = await router.navigate('/params/123?test');

expect(Object.keys(params)).to.have.lengthOf(1);
expect(params.id).to.be.equal('123');
});

it('should match multiple params', async () => {
const { request: { params } } = await router.navigate('/params/view/123');

expect(Object.keys(params)).to.have.lengthOf(2);
expect(params.action).to.be.equal('view');
expect(params.id).to.be.equal('123');
});
});
});