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>
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 | type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => void | Promise<TResult>
|
42 |
|
43 | export interface MiddyfiedHandler<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> extends MiddyInputHandler<TEvent, TResult, TContext> {
|
44 | use: UseFn<TEvent, TResult, TErr, TContext>
|
45 | applyMiddleware: AttachMiddlewareObj<TEvent, TResult, TErr, TContext>
|
46 | before: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
|
47 | after: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
|
48 | onError: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
|
49 | __middlewares: {
|
50 | before: Array<MiddlewareFn<TEvent, TResult, TErr, TContext>>
|
51 | after: Array<MiddlewareFn<TEvent, TResult, TErr, TContext>>
|
52 | onError: Array<MiddlewareFn<TEvent, TResult, TErr, TContext>>
|
53 | }
|
54 | }
|
55 |
|
56 | declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareFn) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
|
57 |
|
58 | declare type AttachMiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareObj) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
|
59 |
|
60 | declare type UseFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
|
61 | (middlewares: MiddlewareObj<TEvent, TResult, TErr, TContext> | Array<MiddlewareObj<TEvent, TResult, TErr, TContext>>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
|
62 |
|
63 | declare type MiddlewareHandler<THandler extends LambdaHandler<any, any>, TContext extends LambdaContext = LambdaContext> =
|
64 | THandler extends LambdaHandler<infer TEvent, infer TResult>
|
65 | ? MiddyInputHandler<TEvent, TResult, TContext>
|
66 | : never
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | 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>
|
74 |
|
75 | declare namespace middy {
|
76 | export {
|
77 | Request,
|
78 | PluginHook,
|
79 | PluginHookWithMiddlewareName,
|
80 | PluginObject,
|
81 | MiddlewareFn,
|
82 | MiddlewareObj,
|
83 | MiddyfiedHandler
|
84 | }
|
85 | }
|
86 |
|
87 | export default middy
|