Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass action (request and response) when resolving middleware from container #1034

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
23 changes: 11 additions & 12 deletions src/driver/express/ExpressDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ export class ExpressDriver extends BaseDriver {
let middlewareWrapper;

// if its an error handler then register it with proper signature in express
if ((middleware.instance as ExpressErrorMiddlewareInterface).error) {
if (middleware.target.prototype && middleware.target.prototype.error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify expression as if (middleware.target?.prototype?.error). Same for others here.

middlewareWrapper = (error: any, request: any, response: any, next: (err?: any) => any) => {
(middleware.instance as ExpressErrorMiddlewareInterface).error(error, request, response, next);
const instance = getFromContainer<ExpressErrorMiddlewareInterface>(middleware.target, { request, response });
instance.error(error, request, response, next);
};
}

// if its a regular middleware then register it as express middleware
else if ((middleware.instance as ExpressMiddlewareInterface).use) {
else if (middleware.target.prototype && middleware.target.prototype.use) {
middlewareWrapper = (request: any, response: any, next: (err: any) => any) => {
try {
const useResult = (middleware.instance as ExpressMiddlewareInterface).use(request, response, next);
const instance = getFromContainer<ExpressMiddlewareInterface>(middleware.target, { request, response });
const useResult = instance.use(request, response, next);
if (isPromiseLike(useResult)) {
useResult.catch((error: any) => {
this.handleError(error, undefined, { request, response, next });
Expand All @@ -84,7 +86,7 @@ export class ExpressDriver extends BaseDriver {
if (middlewareWrapper) {
// Name the function for better debugging
Object.defineProperty(middlewareWrapper, 'name', {
value: middleware.instance.constructor.name,
value: middleware.target.name,
writable: true,
});

Expand Down Expand Up @@ -396,7 +398,8 @@ export class ExpressDriver extends BaseDriver {
// if this is function instance of MiddlewareInterface
middlewareFunctions.push((request: any, response: any, next: (err: any) => any) => {
try {
const useResult = getFromContainer<ExpressMiddlewareInterface>(use.middleware).use(request, response, next);
const instance = getFromContainer<ExpressMiddlewareInterface>(use.middleware, { request, response });
const useResult = instance.use(request, response, next);
if (isPromiseLike(useResult)) {
useResult.catch((error: any) => {
this.handleError(error, undefined, { request, response, next });
Expand All @@ -412,12 +415,8 @@ export class ExpressDriver extends BaseDriver {
} else if (use.middleware.prototype && use.middleware.prototype.error) {
// if this is function instance of ErrorMiddlewareInterface
middlewareFunctions.push(function (error: any, request: any, response: any, next: (err: any) => any) {
return getFromContainer<ExpressErrorMiddlewareInterface>(use.middleware).error(
error,
request,
response,
next
);
const instance = getFromContainer<ExpressErrorMiddlewareInterface>(use.middleware, { request, response });
return instance.error(error, request, response, next);
});
} else {
middlewareFunctions.push(use.middleware);
Expand Down
4 changes: 2 additions & 2 deletions src/driver/koa/KoaDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export class KoaDriver extends BaseDriver {
* Registers middleware that run before controller actions.
*/
registerMiddleware(middleware: MiddlewareMetadata): void {
if ((middleware.instance as KoaMiddlewareInterface).use) {
if (middleware.target.prototype && middleware.target.prototype.use) {
this.koa.use(function (ctx: any, next: any) {
return (middleware.instance as KoaMiddlewareInterface).use(ctx, next);
return getFromContainer<KoaMiddlewareInterface>(middleware.target).use(ctx, next);
});
}
}
Expand Down
17 changes: 0 additions & 17 deletions src/metadata/MiddlewareMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { MiddlewareMetadataArgs } from './args/MiddlewareMetadataArgs';
import { ExpressMiddlewareInterface } from '../driver/express/ExpressMiddlewareInterface';
import { ExpressErrorMiddlewareInterface } from '../driver/express/ExpressErrorMiddlewareInterface';
import { getFromContainer } from '../container';
import { KoaMiddlewareInterface } from '../driver/koa/KoaMiddlewareInterface';

/**
* Middleware metadata.
Expand Down Expand Up @@ -42,17 +38,4 @@ export class MiddlewareMetadata {
this.priority = args.priority;
this.type = args.type;
}

// -------------------------------------------------------------------------
// Accessors
// -------------------------------------------------------------------------

/**
* Gets middleware instance from the container.
*/
get instance(): ExpressMiddlewareInterface | KoaMiddlewareInterface | ExpressErrorMiddlewareInterface {
return getFromContainer<ExpressMiddlewareInterface | KoaMiddlewareInterface | ExpressErrorMiddlewareInterface>(
this.target
);
}
}