@middy/core 2.5.5 → 2.5.6
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.d.ts +3 -1
- package/index.js +82 -84
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -39,8 +39,10 @@ export interface MiddlewareObj<TEvent = any, TResult = any, TErr = Error, TConte
|
|
|
39
39
|
// The AWS provided Handler type uses void | Promise<TResult> so we have no choice but to follow and suppress the linter warning
|
|
40
40
|
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
41
41
|
type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => void | Promise<TResult>
|
|
42
|
+
type MiddyInputPromiseHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext,) => Promise<TResult>
|
|
42
43
|
|
|
43
|
-
export interface MiddyfiedHandler<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> extends MiddyInputHandler<TEvent, TResult, TContext
|
|
44
|
+
export interface MiddyfiedHandler<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> extends MiddyInputHandler<TEvent, TResult, TContext>,
|
|
45
|
+
MiddyInputPromiseHandler<TEvent, TResult, TContext> {
|
|
44
46
|
use: UseFn<TEvent, TResult, TErr, TContext>
|
|
45
47
|
applyMiddleware: AttachMiddlewareObj<TEvent, TResult, TErr, TContext>
|
|
46
48
|
before: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
|
package/index.js
CHANGED
|
@@ -1,130 +1,128 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
1
|
const middy = (baseHandler = () => {}, plugin) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const afterMiddlewares = [];
|
|
9
|
-
const onErrorMiddlewares = [];
|
|
2
|
+
plugin?.beforePrefetch?.()
|
|
3
|
+
const beforeMiddlewares = []
|
|
4
|
+
const afterMiddlewares = []
|
|
5
|
+
const onErrorMiddlewares = []
|
|
10
6
|
|
|
11
7
|
const instance = (event = {}, context = {}) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
plugin === null || plugin === void 0 ? void 0 : (_plugin$requestStart = plugin.requestStart) === null || _plugin$requestStart === void 0 ? void 0 : _plugin$requestStart.call(plugin);
|
|
8
|
+
plugin?.requestStart?.()
|
|
15
9
|
const request = {
|
|
16
10
|
event,
|
|
17
11
|
context,
|
|
18
12
|
response: undefined,
|
|
19
13
|
error: undefined,
|
|
20
14
|
internal: {}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return runRequest(
|
|
18
|
+
request,
|
|
19
|
+
[...beforeMiddlewares],
|
|
20
|
+
baseHandler,
|
|
21
|
+
[...afterMiddlewares],
|
|
22
|
+
[...onErrorMiddlewares],
|
|
23
|
+
plugin
|
|
24
|
+
)
|
|
25
|
+
}
|
|
24
26
|
|
|
25
|
-
instance.use = middlewares => {
|
|
27
|
+
instance.use = (middlewares) => {
|
|
26
28
|
if (Array.isArray(middlewares)) {
|
|
27
29
|
for (const middleware of middlewares) {
|
|
28
|
-
instance.applyMiddleware(middleware)
|
|
30
|
+
instance.applyMiddleware(middleware)
|
|
29
31
|
}
|
|
30
|
-
|
|
31
|
-
return instance;
|
|
32
|
+
return instance
|
|
32
33
|
}
|
|
34
|
+
return instance.applyMiddleware(middlewares)
|
|
35
|
+
}
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
instance.applyMiddleware = middleware => {
|
|
38
|
-
const {
|
|
39
|
-
before,
|
|
40
|
-
after,
|
|
41
|
-
onError
|
|
42
|
-
} = middleware;
|
|
37
|
+
instance.applyMiddleware = (middleware) => {
|
|
38
|
+
const { before, after, onError } = middleware
|
|
43
39
|
|
|
44
40
|
if (!before && !after && !onError) {
|
|
45
|
-
throw new Error(
|
|
41
|
+
throw new Error(
|
|
42
|
+
'Middleware must be an object containing at least one key among "before", "after", "onError"'
|
|
43
|
+
)
|
|
46
44
|
}
|
|
47
45
|
|
|
48
|
-
if (before) instance.before(before)
|
|
49
|
-
if (after) instance.after(after)
|
|
50
|
-
if (onError) instance.onError(onError)
|
|
51
|
-
return instance;
|
|
52
|
-
}; // Inline Middlewares
|
|
46
|
+
if (before) instance.before(before)
|
|
47
|
+
if (after) instance.after(after)
|
|
48
|
+
if (onError) instance.onError(onError)
|
|
53
49
|
|
|
50
|
+
return instance
|
|
51
|
+
}
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
instance.after = afterMiddleware => {
|
|
61
|
-
afterMiddlewares.unshift(afterMiddleware)
|
|
62
|
-
return instance
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
};
|
|
53
|
+
// Inline Middlewares
|
|
54
|
+
instance.before = (beforeMiddleware) => {
|
|
55
|
+
beforeMiddlewares.push(beforeMiddleware)
|
|
56
|
+
return instance
|
|
57
|
+
}
|
|
58
|
+
instance.after = (afterMiddleware) => {
|
|
59
|
+
afterMiddlewares.unshift(afterMiddleware)
|
|
60
|
+
return instance
|
|
61
|
+
}
|
|
62
|
+
instance.onError = (onErrorMiddleware) => {
|
|
63
|
+
onErrorMiddlewares.push(onErrorMiddleware)
|
|
64
|
+
return instance
|
|
65
|
+
}
|
|
69
66
|
|
|
70
67
|
instance.__middlewares = {
|
|
71
68
|
before: beforeMiddlewares,
|
|
72
69
|
after: afterMiddlewares,
|
|
73
70
|
onError: onErrorMiddlewares
|
|
74
|
-
}
|
|
75
|
-
return instance;
|
|
76
|
-
};
|
|
71
|
+
}
|
|
77
72
|
|
|
78
|
-
|
|
73
|
+
return instance
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const runRequest = async (
|
|
77
|
+
request,
|
|
78
|
+
beforeMiddlewares,
|
|
79
|
+
baseHandler,
|
|
80
|
+
afterMiddlewares,
|
|
81
|
+
onErrorMiddlewares,
|
|
82
|
+
plugin
|
|
83
|
+
) => {
|
|
79
84
|
try {
|
|
80
|
-
await runMiddlewares(request, beforeMiddlewares, plugin)
|
|
81
|
-
|
|
85
|
+
await runMiddlewares(request, beforeMiddlewares, plugin)
|
|
86
|
+
// Check if before stack hasn't exit early
|
|
82
87
|
if (request.response === undefined) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
plugin
|
|
86
|
-
|
|
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);
|
|
88
|
+
plugin?.beforeHandler?.()
|
|
89
|
+
request.response = await baseHandler(request.event, request.context)
|
|
90
|
+
plugin?.afterHandler?.()
|
|
91
|
+
await runMiddlewares(request, afterMiddlewares, plugin)
|
|
89
92
|
}
|
|
90
93
|
} catch (e) {
|
|
91
94
|
// Reset response changes made by after stack before error thrown
|
|
92
|
-
request.response = undefined
|
|
93
|
-
request.error = e
|
|
94
|
-
|
|
95
|
+
request.response = undefined
|
|
96
|
+
request.error = e
|
|
95
97
|
try {
|
|
96
|
-
await runMiddlewares(request, onErrorMiddlewares, plugin)
|
|
98
|
+
await runMiddlewares(request, onErrorMiddlewares, plugin)
|
|
97
99
|
} catch (e) {
|
|
98
100
|
// Save error that wasn't handled
|
|
99
|
-
e.originalError = request.error
|
|
100
|
-
request.error = e
|
|
101
|
-
throw request.error;
|
|
102
|
-
} // Catch if onError stack hasn't handled the error
|
|
103
|
-
|
|
101
|
+
e.originalError = request.error
|
|
102
|
+
request.error = e
|
|
104
103
|
|
|
105
|
-
|
|
104
|
+
throw request.error
|
|
105
|
+
}
|
|
106
|
+
// Catch if onError stack hasn't handled the error
|
|
107
|
+
if (request.response === undefined) throw request.error
|
|
106
108
|
} finally {
|
|
107
|
-
|
|
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));
|
|
109
|
+
await plugin?.requestEnd?.(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
|
-
|
|
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
|
-
|
|
117
|
+
plugin?.beforeMiddleware?.(nextMiddleware?.name)
|
|
118
|
+
const res = await nextMiddleware?.(request)
|
|
119
|
+
plugin?.afterMiddleware?.(nextMiddleware?.name)
|
|
120
|
+
// short circuit chaining and respond early
|
|
123
121
|
if (res !== undefined) {
|
|
124
|
-
request.response = res
|
|
125
|
-
return
|
|
122
|
+
request.response = res
|
|
123
|
+
return
|
|
126
124
|
}
|
|
127
125
|
}
|
|
128
|
-
}
|
|
126
|
+
}
|
|
129
127
|
|
|
130
|
-
module.exports = middy
|
|
128
|
+
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.6",
|
|
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": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
|
|
49
49
|
}
|