@webiny/handler 0.0.0-unstable.bca7b3e350 → 0.0.0-unstable.c59b9cc5b9
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/Context.d.ts +19 -10
- package/Context.js +16 -7
- package/Context.js.map +1 -1
- package/fastify.js +35 -91
- package/fastify.js.map +1 -1
- package/index.d.ts +0 -1
- package/index.js +0 -31
- package/index.js.map +1 -1
- package/middleware.js +2 -8
- package/middleware.js.map +1 -1
- package/package.json +16 -17
- package/plugins/BeforeHandlerPlugin.js +0 -10
- package/plugins/BeforeHandlerPlugin.js.map +1 -1
- package/plugins/EventPlugin.js +1 -8
- package/plugins/EventPlugin.js.map +1 -1
- package/plugins/HandlerErrorPlugin.js +0 -9
- package/plugins/HandlerErrorPlugin.js.map +1 -1
- package/plugins/HandlerOnRequestPlugin.js +0 -9
- package/plugins/HandlerOnRequestPlugin.js.map +1 -1
- package/plugins/HandlerResultPlugin.js +0 -9
- package/plugins/HandlerResultPlugin.js.map +1 -1
- package/plugins/ModifyFastifyPlugin.js +0 -9
- package/plugins/ModifyFastifyPlugin.js.map +1 -1
- package/plugins/RoutePlugin.js +0 -8
- package/plugins/RoutePlugin.js.map +1 -1
- package/types.js +0 -1
- package/types.js.map +1 -1
package/Context.d.ts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
import { Context as BaseContext, ContextParams as BaseContextParams } from "@webiny/api";
|
|
2
|
-
import { Context as
|
|
1
|
+
import { Context as BaseContext, ContextParams as BaseContextParams, ContextPlugin as BaseContextPlugin, ContextPluginCallable as BaseContextPluginCallable } from "@webiny/api";
|
|
2
|
+
import { Context as ContextInterface } from "./types";
|
|
3
3
|
export interface ContextParams extends BaseContextParams {
|
|
4
|
-
server:
|
|
5
|
-
routes:
|
|
4
|
+
server: ContextInterface["server"];
|
|
5
|
+
routes: ContextInterface["routes"];
|
|
6
6
|
}
|
|
7
|
-
export declare class Context extends BaseContext implements
|
|
8
|
-
readonly server:
|
|
9
|
-
readonly routes:
|
|
10
|
-
handlerClient:
|
|
11
|
-
request:
|
|
12
|
-
reply:
|
|
7
|
+
export declare class Context extends BaseContext implements ContextInterface {
|
|
8
|
+
readonly server: ContextInterface["server"];
|
|
9
|
+
readonly routes: ContextInterface["routes"];
|
|
10
|
+
handlerClient: ContextInterface["handlerClient"];
|
|
11
|
+
request: ContextInterface["request"];
|
|
12
|
+
reply: ContextInterface["reply"];
|
|
13
13
|
constructor(params: ContextParams);
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.
|
|
17
|
+
*
|
|
18
|
+
* This can be removed when we introduce the type augmentation.
|
|
19
|
+
*/
|
|
20
|
+
export declare type ContextPluginCallable<T extends ContextInterface = ContextInterface> = BaseContextPluginCallable<T>;
|
|
21
|
+
export declare class ContextPlugin<T extends ContextInterface = ContextInterface> extends BaseContextPlugin<T> {
|
|
22
|
+
}
|
|
23
|
+
export declare const createContextPlugin: <T extends ContextInterface = ContextInterface>(callable: ContextPluginCallable<T>) => BaseContextPlugin<T>;
|
package/Context.js
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
|
-
exports.Context = void 0;
|
|
9
|
-
|
|
7
|
+
exports.createContextPlugin = exports.ContextPlugin = exports.Context = void 0;
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _api = require("@webiny/api");
|
|
13
|
-
|
|
14
10
|
class Context extends _api.Context {
|
|
15
11
|
// @ts-ignore
|
|
12
|
+
|
|
16
13
|
// @ts-ignore
|
|
14
|
+
|
|
17
15
|
// @ts-ignore
|
|
16
|
+
|
|
18
17
|
constructor(params) {
|
|
19
18
|
super(params);
|
|
20
19
|
(0, _defineProperty2.default)(this, "server", void 0);
|
|
@@ -25,7 +24,17 @@ class Context extends _api.Context {
|
|
|
25
24
|
this.server = params.server;
|
|
26
25
|
this.routes = params.routes;
|
|
27
26
|
}
|
|
28
|
-
|
|
29
27
|
}
|
|
30
28
|
|
|
31
|
-
|
|
29
|
+
/**
|
|
30
|
+
* We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.
|
|
31
|
+
*
|
|
32
|
+
* This can be removed when we introduce the type augmentation.
|
|
33
|
+
*/
|
|
34
|
+
exports.Context = Context;
|
|
35
|
+
class ContextPlugin extends _api.ContextPlugin {}
|
|
36
|
+
exports.ContextPlugin = ContextPlugin;
|
|
37
|
+
const createContextPlugin = callable => {
|
|
38
|
+
return (0, _api.createContextPlugin)(callable);
|
|
39
|
+
};
|
|
40
|
+
exports.createContextPlugin = createContextPlugin;
|
package/Context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Context","BaseContext","constructor","params","server","routes"],"sources":["Context.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"names":["Context","BaseContext","constructor","params","server","routes","ContextPlugin","BaseContextPlugin","createContextPlugin","callable","baseCreateContextPlugin"],"sources":["Context.ts"],"sourcesContent":["import {\n Context as BaseContext,\n ContextParams as BaseContextParams,\n ContextPlugin as BaseContextPlugin,\n ContextPluginCallable as BaseContextPluginCallable,\n createContextPlugin as baseCreateContextPlugin\n} from \"@webiny/api\";\nimport { Context as ContextInterface } from \"~/types\";\n\nexport interface ContextParams extends BaseContextParams {\n server: ContextInterface[\"server\"];\n routes: ContextInterface[\"routes\"];\n}\n\nexport class Context extends BaseContext implements ContextInterface {\n public readonly server: ContextInterface[\"server\"];\n public readonly routes: ContextInterface[\"routes\"];\n // @ts-ignore\n public handlerClient: ContextInterface[\"handlerClient\"];\n // @ts-ignore\n public request: ContextInterface[\"request\"];\n // @ts-ignore\n public reply: ContextInterface[\"reply\"];\n\n public constructor(params: ContextParams) {\n super(params);\n this.server = params.server;\n this.routes = params.routes;\n }\n}\n\n/**\n * We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.\n *\n * This can be removed when we introduce the type augmentation.\n */\nexport type ContextPluginCallable<T extends ContextInterface = ContextInterface> =\n BaseContextPluginCallable<T>;\n\nexport class ContextPlugin<\n T extends ContextInterface = ContextInterface\n> extends BaseContextPlugin<T> {}\n\nexport const createContextPlugin = <T extends ContextInterface = ContextInterface>(\n callable: ContextPluginCallable<T>\n) => {\n return baseCreateContextPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;AAAA;AAcO,MAAMA,OAAO,SAASC,YAAW,CAA6B;EAGjE;;EAEA;;EAEA;;EAGOC,WAAW,CAACC,MAAqB,EAAE;IACtC,KAAK,CAACA,MAAM,CAAC;IAAC;IAAA;IAAA;IAAA;IAAA;IACd,IAAI,CAACC,MAAM,GAAGD,MAAM,CAACC,MAAM;IAC3B,IAAI,CAACC,MAAM,GAAGF,MAAM,CAACE,MAAM;EAC/B;AACJ;;AAEA;AACA;AACA;AACA;AACA;AAJA;AAQO,MAAMC,aAAa,SAEhBC,kBAAiB,CAAI;AAAE;AAE1B,MAAMC,mBAAmB,GAC5BC,QAAkC,IACjC;EACD,OAAO,IAAAC,wBAAuB,EAAID,QAAQ,CAAC;AAC/C,CAAC;AAAC"}
|
package/fastify.js
CHANGED
|
@@ -1,44 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createHandler = void 0;
|
|
9
|
-
|
|
10
8
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
-
|
|
12
9
|
var _fastify = _interopRequireDefault(require("fastify"));
|
|
13
|
-
|
|
14
10
|
var _utils = require("@webiny/utils");
|
|
15
|
-
|
|
16
11
|
var _Context = require("./Context");
|
|
17
|
-
|
|
18
12
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
19
|
-
|
|
20
13
|
var _RoutePlugin = require("./plugins/RoutePlugin");
|
|
21
|
-
|
|
22
14
|
var _handlerClient = require("@webiny/handler-client");
|
|
23
|
-
|
|
24
15
|
var _cookie = _interopRequireDefault(require("@fastify/cookie"));
|
|
25
|
-
|
|
26
16
|
var _compress = _interopRequireDefault(require("@fastify/compress"));
|
|
27
|
-
|
|
28
17
|
var _middleware = require("./middleware");
|
|
29
|
-
|
|
30
18
|
var _api = require("@webiny/api");
|
|
31
|
-
|
|
32
19
|
var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
|
|
33
|
-
|
|
34
20
|
var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
|
|
35
|
-
|
|
36
21
|
var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
|
|
37
|
-
|
|
38
22
|
var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
|
|
39
|
-
|
|
40
23
|
var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
|
|
41
|
-
|
|
42
24
|
const DEFAULT_HEADERS = (0, _objectSpread2.default)({
|
|
43
25
|
"Cache-Control": "no-store",
|
|
44
26
|
"Content-Type": "application/json; charset=utf-8",
|
|
@@ -46,34 +28,28 @@ const DEFAULT_HEADERS = (0, _objectSpread2.default)({
|
|
|
46
28
|
"Access-Control-Allow-Headers": "*",
|
|
47
29
|
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH"
|
|
48
30
|
}, (0, _utils.getWebinyVersionHeaders)());
|
|
49
|
-
|
|
50
31
|
const getDefaultHeaders = routes => {
|
|
51
32
|
/**
|
|
52
33
|
* If we are accepting all headers, just output that one.
|
|
53
34
|
*/
|
|
54
35
|
const keys = Object.keys(routes);
|
|
55
36
|
const all = keys.every(key => routes[key].length > 0);
|
|
56
|
-
|
|
57
37
|
if (all) {
|
|
58
38
|
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
59
39
|
"Access-Control-Allow-Methods": "*"
|
|
60
40
|
});
|
|
61
41
|
}
|
|
62
|
-
|
|
63
42
|
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
64
43
|
"Access-Control-Allow-Methods": keys.filter(type => {
|
|
65
44
|
if (!routes[type] || Array.isArray(routes[type]) === false) {
|
|
66
45
|
return false;
|
|
67
46
|
}
|
|
68
|
-
|
|
69
47
|
return routes[type].length > 0;
|
|
70
48
|
}).sort().join(",")
|
|
71
49
|
});
|
|
72
50
|
};
|
|
73
|
-
|
|
74
51
|
const stringifyError = error => {
|
|
75
52
|
var _error$constructor;
|
|
76
|
-
|
|
77
53
|
const {
|
|
78
54
|
name,
|
|
79
55
|
message,
|
|
@@ -90,12 +66,10 @@ const stringifyError = error => {
|
|
|
90
66
|
stack: process.env.DEBUG === "true" ? stack : "Turn on the debug flag to see the stack."
|
|
91
67
|
}));
|
|
92
68
|
};
|
|
93
|
-
|
|
94
69
|
const OPTIONS_HEADERS = {
|
|
95
70
|
"Access-Control-Max-Age": "86400",
|
|
96
71
|
"Cache-Control": "public, max-age=86400"
|
|
97
72
|
};
|
|
98
|
-
|
|
99
73
|
const createHandler = params => {
|
|
100
74
|
const definedRoutes = {
|
|
101
75
|
POST: [],
|
|
@@ -115,20 +89,17 @@ const createHandler = params => {
|
|
|
115
89
|
TRACE: [],
|
|
116
90
|
UNLOCK: []
|
|
117
91
|
};
|
|
118
|
-
|
|
119
92
|
const throwOnDefinedRoute = (type, path, options) => {
|
|
120
93
|
if (type === "ALL") {
|
|
121
94
|
const all = Object.keys(definedRoutes).find(key => {
|
|
122
95
|
const routes = definedRoutes[key];
|
|
123
96
|
return routes.includes(path);
|
|
124
97
|
});
|
|
125
|
-
|
|
126
98
|
if (!all) {
|
|
127
99
|
return;
|
|
128
100
|
}
|
|
129
|
-
|
|
130
|
-
console.
|
|
131
|
-
console.log(JSON.stringify(all));
|
|
101
|
+
console.error(`Error while registering onAll route. One of the routes is already defined.`);
|
|
102
|
+
console.error(JSON.stringify(all));
|
|
132
103
|
throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
|
|
133
104
|
type,
|
|
134
105
|
path
|
|
@@ -138,67 +109,54 @@ const createHandler = params => {
|
|
|
138
109
|
} else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
|
|
139
110
|
return;
|
|
140
111
|
}
|
|
141
|
-
|
|
142
|
-
console.log(`Error while trying to override route: [${type}] ${path}`);
|
|
112
|
+
console.error(`Error while trying to override route: [${type}] ${path}`);
|
|
143
113
|
throw new _error.default(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
|
|
144
114
|
type,
|
|
145
115
|
path
|
|
146
116
|
});
|
|
147
117
|
};
|
|
148
|
-
|
|
149
118
|
const addDefinedRoute = (type, path) => {
|
|
150
119
|
if (!definedRoutes[type]) {
|
|
151
120
|
return;
|
|
152
121
|
} else if (definedRoutes[type].includes(path)) {
|
|
153
122
|
return;
|
|
154
123
|
}
|
|
155
|
-
|
|
156
124
|
definedRoutes[type].push(path);
|
|
157
125
|
};
|
|
158
126
|
/**
|
|
159
127
|
* We must attach the server to our internal context if we want to have it accessible.
|
|
160
128
|
*/
|
|
161
|
-
|
|
162
|
-
|
|
163
129
|
const app = (0, _fastify.default)((0, _objectSpread2.default)({}, params.options || {}));
|
|
164
130
|
/**
|
|
165
131
|
* We need to register routes in our system so we can output headers later on and dissallow overriding routes.
|
|
166
132
|
*/
|
|
167
|
-
|
|
168
133
|
app.addHook("onRoute", route => {
|
|
169
134
|
const method = route.method;
|
|
170
|
-
|
|
171
135
|
if (Array.isArray(method)) {
|
|
172
136
|
for (const m of method) {
|
|
173
137
|
addDefinedRoute(m, route.path);
|
|
174
138
|
}
|
|
175
|
-
|
|
176
139
|
return;
|
|
177
140
|
}
|
|
178
|
-
|
|
179
141
|
addDefinedRoute(method, route.path);
|
|
180
142
|
});
|
|
181
143
|
/**
|
|
182
144
|
* ############################
|
|
183
145
|
* Register the Fastify plugins.
|
|
184
146
|
*/
|
|
185
|
-
|
|
186
147
|
/**
|
|
187
148
|
* Package @fastify/cookie
|
|
188
149
|
*
|
|
189
150
|
* https://github.com/fastify/fastify-cookie
|
|
190
151
|
*/
|
|
191
|
-
|
|
192
152
|
app.register(_cookie.default, {
|
|
193
153
|
parseOptions: {} // options for parsing cookies
|
|
194
|
-
|
|
195
154
|
});
|
|
196
155
|
/**
|
|
197
156
|
* Package @fastify/compress
|
|
198
157
|
*
|
|
199
158
|
* https://github.com/fastify/fastify-compress
|
|
200
159
|
*/
|
|
201
|
-
|
|
202
160
|
app.register(_compress.default, {
|
|
203
161
|
global: true,
|
|
204
162
|
threshold: 1024,
|
|
@@ -211,7 +169,6 @@ const createHandler = params => {
|
|
|
211
169
|
/**
|
|
212
170
|
* Route helpers - mostly for users.
|
|
213
171
|
*/
|
|
214
|
-
|
|
215
172
|
const routes = {
|
|
216
173
|
defined: definedRoutes,
|
|
217
174
|
onPost: (path, handler, options) => {
|
|
@@ -248,7 +205,6 @@ const createHandler = params => {
|
|
|
248
205
|
}
|
|
249
206
|
};
|
|
250
207
|
let context;
|
|
251
|
-
|
|
252
208
|
try {
|
|
253
209
|
context = new _Context.Context({
|
|
254
210
|
plugins: [
|
|
@@ -257,7 +213,6 @@ const createHandler = params => {
|
|
|
257
213
|
* And it must be one of the first context plugins applied.
|
|
258
214
|
*/
|
|
259
215
|
(0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
|
|
260
|
-
|
|
261
216
|
/**
|
|
262
217
|
* Inserted via webpack on build time.
|
|
263
218
|
*/
|
|
@@ -266,16 +221,16 @@ const createHandler = params => {
|
|
|
266
221
|
routes
|
|
267
222
|
});
|
|
268
223
|
} catch (ex) {
|
|
269
|
-
console.
|
|
270
|
-
console.
|
|
224
|
+
console.error(`Error while constructing the Context.`);
|
|
225
|
+
console.error(stringifyError(ex));
|
|
271
226
|
throw ex;
|
|
272
227
|
}
|
|
228
|
+
|
|
273
229
|
/**
|
|
274
230
|
* We are attaching our custom context to webiny variable on the fastify app, so it is accessible everywhere.
|
|
275
231
|
*/
|
|
276
|
-
|
|
277
|
-
|
|
278
232
|
app.decorate("webiny", context);
|
|
233
|
+
|
|
279
234
|
/**
|
|
280
235
|
* We have few types of triggers:
|
|
281
236
|
* * Events - EventPlugin
|
|
@@ -283,14 +238,12 @@ const createHandler = params => {
|
|
|
283
238
|
*
|
|
284
239
|
* Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
|
|
285
240
|
*/
|
|
286
|
-
|
|
287
241
|
const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
|
|
242
|
+
|
|
288
243
|
/**
|
|
289
244
|
* Add routes to the system.
|
|
290
245
|
*/
|
|
291
|
-
|
|
292
246
|
let routePluginName;
|
|
293
|
-
|
|
294
247
|
try {
|
|
295
248
|
for (const plugin of routePlugins) {
|
|
296
249
|
routePluginName = plugin.name;
|
|
@@ -299,16 +252,15 @@ const createHandler = params => {
|
|
|
299
252
|
}));
|
|
300
253
|
}
|
|
301
254
|
} catch (ex) {
|
|
302
|
-
console.
|
|
303
|
-
console.
|
|
255
|
+
console.error(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
|
|
256
|
+
console.error(stringifyError(ex));
|
|
304
257
|
throw ex;
|
|
305
258
|
}
|
|
259
|
+
|
|
306
260
|
/**
|
|
307
261
|
* On every request we add default headers, which can be changed later.
|
|
308
262
|
* Also, if it is an options request, we skip everything after this hook and output options headers.
|
|
309
263
|
*/
|
|
310
|
-
|
|
311
|
-
|
|
312
264
|
app.addHook("onRequest", async (request, reply) => {
|
|
313
265
|
/**
|
|
314
266
|
* Our default headers are always set. Users can override them.
|
|
@@ -318,22 +270,19 @@ const createHandler = params => {
|
|
|
318
270
|
/**
|
|
319
271
|
* Users can define their own custom handlers for the onRequest event - so let's run them first.
|
|
320
272
|
*/
|
|
321
|
-
|
|
322
273
|
const plugins = app.webiny.plugins.byType(_HandlerOnRequestPlugin.HandlerOnRequestPlugin.type);
|
|
323
274
|
let name;
|
|
324
|
-
|
|
325
275
|
try {
|
|
326
276
|
for (const plugin of plugins) {
|
|
327
277
|
name = plugin.name;
|
|
328
278
|
const result = await plugin.exec(request, reply);
|
|
329
|
-
|
|
330
279
|
if (result === false) {
|
|
331
280
|
return;
|
|
332
281
|
}
|
|
333
282
|
}
|
|
334
283
|
} catch (ex) {
|
|
335
|
-
console.
|
|
336
|
-
console.
|
|
284
|
+
console.error(`Error while running the "HandlerOnRequestPlugin" ${name ? `(${name})` : ""} plugin in the onRequest hook.`);
|
|
285
|
+
console.error(stringifyError(ex));
|
|
337
286
|
throw ex;
|
|
338
287
|
}
|
|
339
288
|
/**
|
|
@@ -341,23 +290,19 @@ const createHandler = params => {
|
|
|
341
290
|
*
|
|
342
291
|
* Users can prevent this by creating their own HandlerOnRequestPlugin and returning false as the result of the callable.
|
|
343
292
|
*/
|
|
344
|
-
|
|
345
|
-
|
|
346
293
|
if (request.method !== "OPTIONS") {
|
|
347
294
|
return;
|
|
348
295
|
}
|
|
349
|
-
|
|
350
296
|
if (reply.sent) {
|
|
351
297
|
/**
|
|
352
298
|
* At this point throwing an exception will not do anything with the response. So just log it.
|
|
353
299
|
*/
|
|
354
|
-
console.
|
|
300
|
+
console.error(JSON.stringify({
|
|
355
301
|
message: `Output was already sent. Please check custom plugins of type "HandlerOnRequestPlugin".`,
|
|
356
302
|
explanation: "This error can happen if the user plugin ended the reply, but did not return false as response."
|
|
357
303
|
}));
|
|
358
304
|
return;
|
|
359
305
|
}
|
|
360
|
-
|
|
361
306
|
reply.headers((0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS)).code(204).send("").hijack();
|
|
362
307
|
});
|
|
363
308
|
app.addHook("preParsing", async (request, reply) => {
|
|
@@ -365,59 +310,53 @@ const createHandler = params => {
|
|
|
365
310
|
app.webiny.reply = reply;
|
|
366
311
|
const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
|
|
367
312
|
let name;
|
|
368
|
-
|
|
369
313
|
try {
|
|
370
314
|
for (const plugin of plugins) {
|
|
371
315
|
name = plugin.name;
|
|
372
316
|
await plugin.apply(app.webiny);
|
|
373
317
|
}
|
|
374
318
|
} catch (ex) {
|
|
375
|
-
console.
|
|
376
|
-
console.
|
|
319
|
+
console.error(`Error while running the "ContextPlugin" ${name ? `(${name})` : ""} plugin in the preParsing hook.`);
|
|
320
|
+
console.error(stringifyError(ex));
|
|
377
321
|
throw ex;
|
|
378
322
|
}
|
|
379
323
|
});
|
|
380
324
|
/**
|
|
381
325
|
*
|
|
382
326
|
*/
|
|
383
|
-
|
|
384
327
|
app.addHook("preHandler", async () => {
|
|
385
328
|
const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
|
|
386
329
|
let name;
|
|
387
|
-
|
|
388
330
|
try {
|
|
389
331
|
for (const plugin of plugins) {
|
|
390
332
|
name = plugin.name;
|
|
391
333
|
await plugin.apply(app.webiny);
|
|
392
334
|
}
|
|
393
335
|
} catch (ex) {
|
|
394
|
-
console.
|
|
395
|
-
console.
|
|
336
|
+
console.error(`Error while running the "BeforeHandlerPlugin" ${name ? `(${name})` : ""} plugin in the preHandler hook.`);
|
|
337
|
+
console.error(stringifyError(ex));
|
|
396
338
|
throw ex;
|
|
397
339
|
}
|
|
398
340
|
});
|
|
341
|
+
|
|
399
342
|
/**
|
|
400
343
|
*
|
|
401
344
|
*/
|
|
402
|
-
|
|
403
345
|
const preSerialization = async (_, __, payload) => {
|
|
404
346
|
const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
|
|
405
347
|
let name;
|
|
406
|
-
|
|
407
348
|
try {
|
|
408
349
|
for (const plugin of plugins) {
|
|
409
350
|
name = plugin.name;
|
|
410
351
|
await plugin.handle(app.webiny, payload);
|
|
411
352
|
}
|
|
412
353
|
} catch (ex) {
|
|
413
|
-
console.
|
|
414
|
-
console.
|
|
354
|
+
console.error(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
|
|
355
|
+
console.error(stringifyError(ex));
|
|
415
356
|
throw ex;
|
|
416
357
|
}
|
|
417
|
-
|
|
418
358
|
return payload;
|
|
419
359
|
};
|
|
420
|
-
|
|
421
360
|
app.addHook("preSerialization", preSerialization);
|
|
422
361
|
app.setErrorHandler(async (error, request, reply) => {
|
|
423
362
|
return reply.status(500).headers({
|
|
@@ -437,9 +376,8 @@ const createHandler = params => {
|
|
|
437
376
|
/**
|
|
438
377
|
* Log error to cloud, as these can be extremely annoying to debug!
|
|
439
378
|
*/
|
|
440
|
-
|
|
441
|
-
console.
|
|
442
|
-
console.log(stringifyError(error));
|
|
379
|
+
console.error("@webiny/handler");
|
|
380
|
+
console.error(stringifyError(error));
|
|
443
381
|
reply.status(500).headers({
|
|
444
382
|
"Cache-Control": "no-store"
|
|
445
383
|
}).send(
|
|
@@ -460,24 +398,30 @@ const createHandler = params => {
|
|
|
460
398
|
return reply;
|
|
461
399
|
});
|
|
462
400
|
/**
|
|
463
|
-
*
|
|
401
|
+
* We need to output the benchmark results at the end of the request in both response and timeout cases
|
|
464
402
|
*/
|
|
403
|
+
app.addHook("onResponse", async () => {
|
|
404
|
+
await context.benchmark.output();
|
|
405
|
+
});
|
|
406
|
+
app.addHook("onTimeout", async () => {
|
|
407
|
+
await context.benchmark.output();
|
|
408
|
+
});
|
|
465
409
|
|
|
410
|
+
/**
|
|
411
|
+
* With these plugins we give users possibility to do anything they want on our fastify instance.
|
|
412
|
+
*/
|
|
466
413
|
const modifyPlugins = app.webiny.plugins.byType(_ModifyFastifyPlugin.ModifyFastifyPlugin.type);
|
|
467
414
|
let modifyFastifyPluginName;
|
|
468
|
-
|
|
469
415
|
try {
|
|
470
416
|
for (const plugin of modifyPlugins) {
|
|
471
417
|
modifyFastifyPluginName = plugin.name;
|
|
472
418
|
plugin.modify(app);
|
|
473
419
|
}
|
|
474
420
|
} catch (ex) {
|
|
475
|
-
console.
|
|
476
|
-
console.
|
|
421
|
+
console.error(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
|
|
422
|
+
console.error(stringifyError(ex));
|
|
477
423
|
throw ex;
|
|
478
424
|
}
|
|
479
|
-
|
|
480
425
|
return app;
|
|
481
426
|
};
|
|
482
|
-
|
|
483
427
|
exports.createHandler = createHandler;
|