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

Middleware after response #1593

Closed
3 of 4 tasks
SwagiWagi opened this issue Mar 17, 2024 · 7 comments · May be fixed by #1625
Closed
3 of 4 tasks

Middleware after response #1593

SwagiWagi opened this issue Mar 17, 2024 · 7 comments · May be fixed by #1625
Labels

Comments

@SwagiWagi
Copy link

SwagiWagi commented Mar 17, 2024

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

Have the ability to have middlewares of specific routes be called after response has been sent

Current Behavior

Possible Solution

Steps to Reproduce

@route("user")
export class UserController extends Controller {
@middlewares(validateUserId)
@get("{userId}")
async getUser(@path() userId: string) {
this.setStatus(200);
return {userId: userId};
}
}

Now I want a logging middleware to be called (for example), specifically after the response has been sent.

Context (Environment)

Version of the library: 6.1.5
Version of NodeJS: 20.11.0

  • Confirm you were using yarn not npm: Using npm

Detailed Description

Breaking change?

Copy link

Hello there SwagiWagi 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

@jackey8616
Copy link
Collaborator

jackey8616 commented Mar 17, 2024

I assume you are using Express,
there is two way to archive such feature.

  1. custom template and make your own handler function,
    register every route with after middleware by yourself,
    details you can search on StackOverflow, I believe there is some example such as Call middleware after response sent in Express
  2. tsoa indeed can provides such feature by including an AfterMiddleware decorator.
    but I doubt the necessity of it right now.

cc @WoH need your opinion.

@WoH
Copy link
Collaborator

WoH commented Mar 18, 2024

(req, res, next) => {

  next()

  console.log('post')
}

Should do it, right?

@SwagiWagi
Copy link
Author

I believe an AfterMiddleware decorator would be a great feature.
It's great for use cases such as:

  1. Tracking the number of completed requests (for billing purposes for example).
  2. Logging.
  3. Disposing resources.

I believe it wouldn't be much work, I can make a PR for this.

@jackey8616
Copy link
Collaborator

(req, res, next) => {

  next()

  console.log('post')
}

Should do it, right?

try {
validatedArgs = templateService.getValidatedArgs({ args, request, response });
{{#if ../../iocModule}}
const container: IocContainer = typeof iocContainer === 'function' ? (iocContainer as IocContainerFactory)(request) : iocContainer;
const controller: any = await container.get<{{../name}}>({{../name}});
if (typeof controller['setStatus'] === 'function') {
controller.setStatus(undefined);
}
{{else}}
const controller = new {{../name}}();
{{/if}}
templateService.apiHandler({
methodName: '{{name}}',
controller,
response,
next,
validatedArgs,
successStatus: {{successStatus}},
});
} catch (err) {
return next(err);
}

Yes, just call next() in custom template, after L106 would work with after middleware.

Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@ericwooley
Copy link

Sorry to bring up a dead issue. But this doesn't appear to have been implemented.

I'm happy to do the PR if needed, or maybe there is discussion elsewhere as to why this wasn't added?

ericwooley added a commit to ericwooley/tsoa that referenced this issue May 12, 2024
ericwooley added a commit to ericwooley/tsoa that referenced this issue May 12, 2024
ericwooley added a commit to ericwooley/tsoa that referenced this issue May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants