@middy/core 2.5.6 → 2.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +84 -82
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,128 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
1
3
|
const middy = (baseHandler = () => {}, plugin) => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
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 = [];
|
|
6
10
|
|
|
7
11
|
const instance = (event = {}, context = {}) => {
|
|
8
|
-
|
|
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);
|
|
9
15
|
const request = {
|
|
10
16
|
event,
|
|
11
17
|
context,
|
|
12
18
|
response: undefined,
|
|
13
19
|
error: undefined,
|
|
14
20
|
internal: {}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
request,
|
|
19
|
-
[...beforeMiddlewares],
|
|
20
|
-
baseHandler,
|
|
21
|
-
[...afterMiddlewares],
|
|
22
|
-
[...onErrorMiddlewares],
|
|
23
|
-
plugin
|
|
24
|
-
)
|
|
25
|
-
}
|
|
21
|
+
};
|
|
22
|
+
return runRequest(request, [...beforeMiddlewares], baseHandler, [...afterMiddlewares], [...onErrorMiddlewares], plugin);
|
|
23
|
+
};
|
|
26
24
|
|
|
27
|
-
instance.use =
|
|
25
|
+
instance.use = middlewares => {
|
|
28
26
|
if (Array.isArray(middlewares)) {
|
|
29
27
|
for (const middleware of middlewares) {
|
|
30
|
-
instance.applyMiddleware(middleware)
|
|
28
|
+
instance.applyMiddleware(middleware);
|
|
31
29
|
}
|
|
32
|
-
|
|
30
|
+
|
|
31
|
+
return instance;
|
|
33
32
|
}
|
|
34
|
-
return instance.applyMiddleware(middlewares)
|
|
35
|
-
}
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
return instance.applyMiddleware(middlewares);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
instance.applyMiddleware = middleware => {
|
|
38
|
+
const {
|
|
39
|
+
before,
|
|
40
|
+
after,
|
|
41
|
+
onError
|
|
42
|
+
} = middleware;
|
|
39
43
|
|
|
40
44
|
if (!before && !after && !onError) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
'Middleware must be an object containing at least one key among "before", "after", "onError"'
|
|
43
|
-
)
|
|
45
|
+
throw new Error('Middleware must be an object containing at least one key among "before", "after", "onError"');
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
if (before) instance.before(before)
|
|
47
|
-
if (after) instance.after(after)
|
|
48
|
-
if (onError) instance.onError(onError)
|
|
48
|
+
if (before) instance.before(before);
|
|
49
|
+
if (after) instance.after(after);
|
|
50
|
+
if (onError) instance.onError(onError);
|
|
51
|
+
return instance;
|
|
52
|
+
}; // Inline Middlewares
|
|
49
53
|
|
|
50
|
-
return instance
|
|
51
|
-
}
|
|
52
54
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
instance.after =
|
|
59
|
-
afterMiddlewares.unshift(afterMiddleware)
|
|
60
|
-
return instance
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
+
};
|
|
66
69
|
|
|
67
70
|
instance.__middlewares = {
|
|
68
71
|
before: beforeMiddlewares,
|
|
69
72
|
after: afterMiddlewares,
|
|
70
73
|
onError: onErrorMiddlewares
|
|
71
|
-
}
|
|
74
|
+
};
|
|
75
|
+
return instance;
|
|
76
|
+
};
|
|
72
77
|
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const runRequest = async (
|
|
77
|
-
request,
|
|
78
|
-
beforeMiddlewares,
|
|
79
|
-
baseHandler,
|
|
80
|
-
afterMiddlewares,
|
|
81
|
-
onErrorMiddlewares,
|
|
82
|
-
plugin
|
|
83
|
-
) => {
|
|
78
|
+
const runRequest = async (request, beforeMiddlewares, baseHandler, afterMiddlewares, onErrorMiddlewares, plugin) => {
|
|
84
79
|
try {
|
|
85
|
-
await runMiddlewares(request, beforeMiddlewares, plugin)
|
|
86
|
-
|
|
80
|
+
await runMiddlewares(request, beforeMiddlewares, plugin); // Check if before stack hasn't exit early
|
|
81
|
+
|
|
87
82
|
if (request.response === undefined) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
plugin
|
|
91
|
-
await
|
|
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);
|
|
92
89
|
}
|
|
93
90
|
} catch (e) {
|
|
94
91
|
// Reset response changes made by after stack before error thrown
|
|
95
|
-
request.response = undefined
|
|
96
|
-
request.error = e
|
|
92
|
+
request.response = undefined;
|
|
93
|
+
request.error = e;
|
|
94
|
+
|
|
97
95
|
try {
|
|
98
|
-
await runMiddlewares(request, onErrorMiddlewares, plugin)
|
|
96
|
+
await runMiddlewares(request, onErrorMiddlewares, plugin);
|
|
99
97
|
} catch (e) {
|
|
100
98
|
// Save error that wasn't handled
|
|
101
|
-
e.originalError = request.error
|
|
102
|
-
request.error = e
|
|
99
|
+
e.originalError = request.error;
|
|
100
|
+
request.error = e;
|
|
101
|
+
throw request.error;
|
|
102
|
+
} // Catch if onError stack hasn't handled the error
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// Catch if onError stack hasn't handled the error
|
|
107
|
-
if (request.response === undefined) throw request.error
|
|
104
|
+
|
|
105
|
+
if (request.response === undefined) throw request.error;
|
|
108
106
|
} finally {
|
|
109
|
-
|
|
107
|
+
var _plugin$requestEnd;
|
|
108
|
+
|
|
109
|
+
await (plugin === null || plugin === void 0 ? void 0 : (_plugin$requestEnd = plugin.requestEnd) === null || _plugin$requestEnd === void 0 ? void 0 : _plugin$requestEnd.call(plugin, request));
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
return request.response
|
|
113
|
-
}
|
|
112
|
+
return request.response;
|
|
113
|
+
};
|
|
114
114
|
|
|
115
115
|
const runMiddlewares = async (request, middlewares, plugin) => {
|
|
116
116
|
for (const nextMiddleware of middlewares) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
plugin
|
|
120
|
-
|
|
117
|
+
var _plugin$beforeMiddlew, _plugin$afterMiddlewa;
|
|
118
|
+
|
|
119
|
+
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);
|
|
120
|
+
const res = await (nextMiddleware === null || nextMiddleware === void 0 ? void 0 : nextMiddleware(request));
|
|
121
|
+
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); // short circuit chaining and respond early
|
|
122
|
+
|
|
121
123
|
if (res !== undefined) {
|
|
122
|
-
request.response = res
|
|
123
|
-
return
|
|
124
|
+
request.response = res;
|
|
125
|
+
return;
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
|
-
}
|
|
128
|
+
};
|
|
127
129
|
|
|
128
|
-
module.exports = middy
|
|
130
|
+
module.exports = middy;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/core",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.7",
|
|
4
4
|
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"engines": {
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"@types/aws-lambda": "^8.10.76",
|
|
46
46
|
"@types/node": "^16.0.0"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "3983c4b138e1a4d7fcb3ed805d3b8832fff06fc1"
|
|
49
49
|
}
|