Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
7d01fe89bc | |||
c6a7938a3e | |||
41ba5c8e4f | |||
41b6f04db6 | |||
470f47f8bb | |||
8df4e21501 | |||
ee87a6269e | |||
2d76e799a4 | |||
f24520ea89 | |||
5072728118 | |||
bb4df0c47f | |||
101533cddd | |||
3b31f88b0c | |||
daeaac2367 |
@ -12,40 +12,36 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- pnpm install -g pnpm
|
||||||
|
- pnpm install -g @shipzone/npmci
|
||||||
|
- npmci npm prepare
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# security stage
|
||||||
|
# ====================
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
auditProductionDependencies:
|
auditProductionDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm install --production --ignore-scripts
|
- npmci command pnpm audit --audit-level=high --prod
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
|
||||||
- npmci command npm audit --audit-level=high --only=prod --production
|
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
auditDevDependencies:
|
auditDevDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
|
||||||
- npmci command npm install --ignore-scripts
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm audit --audit-level=high --only=dev
|
- npmci command pnpm audit --audit-level=high --dev
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
|
||||||
@ -56,7 +52,6 @@ auditDevDependencies:
|
|||||||
testStable:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@ -67,10 +62,9 @@ testStable:
|
|||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command npm run build
|
- npmci npm build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
@ -96,10 +90,9 @@ codequality:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
@ -119,11 +112,9 @@ trigger:
|
|||||||
pages:
|
pages:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,28 +2,10 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "current file",
|
"command": "npm test",
|
||||||
"type": "node",
|
"name": "Run npm test",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"args": [
|
"type": "node-terminal"
|
||||||
"${relativeFile}"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "test.ts",
|
|
||||||
"type": "node",
|
|
||||||
"request": "launch",
|
|
||||||
"args": [
|
|
||||||
"test/test.ts"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "smartrouter",
|
"gitrepo": "smartrouter",
|
||||||
"shortDescription": "a router for routing on websites",
|
"description": "a router for routing on websites",
|
||||||
"npmPackagename": "@pushrocks/smartrouter",
|
"npmPackagename": "@pushrocks/smartrouter",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"projectDomain": "push.rocks"
|
"projectDomain": "push.rocks"
|
||||||
|
11012
package-lock.json
generated
11012
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrouter",
|
"name": "@pushrocks/smartrouter",
|
||||||
"version": "1.0.7",
|
"version": "1.0.14",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a router for routing on websites",
|
"description": "a router for routing on websites",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -10,19 +10,18 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild --web && tsbundle npm --web)",
|
"build": "(tsbuild --web && tsbundle npm --web)",
|
||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.29",
|
||||||
"@gitzone/tsbundle": "^1.0.72",
|
"@gitzone/tsbundle": "^2.0.7",
|
||||||
"@gitzone/tstest": "^1.0.43",
|
"@gitzone/tstest": "^1.0.60",
|
||||||
"@pushrocks/tapbundle": "^3.2.9",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^14.0.23",
|
"@types/node": "^18.15.3"
|
||||||
"tslint": "^6.1.2",
|
|
||||||
"tslint-config-prettier": "^1.15.0"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"path-to-regexp": "^6.1.0"
|
"path-to-regexp": "^6.2.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@ -38,5 +37,6 @@
|
|||||||
],
|
],
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 chrome versions"
|
"last 1 chrome versions"
|
||||||
]
|
],
|
||||||
|
"type": "module"
|
||||||
}
|
}
|
||||||
|
4460
pnpm-lock.yaml
generated
Normal file
4460
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -21,7 +21,6 @@ Code Style | [](htt
|
|||||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@ -33,7 +32,6 @@ We are always happy for code contributions. If you are not the code contributing
|
|||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
## Legal
|
||||||
|
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
|
||||||
|
@ -1,32 +1,46 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, expectAsync, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartrouter from '../ts/index';
|
import * as smartrouter from '../ts/index.js';
|
||||||
|
|
||||||
let testrouter: smartrouter.SmartRouter;
|
let testrouter: smartrouter.SmartRouter;
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
testrouter = new smartrouter.SmartRouter({});
|
testrouter = new smartrouter.SmartRouter({});
|
||||||
expect(testrouter).to.be.instanceOf(smartrouter.SmartRouter);
|
expect(testrouter).toBeInstanceOf(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).toEqual('hello');
|
||||||
done.resolve();
|
done.resolve();
|
||||||
});
|
});
|
||||||
testrouter.pushUrl('/myawesomeroute/hello');
|
testrouter.pushUrl('/myawesomeroute/hello');
|
||||||
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).toEqual('hello2');
|
||||||
done.resolve();
|
done.resolve();
|
||||||
});
|
});
|
||||||
testrouter.pushUrl('/myawesomeroute2/hello2');
|
testrouter.pushUrl('/myawesomeroute2/hello2');
|
||||||
await done.promise;
|
await done.promise;
|
||||||
expect(window.location.href).to.equal('http://localhost:3007/myawesomeroute2/hello2');
|
expect(window.location.href).toEqual('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).toEqual('hello2');
|
||||||
|
expect(routeInfoArg.queryParams.aparam).toEqual('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).toEqual('http://localhost:3007/myawesomeroute2/hello2?aparam=Yes');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartrouter',
|
||||||
|
version: '1.0.14',
|
||||||
|
description: 'a router for routing on websites'
|
||||||
|
}
|
93
ts/index.ts
93
ts/index.ts
@ -1,92 +1 @@
|
|||||||
import * as plugins from './smartrouter.plugins';
|
export * from './smartrouter.classes.smartrouter.js';
|
||||||
|
|
||||||
const routeLog = (message) => {
|
|
||||||
console.log(`%c[Router]%c ${message}`, 'color: rgb(255, 105, 100);', 'color: inherit');
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface IRouterOptions {
|
|
||||||
debug?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type THandlerFunction = <T extends object>(routeArg: IRouteInfo) => Promise<any>;
|
|
||||||
export interface IRouteInfo {
|
|
||||||
path: string;
|
|
||||||
index: number;
|
|
||||||
params: { [key: string]: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router
|
|
||||||
*/
|
|
||||||
export class SmartRouter {
|
|
||||||
public options: IRouterOptions = {
|
|
||||||
debug: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the routes we are handling
|
|
||||||
*/
|
|
||||||
public routes: Array<{
|
|
||||||
matchFunction: plugins.pathToRegExp.MatchFunction;
|
|
||||||
handler: THandlerFunction;
|
|
||||||
}> = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates an instance of Router.
|
|
||||||
*/
|
|
||||||
constructor(optionsArg: IRouterOptions) {
|
|
||||||
// lets set the router options
|
|
||||||
this.options = {
|
|
||||||
...this.options,
|
|
||||||
...optionsArg,
|
|
||||||
};
|
|
||||||
|
|
||||||
// lets subscribe to route changes
|
|
||||||
window.addEventListener('popstate', (popStateEventArg) => {
|
|
||||||
popStateEventArg.preventDefault();
|
|
||||||
this._handleRouteState();
|
|
||||||
});
|
|
||||||
window.addEventListener('DOMContentLoaded', () => {
|
|
||||||
this._handleRouteState();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Push route state to history stack
|
|
||||||
*/
|
|
||||||
public async pushUrl(url: string = '/', state: any = {}) {
|
|
||||||
if (url !== window.location.pathname) {
|
|
||||||
window.history.pushState(state, window.document.title, url);
|
|
||||||
} else {
|
|
||||||
window.history.replaceState(state, window.document.title, url);
|
|
||||||
}
|
|
||||||
await this._handleRouteState();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attach route with handler
|
|
||||||
* @param {string|RegExp} routeArg
|
|
||||||
* @param {function} handlerArg
|
|
||||||
*/
|
|
||||||
public on(routeArg: string, handlerArg: THandlerFunction) {
|
|
||||||
this.routes.push({
|
|
||||||
matchFunction: plugins.pathToRegExp.match(routeArg),
|
|
||||||
handler: handlerArg,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply routes handler to current route
|
|
||||||
*/
|
|
||||||
async _handleRouteState() {
|
|
||||||
const currentLocation = window.location.pathname;
|
|
||||||
const wantedRoutes = this.routes.filter((routeArg) => {
|
|
||||||
return !!routeArg.matchFunction(currentLocation);
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const wantedRoute of wantedRoutes) {
|
|
||||||
const routeResult = wantedRoute.matchFunction(currentLocation);
|
|
||||||
wantedRoute.handler(routeResult.valueOf() as IRouteInfo); // not waiting here
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
35
ts/smartrouter.classes.queryparams.ts
Normal file
35
ts/smartrouter.classes.queryparams.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import * as plugins from './smartrouter.plugins.js';
|
||||||
|
|
||||||
|
export class QueryParams {
|
||||||
|
constructor() {}
|
||||||
|
|
||||||
|
public getAllAsObject() {
|
||||||
|
const urlSearchParams = new URLSearchParams(window.location.search);
|
||||||
|
return Object.fromEntries((urlSearchParams as any).entries());
|
||||||
|
}
|
||||||
|
|
||||||
|
public setQueryParam(
|
||||||
|
queryKeyArg: string,
|
||||||
|
queryContentArg: string,
|
||||||
|
pushOrReplaceArg: 'push' | 'replace' = 'replace'
|
||||||
|
) {
|
||||||
|
var queryParams = new URLSearchParams(window.location.search);
|
||||||
|
queryParams.set(queryKeyArg, queryContentArg);
|
||||||
|
pushOrReplaceArg === 'push'
|
||||||
|
? history.pushState(null, null, '?' + queryParams.toString())
|
||||||
|
: history.replaceState(null, null, '?' + queryParams.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public deleteQueryParam(queryKeyArg: string, pushOrReplaceArg: 'push' | 'replace' = 'replace') {
|
||||||
|
var queryParams = new URLSearchParams(window.location.search);
|
||||||
|
queryParams.delete(queryKeyArg);
|
||||||
|
pushOrReplaceArg === 'push'
|
||||||
|
? history.pushState(null, null, '?' + queryParams.toString())
|
||||||
|
: history.replaceState(null, null, '?' + queryParams.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public getQueryParam(queryParamName: string) {
|
||||||
|
const queryParams = this.getAllAsObject();
|
||||||
|
return queryParams[queryParamName];
|
||||||
|
}
|
||||||
|
}
|
102
ts/smartrouter.classes.smartrouter.ts
Normal file
102
ts/smartrouter.classes.smartrouter.ts
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
import * as plugins from './smartrouter.plugins.js';
|
||||||
|
|
||||||
|
import { QueryParams } from './smartrouter.classes.queryparams.js';
|
||||||
|
|
||||||
|
const routeLog = (message: string) => {
|
||||||
|
console.log(`%c[Router]%c ${message}`, 'color: rgb(255, 105, 100);', 'color: inherit');
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface IRouterOptions {
|
||||||
|
debug?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type THandlerFunction = <T extends object>(routeArg: IRouteInfo) => Promise<any>;
|
||||||
|
export interface IRouteInfo {
|
||||||
|
path: string;
|
||||||
|
index: number;
|
||||||
|
params: { [key: string]: string };
|
||||||
|
queryParams: { [key: string]: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Router
|
||||||
|
*/
|
||||||
|
export class SmartRouter {
|
||||||
|
public options: IRouterOptions = {
|
||||||
|
debug: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
public queryParams = new QueryParams();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the routes we are handling
|
||||||
|
*/
|
||||||
|
public routes: Array<{
|
||||||
|
matchFunction: plugins.pathToRegExp.MatchFunction;
|
||||||
|
handler: THandlerFunction;
|
||||||
|
}> = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an instance of Router.
|
||||||
|
*/
|
||||||
|
constructor(optionsArg: IRouterOptions) {
|
||||||
|
// lets set the router options
|
||||||
|
this.options = {
|
||||||
|
...this.options,
|
||||||
|
...optionsArg,
|
||||||
|
};
|
||||||
|
|
||||||
|
// lets subscribe to route changes
|
||||||
|
window.addEventListener('popstate', (popStateEventArg) => {
|
||||||
|
popStateEventArg.preventDefault();
|
||||||
|
this._handleRouteState();
|
||||||
|
});
|
||||||
|
window.addEventListener('DOMContentLoaded', () => {
|
||||||
|
this._handleRouteState();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push route state to history stack
|
||||||
|
*/
|
||||||
|
public async pushUrl(url: string = '/', state: any = {}) {
|
||||||
|
if (url !== window.location.pathname) {
|
||||||
|
window.history.pushState(state, window.document.title, url);
|
||||||
|
} else {
|
||||||
|
window.history.replaceState(state, window.document.title, url);
|
||||||
|
}
|
||||||
|
await this._handleRouteState();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attach route with handler
|
||||||
|
* @param {string|RegExp} routeArg
|
||||||
|
* @param {function} handlerArg
|
||||||
|
*/
|
||||||
|
public on(routeArg: string, handlerArg: THandlerFunction) {
|
||||||
|
this.routes.push({
|
||||||
|
matchFunction: plugins.pathToRegExp.match(routeArg),
|
||||||
|
handler: handlerArg,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply routes handler to current route
|
||||||
|
*/
|
||||||
|
async _handleRouteState() {
|
||||||
|
const currentLocation = window.location.pathname;
|
||||||
|
|
||||||
|
// lets find all wanted routes.
|
||||||
|
const wantedRoutes = this.routes.filter((routeArg) => {
|
||||||
|
return !!routeArg.matchFunction(currentLocation);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const wantedRoute of wantedRoutes) {
|
||||||
|
const routeResult = wantedRoute.matchFunction(currentLocation);
|
||||||
|
wantedRoute.handler({
|
||||||
|
...(routeResult.valueOf() as Object),
|
||||||
|
queryParams: this.queryParams.getAllAsObject(), // TODO check wether entries is supported in typings
|
||||||
|
} as IRouteInfo); // not waiting here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
Reference in New Issue
Block a user