-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Question - Composing multiple middleware #36
Comments
Issue-Label Bot is automatically applying the label Links: app homepage, dashboard and code for this bot. |
It should work like this. if you configure the middlewares correctly. E. g. classValidator requires a const wrapper = compose(
// add cors headers last so even error responses from the
// errorHandler middleware have cors headers applied
cors(),
errorHandler(),
jsonSerializer(),
classValidator(),
) If it is not working, I would need to see the actual error and more context. |
So the error I'm getting is
I have created a codesandbox that demonstrates the issue |
Thanks! This is related to TypeScript not being able to infer some generic types yet. I've changed the types in the newest versions of packages so that the error will not appear, as generics are only needed to cover some rather obscure cases of usage. |
Here's the list of updated versions: |
@dbartholomae This is still not working for me. Now I get the error
I have created a codesandbox that demonstrates the issue I did some investigation and I think it only occurs when you set |
Thanks, I'll adapt it so it also work in strict mode next week :) |
Unfortunately, TypeScript does not possess the capabilities to resolve the generics in a compose function in strict mode (see microsoft/TypeScript#29904 point 1). I've added |
@dbartholomae Manual function chaining works with no compilation errors const wrapper_manual =
cors()(
errorHandler()(
jsonSerializer()(
bodyParser()(
zodValidator(nameSchema)(
handler
)
)
)
)
);
const composeHandlerWrapper = composeHandler(
cors(),
errorHandler(),
jsonSerializer(),
bodyParser(),
zodValidator(nameSchema)(handler)
)
const composeHandlerWrapper = composeHandler(
cors(),
errorHandler(),
jsonSerializer(),
bodyParser(),
zodValidator(nameSchema),
handler
)
I have created a codesandbox that demonstrates the issue |
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. |
I'm trying to expose a wrapper function so that all my lambda handlers have the same middleware applied to them.
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?
The text was updated successfully, but these errors were encountered: