1 | import {
|
2 | Context as LambdaContext,
|
3 | Handler as LambdaHandler,
|
4 | Callback as LambdaCallback
|
5 | } from 'aws-lambda'
|
6 |
|
7 | declare type PluginHook = () => void
|
8 | declare type PluginHookWithMiddlewareName = (middlewareName: string) => void
|
9 | declare type PluginHookPromise = (request: Request) => Promise<unknown> | unknown
|
10 |
|
11 | interface PluginObject {
|
12 | internal?: any
|
13 | beforePrefetch?: PluginHook
|
14 | requestStart?: PluginHook
|
15 | beforeMiddleware?: PluginHookWithMiddlewareName
|
16 | afterMiddleware?: PluginHookWithMiddlewareName
|
17 | beforeHandler?: PluginHook
|
18 | timeoutEarlyInMillis?: number
|
19 | timeoutEarlyResponse?: PluginHook
|
20 | afterHandler?: PluginHook
|
21 | requestEnd?: PluginHookPromise
|
22 | }
|
23 |
|
24 | interface Request<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> {
|
25 | event: TEvent
|
26 | context: TContext
|
27 | response: TResult | null
|
28 | error: TErr | null
|
29 | internal: {
|
30 | [key: string]: any
|
31 | }
|
32 | }
|
33 |
|
34 | declare type MiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (request: Request<TEvent, TResult, TErr, TContext>) => any
|
35 |
|
36 | export interface MiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> {
|
37 | before?: MiddlewareFn<TEvent, TResult, TErr, TContext>
|
38 | after?: MiddlewareFn<TEvent, TResult, TErr, TContext>
|
39 | onError?: MiddlewareFn<TEvent, TResult, TErr, TContext>
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 | type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => void | Promise<TResult>
|
45 | type MiddyInputPromiseHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext,) => Promise<TResult>
|
46 |
|
47 | export interface MiddyfiedHandler<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> extends MiddyInputHandler<TEvent, TResult, TContext>,
|
48 | MiddyInputPromiseHandler<TEvent, TResult, TContext> {
|
49 | use: UseFn<TEvent, TResult, TErr, TContext>
|
50 | before: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
|
51 | after: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
|
52 | onError: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
|
53 | }
|
54 |
|
55 | declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareFn) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
|
56 |
|
57 | declare type AttachMiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareObj) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
|
58 |
|
59 | declare type UseFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
|
60 | (middlewares: MiddlewareObj<TEvent, TResult, TErr, TContext> | Array<MiddlewareObj<TEvent, TResult, TErr, TContext>>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
|
61 |
|
62 | declare type MiddlewareHandler<THandler extends LambdaHandler<any, any>, TContext extends LambdaContext = LambdaContext> =
|
63 | THandler extends LambdaHandler<infer TEvent, infer TResult>
|
64 | ? MiddyInputHandler<TEvent, TResult, TContext>
|
65 | : never
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | declare function middy<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> (handler?: MiddlewareHandler<LambdaHandler<TEvent, TResult>, TContext>, plugin?: PluginObject): MiddyfiedHandler<TEvent, TResult, TErr, TContext>
|
73 |
|
74 | declare namespace middy {
|
75 | export {
|
76 | Request,
|
77 | PluginHook,
|
78 | PluginHookWithMiddlewareName,
|
79 | PluginObject,
|
80 | MiddlewareFn,
|
81 | MiddlewareObj,
|
82 | MiddyfiedHandler
|
83 | }
|
84 | }
|
85 |
|
86 | export default middy
|