powertools-lambda-typescript icon indicating copy to clipboard operation
powertools-lambda-typescript copied to clipboard

Feature request: allow parser set event type of handler with middy

Open yamatatsu opened this issue 1 year ago • 1 comments

Use case

middy can pass event type like as this. So we can pass inferred event type from zod schema to handler.

Solution/User Experience

const detailSchema = z.object({
	date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
	message: z.string(),
});

export const handler = middy()
	// when using parser
	.use(parser({ schema: detailSchema, envelope: EventBridgeEnvelope }))
	// handler will get event type already inferred without type annotation.
	.handler(async (event) => {
		logger.inf(event.detail.message)
	});

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

yamatatsu avatar Apr 21 '24 14:04 yamatatsu

Hey @yamatatsu, thanks for raising the issue.

~~In your example, I think you missed the types in the middy() call. Can you modify the solution code block to reflect that? In the meantime, I will investigate how we could add event types to middy call.~~

Edit: now that Andrea pointed out to me, I got your point more clearly. You want to infer the type in the handler if it comes after the parser. I'd need to investigate a bit more what it will take to implement it.

am29d avatar Apr 22 '24 08:04 am29d

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Jul 22 '24 11:07 github-actions[bot]

This is now released under v2.6.0 version!

github-actions[bot] avatar Jul 25 '24 14:07 github-actions[bot]