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,
|
IContext,
|
||||||
IHandler,
|
IHandler,
|
||||||
IHttpKernel,
|
IHttpKernel,
|
||||||
|
IHttpKernelConfig,
|
||||||
IInternalRoute,
|
IInternalRoute,
|
||||||
IMiddleware,
|
IMiddleware,
|
||||||
IRouteBuilder,
|
IRouteBuilder,
|
||||||
IRouteBuilderFactory,
|
|
||||||
IRouteDefinition,
|
IRouteDefinition,
|
||||||
} from './Interfaces/mod.ts';
|
} from './Interfaces/mod.ts';
|
||||||
import { RouteBuilder } from './RouteBuilder.ts';
|
import { RouteBuilder } from './RouteBuilder.ts';
|
||||||
import { ResponseDecorator } from './Types/mod.ts';
|
|
||||||
import { parseQuery } from './Utils/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>
|
export class HttpKernel<TContext extends IContext = IContext>
|
||||||
implements IHttpKernel<TContext> {
|
implements IHttpKernel<TContext> {
|
||||||
|
private cfg: IHttpKernelConfig<TContext>;
|
||||||
/**
|
/**
|
||||||
* The list of internally registered routes, each with method, matcher, middleware, and handler.
|
* 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`.
|
* @param routeBuilderFactory - Optional factory for creating route builders. Defaults to using `RouteBuilder`.
|
||||||
*/
|
*/
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly decorateResponse: ResponseDecorator = (res) => res,
|
config?: Partial<IHttpKernelConfig<TContext>>,
|
||||||
private readonly routeBuilderFactory: IRouteBuilderFactory =
|
|
||||||
RouteBuilder,
|
|
||||||
) {
|
) {
|
||||||
|
this.cfg = {
|
||||||
|
decorateResponse: (res) => res,
|
||||||
|
routeBuilderFactory: RouteBuilder,
|
||||||
|
...config,
|
||||||
|
} as IHttpKernelConfig<TContext>;
|
||||||
|
|
||||||
this.handle = this.handle.bind(this);
|
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>(
|
public route<_TContext extends IContext = TContext>(
|
||||||
definition: IRouteDefinition,
|
definition: IRouteDefinition,
|
||||||
): IRouteBuilder {
|
): IRouteBuilder {
|
||||||
return new this.routeBuilderFactory(
|
return new this.cfg.routeBuilderFactory(
|
||||||
this.registerRoute.bind(this),
|
this.registerRoute,
|
||||||
definition,
|
definition,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -125,6 +130,9 @@ export class HttpKernel<TContext extends IContext = IContext>
|
|||||||
? await fn(ctx, () => dispatch(index + 1))
|
? await fn(ctx, () => dispatch(index + 1))
|
||||||
: await (fn as IHandler<_TContext>)(ctx);
|
: 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 { IRouteDefinition } from './IRouteDefinition.ts';
|
||||||
import { IContext } from './mod.ts';
|
import { IContext } from './mod.ts';
|
||||||
|
|
||||||
export interface IRouteBuilderFactory {
|
export interface IRouteBuilderFactory<TContext extends IContext = IContext> {
|
||||||
new (
|
new (
|
||||||
registerRoute: (route: IInternalRoute) => void,
|
registerRoute: (route: IInternalRoute<TContext>) => void,
|
||||||
def: IRouteDefinition,
|
def: IRouteDefinition,
|
||||||
mws?: IMiddleware[],
|
mws?: IMiddleware<TContext>[],
|
||||||
): IRouteBuilder;
|
): IRouteBuilder<TContext>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,3 +1,6 @@
|
|||||||
|
// deno-coverage-ignore-file
|
||||||
|
|
||||||
|
export type { IHttpKernelConfig } from './HttpKernelConfig.ts';
|
||||||
export type { IContext } from './IContext.ts';
|
export type { IContext } from './IContext.ts';
|
||||||
export type { IHandler } from './IHandler.ts';
|
export type { IHandler } from './IHandler.ts';
|
||||||
export type { IHttpKernel } from './IHttpKernel.ts';
|
export type { IHttpKernel } from './IHttpKernel.ts';
|
||||||
|
@@ -1,3 +1,5 @@
|
|||||||
|
// deno-coverage-ignore-file
|
||||||
|
|
||||||
export { isHttpMethod, validHttpMethods } from './HttpMethod.ts';
|
export { isHttpMethod, validHttpMethods } from './HttpMethod.ts';
|
||||||
export type { HttpMethod } from './HttpMethod.ts';
|
export type { HttpMethod } from './HttpMethod.ts';
|
||||||
export type { Params } from './Params.ts';
|
export type { Params } from './Params.ts';
|
||||||
|
@@ -1,2 +1,4 @@
|
|||||||
|
// deno-coverage-ignore-file
|
||||||
|
|
||||||
export { createRouteMatcher } from './createRouteMatcher.ts';
|
export { createRouteMatcher } from './createRouteMatcher.ts';
|
||||||
export { parseQuery } from './parseQuery.ts';
|
export { parseQuery } from './parseQuery.ts';
|
||||||
|
Reference in New Issue
Block a user