@tinyhttp/router
Advanced tools
Comparing version 1.2.6 to 1.2.7
@@ -33,2 +33,10 @@ export declare type NextFunction = (err?: any) => void; | ||
/** | ||
* Push wares to a middleware array | ||
* @param mw Middleware arrays | ||
*/ | ||
export declare const pushMiddleware: <Req extends unknown = any, Res extends unknown = any>(mw: Middleware[]) => ({ path, handler, method, handlers, type }: MethodHandler<Req, Res> & { | ||
method?: Method; | ||
handlers?: RouterHandler<Req, Res>[]; | ||
}) => void; | ||
/** | ||
* tinyhttp Router. Manages middleware and has HTTP methods aliases, e.g. `app.get`, `app.put` | ||
@@ -93,5 +101,2 @@ */ | ||
* Push middleware to the stack | ||
* @param path path that middleware will handle if request URL starts with it | ||
* @param handler handler function | ||
* @param handlers the rest handler functions | ||
*/ | ||
@@ -98,0 +103,0 @@ use(...args: UseMethodParams<Req, Res, App>): this; |
@@ -45,2 +45,6 @@ /* HELPER TYPES */ | ||
}); | ||
/** | ||
* Push wares to a middleware array | ||
* @param mw Middleware arrays | ||
*/ | ||
const pushMiddleware = (mw) => ({ path, handler, method, handlers, type }) => { | ||
@@ -98,11 +102,8 @@ const m = createMiddlewareFromRoute({ path, handler, method, type }); | ||
const handlers = args.slice(1).flat(); | ||
for (const method of METHODS) { | ||
pushMiddleware(this.middleware)({ | ||
path: args[0], | ||
handler: handlers[0], | ||
handlers: handlers.slice(1), | ||
method, | ||
type: 'route' | ||
}); | ||
} | ||
pushMiddleware(this.middleware)({ | ||
path: args[0], | ||
handler: handlers[0], | ||
handlers: handlers.slice(1), | ||
type: 'route' | ||
}); | ||
return this; | ||
@@ -126,62 +127,21 @@ } | ||
* Push middleware to the stack | ||
* @param path path that middleware will handle if request URL starts with it | ||
* @param handler handler function | ||
* @param handlers the rest handler functions | ||
*/ | ||
use(...args) { | ||
const path = args[0]; | ||
const handler = args[1]; | ||
const handlers = args.slice(2).flat(); | ||
// app.use('/subapp', subApp) | ||
if (typeof path === 'string' && handler instanceof Router) { | ||
// Set mountpath to the specified path | ||
handler.mountpath = path; | ||
// Set App parent to current App | ||
handler.parent = this; | ||
// Prefix paths with a mountpath | ||
for (const mw of handler.middleware) | ||
mw.path = mw.path === '/' ? handler.mountpath : handler.mountpath + mw.path; | ||
this.apps[path] = handler; | ||
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' | ||
}); | ||
} | ||
// app.use(subApp) | ||
else if (path instanceof Router) { | ||
// Set App parent to current App | ||
path.parent = this; | ||
// Mount on root | ||
path.mountpath = '/'; | ||
this.apps['/'] = path; | ||
} | ||
else if (!(handler instanceof Router)) { | ||
let totalHandlers = []; | ||
if (typeof path !== 'string' && Array.isArray(path)) { | ||
for (const h of path.slice(1)) | ||
totalHandlers.push(h); | ||
} | ||
if (handler && Array.isArray(handler)) { | ||
if (typeof path === 'string') { | ||
for (const h of handler.slice(1)) | ||
totalHandlers.push(h); | ||
} | ||
else { | ||
for (const h of handler) | ||
totalHandlers.push(h); | ||
} | ||
} | ||
totalHandlers = totalHandlers.concat(handlers); | ||
let mainHandler; | ||
if (typeof path === 'string') { | ||
if (Array.isArray(handler)) | ||
mainHandler = handler[0]; | ||
else | ||
mainHandler = handler; | ||
} | ||
else { | ||
mainHandler = Array.isArray(path) ? path[0] : path; | ||
if (typeof handler === 'function') | ||
totalHandlers.unshift(handler); | ||
} | ||
else { | ||
pushMiddleware(this.middleware)({ | ||
path: path, | ||
handler: mainHandler, | ||
handlers: totalHandlers, | ||
path: '/', | ||
handler: Array.isArray(base) ? base[0] : base, | ||
handlers: Array.isArray(base) | ||
? [...base.slice(1), ...handlers] | ||
: handlers, | ||
type: 'mw' | ||
@@ -194,2 +154,2 @@ }); | ||
export { Router }; | ||
export { Router, pushMiddleware }; |
{ | ||
"name": "@tinyhttp/router", | ||
"version": "1.2.6", | ||
"version": "1.2.7", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Router for tinyhttp", |
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
15023
407