Skip to content

Commit 9973f09

Browse files
am29ddreamorosi
andauthored
feat(parser): allow parser set event type of handler with middy (#2786)
Co-authored-by: Andrea Amorosi <[email protected]>
1 parent f641c90 commit 9973f09

File tree

13 files changed

+216
-102
lines changed

13 files changed

+216
-102
lines changed

packages/parser/src/envelopes/cloudwatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class CloudWatchEnvelope extends Envelope {
1717
public static parse<T extends ZodSchema>(
1818
data: unknown,
1919
schema: T
20-
): z.infer<T> {
20+
): z.infer<T>[] {
2121
const parsedEnvelope = CloudWatchLogsSchema.parse(data);
2222

2323
return parsedEnvelope.awslogs.data.logEvents.map((record) => {

packages/parser/src/envelopes/dynamodb.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { DynamoDBStreamSchema } from '../schemas/index.js';
33
import type { ParsedResult, ParsedResultError } from '../types/index.js';
44
import { Envelope } from './envelope.js';
55
import { ParseError } from '../errors.js';
6-
7-
type DynamoDBStreamEnvelopeResponse<T extends ZodSchema> = {
8-
NewImage: z.infer<T>;
9-
OldImage: z.infer<T>;
10-
};
6+
import type { DynamoDBStreamEnvelopeResponse } from '../types/envelope.js';
117

128
/**
139
* DynamoDB Stream Envelope to extract data within NewImage/OldImage

packages/parser/src/envelopes/kafka.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class KafkaEnvelope extends Envelope {
2020
public static parse<T extends ZodSchema>(
2121
data: unknown,
2222
schema: T
23-
): z.infer<T> {
23+
): z.infer<T>[] {
2424
// manually fetch event source to deside between Msk or SelfManaged
2525
const eventSource = (data as KafkaMskEvent)['eventSource'];
2626

packages/parser/src/envelopes/kinesis-firehose.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class KinesisFirehoseEnvelope extends Envelope {
2020
public static parse<T extends ZodSchema>(
2121
data: unknown,
2222
schema: T
23-
): z.infer<T> {
23+
): z.infer<T>[] {
2424
const parsedEnvelope = KinesisFirehoseSchema.parse(data);
2525

2626
return parsedEnvelope.records.map((record) => {

packages/parser/src/envelopes/kinesis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class KinesisEnvelope extends Envelope {
1818
public static parse<T extends ZodSchema>(
1919
data: unknown,
2020
schema: T
21-
): z.infer<T> {
21+
): z.infer<T>[] {
2222
const parsedEnvelope = KinesisDataStreamSchema.parse(data);
2323

2424
return parsedEnvelope.Records.map((record) => {

packages/parser/src/envelopes/sns.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class SnsEnvelope extends Envelope {
1818
public static parse<T extends ZodSchema>(
1919
data: unknown,
2020
schema: T
21-
): z.infer<T> {
21+
): z.infer<T>[] {
2222
const parsedEnvelope = SnsSchema.parse(data);
2323

2424
return parsedEnvelope.Records.map((record) => {

packages/parser/src/envelopes/sqs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class SqsEnvelope extends Envelope {
1717
public static parse<T extends ZodSchema>(
1818
data: unknown,
1919
schema: T
20-
): z.infer<T> {
20+
): z.infer<T>[] {
2121
const parsedEnvelope = SqsSchema.parse(data);
2222

2323
return parsedEnvelope.Records.map((record) => {

packages/parser/src/middleware/parser.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { type MiddyLikeRequest } from '@aws-lambda-powertools/commons/types';
22
import { type MiddlewareObj } from '@middy/core';
3-
import { type ZodSchema } from 'zod';
4-
import { type ParserOptions } from '../types/parser.js';
3+
import { ZodType } from 'zod';
4+
import type { ParserOptions, ParserOutput } from '../types/parser.js';
55
import { parse } from '../parser.js';
6+
import type { Envelope } from '../types/envelope.js';
67

78
/**
89
* A middiy middleware to parse your event.
@@ -32,9 +33,13 @@ import { parse } from '../parser.js';
3233
*
3334
* @param options
3435
*/
35-
const parser = <S extends ZodSchema>(
36-
options: ParserOptions<S>
37-
): MiddlewareObj => {
36+
const parser = <
37+
TSchema extends ZodType,
38+
TEnvelope extends Envelope = undefined,
39+
TSafeParse extends boolean = false,
40+
>(
41+
options: ParserOptions<TSchema, TEnvelope, TSafeParse>
42+
): MiddlewareObj<ParserOutput<TSchema, TEnvelope, TSafeParse>> => {
3843
const before = (request: MiddyLikeRequest): void => {
3944
const { schema, envelope, safeParse } = options;
4045

packages/parser/src/parserDecorator.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
22
import type { Context, Handler } from 'aws-lambda';
3-
import { ZodSchema, z } from 'zod';
3+
import { type ZodSchema } from 'zod';
44
import { parse } from './parser.js';
5-
import type { ParserOptions, ParsedResult } from './types/index.js';
5+
import type { ParserOptions, Envelope } from './types/index.js';
6+
import type { ParserOutput } from './types/parser.js';
67

78
/**
89
* A decorator to parse your event.
@@ -67,8 +68,12 @@ import type { ParserOptions, ParsedResult } from './types/index.js';
6768
*
6869
* @param options Configure the parser with the `schema`, `envelope` and whether to `safeParse` or not
6970
*/
70-
export const parser = <S extends ZodSchema>(
71-
options: ParserOptions<S>
71+
export const parser = <
72+
TSchema extends ZodSchema,
73+
TEnvelope extends Envelope = undefined,
74+
TSafeParse extends boolean = false,
75+
>(
76+
options: ParserOptions<TSchema, TEnvelope, TSafeParse>
7277
): HandlerMethodDecorator => {
7378
return (_target, _propertyKey, descriptor) => {
7479
const original = descriptor.value!;
@@ -77,14 +82,11 @@ export const parser = <S extends ZodSchema>(
7782

7883
descriptor.value = async function (
7984
this: Handler,
80-
event: unknown,
85+
event: ParserOutput<TSchema, TEnvelope, TSafeParse>,
8186
context: Context,
8287
callback
8388
) {
84-
const parsedEvent: ParsedResult<
85-
typeof event,
86-
z.infer<typeof schema>
87-
> = parse(event, envelope, schema, safeParse);
89+
const parsedEvent = parse(event, envelope, schema, safeParse);
8890

8991
return original.call(this, parsedEvent, context, callback);
9092
};

packages/parser/src/types/envelope.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ import type {
1414
VpcLatticeEnvelope,
1515
VpcLatticeV2Envelope,
1616
} from '../envelopes/index.js';
17+
import { z, type ZodSchema } from 'zod';
1718

18-
export type Envelope =
19+
type DynamoDBStreamEnvelopeResponse<Schema extends ZodSchema> = {
20+
NewImage: z.infer<Schema>;
21+
OldImage: z.infer<Schema>;
22+
};
23+
24+
type Envelope =
1925
| typeof ApiGatewayEnvelope
2026
| typeof ApiGatewayV2Envelope
2127
| typeof CloudWatchEnvelope
@@ -29,4 +35,23 @@ export type Envelope =
2935
| typeof SnsSqsEnvelope
3036
| typeof SqsEnvelope
3137
| typeof VpcLatticeEnvelope
32-
| typeof VpcLatticeV2Envelope;
38+
| typeof VpcLatticeV2Envelope
39+
| undefined;
40+
41+
/**
42+
* Envelopes that return an array, needed to narrow down the return type of the parser
43+
*/
44+
type EnvelopeArrayReturnType =
45+
| typeof CloudWatchEnvelope
46+
| typeof DynamoDBStreamEnvelope
47+
| typeof KafkaEnvelope
48+
| typeof KinesisEnvelope
49+
| typeof KinesisFirehoseEnvelope
50+
| typeof SnsEnvelope
51+
| typeof SqsEnvelope;
52+
53+
export type {
54+
Envelope,
55+
DynamoDBStreamEnvelopeResponse,
56+
EnvelopeArrayReturnType,
57+
};

0 commit comments

Comments
 (0)