18 Commits

Author SHA1 Message Date
470f47f8bb 1.0.12 2022-01-22 13:29:24 +01:00
8df4e21501 fix(core): update 2022-01-22 13:29:23 +01:00
ee87a6269e 1.0.11 2021-09-08 22:25:52 +02:00
2d76e799a4 fix(core): update 2021-09-08 22:25:52 +02:00
f24520ea89 1.0.10 2021-09-08 22:11:48 +02:00
5072728118 fix(core): update 2021-09-08 22:11:48 +02:00
bb4df0c47f 1.0.9 2021-09-08 21:49:40 +02:00
101533cddd fix(core): update 2021-09-08 21:49:40 +02:00
3b31f88b0c 1.0.8 2021-09-08 21:46:21 +02:00
daeaac2367 fix(core): update 2021-09-08 21:46:21 +02:00
9b9675bd96 1.0.7 2020-11-30 10:40:40 +00:00
e7d7865750 fix(core): update 2020-11-30 10:40:39 +00:00
b7feb5098b 1.0.6 2020-11-30 10:14:22 +00:00
76fcf14831 fix(core): update 2020-11-30 10:14:22 +00:00
50e14811d4 1.0.5 2020-11-30 01:38:22 +00:00
f7e5af4113 fix(core): update 2020-11-30 01:38:21 +00:00
806e121144 1.0.4 2020-07-15 18:51:18 +00:00
b1ecf655ad fix(core): update 2020-07-15 18:51:17 +00:00
9 changed files with 25575 additions and 1428 deletions

View File

@ -19,23 +19,35 @@ mirror:
stage: security
script:
- npmci git mirror
only:
- tags
tags:
- lossless
- docker
- 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
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- 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:
- lossless
- docker
- notpriv
allow_failure: true
# ====================
# test stage
@ -50,9 +62,7 @@ testStable:
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- lossless
- docker
- priv
testBuild:
stage: test
@ -63,9 +73,7 @@ testBuild:
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- lossless
- docker
- notpriv
release:
stage: release
@ -85,6 +93,8 @@ release:
codequality:
stage: metadata
allow_failure: true
only:
- tags
script:
- npmci command npm install -g tslint typescript
- npmci npm prepare

View File

@ -15,7 +15,7 @@
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm"]
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}

26680
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartrouter",
"version": "1.0.3",
"version": "1.0.12",
"private": false,
"description": "a router for routing on websites",
"main": "dist_ts/index.js",
@ -13,16 +13,16 @@
"format": "(gitzone format)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.0.22",
"@gitzone/tsbundle": "^1.0.69",
"@gitzone/tstest": "^1.0.15",
"@pushrocks/tapbundle": "^3.0.7",
"@types/node": "^10.11.7",
"tslint": "^5.11.0",
"@gitzone/tsbuild": "^2.1.29",
"@gitzone/tsbundle": "^1.0.88",
"@gitzone/tstest": "^1.0.60",
"@pushrocks/tapbundle": "^4.0.0",
"@types/node": "^17.0.10",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0"
},
"dependencies": {
"path-to-regexp": "^6.1.0"
"path-to-regexp": "^6.2.0"
},
"files": [
"ts/**/*",
@ -35,5 +35,8 @@
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
]
}

46
test/test.browser.ts Normal file
View File

@ -0,0 +1,46 @@
import { expect, expectAsync, 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).toBeInstanceOf(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).toEqual('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).toEqual('hello2');
done.resolve();
});
testrouter.pushUrl('/myawesomeroute2/hello2');
await done.promise;
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();

View File

@ -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();

View File

@ -1,85 +1 @@
import * as plugins from './smartrouter.plugins';
const routeLog = message => {
console.log(`%c[Router]%c ${message}`, 'color: rgb(255, 105, 100);', 'color: inherit');
};
export interface IRouterOptions {
debug?: boolean;
}
/**
* Router
*/
export class SmartRouter {
public options: IRouterOptions = {
debug: false
};
/**
* the routes we are handling
*/
public routes: Array<{
matchFunction: plugins.pathToRegExp.MatchFunction;
handler: <T extends object>(matchArg: plugins.pathToRegExp.Match<T>) => Promise<any>;
}> = [];
/**
* 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: () => Promise<any>) {
this.routes.push({
matchFunction: plugins.pathToRegExp.match(routeArg),
handler: handlerArg
});
}
/**
* Apply routes handler to current route
*/
async _handleRouteState() {
const currentLocation = window.location.pathname;
const wantedRoute = this.routes.find(routeArg => {
return !!routeArg.matchFunction(currentLocation);
});
if (wantedRoute) {
const routeResult = wantedRoute.matchFunction(currentLocation);
await wantedRoute.handler(routeResult);
}
}
}
export * from './smartrouter.classes.smartrouter';

View File

@ -0,0 +1,31 @@
import * as plugins from './smartrouter.plugins';
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];
}
}

View File

@ -0,0 +1,105 @@
import * as plugins from './smartrouter.plugins';
import { QueryParams } from './smartrouter.classes.queryparams';
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 };
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
}
}
}