add preprocessor handlers
This commit is contained in:
19
mod.ts
19
mod.ts
@@ -25,6 +25,11 @@ type RouteMiddlewareHandler = (
|
||||
done: () => Promise<number[]>,
|
||||
) => Promise<void>;
|
||||
|
||||
type RoutePreprocessor = (
|
||||
req: RouteRequest,
|
||||
rep: RouteReply,
|
||||
) => void;
|
||||
|
||||
type RouteParam = {
|
||||
idx: number;
|
||||
paramKey: string;
|
||||
@@ -36,6 +41,7 @@ export class HTTPServer {
|
||||
private staticLocalDir?: string;
|
||||
private staticServePath?: string;
|
||||
private notFoundHandler?: RouteHandler;
|
||||
private preprocessors: RoutePreprocessor[] = [];
|
||||
private middlewareHandler?: RouteMiddlewareHandler;
|
||||
|
||||
async listen(options: ListenOptions) {
|
||||
@@ -116,6 +122,10 @@ export class HTTPServer {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.preprocessors.forEach((preProcessor) =>
|
||||
preProcessor(routeRequest, routeReply)
|
||||
);
|
||||
|
||||
let resolveAction: (value: number[]) => void = () => {};
|
||||
let middlewarePromise;
|
||||
const perStart = performance.now();
|
||||
@@ -198,7 +208,10 @@ export class HTTPServer {
|
||||
(accum: { [key: string]: string }, curr: RouteParam) => {
|
||||
return {
|
||||
...accum,
|
||||
[curr.paramKey]: routeSegments[curr.idx].replace(/(?!\/)\W\D.*/gm, ""),
|
||||
[curr.paramKey]: routeSegments[curr.idx].replace(
|
||||
/(?!\/)\W\D.*/gm,
|
||||
"",
|
||||
),
|
||||
};
|
||||
},
|
||||
{},
|
||||
@@ -237,6 +250,10 @@ export class HTTPServer {
|
||||
}
|
||||
}
|
||||
|
||||
preprocessor(handler: RoutePreprocessor) {
|
||||
this.preprocessors.push(handler);
|
||||
}
|
||||
|
||||
middleware(handler: RouteMiddlewareHandler) {
|
||||
this.middlewareHandler = handler;
|
||||
}
|
||||
|
Reference in New Issue
Block a user