forked from middyjs/middy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
56 lines (45 loc) · 1.75 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
Callback,
Context,
Handler
} from 'aws-lambda';
declare type EventType<T> =
T extends (event: infer EventArgType, context: Context, callback: Callback<any>) => void ? EventArgType :
T extends (event: infer EventArgType, context: Context) => Promise<any> ? EventArgType :
never;
declare type HandlerReturnType<T> =
T extends (event: any, context: Context) => Promise<infer RetType> ? RetType :
T extends (event: any, context: Context, callback: Callback<infer RetType>) => void ? RetType :
never;
declare type AsyncHandler<C extends Context> =
((event: any, context: C, callback: Callback<any>) => void) |
((event: any, context: C) => Promise<any>);
declare const middy: <H extends AsyncHandler<C>, C extends Context = Context>(handler: H) => middy.Middy<
EventType<H>,
HandlerReturnType<H>
>;
declare namespace middy {
interface Middy<T, R> extends Handler<T, R> {
use: <C extends MiddlewareObject<T, R>>(middleware: C) => Middy<T, R>;
before: (callbackFn: MiddlewareFunction<T, R>) => Middy<T, R>;
after: (callbackFn: MiddlewareFunction<T, R>) => Middy<T, R>;
onError: (callbackFn: MiddlewareFunction<T, R>) => Middy<T, R>;
}
type Middleware<C extends any, T = any, R = any> = (config?: C) => MiddlewareObject<T, R>;
interface MiddlewareObject<T, R> {
before?: MiddlewareFunction<T, R>;
after?: MiddlewareFunction<T, R>;
onError?: MiddlewareFunction<T, R>;
}
type MiddlewareFunction<T, R> = (handler: HandlerLambda<T, R>, next: NextFunction) => void | Promise<any>;
type NextFunction = (error?: any) => void;
interface HandlerLambda<T = any, V = {}> {
event: T;
context: Context;
response: V;
error: Error;
callback: Callback<T>;
}
}
export = middy;
export as namespace middy;