@akala/core
Version:
59 lines • 2.09 kB
JavaScript
import { MiddlewareComposite } from '../middlewares/composite-sync.js';
import { match, parse } from '../uri-template/index.js';
export class MiddlewareRoute extends MiddlewareComposite {
// delimiter: string;
routePath;
constructor(route) {
super(route.toString());
// this.delimiter = options && options.delimiter || '/';
this.routePath = Array.isArray(route) ? route : parse(route);
// if ('keys' in routePath)
// this.params = routePath.keys;
// this.match = match(route.toString(), options)
}
route(...args) {
return new MiddlewareRoute(...args);
}
isApplicable;
handle(...context) {
const req = context[0];
const isMatch = match(req.path, this.routePath);
if (isMatch && (!this.isApplicable || this.isApplicable(req))) {
const oldPath = req.path;
const c = oldPath[oldPath.length - isMatch.remainder.length];
if (c && c !== '/')
return;
req.path = isMatch.remainder || '/';
const oldParams = req.params;
req.params = isMatch.variables;
try {
return super.handle(...context);
}
finally {
req.params = oldParams;
req.path = oldPath;
}
;
}
return;
}
useMiddleware(route, ...middlewares) {
if (typeof route === 'string' || Array.isArray(route)) {
const routed = new MiddlewareRoute(route);
routed.useMiddleware(...middlewares);
super.useMiddleware(routed);
}
else
super.useMiddleware(route, ...middlewares);
return this;
}
use(route, ...middlewares) {
if (typeof route === 'string' || Array.isArray(route)) {
const routed = new MiddlewareRoute(route);
routed.use(...middlewares);
return super.useMiddleware(routed);
}
return super.use(route, ...middlewares);
}
}
//# sourceMappingURL=route.js.map