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