-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathmiddleware.ts
50 lines (45 loc) · 1.48 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Context } from 'aws-lambda';
import { ArgsDict, BaseProxyEvent, Response } from './types';
import { JSONData } from './types';
/** HTTP middleware function that wraps the route invocation in a AWS Lambda function */
type Middleware<T = JSONData | Response> = (
event: BaseProxyEvent,
context: Context,
args: ArgsDict,
next: () => Promise<T>
) => Promise<T>;
/** Model for an AWS Lambda HTTP handler function */
type Handler<T = JSONData | Response> = (
event: BaseProxyEvent,
context: Context,
args?: ArgsDict
) => Promise<T>;
/** Wraps the AWS Lambda handler function with the provided middlewares.
*
* @remarks
* The middewares are stacked in a classic onion-like pattern,
*
* @typeParam T - the response type of the handler function
*
* @param middlewares middlewares that must be wrapped around the handler
* @param handler the handler function
* @param args arguments for the handler function
* @returns a handler function that is wrapped around the middlewares
*
* @internal
*/
const wrapWithMiddlewares =
<T>(
middlewares: Middleware<T>[],
handler: Handler<T>,
args: ArgsDict
): Handler<T> =>
async (event: BaseProxyEvent, context: Context): Promise<T> => {
const chain = middlewares.reduceRight(
(next: () => Promise<T>, middleware: Middleware<T>) => () =>
middleware(event, context, args, next),
() => handler(event, context, args)
);
return await chain();
};
export { Handler, Middleware, wrapWithMiddlewares };