question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question - Composing multiple middleware

See original GitHub issue

I’m trying to expose a wrapper function so that all my lambda handlers have the same middleware applied to them.

const wrapper = (handler) =>
  compose(
    // add cors headers last so even error responses from the
    // errorHandler middleware have cors headers applied
    cors(),
    errorHandler(),
    jsonSerializer(),
    classValidator(),
  )(handler)

However I’m struggling to get the type information to line up.

Is it possible to chain these middlewares like this as you can do using middy?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dbartholomaecommented, Nov 2, 2020

That’s the same problem - unfortunately currently not fully solvable in TypeScript. I would recommend going with the solution where the handler is wrapped for now until TypeScript has better inference for generics.

1reaction
dbartholomaecommented, Oct 25, 2020

Thanks, I’ll adapt it so it also work in strict mode next week 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chaining multiple pieces of middleware for specific route in ...
Consider following example: const middleware = { requireAuthentication: function(req, res, next) { console.log('private route list!
Read more >
Multiple middleware functions #37978 - vercel/next.js - GitHub
I was a bit overwhelmed with multiple middleware, because of our app size, that I put everything in the top level one, from...
Read more >
Complete Guide to Express Middleware - Reflectoring
Middleware in Express are functions that come into play after the server receives the request and before the response is sent to the...
Read more >
20 Middleware Interview Questions and Answers - CLIMB
Here are 20 commonly asked Middleware interview questions and ... Yes, it is possible to use multiple middlewares for a single application.
Read more >
Middleware: Part of the Solution, or Part of the Problem?
In the most general of senses, middleware is software that intermediates functionality or data between two otherwise independent and separate systems.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found