Comparing version 0.1.2 to 1.0.0-beta
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -13,4 +17,5 @@ if (k2 === undefined) k2 = k; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./route"), exports); | ||
__exportStar(require("./router"), exports); | ||
__exportStar(require("./contracts.js"), exports); | ||
__exportStar(require("./route.js"), exports); | ||
__exportStar(require("./router.js"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -11,3 +11,2 @@ "use strict"; | ||
this._static = true; | ||
this._params = {}; | ||
this.regex = null; | ||
@@ -25,2 +24,5 @@ this.paramKeys = []; | ||
} | ||
as(name) { | ||
this._name = name; | ||
} | ||
setPathParamKeys() { | ||
@@ -41,5 +43,5 @@ const keys = this.path.match(/:\w+/g); | ||
} | ||
match(method, path) { | ||
match(method, path, params = {}) { | ||
// route method should match with given method | ||
if (this.method !== method) { | ||
if (this.method != '*' && this.method !== method) { | ||
return false; | ||
@@ -57,11 +59,8 @@ } | ||
for (let i = 2; i < matched.length; i++) { | ||
this._params[this.paramKeys[i - 2]] = matched[i]; | ||
params[this.paramKeys[i - 2]] = matched[i]; | ||
} | ||
return true; | ||
} | ||
params() { | ||
return this._params; | ||
} | ||
clone() { | ||
return new Route(this.method, this.path, this.handler, this.metadata); | ||
return new Route(this.method, this.path, this.handler, { ...this.metadata }); | ||
} | ||
@@ -68,0 +67,0 @@ meta(meta) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Router = void 0; | ||
const route_1 = require("./route"); | ||
const route_js_1 = require("./route.js"); | ||
class Router { | ||
constructor(options = {}) { | ||
this.routes = []; | ||
this.opts = { | ||
prefix: '', | ||
merge: false, | ||
ignoreTrailingSlash: true, | ||
}; | ||
this.staticRoutes = {}; | ||
@@ -18,2 +13,3 @@ this.opts = { | ||
ignoreTrailingSlash: true, | ||
staticref: true, | ||
...options, | ||
@@ -25,3 +21,3 @@ }; | ||
} | ||
if (this.opts.prefix.length > 1 && this.opts.prefix.substr(-1) === '/') { | ||
if (this.opts.prefix.length > 1 && this.opts.prefix.slice(-1) === '/') { | ||
throw new Error(`Prefix ${this.opts.prefix} must not end with slash(/).`); | ||
@@ -52,2 +48,12 @@ } | ||
} | ||
all(methods, path, handler) { | ||
const routes = []; | ||
for (const method of methods) { | ||
routes.push(this.add(method, path, handler)); | ||
} | ||
return routes; | ||
} | ||
any(path, handler) { | ||
return this.add('*', path, handler); | ||
} | ||
add(method, path, handler) { | ||
@@ -57,6 +63,6 @@ if (path[0] !== '/') { | ||
} | ||
if (path.length > 1 && path.substr(-1) === '/') { | ||
if (path.length > 1 && path.slice(-1) === '/') { | ||
throw new Error(`Path ${path} must not end with slash(/).`); | ||
} | ||
const route = new route_1.Route(method, this.makePath(path), handler); | ||
const route = new route_js_1.Route(method, this.makePath(path), handler); | ||
this.routes.push(route); | ||
@@ -68,2 +74,8 @@ this.static(route); | ||
if (route.static()) { | ||
// if (Array.isArray(route.method)) { | ||
// for (const m of route.method) { | ||
// this.staticRoutes[`${m}${route.path}`] = route; | ||
// } | ||
// return; | ||
// } | ||
this.staticRoutes[`${route.method}${route.path}`] = route; | ||
@@ -73,3 +85,3 @@ } | ||
merge(method, path, handler) { | ||
const route = new route_1.Route(method, path, handler); | ||
const route = new route_js_1.Route(method, path, handler); | ||
this.routes.push(route); | ||
@@ -92,2 +104,3 @@ this.static(route); | ||
this.routes.push(...router.routes.map((route) => { | ||
route = route.clone(); | ||
this.static(route); | ||
@@ -123,2 +136,3 @@ return route; | ||
} | ||
throw new Error('Expects an instance of Router.'); | ||
} | ||
@@ -135,3 +149,3 @@ else if (args.length === 2) { | ||
} | ||
find(method, path) { | ||
find(method, path, params = {}) { | ||
const staticRoute = this.staticRoutes[`${method}${path}`]; | ||
@@ -144,3 +158,3 @@ if (staticRoute) { | ||
const r = this.routes[i]; | ||
const matched = r.match(method, path); | ||
const matched = r.match(method, path, params); | ||
if (matched) { | ||
@@ -147,0 +161,0 @@ return r; |
@@ -1,3 +0,4 @@ | ||
export * from './route'; | ||
export * from './router'; | ||
export * from './contracts.js'; | ||
export * from './route.js'; | ||
export * from './router.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -8,3 +8,2 @@ export class Route { | ||
this._static = true; | ||
this._params = {}; | ||
this.regex = null; | ||
@@ -22,2 +21,5 @@ this.paramKeys = []; | ||
} | ||
as(name) { | ||
this._name = name; | ||
} | ||
setPathParamKeys() { | ||
@@ -38,5 +40,5 @@ const keys = this.path.match(/:\w+/g); | ||
} | ||
match(method, path) { | ||
match(method, path, params = {}) { | ||
// route method should match with given method | ||
if (this.method !== method) { | ||
if (this.method != '*' && this.method !== method) { | ||
return false; | ||
@@ -54,11 +56,8 @@ } | ||
for (let i = 2; i < matched.length; i++) { | ||
this._params[this.paramKeys[i - 2]] = matched[i]; | ||
params[this.paramKeys[i - 2]] = matched[i]; | ||
} | ||
return true; | ||
} | ||
params() { | ||
return this._params; | ||
} | ||
clone() { | ||
return new Route(this.method, this.path, this.handler, this.metadata); | ||
return new Route(this.method, this.path, this.handler, { ...this.metadata }); | ||
} | ||
@@ -65,0 +64,0 @@ meta(meta) { |
@@ -1,10 +0,5 @@ | ||
import { Route } from './route'; | ||
import { Route } from './route.js'; | ||
export class Router { | ||
constructor(options = {}) { | ||
this.routes = []; | ||
this.opts = { | ||
prefix: '', | ||
merge: false, | ||
ignoreTrailingSlash: true, | ||
}; | ||
this.staticRoutes = {}; | ||
@@ -15,2 +10,3 @@ this.opts = { | ||
ignoreTrailingSlash: true, | ||
staticref: true, | ||
...options, | ||
@@ -22,3 +18,3 @@ }; | ||
} | ||
if (this.opts.prefix.length > 1 && this.opts.prefix.substr(-1) === '/') { | ||
if (this.opts.prefix.length > 1 && this.opts.prefix.slice(-1) === '/') { | ||
throw new Error(`Prefix ${this.opts.prefix} must not end with slash(/).`); | ||
@@ -49,2 +45,12 @@ } | ||
} | ||
all(methods, path, handler) { | ||
const routes = []; | ||
for (const method of methods) { | ||
routes.push(this.add(method, path, handler)); | ||
} | ||
return routes; | ||
} | ||
any(path, handler) { | ||
return this.add('*', path, handler); | ||
} | ||
add(method, path, handler) { | ||
@@ -54,3 +60,3 @@ if (path[0] !== '/') { | ||
} | ||
if (path.length > 1 && path.substr(-1) === '/') { | ||
if (path.length > 1 && path.slice(-1) === '/') { | ||
throw new Error(`Path ${path} must not end with slash(/).`); | ||
@@ -65,2 +71,8 @@ } | ||
if (route.static()) { | ||
// if (Array.isArray(route.method)) { | ||
// for (const m of route.method) { | ||
// this.staticRoutes[`${m}${route.path}`] = route; | ||
// } | ||
// return; | ||
// } | ||
this.staticRoutes[`${route.method}${route.path}`] = route; | ||
@@ -88,2 +100,3 @@ } | ||
this.routes.push(...router.routes.map((route) => { | ||
route = route.clone(); | ||
this.static(route); | ||
@@ -119,2 +132,3 @@ return route; | ||
} | ||
throw new Error('Expects an instance of Router.'); | ||
} | ||
@@ -131,3 +145,3 @@ else if (args.length === 2) { | ||
} | ||
find(method, path) { | ||
find(method, path, params = {}) { | ||
const staticRoute = this.staticRoutes[`${method}${path}`]; | ||
@@ -140,3 +154,3 @@ if (staticRoute) { | ||
const r = this.routes[i]; | ||
const matched = r.match(method, path); | ||
const matched = r.match(method, path, params); | ||
if (matched) { | ||
@@ -143,0 +157,0 @@ return r; |
{ | ||
"name": "repulser", | ||
"version": "0.1.2", | ||
"version": "1.0.0-beta", | ||
"description": "Routing library for node.js and browser", | ||
@@ -21,3 +21,3 @@ "type": "module", | ||
"scripts": { | ||
"test": "NODE_OPTIONS=\"--loader ts-node/esm --experimental-specifier-resolution=node\" tap --ts ./src/*.spec.ts", | ||
"test": "NODE_OPTIONS=\"--loader ts-node/esm --experimental-specifier-resolution=node\" c8 tap --ts ./src/*.spec.ts --no-coverage", | ||
"compile": "npx tsc -p tsconfig.json && npx tsc -p tsconfig.cjs.json && node fixup.cjs", | ||
@@ -32,6 +32,5 @@ "compile:docs": "npx typedoc --plugin typedoc-plugin-markdown", | ||
"keywords": [ | ||
"javascript", | ||
"nodejs", | ||
"ioc", | ||
"container", | ||
"route", | ||
"router", | ||
"fast", | ||
"typescript" | ||
@@ -47,8 +46,9 @@ ], | ||
"@types/tap": "^15.0.5", | ||
"c8": "^7.12.0", | ||
"tap": "^15.0.10", | ||
"ts-node": "^10.3.0", | ||
"typedoc": "^0.21.9", | ||
"typedoc-plugin-markdown": "3.10.4" | ||
}, | ||
"dependencies": {} | ||
} | ||
"typedoc": "^0.23.14", | ||
"typedoc-plugin-markdown": "3.13", | ||
"typescript": "4.6" | ||
} | ||
} |
@@ -5,3 +5,4 @@ export interface IRouterOptions { | ||
ignoreTrailingSlash: boolean; | ||
staticref: boolean; | ||
} | ||
export declare type HTTP_METHOD = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS'; |
@@ -1,2 +0,3 @@ | ||
export * from './route'; | ||
export * from './router'; | ||
export * from './contracts.js'; | ||
export * from './route.js'; | ||
export * from './router.js'; |
/// <reference types="node" /> | ||
import { HTTP_METHOD } from "./contracts"; | ||
import { HTTP_METHOD } from "./contracts.js"; | ||
export declare class Route<T = any> { | ||
@@ -9,3 +9,2 @@ readonly method: HTTP_METHOD; | ||
private _static; | ||
private readonly _params; | ||
private regex; | ||
@@ -16,6 +15,6 @@ private paramKeys; | ||
get name(): string; | ||
setPathParamKeys(): void; | ||
setRegex(): void; | ||
match(method: HTTP_METHOD, path: string): boolean; | ||
params(): NodeJS.Dict<string>; | ||
as(name: string): void; | ||
protected setPathParamKeys(): void; | ||
protected setRegex(): void; | ||
match(method: HTTP_METHOD, path: string, params?: NodeJS.Dict<string>): boolean; | ||
clone(): Route<T>; | ||
@@ -22,0 +21,0 @@ meta(meta?: NodeJS.Dict<any>): NodeJS.Dict<any>; |
/// <reference types="node" /> | ||
import { IRouterOptions, HTTP_METHOD } from './contracts'; | ||
import { Route } from './route'; | ||
import { IRouterOptions, HTTP_METHOD } from './contracts.js'; | ||
import { Route } from './route.js'; | ||
export declare class Router { | ||
readonly routes: Array<Route>; | ||
private opts; | ||
readonly opts: IRouterOptions; | ||
readonly staticRoutes: NodeJS.Dict<Route>; | ||
@@ -16,2 +16,4 @@ constructor(options?: Partial<IRouterOptions>); | ||
options<T>(path: string, handler: T): Route<T>; | ||
all<T>(methods: HTTP_METHOD[], path: string, handler: T): Route<T>[]; | ||
any<T>(path: string, handler: T): Route<T>; | ||
add<T = any>(method: HTTP_METHOD, path: string, handler: T): Route<T>; | ||
@@ -23,3 +25,3 @@ static(route: Route): void; | ||
use(...args: any[]): void; | ||
find(method: HTTP_METHOD, path: string): false | Route<any>; | ||
find(method: HTTP_METHOD, path: string, params?: NodeJS.Dict<string>): false | Route<any>; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40352
533
10