fix(core): update

This commit is contained in:
Philipp Kunz 2021-09-08 21:46:21 +02:00
parent 9b9675bd96
commit daeaac2367
4 changed files with 18943 additions and 2844 deletions

21696
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,16 +13,16 @@
"format": "(gitzone format)" "format": "(gitzone format)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.0.22", "@gitzone/tsbuild": "^2.1.26",
"@gitzone/tsbundle": "^1.0.72", "@gitzone/tsbundle": "^1.0.87",
"@gitzone/tstest": "^1.0.43", "@gitzone/tstest": "^1.0.57",
"@pushrocks/tapbundle": "^3.2.9", "@pushrocks/tapbundle": "^3.2.14",
"@types/node": "^14.0.23", "@types/node": "^16.7.13",
"tslint": "^6.1.2", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0" "tslint-config-prettier": "^1.15.0"
}, },
"dependencies": { "dependencies": {
"path-to-regexp": "^6.1.0" "path-to-regexp": "^6.2.0"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

View File

@ -8,8 +8,8 @@ tap.test('first test', async () => {
expect(testrouter).to.be.instanceOf(smartrouter.SmartRouter); expect(testrouter).to.be.instanceOf(smartrouter.SmartRouter);
}); });
tap.test('should handle a route change', async (tools) => { tap.test('should handle a route change', async (toolsArg) => {
const done = tools.defer(); const done = toolsArg.defer();
testrouter.on('/myawesomeroute/:any', async (routeInfoArg) => { testrouter.on('/myawesomeroute/:any', async (routeInfoArg) => {
expect(routeInfoArg.params.any).to.equal('hello'); expect(routeInfoArg.params.any).to.equal('hello');
done.resolve(); done.resolve();
@ -18,8 +18,8 @@ tap.test('should handle a route change', async (tools) => {
await done.promise; await done.promise;
}); });
tap.test('should handle a route change', async (tools) => { tap.test('should handle a route change', async (toolsArg) => {
const done = tools.defer(); const done = toolsArg.defer();
testrouter.on('/myawesomeroute2/:wow', async (routeInfoArg) => { testrouter.on('/myawesomeroute2/:wow', async (routeInfoArg) => {
expect(routeInfoArg.params.wow).to.equal('hello2'); expect(routeInfoArg.params.wow).to.equal('hello2');
done.resolve(); done.resolve();
@ -29,4 +29,18 @@ tap.test('should handle a route change', async (tools) => {
expect(window.location.href).to.equal('http://localhost:3007/myawesomeroute2/hello2'); expect(window.location.href).to.equal('http://localhost:3007/myawesomeroute2/hello2');
}); });
tap.test('should find a query param', async (toolsArg) => {
const done = toolsArg.defer();
testrouter.on('/myawesomeroute2/:wow', async (routeInfoArg) => {
expect(routeInfoArg.params.wow).to.equal('hello2');
expect(routeInfoArg.queryParams.aparam).to.equal('Yes');
console.log('Here is what queryParams looks like');
console.log(JSON.stringify(routeInfoArg.queryParams))
done.resolve();
});
testrouter.pushUrl('/myawesomeroute2/hello2?aparam=Yes');
await done.promise;
expect(window.location.href).to.equal('http://localhost:3007/myawesomeroute2/hello2?aparam=Yes');
})
tap.start(); tap.start();

View File

@ -13,6 +13,7 @@ export interface IRouteInfo {
path: string; path: string;
index: number; index: number;
params: { [key: string]: string }; params: { [key: string]: string };
queryParams: { [key: string]: string };
} }
/** /**
@ -80,13 +81,19 @@ export class SmartRouter {
*/ */
async _handleRouteState() { async _handleRouteState() {
const currentLocation = window.location.pathname; const currentLocation = window.location.pathname;
const urlSearchParams = new URLSearchParams(window.location.search);
// lets find all wanted routes.
const wantedRoutes = this.routes.filter((routeArg) => { const wantedRoutes = this.routes.filter((routeArg) => {
return !!routeArg.matchFunction(currentLocation); return !!routeArg.matchFunction(currentLocation);
}); });
for (const wantedRoute of wantedRoutes) { for (const wantedRoute of wantedRoutes) {
const routeResult = wantedRoute.matchFunction(currentLocation); const routeResult = wantedRoute.matchFunction(currentLocation);
wantedRoute.handler(routeResult.valueOf() as IRouteInfo); // not waiting here wantedRoute.handler({
...(routeResult.valueOf() as Object),
queryParams: Object.fromEntries(urlSearchParams.entries()),
} as IRouteInfo); // not waiting here
} }
} }
} }