-
-
Notifications
You must be signed in to change notification settings - Fork 376
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
Latest version of @middy/core breaks types in the handler function #1221
Comments
I'm also experiencing this! |
Hey, What you need to do is to provide the generic type to the import middy from "@middy/core";
import secretsManager from "@middy/secrets-manager";
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
export const handler = middy()
.use(
secretsManager({
fetchData: {
apiToken: "dev/api_token",
},
awsClientOptions: {
region: "us-east-1",
},
setToContext: true,
}),
)
.handler<APIGatewayProxyEvent, APIGatewayProxyResult>(async (req, context) => {
// The context type gets augmented here by the secretsManager middleware.
// This is just an example, obviously don't ever log your secret in real life!
console.log(context.apiToken);
return {
statusCode: 200,
body: JSON.stringify({
message: `Hello from ${req.path}`,
req,
}),
};
});
// OR
import middy from "@middy/core";
import secretsManager from "@middy/secrets-manager";
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
export const handler = middy<APIGatewayProxyEvent, APIGatewayProxyResult>(
async (req, context) => {
// The context type gets augmented here by the secretsManager middleware.
// This is just an example, obviously don't ever log your secret in real life!
console.log(context.apiToken);
return {
statusCode: 200,
body: JSON.stringify({
message: `Hello from ${req.path}`,
req,
}),
};
}
)
.use(
secretsManager({
fetchData: {
apiToken: "dev/api_token",
},
awsClientOptions: {
region: "us-east-1",
},
setToContext: true,
}),
); |
Please reopen if this needs more attention. |
Related discussion in #1217 |
Will be able to further investigate in few hours, will keep you posted. Sorry for the inconvenience. |
Hey guys, I've created a PR to partially revert those changes, If anyone has an idea please let me know 🙏 |
@naorpeled wouldn’t it look something like: import middyCore from "@middy/core";
import type { S3Event } from "aws-lambda";
export const middy = middyCore<S3Event, void>();
export const handler = middy.handler(({ Records }) =>
console.log(JSON.stringify(Records, undefined, 2))
); Must be version The key with the previous middy types is focusing on So you don’t want the export type S3Handler = Handler<S3Event, void>; You’ll find the types you want— The main problem I have with middy types is that middy’s default middleware augments types with |
Could this be reopened please? Here's something which breaks with 5.4.2 for me (a simplified version of our codebase). We have handlers which return objects satisfying an internal interface, and then middleware to transform from that into an import middy, { MiddlewareObj } from "@middy/core";
import type {
APIGatewayProxyEventV2,
APIGatewayProxyResultV2,
Context,
} from "aws-lambda";
export interface LoggerContext extends Context {
logger: unknown;
}
// Adds a logger to the context
export function loggerMiddleware<TEvent, TResult>(): MiddlewareObj<
TEvent,
TResult,
Error,
LoggerContext
> {
return {
before: ({ context }) => {
context.logger = undefined;
},
};
}
// Converts from our type to an AWS response type
export function renderableMiddleware<TErr>(): MiddlewareObj<
APIGatewayProxyEventV2,
APIGatewayProxyResultV2 & MyType,
TErr,
LoggerContext
> {
return {
after: (request) => {
if (request.response === null) {
return;
}
// Imagine some logging here making use of `request.context`.
Object.assign(request.response, {
statusCode: 200,
body: `Got ${request.response.apples} apples`,
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
},
};
}
interface MyType {
apples: number;
}
function baseHandler(): MyType {
return {
apples: 1337,
};
}
const handler = middy()
.use(loggerMiddleware())
.use(renderableMiddleware())
.handler(baseHandler);
// With 5.4.1, type is `Promise<any>`.
// With 5.4.2, type is `Promise<MyType>`. #1222 doesn't change this either :(
const result = handler(
{} as APIGatewayProxyEventV2,
{} as LoggerContext & Context,
); The types weren't the best before - it would be nice if But given the existing type setup, a type that would work and the one I'd expect to see would be
|
I couldn't resist fiddling with the type definitions a bit. I came up with this: a66f358. In there we add a couple of new generic parameters which exist to propagate along the types from It's not a proposal right now. Partly because it breaks existing programs and partly because I'm not sure it's all correct. It does seem to improve things for me, though. I wanted to share it to get feedback and see if folks thing it's a valuable direction to try to move in. |
Describe the bug
The 5.4.2 release of
@middy/core
breaks typing that works fine in version 5.4.1. Have tried it with TypeScript5.4.x
and5.5.x
and encounter the same issue.To Reproduce
You can see this issue with the sample TypeScript in the documentation. The
req
andcontext
variables in the handler function are now no longer correctly typed, but instead return asany
. Downgrading back to 5.4.1 fixes the issue and nowreq
andcontext
are correctly typed again.Expected behaviour
Above example should work with TypeScript
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: