Skip to content

Commit a383256

Browse files
authored
improv: decorate methods without destructuring args (#4345)
1 parent 1730b1a commit a383256

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

packages/logger/src/Logger.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
getXRayTraceIdFromEnv,
1414
isDevMode,
1515
} from '@aws-lambda-powertools/commons/utils/env';
16-
import type { Context, Handler } from 'aws-lambda';
16+
import type { Callback, Context, Handler } from 'aws-lambda';
1717
import merge from 'lodash.merge';
1818
import {
1919
LogJsonIndent,
@@ -493,19 +493,17 @@ class Logger extends Utility implements LoggerInterface {
493493
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
494494
descriptor.value = async function (
495495
this: Handler,
496-
event,
497-
context,
498-
callback
496+
...args: [unknown, Context, Callback]
499497
) {
500498
loggerRef.refreshSampleRateCalculation();
501-
loggerRef.addContext(context);
502-
loggerRef.logEventIfEnabled(event, options?.logEvent);
499+
loggerRef.addContext(args[1]);
500+
loggerRef.logEventIfEnabled(args[0], options?.logEvent);
503501
if (options?.correlationIdPath) {
504-
loggerRef.setCorrelationId(event, options?.correlationIdPath);
502+
loggerRef.setCorrelationId(args[0], options?.correlationIdPath);
505503
}
506504

507505
try {
508-
return await originalMethod.apply(this, [event, context, callback]);
506+
return await originalMethod.apply(this, args);
509507
} catch (error) {
510508
if (options?.flushBufferOnUncaughtError) {
511509
loggerRef.flushBuffer();

packages/metrics/src/Metrics.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,17 +598,15 @@ class Metrics extends Utility implements MetricsInterface {
598598
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
599599
descriptor.value = async function (
600600
this: Handler,
601-
event: unknown,
602-
context: Context,
603-
callback: Callback
601+
...args: [unknown, Context, Callback]
604602
): Promise<unknown> {
605603
if (captureColdStartMetric) {
606-
metricsRef.captureColdStartMetric(context.functionName);
604+
metricsRef.captureColdStartMetric(args[1].functionName);
607605
}
608606

609607
let result: unknown;
610608
try {
611-
result = await originalMethod.apply(this, [event, context, callback]);
609+
result = await originalMethod.apply(this, args);
612610
} finally {
613611
metricsRef.publishStoredMetrics();
614612
}

packages/parser/src/parserDecorator.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
22
import type { StandardSchemaV1 } from '@standard-schema/spec';
3-
import type { Context, Handler } from 'aws-lambda';
3+
import type { Callback, Context, Handler } from 'aws-lambda';
44
import { parse } from './parser.js';
55
import type { Envelope, ParserOptions } from './types/index.js';
66
import type { ParserOutput } from './types/parser.js';
@@ -75,21 +75,22 @@ export const parser = <
7575
>(
7676
options: ParserOptions<TSchema, TEnvelope, TSafeParse>
7777
): HandlerMethodDecorator => {
78-
return (_target, _propertyKey, descriptor) => {
79-
// biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
80-
const original = descriptor.value!;
78+
return (
79+
_target: unknown,
80+
_propertyKey: string | symbol,
81+
descriptor: PropertyDescriptor
82+
) => {
83+
const original = descriptor.value;
8184

8285
const { schema, envelope, safeParse } = options;
8386

8487
descriptor.value = async function (
8588
this: Handler,
86-
event: ParserOutput<TSchema, TEnvelope, TSafeParse>,
87-
context: Context,
88-
callback
89+
...args: [ParserOutput<TSchema, TEnvelope, TSafeParse>, Context, Callback]
8990
) {
90-
const parsedEvent = parse(event, envelope, schema, safeParse);
91+
const parsedEvent = parse(args[0], envelope, schema, safeParse);
9192

92-
return original.call(this, parsedEvent, context, callback);
93+
return original.apply(this, [parsedEvent, ...args.slice(1)]);
9394
};
9495

9596
return descriptor;

0 commit comments

Comments
 (0)