UNPKG

4.31 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, TContext>
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>
42type MiddyInputPromiseHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext,) => Promise<TResult>
43
44export 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
58declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareFn) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
59
60declare type AttachMiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareObj) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
61
62declare 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
65declare type MiddlewareHandler<THandler extends LambdaHandler<any, any>, TContext extends LambdaContext = LambdaContext> =
66 THandler extends LambdaHandler<infer TEvent, infer TResult> // always true
67 ? MiddyInputHandler<TEvent, TResult, TContext>
68 : never
69
70/**
71 * Middy factory function. Use it to wrap your existing handler to enable middlewares on it.
72 * @param handler your original AWS Lambda function
73 * @param plugin wraps around each middleware and handler to add custom lifecycle behaviours (e.g. to profile performance)
74 */
75declare 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
77declare namespace middy {
78 export {
79 Request,
80 PluginHook,
81 PluginHookWithMiddlewareName,
82 PluginObject,
83 MiddlewareFn,
84 MiddlewareObj,
85 MiddyfiedHandler
86 }
87}
88
89export default middy