Socket
Socket
Sign inDemoInstall

@tinyhttp/router

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tinyhttp/router - npm Package Compare versions

Comparing version 2.2.2 to 2.2.3

tsconfig.tsbuildinfo

6

dist/index.d.ts
export type NextFunction = (err?: any) => void;
export type SyncHandler<Request = any, Response = any> = (req: Request, res: Response, next: NextFunction) => void;
export type AsyncHandler<Request = any, Response = any> = (req: Request, res: Response, next: NextFunction) => Promise<void>;
export type SyncHandler<Request = any, Response = any> = (req: Request, res: Response, next?: NextFunction) => void;
export type AsyncHandler<Request = any, Response = any> = (req: Request, res: Response, next?: NextFunction) => Promise<void>;
export type Handler<Request = any, Response = any> = AsyncHandler<Request, Response> | SyncHandler<Request, Response>;

@@ -86,3 +86,3 @@ declare const METHODS: readonly ["ACL", "BIND", "CHECKOUT", "CONNECT", "COPY", "DELETE", "GET", "HEAD", "LINK", "LOCK", "M-SEARCH", "MERGE", "MKACTIVITY", "MKCALENDAR", "MKCOL", "MOVE", "NOTIFY", "OPTIONS", "PATCH", "POST", "PRI", "PROPFIND", "PROPPATCH", "PURGE", "PUT", "REBIND", "REPORT", "SEARCH", "SOURCE", "SUBSCRIBE", "TRACE", "UNBIND", "UNLINK", "UNLOCK", "UNSUBSCRIBE"];

constructor();
add(method: Method): (path: string | string[] | Handler<Req, Res>, handler?: RouterHandler<Req, Res>, ...handlers: RouterHandler<Req, Res>[]) => this;
add(method: Method): (path: string | string[] | Handler<Req, Res>, handler?: RouterHandler<Req, Res> | undefined, ...handlers: RouterHandler<Req, Res>[]) => this;
msearch(...args: RouterMethodParams<Req, Res>): this;

@@ -89,0 +89,0 @@ all(...args: RouterMethodParams<Req, Res>): this;

@@ -0,159 +1,154 @@

