@middy/core 2.5.1 → 3.0.0-alpha.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
3
+ Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -47,7 +47,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
47
47
 
48
48
  ## License
49
49
 
50
- Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
50
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
51
51
 
52
52
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
53
53
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
package/index.d.ts CHANGED
@@ -6,14 +6,17 @@ import {
6
6
 
7
7
  declare type PluginHook = () => void
8
8
  declare type PluginHookWithMiddlewareName = (middlewareName: string) => void
9
- declare type PluginHookPromise = () => Promise<any>
9
+ declare type PluginHookPromise = (request: Request) => Promise<unknown> | unknown
10
10
 
11
11
  interface PluginObject {
12
+ internal?: any
12
13
  beforePrefetch?: PluginHook
13
14
  requestStart?: PluginHook
14
15
  beforeMiddleware?: PluginHookWithMiddlewareName
15
16
  afterMiddleware?: PluginHookWithMiddlewareName
16
17
  beforeHandler?: PluginHook
18
+ timeoutEarlyInMillis?: number
19
+ timeoutEarlyResponse?: PluginHook
17
20
  afterHandler?: PluginHook
18
21
  requestEnd?: PluginHookPromise
19
22
  }
@@ -33,7 +36,7 @@ declare type MiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext ex
33
36
  export interface MiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> {
34
37
  before?: MiddlewareFn<TEvent, TResult, TErr, TContext>
35
38
  after?: MiddlewareFn<TEvent, TResult, TErr, TContext>
36
- onError?: MiddlewareFn<TEvent, TResult, TErr>
39
+ onError?: MiddlewareFn<TEvent, TResult, TErr, TContext>
37
40
  }
38
41
 
39
42
  // The AWS provided Handler type uses void | Promise<TResult> so we have no choice but to follow and suppress the linter warning
@@ -42,15 +45,9 @@ type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaC
42
45
 
43
46
  export interface MiddyfiedHandler<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> extends MiddyInputHandler<TEvent, TResult, TContext> {
44
47
  use: UseFn<TEvent, TResult, TErr, TContext>
45
- applyMiddleware: AttachMiddlewareObj<TEvent, TResult, TErr, TContext>
46
48
  before: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
47
49
  after: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
48
50
  onError: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
49
- __middlewares: {
50
- before: Array<MiddlewareFn<TEvent, TResult, TErr, TContext>>
51
- after: Array<MiddlewareFn<TEvent, TResult, TErr, TContext>>
52
- onError: Array<MiddlewareFn<TEvent, TResult, TErr, TContext>>
53
- }
54
51
  }
55
52
 
56
53
  declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (middleware: MiddlewareFn) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@middy/core",
3
- "version": "2.5.1",
3
+ "version": "3.0.0-alpha.0",
4
4
  "description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)",
5
- "type": "commonjs",
5
+ "type": "module",
6
6
  "engines": {
7
- "node": ">=12"
7
+ "node": ">=14"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "index.js",
13
+ "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
16
  "index.d.ts"
@@ -43,7 +43,10 @@
43
43
  "homepage": "https://github.com/middyjs/middy#readme",
44
44
  "devDependencies": {
45
45
  "@types/aws-lambda": "^8.10.76",
46
- "@types/node": "^16.0.0"
46
+ "@types/node": "^17.0.0"
47
47
  },
48
- "gitHead": "df18e5eff7d73492a96a2ca4780a2eae45d1cedb"
48
+ "gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db",
49
+ "dependencies": {
50
+ "node-abort-controller": "3.0.1"
51
+ }
49
52
  }
package/index.js DELETED
@@ -1,130 +0,0 @@
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
- }; // Inline Middlewares
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); // Check if before stack hasn't exit early
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
- // Reset response changes made by after stack before error thrown
92
- request.response = undefined;
93
- request.error = e;
94
-
95
- try {
96
- await runMiddlewares(request, onErrorMiddlewares, plugin);
97
- } catch (e) {
98
- // 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
-
104
-
105
- if (request.response === undefined) throw request.error;
106
- } finally {
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
- }
111
-
112
- return request.response;
113
- };
114
-
115
- const runMiddlewares = async (request, middlewares, plugin) => {
116
- for (const nextMiddleware of middlewares) {
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
-
123
- if (res !== undefined) {
124
- request.response = res;
125
- return;
126
- }
127
- }
128
- };
129
-
130
- module.exports = middy;