Skip to content

Commit

Permalink
add route to express middleware interface (#7841)
Browse files Browse the repository at this point in the history
  • Loading branch information
guysaar223 authored Aug 31, 2023
1 parent a51feaf commit 6fc3c7d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
12 changes: 7 additions & 5 deletions scopes/harmony/express/express.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ export class ExpressMain {
});
if (!options?.disableBodyParser) this.bodyParser(app);

this.middlewareSlot
.toArray()
.flatMap(([, middlewares]) =>
middlewares.flatMap((middlewareManifest) => app.use(middlewareManifest.middleware))
);
const middlewaresSlot = this.middlewareSlot.values().flat();
middlewaresSlot.forEach(({ route, middleware }) => {
if (!route) app.use(middleware);
// eslint-disable-next-line @typescript-eslint/no-misused-promises
if (route) app.use(route, middleware);
});

sortedRoutes.forEach((routeInfo) => {
const { method, path, middlewares, disableNamespace } = routeInfo;
// TODO: @guy make sure to support single middleware here.
Expand Down
3 changes: 2 additions & 1 deletion scopes/harmony/express/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { RouteSlot } from './express.main.runtime';
export { Route, Verb } from './types';
export { Request, Response, NextFunction } from './types';
export { Request, Response, NextFunction, Middleware } from './types';
export type { MiddlewareManifest } from './middleware-manifest';
export type { ExpressMain } from './express.main.runtime';
export { ExpressAspect } from './express.aspect';
1 change: 1 addition & 0 deletions scopes/harmony/express/middleware-manifest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Middleware } from './types';

export interface MiddlewareManifest {
route?: string;
middleware: Middleware;
}
2 changes: 1 addition & 1 deletion scopes/harmony/express/types/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Response } from './response';
/**
* define express Middleware
*/
export type Middleware = (req: Request, res: Response, next: NextFunction) => Promise<any>;
export type Middleware = (req: Request, res: Response, next: NextFunction) => void | Promise<any>;

export enum Verb {
WRITE = 'write',
Expand Down

0 comments on commit 6fc3c7d

Please sign in to comment.