Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 125 additions & 18 deletions packages/event-handler/src/rest/BaseRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class BaseRouter {

protected readonly routeRegistry: RouteHandlerRegistry;
protected readonly errorHandlerRegistry: ErrorHandlerRegistry;
protected readonly middlwares: Middleware[] = [];
protected readonly middleware: Middleware[] = [];

/**
* A logger instance to be used for logging debug, warning, and error messages.
Expand Down Expand Up @@ -170,7 +170,7 @@ abstract class BaseRouter {
* ```
*/
public use(middleware: Middleware): void {
this.middlwares.push(middleware);
this.middleware.push(middleware);
}

/**
Expand Down Expand Up @@ -223,7 +223,10 @@ abstract class BaseRouter {
? route.handler.bind(options.scope)
: route.handler;

const middleware = composeMiddleware([...this.middlwares]);
const middleware = composeMiddleware([
...this.middleware,
...route.middleware,
]);

const result = await middleware(
route.params,
Expand Down Expand Up @@ -255,11 +258,11 @@ abstract class BaseRouter {
}

public route(handler: RouteHandler, options: RouteOptions): void {
const { method, path } = options;
const { method, path, middleware = [] } = options;
const methods = Array.isArray(method) ? method : [method];

for (const method of methods) {
this.routeRegistry.register(new Route(method, path, handler));
this.routeRegistry.register(new Route(method, path, handler, middleware));
}
}

Expand Down Expand Up @@ -333,10 +336,26 @@ abstract class BaseRouter {
#handleHttpMethod(
method: HttpMethod,
path: Path,
middlewareOrHandler?: Middleware[] | RouteHandler,
handler?: RouteHandler
): MethodDecorator | undefined {
if (handler && typeof handler === 'function') {
this.route(handler, { method, path });
if (Array.isArray(middlewareOrHandler)) {
if (handler && typeof handler === 'function') {
this.route(handler, { method, path, middleware: middlewareOrHandler });
return;
}
return (_target, _propertyKey, descriptor: PropertyDescriptor) => {
this.route(descriptor.value, {
method,
path,
middleware: middlewareOrHandler,
});
return descriptor;
};
}

if (middlewareOrHandler && typeof middlewareOrHandler === 'function') {
this.route(middlewareOrHandler, { method, path });
return;
}

Expand All @@ -347,54 +366,142 @@ abstract class BaseRouter {
}

public get(path: Path, handler: RouteHandler): void;
public get(path: Path, middleware: Middleware[], handler: RouteHandler): void;
public get(path: Path): MethodDecorator;
public get(path: Path, handler?: RouteHandler): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.GET, path, handler);
public get(path: Path, middleware: Middleware[]): MethodDecorator;
public get(
path: Path,
middlewareOrHandler?: Middleware[] | RouteHandler,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(
HttpVerbs.GET,
path,
middlewareOrHandler,
handler
);
}

public post(path: Path, handler: RouteHandler): void;
public post(
path: Path,
middleware: Middleware[],
handler: RouteHandler
): void;
public post(path: Path): MethodDecorator;
public post(path: Path, handler?: RouteHandler): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.POST, path, handler);
public post(path: Path, middleware: Middleware[]): MethodDecorator;
public post(
path: Path,
middlewareOrHandler?: Middleware[] | RouteHandler,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(
HttpVerbs.POST,
path,
middlewareOrHandler,
handler
);
}

public put(path: Path, handler: RouteHandler): void;
public put(path: Path, middleware: Middleware[], handler: RouteHandler): void;
public put(path: Path): MethodDecorator;
public put(path: Path, handler?: RouteHandler): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.PUT, path, handler);
public put(path: Path, middleware: Middleware[]): MethodDecorator;
public put(
path: Path,
middlewareOrHandler?: Middleware[] | RouteHandler,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(
HttpVerbs.PUT,
path,
middlewareOrHandler,
handler
);
}

public patch(path: Path, handler: RouteHandler): void;
public patch(
path: Path,
middleware: Middleware[],
handler: RouteHandler
): void;
public patch(path: Path): MethodDecorator;
public patch(path: Path, middleware: Middleware[]): MethodDecorator;
public patch(
path: Path,
middlewareOrHandler?: Middleware[] | RouteHandler,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.PATCH, path, handler);
return this.#handleHttpMethod(
HttpVerbs.PATCH,
path,
middlewareOrHandler,
handler
);
}

public delete(path: Path, handler: RouteHandler): void;
public delete(
path: Path,
middleware: Middleware[],
handler: RouteHandler
): void;
public delete(path: Path): MethodDecorator;
public delete(path: Path, middleware: Middleware[]): MethodDecorator;
public delete(
path: Path,
middlewareOrHandler?: Middleware[] | RouteHandler,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.DELETE, path, handler);
return this.#handleHttpMethod(
HttpVerbs.DELETE,
path,
middlewareOrHandler,
handler
);
}

public head(path: Path, handler: RouteHandler): void;
public head(
path: Path,
middleware: Middleware[],
handler: RouteHandler
): void;
public head(path: Path): MethodDecorator;
public head(path: Path, handler?: RouteHandler): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.HEAD, path, handler);
public head(path: Path, middleware: Middleware[]): MethodDecorator;
public head(
path: Path,
middlewareOrHandler?: Middleware[] | RouteHandler,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(
HttpVerbs.HEAD,
path,
middlewareOrHandler,
handler
);
}

public options(path: Path, handler: RouteHandler): void;
public options(
path: Path,
middleware: Middleware[],
handler: RouteHandler
): void;
public options(path: Path): MethodDecorator;
public options(path: Path, middleware: Middleware[]): MethodDecorator;
public options(
path: Path,
middlewareOrHandler?: Middleware[] | RouteHandler,
handler?: RouteHandler
): MethodDecorator | undefined {
return this.#handleHttpMethod(HttpVerbs.OPTIONS, path, handler);
return this.#handleHttpMethod(
HttpVerbs.OPTIONS,
path,
middlewareOrHandler,
handler
);
}
}

Expand Down
16 changes: 14 additions & 2 deletions packages/event-handler/src/rest/Route.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import type { HttpMethod, Path, RouteHandler } from '../types/rest.js';
import type {
HttpMethod,
Middleware,
Path,
RouteHandler,
} from '../types/rest.js';

class Route {
readonly id: string;
readonly method: string;
readonly path: Path;
readonly handler: RouteHandler;
readonly middleware: Middleware[];

constructor(method: HttpMethod, path: Path, handler: RouteHandler) {
constructor(
method: HttpMethod,
path: Path,
handler: RouteHandler,
middleware: Middleware[] = []
) {
this.id = `${method}:${path}`;
this.method = method;
this.path = path;
this.handler = handler;
this.middleware = middleware;
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/event-handler/src/rest/RouteHandlerRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class RouteHandlerRegistry {
handler: staticRoute.handler,
rawParams: {},
params: {},
middleware: staticRoute.middleware,
};
}

Expand All @@ -182,6 +183,7 @@ class RouteHandlerRegistry {
handler: route.handler,
params: processedParams,
rawParams: params,
middleware: route.middleware,
};
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/event-handler/src/rest/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export const isAPIGatewayProxyResult = (
* follows the onion model where middleware executes in order before `next()` and in
* reverse order after `next()`.
*
* @param middlewares - Array of middleware functions to compose
* @returns A single middleware function that executes all provided middlewares in sequence
* @param middleware - Array of middleware functions to compose
* @returns A single middleware function that executes all provided middleware in sequence
*
* @example
* ```typescript
Expand All @@ -143,7 +143,7 @@ export const isAPIGatewayProxyResult = (
* // -> middleware1 end
* ```
*/
export const composeMiddleware = (middlewares: Middleware[]): Middleware => {
export const composeMiddleware = (middleware: Middleware[]): Middleware => {
return async (
params: Record<string, string>,
options: RequestOptions,
Expand All @@ -156,16 +156,16 @@ export const composeMiddleware = (middlewares: Middleware[]): Middleware => {
if (i <= index) throw new Error('next() called multiple times');
index = i;

if (i === middlewares.length) {
if (i === middleware.length) {
const nextResult = await next();
if (nextResult !== undefined) {
result = nextResult;
}
return;
}

const middleware = middlewares[i];
const middlewareResult = await middleware(params, options, () =>
const middlewareFn = middleware[i];
const middlewareResult = await middlewareFn(params, options, () =>
dispatch(i + 1)
);

Expand Down
2 changes: 2 additions & 0 deletions packages/event-handler/src/types/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ type RouteHandlerOptions = {
handler: RouteHandler;
params: Record<string, string>;
rawParams: Record<string, string>;
middleware: Middleware[];
};

type RouteOptions = {
method: HttpMethod | HttpMethod[];
path: Path;
middleware?: Middleware[];
};

type NextFunction = () => Promise<HandlerResponse | void>;
Expand Down
Loading
Loading