1 | "use strict";
|
2 |
|
3 | const middy = (baseHandler = () => {}, plugin) => {
|
4 | var _plugin$beforePrefetc;
|
5 |
|
6 | plugin === null || plugin === void 0 ? void 0 : (_plugin$beforePrefetc = plugin.beforePrefetch) === null || _plugin$beforePrefetc === void 0 ? void 0 : _plugin$beforePrefetc.call(plugin);
|
7 | const beforeMiddlewares = [];
|
8 | const afterMiddlewares = [];
|
9 | const onErrorMiddlewares = [];
|
10 |
|
11 | const instance = (event = {}, context = {}) => {
|
12 | var _plugin$requestStart;
|
13 |
|
14 | plugin === null || plugin === void 0 ? void 0 : (_plugin$requestStart = plugin.requestStart) === null || _plugin$requestStart === void 0 ? void 0 : _plugin$requestStart.call(plugin);
|
15 | const request = {
|
16 | event,
|
17 | context,
|
18 | response: undefined,
|
19 | error: undefined,
|
20 | internal: {}
|
21 | };
|
22 | return runRequest(request, [...beforeMiddlewares], baseHandler, [...afterMiddlewares], [...onErrorMiddlewares], plugin);
|
23 | };
|
24 |
|
25 | instance.use = middlewares => {
|
26 | if (Array.isArray(middlewares)) {
|
27 | for (const middleware of middlewares) {
|
28 | instance.applyMiddleware(middleware);
|
29 | }
|
30 |
|
31 | return instance;
|
32 | }
|
33 |
|
34 | return instance.applyMiddleware(middlewares);
|
35 | };
|
36 |
|
37 | instance.applyMiddleware = middleware => {
|
38 | const {
|
39 | before,
|
40 | after,
|
41 | onError
|
42 | } = middleware;
|
43 |
|
44 | if (!before && !after && !onError) {
|
45 | throw new Error('Middleware must be an object containing at least one key among "before", "after", "onError"');
|
46 | }
|
47 |
|
48 | if (before) instance.before(before);
|
49 | if (after) instance.after(after);
|
50 | if (onError) instance.onError(onError);
|
51 | return instance;
|
52 | };
|
53 |
|
54 |
|
55 | instance.before = beforeMiddleware => {
|
56 | beforeMiddlewares.push(beforeMiddleware);
|
57 | return instance;
|
58 | };
|
59 |
|
60 | instance.after = afterMiddleware => {
|
61 | afterMiddlewares.unshift(afterMiddleware);
|
62 | return instance;
|
63 | };
|
64 |
|
65 | instance.onError = onErrorMiddleware => {
|
66 | onErrorMiddlewares.push(onErrorMiddleware);
|
67 | return instance;
|
68 | };
|
69 |
|
70 | instance.__middlewares = {
|
71 | before: beforeMiddlewares,
|
72 | after: afterMiddlewares,
|
73 | onError: onErrorMiddlewares
|
74 | };
|
75 | return instance;
|
76 | };
|
77 |
|
78 | const runRequest = async (request, beforeMiddlewares, baseHandler, afterMiddlewares, onErrorMiddlewares, plugin) => {
|
79 | try {
|
80 | await runMiddlewares(request, beforeMiddlewares, plugin);
|
81 |
|
82 | if (request.response === undefined) {
|
83 | var _plugin$beforeHandler, _plugin$afterHandler;
|
84 |
|
85 | plugin === null || plugin === void 0 ? void 0 : (_plugin$beforeHandler = plugin.beforeHandler) === null || _plugin$beforeHandler === void 0 ? void 0 : _plugin$beforeHandler.call(plugin);
|
86 | request.response = await baseHandler(request.event, request.context);
|
87 | plugin === null || plugin === void 0 ? void 0 : (_plugin$afterHandler = plugin.afterHandler) === null || _plugin$afterHandler === void 0 ? void 0 : _plugin$afterHandler.call(plugin);
|
88 | await runMiddlewares(request, afterMiddlewares, plugin);
|
89 | }
|
90 | } catch (e) {
|
91 |
|
92 | request.response = undefined;
|
93 | request.error = e;
|
94 |
|
95 | try {
|
96 | await runMiddlewares(request, onErrorMiddlewares, plugin);
|
97 |
|
98 | if (request.response === undefined) throw request.error;
|
99 | } catch (e) {
|
100 |
|
101 | e.originalError = request.error;
|
102 | request.error = e;
|
103 | throw request.error;
|
104 | }
|
105 | } finally {
|
106 | var _plugin$requestEnd;
|
107 |
|
108 | await (plugin === null || plugin === void 0 ? void 0 : (_plugin$requestEnd = plugin.requestEnd) === null || _plugin$requestEnd === void 0 ? void 0 : _plugin$requestEnd.call(plugin));
|
109 | }
|
110 |
|
111 | return request.response;
|
112 | };
|
113 |
|
114 | const runMiddlewares = async (request, middlewares, plugin) => {
|
115 | for (const nextMiddleware of middlewares) {
|
116 | var _plugin$beforeMiddlew, _plugin$afterMiddlewa;
|
117 |
|
118 | plugin === null || plugin === void 0 ? void 0 : (_plugin$beforeMiddlew = plugin.beforeMiddleware) === null || _plugin$beforeMiddlew === void 0 ? void 0 : _plugin$beforeMiddlew.call(plugin, nextMiddleware === null || nextMiddleware === void 0 ? void 0 : nextMiddleware.name);
|
119 | const res = await (nextMiddleware === null || nextMiddleware === void 0 ? void 0 : nextMiddleware(request));
|
120 | plugin === null || plugin === void 0 ? void 0 : (_plugin$afterMiddlewa = plugin.afterMiddleware) === null || _plugin$afterMiddlewa === void 0 ? void 0 : _plugin$afterMiddlewa.call(plugin, nextMiddleware === null || nextMiddleware === void 0 ? void 0 : nextMiddleware.name);
|
121 |
|
122 | if (res !== undefined) {
|
123 | request.response = res;
|
124 | return;
|
125 | }
|
126 | }
|
127 | };
|
128 |
|
129 | module.exports = middy;
|