/* HELPER TYPES */
const METHODS = [
"ACL",
"BIND",
"CHECKOUT",
"CONNECT",
"COPY",
"DELETE",
"GET",
"HEAD",
"LINK",
"LOCK",
"M-SEARCH",
"MERGE",
"MKACTIVITY",
"MKCALENDAR",
"MKCOL",
"MOVE",
"NOTIFY",
"OPTIONS",
"PATCH",
"POST",
"PRI",
"PROPFIND",
"PROPPATCH",
"PURGE",
"PUT",
"REBIND",
"REPORT",
"SEARCH",
"SOURCE",
"SUBSCRIBE",
"TRACE",
"UNBIND",
"UNLINK",
"UNLOCK",
"UNSUBSCRIBE"
'ACL',
'BIND',
'CHECKOUT',
'CONNECT',
'COPY',
'DELETE',
'GET',
'HEAD',
'LINK',
'LOCK',
'M-SEARCH',
'MERGE',
'MKACTIVITY',
'MKCALENDAR',
'MKCOL',
'MOVE',
'NOTIFY',
'OPTIONS',
'PATCH',
'POST',
'PRI',
'PROPFIND',
'PROPPATCH',
'PURGE',
'PUT',
'REBIND',
'REPORT',
'SEARCH',
'SOURCE',
'SUBSCRIBE',
'TRACE',
'UNBIND',
'UNLINK',
'UNLOCK',
'UNSUBSCRIBE'
];
const createMiddlewareFromRoute = ({
path,
handler,
fullPath,
method
}) => ({
method,
handler: handler || path,
path: typeof path === "string" ? path : "/",
fullPath: typeof path === "string" ? fullPath : path
/** HELPER METHODS */
const createMiddlewareFromRoute = ({ path, handler, fullPath, method }) => ({
method,
handler: handler || path,
path: typeof path === 'string' ? path : '/',
fullPath: typeof path === 'string' ? fullPath : path
});
const pushMiddleware = (mw) => ({
path,
handler,
method,
handlers,
type,
fullPaths
}) => {
const m = createMiddlewareFromRoute({ path, handler, method, type, fullPath: fullPaths == null ? void 0 : fullPaths[0] });
let waresFromHandlers = [];
let idx = 1;
if (handlers) {
waresFromHandlers = handlers.flat().map(
(handler2) => createMiddlewareFromRoute({
path,
handler: handler2,
method,
type,
fullPath: fullPaths == null ? null : fullPaths[idx++]
})
);
}
for (const mdw of [m, ...waresFromHandlers])
mw.push({ ...mdw, type });
/**
* Push wares to a middleware array
* @param mw Middleware arrays
*/
export const pushMiddleware = (mw) => ({ path, handler, method, handlers, type, fullPaths }) => {
const m = createMiddlewareFromRoute({ path, handler, method, type, fullPath: fullPaths === null || fullPaths === void 0 ? void 0 : fullPaths[0] });
let waresFromHandlers = [];
let idx = 1;
if (handlers) {
waresFromHandlers = handlers.flat().map((handler) => createMiddlewareFromRoute({
path,
handler: handler,
method,
type,
fullPath: fullPaths == null ? undefined : fullPaths[idx++]
}));
}
for (const mdw of [m, ...waresFromHandlers])
mw.push({ ...mdw, type });
};
class Router {
constructor() {
this.middleware = [];
this.mountpath = "/";
this.apps = {};
for (const m of METHODS) {
this[m.toLowerCase()] = this.add(m);
/**
* tinyhttp Router. Manages middleware and has HTTP methods aliases, e.g. `app.get`, `app.put`
*/
export class Router {
constructor() {
this.middleware = [];
this.mountpath = '/';
this.apps = {};
for (const m of METHODS) {
this[m.toLowerCase()] = this.add(m);
}
}
}
add(method) {
return (...args) => {
const handlers = args.slice(1).flat();
if (Array.isArray(args[0])) {
Object.values(args[0]).forEach((arg) => {
if (typeof arg == "string") {
pushMiddleware(this.middleware)({
path: arg,
handler: handlers[0],
handlers: handlers.slice(1),
method,
type: "route"
});
}
add(method) {
return (...args) => {
const handlers = args.slice(1).flat();
if (Array.isArray(args[0])) {
for (const arg of Object.values(args[0])) {
if (typeof arg === 'string') {
pushMiddleware(this.middleware)({
path: arg,
handler: handlers[0],
handlers: handlers.slice(1),
method,
type: 'route'
});
}
}
}
else {
pushMiddleware(this.middleware)({
path: args[0],
handler: handlers[0],
handlers: handlers.slice(1),
method,
type: 'route'
});
}
return this;
};
}
msearch(...args) {
const handlers = args.slice(1).flat();
pushMiddleware(this.middleware)({
path: args[0],
handler: handlers[0],
handlers: handlers.slice(1),
method: 'M-SEARCH',
type: 'route'
});
} else {
return this;
}
all(...args) {
const handlers = args.slice(1).flat();
pushMiddleware(this.middleware)({
path: args[0],
handler: handlers[0],
handlers: handlers.slice(1),
method,
type: "route"
path: args[0],
handler: handlers[0],
handlers: handlers.slice(1),
type: 'route'
});
}
return this;
};
}
msearch(...args) {
const handlers = args.slice(1).flat();
pushMiddleware(this.middleware)({
path: args[0],
handler: handlers[0],
handlers: handlers.slice(1),
method: "M-SEARCH",
type: "route"
});
return this;
}
all(...args) {
const handlers = args.slice(1).flat();
pushMiddleware(this.middleware)({
path: args[0],
handler: handlers[0],
handlers: handlers.slice(1),
type: "route"
});
return this;
}
/**
* Push middleware to the stack
*/
use(...args) {
const base = args[0];
const handlers = args.slice(1).flat();
if (typeof base === "string") {
pushMiddleware(this.middleware)({
path: base,
handler: handlers[0],
handlers: handlers.slice(1),
type: "mw"
});
} else {
pushMiddleware(this.middleware)({
path: "/",
handler: Array.isArray(base) ? base[0] : base,
handlers: Array.isArray(base) ? [...base.slice(1), ...handlers] : handlers,
type: "mw"
});
return this;
}
return this;
}
/**
* Push middleware to the stack
*/
use(...args) {
const base = args[0];
const handlers = args.slice(1).flat();
if (typeof base === 'string') {
pushMiddleware(this.middleware)({
path: base,
handler: handlers[0],
handlers: handlers.slice(1),
type: 'mw'
});
}
else {
pushMiddleware(this.middleware)({
path: '/',
handler: Array.isArray(base) ? base[0] : base,
handlers: Array.isArray(base)
? [...base.slice(1), ...handlers]
: handlers,
type: 'mw'
});
}
return this;
}
}
export {
Router,
pushMiddleware
};
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map
{
"name": "@tinyhttp/router",
"version": "2.2.2",
"version": "2.2.3",
"type": "module",

@@ -27,9 +27,5 @@ "description": "Router for tinyhttp",

"license": "MIT",
"files": [
"dist"
],
"scripts": {
"dev": "vite",
"build": "vite build"
"build": "tsc"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc