Skip to content

Commit

Permalink
Type declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkumardusad committed Feb 28, 2023
1 parent d5502a0 commit a01232d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
7 changes: 1 addition & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ declare interface Route {

declare type handler = (req: any, res: any) => void;

interface RouterOptions {
caseSensitive?: boolean;
host?: string;
}

export declare class Router {
constructor(options?: RouterOptions);
constructor(options?: { caseSensitive?: boolean; host?: string });
checkout(path: string, ...callbacks: any[]): this;
copy(path: string, ...callbacks: any[]): this;
delete(path: string, ...callbacks: any[]): this;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@routejs/router",
"version": "2.1.0",
"version": "2.1.1",
"description": "Fast and lightweight http routing engine for nodejs",
"main": "index.mjs",
"type": "module",
Expand Down
14 changes: 7 additions & 7 deletions src/route.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ module.exports = class Route {
caseSensitive = false;

constructor({ host, method, path, name, group, callbacks, caseSensitive }) {
if (!!host && !(typeof host === "string" || host instanceof String)) {
if (host && !(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: route host accepts only string as an argument"
);
}

if (!!method) {
if (method) {
if (Array.isArray(method)) {
method = method.map((e) => {
if (!(typeof e === "string" || e instanceof String)) {
Expand Down Expand Up @@ -49,7 +49,7 @@ module.exports = class Route {
}
}

if (!!path && !(typeof path === "string" || path instanceof String)) {
if (path && !(typeof path === "string" || path instanceof String)) {
throw new TypeError(
"Error: route path accepts only string as an argument"
);
Expand Down Expand Up @@ -80,7 +80,7 @@ module.exports = class Route {
if (typeof callback !== "function") {
throw new TypeError(
`Error: ${
!!path ? "route" : "middleware"
path ? "route" : "middleware"
} callback accepts only function as an argument`
);
}
Expand All @@ -95,7 +95,7 @@ module.exports = class Route {
}

match({ host, method, path }) {
if (!!host && !(typeof host === "string" || host instanceof String)) {
if (host && !(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: request host accepts only string as an argument"
);
Expand Down Expand Up @@ -134,7 +134,7 @@ module.exports = class Route {
subdomains: {},
};

if (!!this.hostRegexp) {
if (this.hostRegexp) {
const match = this.hostRegexp.exec(host);
if (match === null) {
return false;
Expand All @@ -150,7 +150,7 @@ module.exports = class Route {
}
}

if (!!this.method) {
if (this.method) {
if (Array.isArray(this.method)) {
if (!this.method.includes(method.toUpperCase())) {
return false;
Expand Down
14 changes: 7 additions & 7 deletions src/route.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export default class Route {
caseSensitive = false;

constructor({ host, method, path, name, group, callbacks, caseSensitive }) {
if (!!host && !(typeof host === "string" || host instanceof String)) {
if (host && !(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: route host accepts only string as an argument"
);
}

if (!!method) {
if (method) {
if (Array.isArray(method)) {
method = method.map((e) => {
if (!(typeof e === "string" || e instanceof String)) {
Expand Down Expand Up @@ -49,7 +49,7 @@ export default class Route {
}
}

if (!!path && !(typeof path === "string" || path instanceof String)) {
if (path && !(typeof path === "string" || path instanceof String)) {
throw new TypeError(
"Error: route path accepts only string as an argument"
);
Expand Down Expand Up @@ -80,7 +80,7 @@ export default class Route {
if (typeof callback !== "function") {
throw new TypeError(
`Error: ${
!!path ? "route" : "middleware"
path ? "route" : "middleware"
} callback accepts only function as an argument`
);
}
Expand All @@ -95,7 +95,7 @@ export default class Route {
}

match({ host, method, path }) {
if (!!host && !(typeof host === "string" || host instanceof String)) {
if (host && !(typeof host === "string" || host instanceof String)) {
throw new TypeError(
"Error: request host accepts only string as an argument"
);
Expand Down Expand Up @@ -134,7 +134,7 @@ export default class Route {
subdomains: {},
};

if (!!this.hostRegexp) {
if (this.hostRegexp) {
const match = this.hostRegexp.exec(host);
if (match === null) {
return false;
Expand All @@ -150,7 +150,7 @@ export default class Route {
}
}

if (!!this.method) {
if (this.method) {
if (Array.isArray(this.method)) {
if (!this.method.includes(method.toUpperCase())) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/router.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ module.exports = class Router {
}
});
let routePath = namedRoute && namedRoute.path;
if (!!namedRoute.params) {
if (namedRoute.params) {
if (
!Array.isArray(params) ||
namedRoute.params.length !== params.length
Expand Down
2 changes: 1 addition & 1 deletion src/router.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default class Router {
}
});
let routePath = namedRoute && namedRoute.path;
if (!!namedRoute.params) {
if (namedRoute.params) {
if (
!Array.isArray(params) ||
namedRoute.params.length !== params.length
Expand Down

0 comments on commit a01232d

Please sign in to comment.