From 660dca3a1c5da29884a3a44228f96d1fe9f45aef Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 24 Sep 2025 16:59:08 +0200 Subject: [PATCH 1/2] style(parser): apply stricter linting --- packages/parser/src/parserDecorator.ts | 2 +- packages/parser/src/schemas/cognito.ts | 2 +- packages/parser/src/types/parser.ts | 2 +- packages/parser/tests/types/parser.test-d.ts | 2 +- .../parser/tests/unit/parser.decorator.test.ts | 7 ++++++- .../parser/tests/unit/parser.middy.test.ts | 6 +++--- packages/parser/tests/unit/parser.test.ts | 18 +++++++++--------- 7 files changed, 22 insertions(+), 17 deletions(-) diff --git a/packages/parser/src/parserDecorator.ts b/packages/parser/src/parserDecorator.ts index 2089091894..d0166a4518 100644 --- a/packages/parser/src/parserDecorator.ts +++ b/packages/parser/src/parserDecorator.ts @@ -84,7 +84,7 @@ export const parser = < const { schema, envelope, safeParse } = options; - descriptor.value = async function ( + descriptor.value = function ( this: Handler, ...args: [ParserOutput, Context, Callback] ) { diff --git a/packages/parser/src/schemas/cognito.ts b/packages/parser/src/schemas/cognito.ts index e7240cd0e8..08bcc5ac97 100644 --- a/packages/parser/src/schemas/cognito.ts +++ b/packages/parser/src/schemas/cognito.ts @@ -132,7 +132,7 @@ const PostConfirmationTriggerSchema = CognitoTriggerBaseSchema.extend({ * } * ``` * - * * @see {@link https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-authentication.html | Amazon Cognito Developer Guide} + * @see {@link https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-authentication.html | Amazon Cognito Developer Guide} */ const PreAuthenticationTriggerSchema = CognitoTriggerBaseSchema.extend({ triggerSource: z.literal('PreAuthentication_Authentication'), diff --git a/packages/parser/src/types/parser.ts b/packages/parser/src/types/parser.ts index 56f4f1d109..de6c4c982d 100644 --- a/packages/parser/src/types/parser.ts +++ b/packages/parser/src/types/parser.ts @@ -85,7 +85,7 @@ type ParserOutput< /** * The parser function that can parse the data using the provided schema and envelope * we use function overloads to provide the correct return type based on the provided envelope - **/ + */ type ParseFunction = { // No envelope, no safeParse ( diff --git a/packages/parser/tests/types/parser.test-d.ts b/packages/parser/tests/types/parser.test-d.ts index 6ac8f86cf5..3cc9ec3a95 100644 --- a/packages/parser/tests/types/parser.test-d.ts +++ b/packages/parser/tests/types/parser.test-d.ts @@ -73,7 +73,7 @@ describe('Parser types', () => { envelope: SqsEnvelope, }) ) - .handler(async (event) => { + .handler((event) => { expectTypeOf(event).toEqualTypeOf(); }); }); diff --git a/packages/parser/tests/unit/parser.decorator.test.ts b/packages/parser/tests/unit/parser.decorator.test.ts index 7955bca551..13ca3f32b9 100644 --- a/packages/parser/tests/unit/parser.decorator.test.ts +++ b/packages/parser/tests/unit/parser.decorator.test.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; import type { Context } from 'aws-lambda'; import { describe, expect, it } from 'vitest'; @@ -30,6 +31,7 @@ describe('Decorator: parser', () => { class TestClass implements LambdaInterface { @parser({ schema: extendedSchema }) public async handler(event: event, _context: Context): Promise { + await setTimeout(1); // simulate some processing time return event; } @@ -38,6 +40,7 @@ describe('Decorator: parser', () => { event: z.infer, _context: Context ): Promise { + await setTimeout(1); // simulate some processing time return this.anotherMethod(event); } @@ -49,6 +52,7 @@ describe('Decorator: parser', () => { event: ParsedResult, _context: Context ): Promise> { + await setTimeout(1); // simulate some processing time return event; } @@ -61,10 +65,11 @@ describe('Decorator: parser', () => { event: ParsedResult, _context: Context ): Promise { + await setTimeout(1); // simulate some processing time return event; } - private async anotherMethod(event: unknown): Promise { + private anotherMethod(event: unknown): unknown { return event; } } diff --git a/packages/parser/tests/unit/parser.middy.test.ts b/packages/parser/tests/unit/parser.middy.test.ts index 34a3f1caf8..c138a3e5cf 100644 --- a/packages/parser/tests/unit/parser.middy.test.ts +++ b/packages/parser/tests/unit/parser.middy.test.ts @@ -54,7 +54,7 @@ describe('Middleware: parser', () => { const event = structuredClone(baseEventBridgeEvent); // Act & Assess - expect( + await expect( middy() .use(parser({ schema: z.string(), envelope: SqsEnvelope })) .handler((event) => event)(event as unknown as string[], {} as Context) @@ -68,7 +68,7 @@ describe('Middleware: parser', () => { event.Records[1].body = undefined; // Act & Assess - expect( + await expect( handlerWithSchemaAndEnvelope(event as unknown as string[], {} as Context) ).rejects.toThrow(); }); @@ -91,7 +91,7 @@ describe('Middleware: parser', () => { const event = structuredClone(JSONPayload); // Act & Assess - expect( + await expect( middy((event) => event).use(parser({ schema: z.number() }))( event as unknown as number, {} as Context diff --git a/packages/parser/tests/unit/parser.test.ts b/packages/parser/tests/unit/parser.test.ts index 374e41c245..a7fb4fee58 100644 --- a/packages/parser/tests/unit/parser.test.ts +++ b/packages/parser/tests/unit/parser.test.ts @@ -24,7 +24,7 @@ describe('Parser', () => { }); const JSONPayload = { name: 'John', age: 18 }; - it('parses an event with schema and envelope', async () => { + it('parses an event with schema and envelope', () => { // Prepare const event = structuredClone(baseSqsEvent); event.Records[1].body = 'bar'; @@ -37,7 +37,7 @@ describe('Parser', () => { expect(result).toStrictEqual(['Test message.', 'bar']); }); - it('throws when envelope does not match', async () => { + it('throws when envelope does not match', () => { // Prepare const event = structuredClone(baseEventBridgeEvent); @@ -45,7 +45,7 @@ describe('Parser', () => { expect(() => parse(event, SqsEnvelope, z.string())).toThrow(); }); - it('throws when schema does not match', async () => { + it('throws when schema does not match', () => { // Prepare const event = structuredClone(baseSqsEvent); // @ts-expect-error - setting an invalid body @@ -55,7 +55,7 @@ describe('Parser', () => { expect(() => parse(event, SqsEnvelope, z.string())).toThrow(); }); - it('parses the event successfully', async () => { + it('parses the event successfully', () => { // Prepare const event = 42; @@ -66,7 +66,7 @@ describe('Parser', () => { expect(result).toEqual(event); }); - it('throws when the event does not match the schema', async () => { + it('throws when the event does not match the schema', () => { // Prepare const event = structuredClone(JSONPayload); @@ -74,7 +74,7 @@ describe('Parser', () => { expect(() => parse(event, undefined, z.number())).toThrow(); }); - it('returns the payload when using safeParse', async () => { + it('returns the payload when using safeParse', () => { // Prepare const event = structuredClone(JSONPayload); @@ -88,7 +88,7 @@ describe('Parser', () => { }); }); - it('returns the error when using safeParse and the payload is invalid', async () => { + it('returns the error when using safeParse and the payload is invalid', () => { // Prepare const event = structuredClone(JSONPayload); @@ -103,7 +103,7 @@ describe('Parser', () => { }); }); - it('returns the payload when using safeParse with envelope', async () => { + it('returns the payload when using safeParse with envelope', () => { // Prepare const detail = structuredClone(JSONPayload); const event = structuredClone(baseEventBridgeEvent); @@ -119,7 +119,7 @@ describe('Parser', () => { }); }); - it('returns an error when using safeParse with envelope and the payload is invalid', async () => { + it('returns an error when using safeParse with envelope and the payload is invalid', () => { // Prepare const event = structuredClone(baseEventBridgeEvent); From c2b4a163a28c452ede2d8e0675b3eac45b3bf4cf Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 24 Sep 2025 17:15:09 +0200 Subject: [PATCH 2/2] style(parser): address SonarCloud issues --- packages/parser/src/envelopes/sns-sqs.ts | 6 +++--- packages/parser/src/schemas/appsync-events.ts | 8 +++++--- packages/parser/src/schemas/appsync.ts | 6 ++++-- packages/parser/src/schemas/kafka.ts | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/parser/src/envelopes/sns-sqs.ts b/packages/parser/src/envelopes/sns-sqs.ts index 1aa17288e0..452930c0ed 100644 --- a/packages/parser/src/envelopes/sns-sqs.ts +++ b/packages/parser/src/envelopes/sns-sqs.ts @@ -150,11 +150,11 @@ export const SnsSqsEnvelope = { }>( (acc, record, index) => { const parsed = parseRecord(record, index); - if (!parsed.success) { + if (parsed.success) { + acc.records.push(parsed.data); + } else { acc.success = false; acc.errors[index] = parsed.error; - } else { - acc.records.push(parsed.data); } return acc; }, diff --git a/packages/parser/src/schemas/appsync-events.ts b/packages/parser/src/schemas/appsync-events.ts index 5c7b39f809..14bcdd1556 100644 --- a/packages/parser/src/schemas/appsync-events.ts +++ b/packages/parser/src/schemas/appsync-events.ts @@ -169,12 +169,14 @@ const AppSyncEventsSubscribeSchema = AppSyncEventsBaseSchema.extend({ export { AppSyncEventsBaseSchema, - AppSyncCognitoIdentity, - AppSyncIamIdentity, AppSyncLambdaAuthIdentity, - AppSyncOidcIdentity, AppSyncEventsRequestSchema, AppSyncEventsInfoSchema, AppSyncEventsPublishSchema, AppSyncEventsSubscribeSchema, }; +export { + AppSyncCognitoIdentity, + AppSyncIamIdentity, + AppSyncOidcIdentity, +} from './appsync-shared.js'; diff --git a/packages/parser/src/schemas/appsync.ts b/packages/parser/src/schemas/appsync.ts index ca2c554bec..dfdef7c29c 100644 --- a/packages/parser/src/schemas/appsync.ts +++ b/packages/parser/src/schemas/appsync.ts @@ -236,8 +236,10 @@ const AppSyncBatchResolverSchema = z.array(AppSyncResolverSchema); export { AppSyncResolverSchema, AppSyncBatchResolverSchema, + AppSyncLambdaIdentity, +}; +export { AppSyncCognitoIdentity, AppSyncIamIdentity, AppSyncOidcIdentity, - AppSyncLambdaIdentity, -}; +} from './appsync-shared.js'; diff --git a/packages/parser/src/schemas/kafka.ts b/packages/parser/src/schemas/kafka.ts index 3747755a2d..28962752e0 100644 --- a/packages/parser/src/schemas/kafka.ts +++ b/packages/parser/src/schemas/kafka.ts @@ -23,7 +23,7 @@ const KafkaRecordSchema = z.object({ z.record( z.string(), z.array(z.number()).transform((value) => { - return String.fromCharCode(...value); + return String.fromCodePoint(...value); }) ) ),