Comments (10)
Here's the list of updated versions:
"@lambda-middleware/class-validator": "^2.0.0",
"@lambda-middleware/compose": "^1.1.0",
"@lambda-middleware/cors": "^2.0.0",
"@lambda-middleware/json-serializer": "^2.0.0",
"@lambda-middleware/jwt-auth": "^1.0.2",
"@lambda-middleware/http-error-handler": "^2.0.0",
from lambda-middleware.
Thanks, I'll adapt it so it also work in strict mode next week :)
from lambda-middleware.
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.
from lambda-middleware.
Issue-Label Bot is automatically applying the label question
to this issue, with a confidence of 0.75. Please mark this comment with 👍 or 👎 to give our bot feedback!
Links: app homepage, dashboard and code for this bot.
from lambda-middleware.
It should work like this. if you configure the middlewares correctly. E. g. classValidator requires a bodyType
.
You could even write it as:
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.
from lambda-middleware.
So the error I'm getting is
Argument of type '<Event_1 extends { headers: { [name: string]: string; }; httpMethod: string; }>(handler: PromiseHandler<Event_1, APIGatewayProxyResult>) => (event: Event_1, context: Context) => Promise<...>' is not assignable to parameter of type '(x: unknown) => unknown'.
Types of parameters 'handler' and 'x' are incompatible.
Type 'unknown' is not assignable to type 'PromiseHandler<{ headers: { [name: string]: string; }; httpMethod: string; }, APIGatewayProxyResult>'
I have created a codesandbox that demonstrates the issue
from lambda-middleware.
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.
I will post a list of new versions once all have been published.
from lambda-middleware.
@dbartholomae This is still not working for me. Now I get the error
Argument of type '<R>(handler: PromiseHandler<WithBody<APIGatewayProxyEvent, NameBody>, R>) => (event: APIGatewayProxyEvent, context: Context) => Promise<...>' is not assignable to parameter of type '(handler: PromiseHandler<WithBody<APIGatewayProxyEvent, NameBody>, unknown>) => PromiseHandler<APIGatewayProxyEvent, { ...; } | ... 1 more ... | undefined>'.
Call signature return types '(event: APIGatewayProxyEvent, context: Context) => Promise<unknown>' and 'PromiseHandler<APIGatewayProxyEvent, { [key: string]: JSONPrimitive; } | JSONObject[] | undefined>' are incompatible.
Type 'Promise<unknown>' is not assignable to type 'Promise<{ [key: string]: JSONPrimitive; } | JSONObject[] | undefined>'.
Type 'unknown' is not assignable to type '{ [key: string]: JSONPrimitive; } | JSONObject[] | undefined'.
Type 'unknown' is not assignable to type 'JSONObject[]'.ts(2345)
I have created a codesandbox that demonstrates the issue
I did some investigation and I think it only occurs when you set "strict": true
in your tsconfig.json
from lambda-middleware.
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 composeHandler
as a helper function that can be used to achieve full type safety.
from lambda-middleware.
@dbartholomae composeHandler
is not working for me under specific scenarios
Manual function chaining works with no compilation errors
const wrapper_manual =
cors()(
errorHandler()(
jsonSerializer()(
bodyParser()(
zodValidator(nameSchema)(
handler
)
)
)
)
);
composeHandler
works as long as I invoke the second last function with the handler
const composeHandlerWrapper = composeHandler(
cors(),
errorHandler(),
jsonSerializer(),
bodyParser(),
zodValidator(nameSchema)(handler)
)
composeHandler
causes a compilation error when invoked as per the documentation
const composeHandlerWrapper = composeHandler(
cors(),
errorHandler(),
jsonSerializer(),
bodyParser(),
zodValidator(nameSchema),
handler
)
Argument of type '<TEvent extends APIGatewayProxyEvent, TResult>(handler: PromiseHandler<WithBody<TEvent, object>, TResult>) => (event: TEvent, context: Context) => Promise<TResult>' is not assignable to parameter of type '(x: unknown) => PromiseHandler<APIGatewayProxyEvent, { [key: string]: JSONPrimitive; } | JSONObject[] | undefined>'.
Types of parameters 'handler' and 'x' are incompatible.
Type 'unknown' is not assignable to type 'PromiseHandler<WithBody<APIGatewayProxyEvent, object>, { [key: string]: JSONPrimitive; } | JSONObject[] | undefined>'
I have created a codesandbox that demonstrates the issue
from lambda-middleware.
Related Issues (20)
- Enable Github Actions to automatically build
- Add typeorm middleware for connecting to a database HOT 3
- Unable to JSON serialize type with optional keys HOT 4
- jsonSerializer does not pass through correct event type HOT 4
- Interest in a path parser middleware? HOT 3
- Allowing PromiseHandler to have generic Context HOT 1
- Help with composeHandler response HOT 1
- Middleware ordering causes type conflicts on remaining middleware HOT 1
- @lambda-middleware/utils package is pulling in the entire aws-sdk package HOT 1
- Add a JSON Deserializer Middleware HOT 5
- Add io-ts validator middleware HOT 5
- Should doNotWait middleware make context.callbackWaitsForEmptyEventLoop true instead of boolean? HOT 1
- class-validator seems to break the type inference for the handler HOT 1
- JsonDeserializer does not accept content-type header with charset HOT 2
- JSONSchema validator with AJV HOT 2
- Can we use different event type? Now strict to `APIGatewayEvent ` HOT 2
- Allow deserializeBody to work with APIGatewayProxyEventV2 HOT 1
- Can you please show how to add cors() to a compose chain? HOT 4
- Compatibility with Http API HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lambda-middleware.