Skip to content

Commit 8674f5e

Browse files
committed
fix: remove handler callback argument
Middy does not support callback handlers since 1.x, the third argument is actually the timeout signal.
1 parent c8a0e5c commit 8674f5e

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

packages/core/index.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
Callback as LambdaCallback,
32
Context as LambdaContext,
43
Handler as LambdaHandler
54
} from 'aws-lambda'
@@ -59,6 +58,14 @@ export interface MiddlewareObj<
5958
name?: string
6059
}
6160

61+
export interface MiddyHandlerObject {
62+
/**
63+
* An abort signal that will be canceled just before the lambda times out.
64+
* @see timeoutEarlyInMillis
65+
*/
66+
signal: AbortSignal
67+
}
68+
6269
// The AWS provided Handler type uses void | Promise<TResult> so we have no choice but to follow and suppress the linter warning
6370
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
6471
type MiddyInputHandler<
@@ -68,7 +75,7 @@ type MiddyInputHandler<
6875
> = (
6976
event: TEvent,
7077
context: TContext,
71-
callback: LambdaCallback<TResult>
78+
opts: MiddyHandlerObject,
7279
) => // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
7380
void | Promise<TResult> | TResult
7481
type MiddyInputPromiseHandler<

packages/core/index.test-d.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expectType } from 'tsd'
1+
import { expectType, expectAssignable } from 'tsd'
22
import middy from '.'
33
import {
44
APIGatewayProxyEvent,
@@ -11,12 +11,17 @@ import {
1111
type EnhanceHandlerType<T, NewReturn> = T extends (
1212
event: infer TEvent,
1313
context: infer TContextType,
14-
callback: infer TCallbackType
14+
opts: infer TOptsType
1515
) => infer R
16-
? (event: TEvent, context: TContextType, callback: TCallbackType) => R | NewReturn
16+
? (event: TEvent, context: TContextType, opts: TOptsType) => R | NewReturn
1717
: never
1818

19-
type LambdaHandler<TEvent = any, TResult = any> = EnhanceHandlerType<AWSLambdaHandler<TEvent, TResult>, TResult>
19+
type AWSLambdaHandlerWithoutCallback<TEvent = any, TResult = any> = (
20+
event: TEvent,
21+
context: Context,
22+
) => void | Promise<TResult>;
23+
24+
type LambdaHandler<TEvent = any, TResult = any> = EnhanceHandlerType<AWSLambdaHandlerWithoutCallback<TEvent, TResult>, TResult>
2025

2126
const lambdaHandler: LambdaHandler<APIGatewayProxyEvent, APIGatewayProxyResult> = async (event) => {
2227
return {
@@ -54,6 +59,12 @@ handler = middy(lambdaHandler, {
5459
})
5560
expectType<Handler>(handler)
5661

62+
// middy wrapped handler should be assignable to aws-lambda handler type.
63+
expectAssignable<AWSLambdaHandler<APIGatewayProxyEvent, APIGatewayProxyResult>>(handler)
64+
65+
// Middy handlers third argument is an object containing a abort signal
66+
middy((event, context, { signal }) => expectType<AbortSignal>(signal));
67+
5768
// invokes the handler to test that it is callable
5869
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
5970
async function invokeHandler (): Promise<void | APIGatewayProxyResult> {
@@ -127,7 +138,7 @@ async function invokeHandler (): Promise<void | APIGatewayProxyResult> {
127138
fail: (_) => { },
128139
succeed: () => { }
129140
}
130-
return await handler(sampleEvent, sampleContext, () => {})
141+
return await handler(sampleEvent, sampleContext)
131142
}
132143
invokeHandler().catch(console.error)
133144

@@ -315,7 +326,7 @@ async function invokSyncedHandler (): Promise<void | APIGatewayProxyResult> {
315326
fail: (_) => { },
316327
succeed: () => { }
317328
}
318-
return await syncedHandler(sampleEvent, sampleContext, () => {})
329+
return await syncedHandler(sampleEvent, sampleContext)
319330
}
320331
invokSyncedHandler().catch(console.error)
321332

0 commit comments

Comments
 (0)