refactor(httpkernel): introduce configuration object for flexibility
- Replace individual constructor arguments with a configuration object. - Add IHttpKernelConfig interface to standardize configuration structure. - Refactor route builder and response decorator usage to use config. - Simplify code and improve extensibility by consolidating parameters. Signed-off-by: Max P. <Mail@MPassarello.de>
This commit is contained in:
@@ -2,14 +2,13 @@ import {
|
||||
IContext,
|
||||
IHandler,
|
||||
IHttpKernel,
|
||||
IHttpKernelConfig,
|
||||
IInternalRoute,
|
||||
IMiddleware,
|
||||
IRouteBuilder,
|
||||
IRouteBuilderFactory,
|
||||
IRouteDefinition,
|
||||
} from './Interfaces/mod.ts';
|
||||
import { RouteBuilder } from './RouteBuilder.ts';
|
||||
import { ResponseDecorator } from './Types/mod.ts';
|
||||
import { parseQuery } from './Utils/mod.ts';
|
||||
|
||||
/**
|
||||
@@ -21,6 +20,7 @@ import { parseQuery } from './Utils/mod.ts';
|
||||
*/
|
||||
export class HttpKernel<TContext extends IContext = IContext>
|
||||
implements IHttpKernel<TContext> {
|
||||
private cfg: IHttpKernelConfig<TContext>;
|
||||
/**
|
||||
* The list of internally registered routes, each with method, matcher, middleware, and handler.
|
||||
*/
|
||||
@@ -34,11 +34,16 @@ export class HttpKernel<TContext extends IContext = IContext>
|
||||
* @param routeBuilderFactory - Optional factory for creating route builders. Defaults to using `RouteBuilder`.
|
||||
*/
|
||||
public constructor(
|
||||
private readonly decorateResponse: ResponseDecorator = (res) => res,
|
||||
private readonly routeBuilderFactory: IRouteBuilderFactory =
|
||||
RouteBuilder,
|
||||
config?: Partial<IHttpKernelConfig<TContext>>,
|
||||
) {
|
||||
this.cfg = {
|
||||
decorateResponse: (res) => res,
|
||||
routeBuilderFactory: RouteBuilder,
|
||||
...config,
|
||||
} as IHttpKernelConfig<TContext>;
|
||||
|
||||
this.handle = this.handle.bind(this);
|
||||
this.registerRoute = this.registerRoute.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,8 +52,8 @@ export class HttpKernel<TContext extends IContext = IContext>
|
||||
public route<_TContext extends IContext = TContext>(
|
||||
definition: IRouteDefinition,
|
||||
): IRouteBuilder {
|
||||
return new this.routeBuilderFactory(
|
||||
this.registerRoute.bind(this),
|
||||
return new this.cfg.routeBuilderFactory(
|
||||
this.registerRoute,
|
||||
definition,
|
||||
);
|
||||
}
|
||||
@@ -125,6 +130,9 @@ export class HttpKernel<TContext extends IContext = IContext>
|
||||
? await fn(ctx, () => dispatch(index + 1))
|
||||
: await (fn as IHandler<_TContext>)(ctx);
|
||||
};
|
||||
return this.decorateResponse(await dispatch(0), ctx);
|
||||
return this.cfg.decorateResponse(
|
||||
await dispatch(0),
|
||||
ctx as unknown as TContext,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
8
src/Interfaces/HttpKernelConfig.ts
Normal file
8
src/Interfaces/HttpKernelConfig.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { ResponseDecorator } from '../Types/mod.ts';
|
||||
import { IContext } from './IContext.ts';
|
||||
import { IRouteBuilderFactory } from './IRouteBuilder.ts';
|
||||
|
||||
export interface IHttpKernelConfig<TContext extends IContext = IContext> {
|
||||
decorateResponse: ResponseDecorator<TContext>;
|
||||
routeBuilderFactory: IRouteBuilderFactory;
|
||||
}
|
@@ -4,12 +4,12 @@ import { IMiddleware } from './IMiddleware.ts';
|
||||
import { IRouteDefinition } from './IRouteDefinition.ts';
|
||||
import { IContext } from './mod.ts';
|
||||
|
||||
export interface IRouteBuilderFactory {
|
||||
export interface IRouteBuilderFactory<TContext extends IContext = IContext> {
|
||||
new (
|
||||
registerRoute: (route: IInternalRoute) => void,
|
||||
registerRoute: (route: IInternalRoute<TContext>) => void,
|
||||
def: IRouteDefinition,
|
||||
mws?: IMiddleware[],
|
||||
): IRouteBuilder;
|
||||
mws?: IMiddleware<TContext>[],
|
||||
): IRouteBuilder<TContext>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,3 +1,6 @@
|
||||
// deno-coverage-ignore-file
|
||||
|
||||
export type { IHttpKernelConfig } from './HttpKernelConfig.ts';
|
||||
export type { IContext } from './IContext.ts';
|
||||
export type { IHandler } from './IHandler.ts';
|
||||
export type { IHttpKernel } from './IHttpKernel.ts';
|
||||
|
@@ -1,3 +1,5 @@
|
||||
// deno-coverage-ignore-file
|
||||
|
||||
export { isHttpMethod, validHttpMethods } from './HttpMethod.ts';
|
||||
export type { HttpMethod } from './HttpMethod.ts';
|
||||
export type { Params } from './Params.ts';
|
||||
|
@@ -1,2 +1,4 @@
|
||||
// deno-coverage-ignore-file
|
||||
|
||||
export { createRouteMatcher } from './createRouteMatcher.ts';
|
||||
export { parseQuery } from './parseQuery.ts';
|
||||
|
Reference in New Issue
Block a user