UNPKG

4.09 kBTypeScriptView Raw
1import {
2 Context as LambdaContext,
3 Handler as LambdaHandler,
4 Callback as LambdaCallback
5} from 'aws-lambda'
6
7declare type PluginHook = () => void
8declare type PluginHookWithMiddlewareName = (middlewareName: string) => void
9declare type PluginHookPromise = (request: Request) => Promise<unknown> | unknown
10
11interface PluginObject {
12 beforePrefetch?: PluginHook
13 requestStart?: PluginHook
14 beforeMiddleware?: PluginHookWithMiddlewareName
15 afterMiddleware?: PluginHookWithMiddlewareName
16 beforeHandler?: PluginHook
17 afterHandler?: PluginHook
18 requestEnd?: PluginHookPromise
19}
20
21interface 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
31declare type MiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (request: Request<TEvent, TResult, TErr, TContext>) => any
32
33export 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// The AWS provided Handler type uses void | Promise<TResult> so we have no choice but to follow and suppress the linter warning
40// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
41type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => void | Promise<TResult>
42
43export 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
56declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareFn) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
57
58declare type AttachMiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareObj) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
59
60declare 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
63declare type MiddlewareHandler<THandler extends LambdaHandler<any, any>, TContext extends LambdaContext = LambdaContext> =
64 THandler extends LambdaHandler<infer TEvent, infer TResult> // always true
65 ? MiddyInputHandler<TEvent, TResult, TContext>
66 : never
67
68/**
69 * Middy factory function. Use it to wrap your existing handler to enable middlewares on it.
70 * @param handler your original AWS Lambda function
71 * @param plugin wraps around each middleware and handler to add custom lifecycle behaviours (e.g. to profile performance)
72 */
73declare 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
75declare namespace middy {
76 export {
77 Request,
78 PluginHook,
79 PluginHookWithMiddlewareName,
80 PluginObject,
81 MiddlewareFn,
82 MiddlewareObj,
83 MiddyfiedHandler
84 }
85}
86
87export default middy