Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
f24520ea89 | |||
5072728118 | |||
bb4df0c47f | |||
101533cddd | |||
3b31f88b0c | |||
daeaac2367 | |||
9b9675bd96 | |||
e7d7865750 | |||
b7feb5098b | |||
76fcf14831 | |||
50e14811d4 | |||
f7e5af4113 | |||
806e121144 | |||
b1ecf655ad | |||
8955fd5fa3 | |||
9c4a81d9cd |
@ -19,23 +19,35 @@ mirror:
|
|||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
audit:
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --production --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
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 npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- 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
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
@ -50,9 +62,7 @@ testStable:
|
|||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- priv
|
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
@ -63,9 +73,7 @@ testBuild:
|
|||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@ -85,6 +93,8 @@ release:
|
|||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g tslint typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -15,7 +15,7 @@
|
|||||||
"properties": {
|
"properties": {
|
||||||
"projectType": {
|
"projectType": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["website", "element", "service", "npm"]
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
25065
package-lock.json
generated
25065
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrouter",
|
"name": "@pushrocks/smartrouter",
|
||||||
"version": "1.0.2",
|
"version": "1.0.10",
|
||||||
"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",
|
||||||
@ -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.69",
|
"@gitzone/tsbundle": "^1.0.87",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.57",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@pushrocks/tapbundle": "^3.2.14",
|
||||||
"@types/node": "^10.11.7",
|
"@types/node": "^16.7.13",
|
||||||
"tslint": "^5.11.0",
|
"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/**/*",
|
||||||
@ -35,5 +35,8 @@
|
|||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ TypeScript Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
Code Style | [](https://lossless.cloud)
|
Code Style | [](https://lossless.cloud)
|
||||||
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)
|
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||||
|
|
||||||
|
46
test/test.browser.ts
Normal file
46
test/test.browser.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
import * as smartrouter from '../ts/index';
|
||||||
|
|
||||||
|
let testrouter: smartrouter.SmartRouter;
|
||||||
|
|
||||||
|
tap.test('first test', async () => {
|
||||||
|
testrouter = new smartrouter.SmartRouter({});
|
||||||
|
expect(testrouter).to.be.instanceOf(smartrouter.SmartRouter);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should handle a route change', async (toolsArg) => {
|
||||||
|
const done = toolsArg.defer();
|
||||||
|
testrouter.on('/myawesomeroute/:any', async (routeInfoArg) => {
|
||||||
|
expect(routeInfoArg.params.any).to.equal('hello');
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
testrouter.pushUrl('/myawesomeroute/hello');
|
||||||
|
await done.promise;
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should handle a route change', async (toolsArg) => {
|
||||||
|
const done = toolsArg.defer();
|
||||||
|
testrouter.on('/myawesomeroute2/:wow', async (routeInfoArg) => {
|
||||||
|
expect(routeInfoArg.params.wow).to.equal('hello2');
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
testrouter.pushUrl('/myawesomeroute2/hello2');
|
||||||
|
await done.promise;
|
||||||
|
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();
|
@ -1,8 +0,0 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
|
||||||
import * as smartrouter from '../ts/index';
|
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
|
||||||
const router = new smartrouter.SmartRouter({});
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.start();
|
|
28
ts/index.ts
28
ts/index.ts
@ -8,6 +8,14 @@ export interface IRouterOptions {
|
|||||||
debug?: boolean;
|
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
|
* Router
|
||||||
*/
|
*/
|
||||||
@ -19,7 +27,10 @@ export class SmartRouter {
|
|||||||
/**
|
/**
|
||||||
* the routes we are handling
|
* the routes we are handling
|
||||||
*/
|
*/
|
||||||
public routes: Array<{ matchFunction: plugins.pathToRegExp.MatchFunction; handler: <T extends object>(matchArg: plugins.pathToRegExp.Match<T>) => Promise<any> }> = [];
|
public routes: Array<{
|
||||||
|
matchFunction: plugins.pathToRegExp.MatchFunction;
|
||||||
|
handler: THandlerFunction;
|
||||||
|
}> = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an instance of Router.
|
* Creates an instance of Router.
|
||||||
@ -58,8 +69,7 @@ export class SmartRouter {
|
|||||||
* @param {string|RegExp} routeArg
|
* @param {string|RegExp} routeArg
|
||||||
* @param {function} handlerArg
|
* @param {function} handlerArg
|
||||||
*/
|
*/
|
||||||
public on(routeArg: string, handlerArg: () => Promise<any>) {
|
public on(routeArg: string, handlerArg: THandlerFunction) {
|
||||||
|
|
||||||
this.routes.push({
|
this.routes.push({
|
||||||
matchFunction: plugins.pathToRegExp.match(routeArg),
|
matchFunction: plugins.pathToRegExp.match(routeArg),
|
||||||
handler: handlerArg,
|
handler: handlerArg,
|
||||||
@ -71,13 +81,19 @@ export class SmartRouter {
|
|||||||
*/
|
*/
|
||||||
async _handleRouteState() {
|
async _handleRouteState() {
|
||||||
const currentLocation = window.location.pathname;
|
const currentLocation = window.location.pathname;
|
||||||
const wantedRoute = this.routes.find(routeArg => {
|
const urlSearchParams = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
|
// lets find all wanted routes.
|
||||||
|
const wantedRoutes = this.routes.filter((routeArg) => {
|
||||||
return !!routeArg.matchFunction(currentLocation);
|
return !!routeArg.matchFunction(currentLocation);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (wantedRoute) {
|
for (const wantedRoute of wantedRoutes) {
|
||||||
const routeResult = wantedRoute.matchFunction(currentLocation);
|
const routeResult = wantedRoute.matchFunction(currentLocation);
|
||||||
await wantedRoute.handler(routeResult);
|
wantedRoute.handler({
|
||||||
|
...(routeResult.valueOf() as Object),
|
||||||
|
queryParams: Object.fromEntries(urlSearchParams.entries()),
|
||||||
|
} as IRouteInfo); // not waiting here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import * as pathToRegExp from 'path-to-regexp';
|
import * as pathToRegExp from 'path-to-regexp';
|
||||||
|
|
||||||
export {
|
export { pathToRegExp };
|
||||||
pathToRegExp
|
|
||||||
};
|
|
||||||
|
Reference in New Issue
Block a user