@webiny/handler 0.0.0-unstable.6e5425ee89 → 0.0.0-unstable.7f63ea0744

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 CHANGED
@@ -9,5 +9,6 @@ export declare class Context extends BaseContext implements BaseContextType {
9
9
  readonly routes: BaseContextType["routes"];
10
10
  handlerClient: BaseContextType["handlerClient"];
11
11
  request: BaseContextType["request"];
12
+ reply: BaseContextType["reply"];
12
13
  constructor(params: ContextParams);
13
14
  }
package/Context.js CHANGED
@@ -12,12 +12,15 @@ class Context extends _api.Context {
12
12
 
13
13
  // @ts-ignore
14
14
 
15
+ // @ts-ignore
16
+
15
17
  constructor(params) {
16
18
  super(params);
17
19
  (0, _defineProperty2.default)(this, "server", void 0);
18
20
  (0, _defineProperty2.default)(this, "routes", void 0);
19
21
  (0, _defineProperty2.default)(this, "handlerClient", void 0);
20
22
  (0, _defineProperty2.default)(this, "request", void 0);
23
+ (0, _defineProperty2.default)(this, "reply", void 0);
21
24
  this.server = params.server;
22
25
  this.routes = params.routes;
23
26
  }
package/Context.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["Context","BaseContext","constructor","params","server","routes"],"sources":["Context.ts"],"sourcesContent":["import { Context as BaseContext, ContextParams as BaseContextParams } from \"@webiny/api\";\nimport { Context as BaseContextType } from \"~/types\";\n\nexport interface ContextParams extends BaseContextParams {\n server: BaseContextType[\"server\"];\n routes: BaseContextType[\"routes\"];\n}\n\nexport class Context extends BaseContext implements BaseContextType {\n public readonly server: BaseContextType[\"server\"];\n public readonly routes: BaseContextType[\"routes\"];\n // @ts-ignore\n public handlerClient: BaseContextType[\"handlerClient\"];\n // @ts-ignore\n public request: BaseContextType[\"request\"];\n\n public constructor(params: ContextParams) {\n super(params);\n this.server = params.server;\n this.routes = params.routes;\n }\n}\n"],"mappings":";;;;;;;;AAAA;AAQO,MAAMA,OAAO,SAASC,YAAW,CAA4B;EAGhE;;EAEA;;EAGOC,WAAW,CAACC,MAAqB,EAAE;IACtC,KAAK,CAACA,MAAM,CAAC;IAAC;IAAA;IAAA;IAAA;IACd,IAAI,CAACC,MAAM,GAAGD,MAAM,CAACC,MAAM;IAC3B,IAAI,CAACC,MAAM,GAAGF,MAAM,CAACE,MAAM;EAC/B;AACJ;AAAC"}
1
+ {"version":3,"names":["Context","BaseContext","constructor","params","server","routes"],"sources":["Context.ts"],"sourcesContent":["import { Context as BaseContext, ContextParams as BaseContextParams } from \"@webiny/api\";\nimport { Context as BaseContextType } from \"~/types\";\n\nexport interface ContextParams extends BaseContextParams {\n server: BaseContextType[\"server\"];\n routes: BaseContextType[\"routes\"];\n}\n\nexport class Context extends BaseContext implements BaseContextType {\n public readonly server: BaseContextType[\"server\"];\n public readonly routes: BaseContextType[\"routes\"];\n // @ts-ignore\n public handlerClient: BaseContextType[\"handlerClient\"];\n // @ts-ignore\n public request: BaseContextType[\"request\"];\n // @ts-ignore\n public reply: BaseContextType[\"reply\"];\n\n public constructor(params: ContextParams) {\n super(params);\n this.server = params.server;\n this.routes = params.routes;\n }\n}\n"],"mappings":";;;;;;;;AAAA;AAQO,MAAMA,OAAO,SAASC,YAAW,CAA4B;EAGhE;;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;AAAC"}
package/fastify.js CHANGED
@@ -13,11 +13,14 @@ var _error = _interopRequireDefault(require("@webiny/error"));
13
13
  var _RoutePlugin = require("./plugins/RoutePlugin");
14
14
  var _handlerClient = require("@webiny/handler-client");
15
15
  var _cookie = _interopRequireDefault(require("@fastify/cookie"));
16
+ var _compress = _interopRequireDefault(require("@fastify/compress"));
16
17
  var _middleware = require("./middleware");
17
18
  var _api = require("@webiny/api");
18
19
  var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
19
20
  var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
20
21
  var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
22
+ var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
23
+ var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
21
24
  const DEFAULT_HEADERS = (0, _objectSpread2.default)({
22
25
  "Cache-Control": "no-store",
23
26
  "Content-Type": "application/json; charset=utf-8",
@@ -37,8 +40,7 @@ const getDefaultHeaders = routes => {
37
40
  });
38
41
  }
39
42
  return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
40
- "Access-Control-Allow-Methods": keys.filter(key => {
41
- const type = key;
43
+ "Access-Control-Allow-Methods": keys.filter(type => {
42
44
  if (!routes[type] || Array.isArray(routes[type]) === false) {
43
45
  return false;
44
46
  }
@@ -46,6 +48,24 @@ const getDefaultHeaders = routes => {
46
48
  }).sort().join(",")
47
49
  });
48
50
  };
51
+ const stringifyError = error => {
52
+ var _error$constructor;
53
+ const {
54
+ name,
55
+ message,
56
+ code,
57
+ stack,
58
+ data
59
+ } = error;
60
+ return JSON.stringify((0, _objectSpread2.default)((0, _objectSpread2.default)({}, error), {}, {
61
+ constructorName: ((_error$constructor = error.constructor) === null || _error$constructor === void 0 ? void 0 : _error$constructor.name) || "UnknownError",
62
+ name: name || "No error name",
63
+ message: message || "No error message",
64
+ code: code || "NO_CODE",
65
+ data,
66
+ stack: process.env.DEBUG === "true" ? stack : "Turn on the debug flag to see the stack."
67
+ }));
68
+ };
49
69
  const OPTIONS_HEADERS = {
50
70
  "Access-Control-Max-Age": "86400",
51
71
  "Cache-Control": "public, max-age=86400"
@@ -71,13 +91,15 @@ const createHandler = params => {
71
91
  };
72
92
  const throwOnDefinedRoute = (type, path, options) => {
73
93
  if (type === "ALL") {
74
- const all = Object.keys(definedRoutes).some(key => {
94
+ const all = Object.keys(definedRoutes).find(key => {
75
95
  const routes = definedRoutes[key];
76
96
  return routes.includes(path);
77
97
  });
78
98
  if (!all) {
79
99
  return;
80
100
  }
101
+ console.log(`Error while registering onAll route. One of the routes is already defined.`);
102
+ console.log(JSON.stringify(all));
81
103
  throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
82
104
  type,
83
105
  path
@@ -87,13 +109,13 @@ const createHandler = params => {
87
109
  } else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
88
110
  return;
89
111
  }
112
+ console.log(`Error while trying to override route: [${type}] ${path}`);
90
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", {
91
114
  type,
92
115
  path
93
116
  });
94
117
  };
95
- const addDefinedRoute = (inputType, path) => {
96
- const type = inputType.toUpperCase();
118
+ const addDefinedRoute = (type, path) => {
97
119
  if (!definedRoutes[type]) {
98
120
  return;
99
121
  } else if (definedRoutes[type].includes(path)) {
@@ -119,11 +141,31 @@ const createHandler = params => {
119
141
  addDefinedRoute(method, route.path);
120
142
  });
121
143
  /**
144
+ * ############################
145
+ * Register the Fastify plugins.
146
+ */
147
+ /**
148
+ * Package @fastify/cookie
122
149
  *
150
+ * https://github.com/fastify/fastify-cookie
123
151
  */
124
152
  app.register(_cookie.default, {
125
153
  parseOptions: {} // options for parsing cookies
126
154
  });
155
+ /**
156
+ * Package @fastify/compress
157
+ *
158
+ * https://github.com/fastify/fastify-compress
159
+ */
160
+ app.register(_compress.default, {
161
+ global: true,
162
+ threshold: 1024,
163
+ onUnsupportedEncoding: (encoding, _, reply) => {
164
+ reply.code(406);
165
+ return `We do not support the ${encoding} encoding.`;
166
+ },
167
+ inflateIfDeflated: true
168
+ });
127
169
  /**
128
170
  * Route helpers - mostly for users.
129
171
  */
@@ -162,22 +204,30 @@ const createHandler = params => {
162
204
  app.head(path, handler);
163
205
  }
164
206
  };
165
- const context = new _Context.Context({
166
- plugins: [
167
- /**
168
- * We must have handlerClient by default.
169
- * And it must be one of the first context plugins applied.
170
- */
171
- (0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
172
- /**
173
- * Inserted via webpack on build time.
174
- */
175
- WEBINY_VERSION: process.env.WEBINY_VERSION,
176
- server: app,
177
- routes
178
- });
207
+ let context;
208
+ try {
209
+ context = new _Context.Context({
210
+ plugins: [
211
+ /**
212
+ * We must have handlerClient by default.
213
+ * And it must be one of the first context plugins applied.
214
+ */
215
+ (0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
216
+ /**
217
+ * Inserted via webpack on build time.
218
+ */
219
+ WEBINY_VERSION: process.env.WEBINY_VERSION,
220
+ server: app,
221
+ routes
222
+ });
223
+ } catch (ex) {
224
+ console.log(`Error while constructing the Context.`);
225
+ console.log(stringifyError(ex));
226
+ throw ex;
227
+ }
228
+
179
229
  /**
180
- * We are attaching our custom context to webiny variable on the fastify app so it is accessible everywhere
230
+ * We are attaching our custom context to webiny variable on the fastify app, so it is accessible everywhere.
181
231
  */
182
232
  app.decorate("webiny", context);
183
233
 
@@ -193,10 +243,18 @@ const createHandler = params => {
193
243
  /**
194
244
  * Add routes to the system.
195
245
  */
196
- for (const plugin of routePlugins) {
197
- plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
198
- context: app.webiny
199
- }));
246
+ let routePluginName;
247
+ try {
248
+ for (const plugin of routePlugins) {
249
+ routePluginName = plugin.name;
250
+ plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
251
+ context: app.webiny
252
+ }));
253
+ }
254
+ } catch (ex) {
255
+ console.log(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
256
+ console.log(stringifyError(ex));
257
+ throw ex;
200
258
  }
201
259
 
202
260
  /**
@@ -204,23 +262,63 @@ const createHandler = params => {
204
262
  * Also, if it is an options request, we skip everything after this hook and output options headers.
205
263
  */
206
264
  app.addHook("onRequest", async (request, reply) => {
265
+ /**
266
+ * Our default headers are always set. Users can override them.
267
+ */
207
268
  const defaultHeaders = getDefaultHeaders(definedRoutes);
208
269
  reply.headers(defaultHeaders);
270
+ /**
271
+ * Users can define their own custom handlers for the onRequest event - so let's run them first.
272
+ */
273
+ const plugins = app.webiny.plugins.byType(_HandlerOnRequestPlugin.HandlerOnRequestPlugin.type);
274
+ let name;
275
+ try {
276
+ for (const plugin of plugins) {
277
+ name = plugin.name;
278
+ const result = await plugin.exec(request, reply);
279
+ if (result === false) {
280
+ return;
281
+ }
282
+ }
283
+ } catch (ex) {
284
+ console.log(`Error while running the "HandlerOnRequestPlugin" ${name ? `(${name})` : ""} plugin in the onRequest hook.`);
285
+ console.log(stringifyError(ex));
286
+ throw ex;
287
+ }
288
+ /**
289
+ * When we receive the OPTIONS request, we end it before it goes any further as there is no need for anything to run after this - at least for our use cases.
290
+ *
291
+ * Users can prevent this by creating their own HandlerOnRequestPlugin and returning false as the result of the callable.
292
+ */
209
293
  if (request.method !== "OPTIONS") {
210
294
  return;
211
295
  }
212
- const raw = reply.code(204).hijack().raw;
213
- const headers = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS);
214
- for (const key in headers) {
215
- raw.setHeader(key, headers[key]);
296
+ if (reply.sent) {
297
+ /**
298
+ * At this point throwing an exception will not do anything with the response. So just log it.
299
+ */
300
+ console.log(JSON.stringify({
301
+ message: `Output was already sent. Please check custom plugins of type "HandlerOnRequestPlugin".`,
302
+ explanation: "This error can happen if the user plugin ended the reply, but did not return false as response."
303
+ }));
304
+ return;
216
305
  }
217
- raw.end("");
306
+ reply.headers((0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS)).code(204).send("").hijack();
218
307
  });
219
- app.addHook("preParsing", async request => {
308
+ app.addHook("preParsing", async (request, reply) => {
220
309
  app.webiny.request = request;
310
+ app.webiny.reply = reply;
221
311
  const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
222
- for (const plugin of plugins) {
223
- await plugin.apply(app.webiny);
312
+ let name;
313
+ try {
314
+ for (const plugin of plugins) {
315
+ name = plugin.name;
316
+ await plugin.apply(app.webiny);
317
+ }
318
+ } catch (ex) {
319
+ console.log(`Error while running the "ContextPlugin" ${name ? `(${name})` : ""} plugin in the preParsing hook.`);
320
+ console.log(stringifyError(ex));
321
+ throw ex;
224
322
  }
225
323
  });
226
324
  /**
@@ -228,8 +326,16 @@ const createHandler = params => {
228
326
  */
229
327
  app.addHook("preHandler", async () => {
230
328
  const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
231
- for (const plugin of plugins) {
232
- await plugin.apply(app.webiny);
329
+ let name;
330
+ try {
331
+ for (const plugin of plugins) {
332
+ name = plugin.name;
333
+ await plugin.apply(app.webiny);
334
+ }
335
+ } catch (ex) {
336
+ console.log(`Error while running the "BeforeHandlerPlugin" ${name ? `(${name})` : ""} plugin in the preHandler hook.`);
337
+ console.log(stringifyError(ex));
338
+ throw ex;
233
339
  }
234
340
  });
235
341
 
@@ -238,30 +344,75 @@ const createHandler = params => {
238
344
  */
239
345
  const preSerialization = async (_, __, payload) => {
240
346
  const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
241
- for (const plugin of plugins) {
242
- await plugin.handle(app.webiny, payload);
347
+ let name;
348
+ try {
349
+ for (const plugin of plugins) {
350
+ name = plugin.name;
351
+ await plugin.handle(app.webiny, payload);
352
+ }
353
+ } catch (ex) {
354
+ console.log(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
355
+ console.log(stringifyError(ex));
356
+ throw ex;
243
357
  }
244
358
  return payload;
245
359
  };
246
360
  app.addHook("preSerialization", preSerialization);
361
+ app.setErrorHandler(async (error, request, reply) => {
362
+ return reply.status(500).headers({
363
+ "Cache-Control": "no-store"
364
+ }).send(
365
+ /**
366
+ * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
367
+ */
368
+ JSON.stringify({
369
+ message: error.message,
370
+ code: error.code,
371
+ data: error.data
372
+ }));
373
+ });
247
374
  app.addHook("onError", async (_, reply, error) => {
248
375
  const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
249
- // Log error to cloud, as these can be extremely annoying to debug!
376
+ /**
377
+ * Log error to cloud, as these can be extremely annoying to debug!
378
+ */
250
379
  console.log("@webiny/handler");
251
- console.log(JSON.stringify((0, _objectSpread2.default)((0, _objectSpread2.default)({}, error || {}), {}, {
252
- message: error === null || error === void 0 ? void 0 : error.message,
253
- code: error === null || error === void 0 ? void 0 : error.code
254
- })));
380
+ console.log(stringifyError(error));
381
+ reply.status(500).headers({
382
+ "Cache-Control": "no-store"
383
+ }).send(
384
+ /**
385
+ * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
386
+ */
387
+ JSON.stringify({
388
+ message: error.message,
389
+ code: error.code,
390
+ data: error.data
391
+ }));
255
392
  const handler = (0, _middleware.middleware)(plugins.map(pl => {
256
393
  return (context, error, next) => {
257
394
  return pl.handle(context, error, next);
258
395
  };
259
396
  }));
260
397
  await handler(app.webiny, error);
261
- return reply.headers({
262
- "Cache-Control": "no-store"
263
- }).status(500);
398
+ return reply;
264
399
  });
400
+
401
+ /**
402
+ * With these plugins we give users possibility to do anything they want on our fastify instance.
403
+ */
404
+ const modifyPlugins = app.webiny.plugins.byType(_ModifyFastifyPlugin.ModifyFastifyPlugin.type);
405
+ let modifyFastifyPluginName;
406
+ try {
407
+ for (const plugin of modifyPlugins) {
408
+ modifyFastifyPluginName = plugin.name;
409
+ plugin.modify(app);
410
+ }
411
+ } catch (ex) {
412
+ console.log(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
413
+ console.log(stringifyError(ex));
414
+ throw ex;
415
+ }
265
416
  return app;
266
417
  };
267
418
  exports.createHandler = createHandler;
package/fastify.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["DEFAULT_HEADERS","getWebinyVersionHeaders","getDefaultHeaders","routes","keys","Object","all","every","key","length","filter","type","Array","isArray","sort","join","OPTIONS_HEADERS","createHandler","params","definedRoutes","POST","GET","OPTIONS","DELETE","PATCH","PUT","HEAD","COPY","LOCK","MKCOL","MOVE","PROPFIND","PROPPATCH","SEARCH","TRACE","UNLOCK","throwOnDefinedRoute","path","options","some","includes","WebinyError","override","addDefinedRoute","inputType","toUpperCase","push","app","fastify","addHook","route","method","m","register","fastifyCookies","parseOptions","defined","onPost","handler","post","onGet","get","onOptions","onDelete","delete","onPatch","patch","onPut","put","onAll","onHead","head","context","Context","plugins","createHandlerClient","WEBINY_VERSION","process","env","server","decorate","routePlugins","webiny","byType","RoutePlugin","plugin","cb","request","reply","defaultHeaders","headers","raw","code","hijack","setHeader","end","ContextPlugin","apply","BeforeHandlerPlugin","preSerialization","_","__","payload","HandlerResultPlugin","handle","error","HandlerErrorPlugin","console","log","JSON","stringify","message","middleware","map","pl","next","status"],"sources":["fastify.ts"],"sourcesContent":["import { PluginCollection } from \"@webiny/plugins/types\";\nimport fastify, {\n FastifyServerOptions as ServerOptions,\n preSerializationAsyncHookHandler\n} from \"fastify\";\nimport { getWebinyVersionHeaders } from \"@webiny/utils\";\nimport { ContextRoutes, DefinedContextRoutes, RouteMethodOptions, HTTPMethods } from \"~/types\";\nimport { Context } from \"~/Context\";\nimport WebinyError from \"@webiny/error\";\nimport { RoutePlugin } from \"./plugins/RoutePlugin\";\nimport { createHandlerClient } from \"@webiny/handler-client\";\nimport fastifyCookies from \"@fastify/cookie\";\nimport { middleware } from \"~/middleware\";\nimport { ContextPlugin } from \"@webiny/api\";\nimport { BeforeHandlerPlugin } from \"./plugins/BeforeHandlerPlugin\";\nimport { HandlerResultPlugin } from \"./plugins/HandlerResultPlugin\";\nimport { HandlerErrorPlugin } from \"./plugins/HandlerErrorPlugin\";\n\nconst DEFAULT_HEADERS: Record<string, string> = {\n \"Cache-Control\": \"no-store\",\n \"Content-Type\": \"application/json; charset=utf-8\",\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Headers\": \"*\",\n \"Access-Control-Allow-Methods\": \"OPTIONS,POST,GET,DELETE,PUT,PATCH\",\n ...getWebinyVersionHeaders()\n};\n\nconst getDefaultHeaders = (routes: DefinedContextRoutes): Record<string, string> => {\n /**\n * If we are accepting all headers, just output that one.\n */\n const keys = Object.keys(routes);\n const all = keys.every(key => routes[key as HTTPMethods].length > 0);\n if (all) {\n return {\n ...DEFAULT_HEADERS,\n \"Access-Control-Allow-Methods\": \"*\"\n };\n }\n return {\n ...DEFAULT_HEADERS,\n \"Access-Control-Allow-Methods\": keys\n .filter(key => {\n const type = key as unknown as HTTPMethods;\n if (!routes[type] || Array.isArray(routes[type]) === false) {\n return false;\n }\n return routes[type].length > 0;\n })\n .sort()\n .join(\",\")\n };\n};\n\nconst OPTIONS_HEADERS: Record<string, string> = {\n \"Access-Control-Max-Age\": \"86400\",\n \"Cache-Control\": \"public, max-age=86400\"\n};\n\nexport interface CreateHandlerParams {\n plugins: PluginCollection;\n options?: ServerOptions;\n}\n\nexport const createHandler = (params: CreateHandlerParams) => {\n const definedRoutes: DefinedContextRoutes = {\n POST: [],\n GET: [],\n OPTIONS: [],\n DELETE: [],\n PATCH: [],\n PUT: [],\n HEAD: [],\n COPY: [],\n LOCK: [],\n MKCOL: [],\n MOVE: [],\n PROPFIND: [],\n PROPPATCH: [],\n SEARCH: [],\n TRACE: [],\n UNLOCK: []\n };\n\n const throwOnDefinedRoute = (\n type: HTTPMethods | \"ALL\",\n path: string,\n options?: RouteMethodOptions\n ): void => {\n if (type === \"ALL\") {\n const all = Object.keys(definedRoutes).some(key => {\n const routes = definedRoutes[key as HTTPMethods];\n return routes.includes(path);\n });\n if (!all) {\n return;\n }\n throw new WebinyError(\n `You cannot override a route with onAll() method, please remove unnecessary route from the system.`,\n \"OVERRIDE_ROUTE_ERROR\",\n {\n type,\n path\n }\n );\n } else if (definedRoutes[type].includes(path) === false) {\n return;\n } else if (options?.override === true) {\n return;\n }\n throw new WebinyError(\n `When you are trying to override existing route, you must send \"override\" parameter when adding that route.`,\n \"OVERRIDE_ROUTE_ERROR\",\n {\n type,\n path\n }\n );\n };\n\n const addDefinedRoute = (inputType: HTTPMethods, path: string): void => {\n const type = (inputType as string).toUpperCase() as HTTPMethods;\n if (!definedRoutes[type]) {\n return;\n } else if (definedRoutes[type].includes(path)) {\n return;\n }\n definedRoutes[type].push(path);\n };\n /**\n * We must attach the server to our internal context if we want to have it accessible.\n */\n const app = fastify({\n ...(params.options || {})\n });\n /**\n * We need to register routes in our system so we can output headers later on and dissallow overriding routes.\n */\n app.addHook(\"onRoute\", route => {\n const method = route.method;\n if (Array.isArray(method)) {\n for (const m of method) {\n addDefinedRoute(m, route.path);\n }\n return;\n }\n addDefinedRoute(method, route.path);\n });\n /**\n *\n */\n app.register(fastifyCookies, {\n parseOptions: {} // options for parsing cookies\n });\n /**\n * Route helpers - mostly for users.\n */\n const routes: ContextRoutes = {\n defined: definedRoutes,\n onPost: (path, handler, options) => {\n throwOnDefinedRoute(\"POST\", path, options);\n app.post(path, handler);\n },\n onGet: (path, handler, options) => {\n throwOnDefinedRoute(\"GET\", path, options);\n app.get(path, handler);\n },\n onOptions: (path, handler, options) => {\n throwOnDefinedRoute(\"OPTIONS\", path, options);\n app.options(path, handler);\n },\n onDelete: (path, handler, options) => {\n throwOnDefinedRoute(\"DELETE\", path, options);\n app.delete(path, handler);\n },\n onPatch: (path, handler, options) => {\n throwOnDefinedRoute(\"PATCH\", path, options);\n app.patch(path, handler);\n },\n onPut: (path, handler, options) => {\n throwOnDefinedRoute(\"PUT\", path, options);\n app.put(path, handler);\n },\n onAll: (path, handler, options) => {\n throwOnDefinedRoute(\"ALL\", path, options);\n app.all(path, handler);\n },\n onHead: (path, handler, options) => {\n throwOnDefinedRoute(\"HEAD\", path, options);\n app.head(path, handler);\n }\n };\n const context = new Context({\n plugins: [\n /**\n * We must have handlerClient by default.\n * And it must be one of the first context plugins applied.\n */\n createHandlerClient(),\n ...(params.plugins || [])\n ],\n /**\n * Inserted via webpack on build time.\n */\n WEBINY_VERSION: process.env.WEBINY_VERSION as string,\n server: app,\n routes\n });\n /**\n * We are attaching our custom context to webiny variable on the fastify app so it is accessible everywhere\n */\n app.decorate(\"webiny\", context);\n\n /**\n * We have few types of triggers:\n * * Events - EventPlugin\n * * Routes - RoutePlugin\n *\n * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.\n */\n const routePlugins = app.webiny.plugins.byType<RoutePlugin>(RoutePlugin.type);\n\n /**\n * Add routes to the system.\n */\n for (const plugin of routePlugins) {\n plugin.cb({\n ...app.webiny.routes,\n context: app.webiny\n });\n }\n\n /**\n * On every request we add default headers, which can be changed later.\n * Also, if it is an options request, we skip everything after this hook and output options headers.\n */\n app.addHook(\"onRequest\", async (request, reply) => {\n const defaultHeaders = getDefaultHeaders(definedRoutes);\n reply.headers(defaultHeaders);\n if (request.method !== \"OPTIONS\") {\n return;\n }\n const raw = reply.code(204).hijack().raw;\n const headers = { ...defaultHeaders, ...OPTIONS_HEADERS };\n for (const key in headers) {\n raw.setHeader(key, headers[key]);\n }\n\n raw.end(\"\");\n });\n\n app.addHook(\"preParsing\", async request => {\n app.webiny.request = request;\n const plugins = app.webiny.plugins.byType<ContextPlugin>(ContextPlugin.type);\n for (const plugin of plugins) {\n await plugin.apply(app.webiny);\n }\n });\n /**\n *\n */\n app.addHook(\"preHandler\", async () => {\n const plugins = app.webiny.plugins.byType<BeforeHandlerPlugin>(BeforeHandlerPlugin.type);\n for (const plugin of plugins) {\n await plugin.apply(app.webiny);\n }\n });\n\n /**\n *\n */\n const preSerialization: preSerializationAsyncHookHandler<any> = async (_, __, payload) => {\n const plugins = app.webiny.plugins.byType<HandlerResultPlugin>(HandlerResultPlugin.type);\n for (const plugin of plugins) {\n await plugin.handle(app.webiny, payload);\n }\n return payload;\n };\n\n app.addHook(\"preSerialization\", preSerialization);\n\n app.addHook(\"onError\", async (_, reply, error) => {\n const plugins = app.webiny.plugins.byType<HandlerErrorPlugin>(HandlerErrorPlugin.type);\n // Log error to cloud, as these can be extremely annoying to debug!\n console.log(\"@webiny/handler\");\n console.log(\n JSON.stringify({\n ...(error || {}),\n message: error?.message,\n code: error?.code\n })\n );\n const handler = middleware(\n plugins.map(pl => {\n return (context: Context, error: Error, next: Function) => {\n return pl.handle(context, error, next);\n };\n })\n );\n await handler(app.webiny, error);\n\n return reply\n .headers({\n \"Cache-Control\": \"no-store\"\n })\n .status(500);\n });\n\n return app;\n};\n"],"mappings":";;;;;;;;AACA;AAIA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA,MAAMA,eAAuC;EACzC,eAAe,EAAE,UAAU;EAC3B,cAAc,EAAE,iCAAiC;EACjD,6BAA6B,EAAE,GAAG;EAClC,8BAA8B,EAAE,GAAG;EACnC,8BAA8B,EAAE;AAAmC,GAChE,IAAAC,8BAAuB,GAAE,CAC/B;AAED,MAAMC,iBAAiB,GAAIC,MAA4B,IAA6B;EAChF;AACJ;AACA;EACI,MAAMC,IAAI,GAAGC,MAAM,CAACD,IAAI,CAACD,MAAM,CAAC;EAChC,MAAMG,GAAG,GAAGF,IAAI,CAACG,KAAK,CAACC,GAAG,IAAIL,MAAM,CAACK,GAAG,CAAgB,CAACC,MAAM,GAAG,CAAC,CAAC;EACpE,IAAIH,GAAG,EAAE;IACL,mEACON,eAAe;MAClB,8BAA8B,EAAE;IAAG;EAE3C;EACA,mEACOA,eAAe;IAClB,8BAA8B,EAAEI,IAAI,CAC/BM,MAAM,CAACF,GAAG,IAAI;MACX,MAAMG,IAAI,GAAGH,GAA6B;MAC1C,IAAI,CAACL,MAAM,CAACQ,IAAI,CAAC,IAAIC,KAAK,CAACC,OAAO,CAACV,MAAM,CAACQ,I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,OAAO,CAAC;MACL,eAAe,EAAE;IACrB,CAAC,CAAC,CACD0B,MAAM,CAAC,GAAG,CAAC;EACpB,CAAC,CAAC;EAEF,OAAOrE,GAAG;AACd,CAAC;AAAC"}
1
+ {"version":3,"names":["DEFAULT_HEADERS","getWebinyVersionHeaders","getDefaultHeaders","routes","keys","Object","all","every","key","length","filter","type","Array","isArray","sort","join","stringifyError","error","name","message","code","stack","data","JSON","stringify","constructorName","constructor","process","env","DEBUG","OPTIONS_HEADERS","createHandler","params","definedRoutes","POST","GET","OPTIONS","DELETE","PATCH","PUT","HEAD","COPY","LOCK","MKCOL","MOVE","PROPFIND","PROPPATCH","SEARCH","TRACE","UNLOCK","throwOnDefinedRoute","path","options","find","includes","console","log","WebinyError","override","addDefinedRoute","push","app","fastify","addHook","route","method","m","register","fastifyCookie","parseOptions","fastifyCompress","global","threshold","onUnsupportedEncoding","encoding","_","reply","inflateIfDeflated","defined","onPost","handler","post","onGet","get","onOptions","onDelete","delete","onPatch","patch","onPut","put","onAll","onHead","head","context","Context","plugins","createHandlerClient","WEBINY_VERSION","server","ex","decorate","routePlugins","webiny","byType","RoutePlugin","routePluginName","plugin","cb","request","defaultHeaders","headers","HandlerOnRequestPlugin","result","exec","sent","explanation","send","hijack","ContextPlugin","apply","BeforeHandlerPlugin","preSerialization","__","payload","HandlerResultPlugin","handle","setErrorHandler","status","HandlerErrorPlugin","middleware","map","pl","next","modifyPlugins","ModifyFastifyPlugin","modifyFastifyPluginName","modify"],"sources":["fastify.ts"],"sourcesContent":["import { PluginCollection } from \"@webiny/plugins/types\";\nimport fastify, {\n FastifyServerOptions as ServerOptions,\n preSerializationAsyncHookHandler\n} from \"fastify\";\nimport { getWebinyVersionHeaders } from \"@webiny/utils\";\nimport { ContextRoutes, DefinedContextRoutes, RouteMethodOptions, HTTPMethods } from \"~/types\";\nimport { Context } from \"~/Context\";\nimport WebinyError from \"@webiny/error\";\nimport { RoutePlugin } from \"./plugins/RoutePlugin\";\nimport { createHandlerClient } from \"@webiny/handler-client\";\nimport fastifyCookie from \"@fastify/cookie\";\nimport fastifyCompress from \"@fastify/compress\";\nimport { middleware } from \"~/middleware\";\nimport { ContextPlugin } from \"@webiny/api\";\nimport { BeforeHandlerPlugin } from \"./plugins/BeforeHandlerPlugin\";\nimport { HandlerResultPlugin } from \"./plugins/HandlerResultPlugin\";\nimport { HandlerErrorPlugin } from \"./plugins/HandlerErrorPlugin\";\nimport { ModifyFastifyPlugin } from \"~/plugins/ModifyFastifyPlugin\";\nimport { HandlerOnRequestPlugin } from \"~/plugins/HandlerOnRequestPlugin\";\n\nconst DEFAULT_HEADERS: Record<string, string> = {\n \"Cache-Control\": \"no-store\",\n \"Content-Type\": \"application/json; charset=utf-8\",\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Headers\": \"*\",\n \"Access-Control-Allow-Methods\": \"OPTIONS,POST,GET,DELETE,PUT,PATCH\",\n ...getWebinyVersionHeaders()\n};\n\nconst getDefaultHeaders = (routes: DefinedContextRoutes): Record<string, string> => {\n /**\n * If we are accepting all headers, just output that one.\n */\n const keys = Object.keys(routes) as HTTPMethods[];\n const all = keys.every(key => routes[key].length > 0);\n if (all) {\n return {\n ...DEFAULT_HEADERS,\n \"Access-Control-Allow-Methods\": \"*\"\n };\n }\n return {\n ...DEFAULT_HEADERS,\n \"Access-Control-Allow-Methods\": keys\n .filter(type => {\n if (!routes[type] || Array.isArray(routes[type]) === false) {\n return false;\n }\n return routes[type].length > 0;\n })\n .sort()\n .join(\",\")\n };\n};\n\nconst stringifyError = (error: Error) => {\n const { name, message, code, stack, data } = error as any;\n return JSON.stringify({\n ...error,\n constructorName: error.constructor?.name || \"UnknownError\",\n name: name || \"No error name\",\n message: message || \"No error message\",\n code: code || \"NO_CODE\",\n data,\n stack: process.env.DEBUG === \"true\" ? stack : \"Turn on the debug flag to see the stack.\"\n });\n};\n\nconst OPTIONS_HEADERS: Record<string, string> = {\n \"Access-Control-Max-Age\": \"86400\",\n \"Cache-Control\": \"public, max-age=86400\"\n};\n\nexport interface CreateHandlerParams {\n plugins: PluginCollection;\n options?: ServerOptions;\n}\n\nexport const createHandler = (params: CreateHandlerParams) => {\n const definedRoutes: DefinedContextRoutes = {\n POST: [],\n GET: [],\n OPTIONS: [],\n DELETE: [],\n PATCH: [],\n PUT: [],\n HEAD: [],\n COPY: [],\n LOCK: [],\n MKCOL: [],\n MOVE: [],\n PROPFIND: [],\n PROPPATCH: [],\n SEARCH: [],\n TRACE: [],\n UNLOCK: []\n };\n\n const throwOnDefinedRoute = (\n type: HTTPMethods | \"ALL\",\n path: string,\n options?: RouteMethodOptions\n ): void => {\n if (type === \"ALL\") {\n const all = Object.keys(definedRoutes).find(key => {\n const routes = definedRoutes[key as HTTPMethods];\n return routes.includes(path);\n });\n if (!all) {\n return;\n }\n console.log(\n `Error while registering onAll route. One of the routes is already defined.`\n );\n console.log(JSON.stringify(all));\n throw new WebinyError(\n `You cannot override a route with onAll() method, please remove unnecessary route from the system.`,\n \"OVERRIDE_ROUTE_ERROR\",\n {\n type,\n path\n }\n );\n } else if (definedRoutes[type].includes(path) === false) {\n return;\n } else if (options?.override === true) {\n return;\n }\n console.log(`Error while trying to override route: [${type}] ${path}`);\n throw new WebinyError(\n `When you are trying to override existing route, you must send \"override\" parameter when adding that route.`,\n \"OVERRIDE_ROUTE_ERROR\",\n {\n type,\n path\n }\n );\n };\n\n const addDefinedRoute = (type: HTTPMethods, path: string): void => {\n if (!definedRoutes[type]) {\n return;\n } else if (definedRoutes[type].includes(path)) {\n return;\n }\n definedRoutes[type].push(path);\n };\n /**\n * We must attach the server to our internal context if we want to have it accessible.\n */\n const app = fastify({\n ...(params.options || {})\n });\n /**\n * We need to register routes in our system so we can output headers later on and dissallow overriding routes.\n */\n app.addHook(\"onRoute\", route => {\n const method = route.method;\n if (Array.isArray(method)) {\n for (const m of method) {\n addDefinedRoute(m, route.path);\n }\n return;\n }\n addDefinedRoute(method, route.path);\n });\n /**\n * ############################\n * Register the Fastify plugins.\n */\n /**\n * Package @fastify/cookie\n *\n * https://github.com/fastify/fastify-cookie\n */\n app.register(fastifyCookie, {\n parseOptions: {} // options for parsing cookies\n });\n /**\n * Package @fastify/compress\n *\n * https://github.com/fastify/fastify-compress\n */\n app.register(fastifyCompress, {\n global: true,\n threshold: 1024,\n onUnsupportedEncoding: (encoding, _, reply) => {\n reply.code(406);\n return `We do not support the ${encoding} encoding.`;\n },\n inflateIfDeflated: true\n });\n /**\n * Route helpers - mostly for users.\n */\n const routes: ContextRoutes = {\n defined: definedRoutes,\n onPost: (path, handler, options) => {\n throwOnDefinedRoute(\"POST\", path, options);\n app.post(path, handler);\n },\n onGet: (path, handler, options) => {\n throwOnDefinedRoute(\"GET\", path, options);\n app.get(path, handler);\n },\n onOptions: (path, handler, options) => {\n throwOnDefinedRoute(\"OPTIONS\", path, options);\n app.options(path, handler);\n },\n onDelete: (path, handler, options) => {\n throwOnDefinedRoute(\"DELETE\", path, options);\n app.delete(path, handler);\n },\n onPatch: (path, handler, options) => {\n throwOnDefinedRoute(\"PATCH\", path, options);\n app.patch(path, handler);\n },\n onPut: (path, handler, options) => {\n throwOnDefinedRoute(\"PUT\", path, options);\n app.put(path, handler);\n },\n onAll: (path, handler, options) => {\n throwOnDefinedRoute(\"ALL\", path, options);\n app.all(path, handler);\n },\n onHead: (path, handler, options) => {\n throwOnDefinedRoute(\"HEAD\", path, options);\n app.head(path, handler);\n }\n };\n let context: Context;\n try {\n context = new Context({\n plugins: [\n /**\n * We must have handlerClient by default.\n * And it must be one of the first context plugins applied.\n */\n createHandlerClient(),\n ...(params.plugins || [])\n ],\n /**\n * Inserted via webpack on build time.\n */\n WEBINY_VERSION: process.env.WEBINY_VERSION as string,\n server: app,\n routes\n });\n } catch (ex) {\n console.log(`Error while constructing the Context.`);\n console.log(stringifyError(ex));\n throw ex;\n }\n\n /**\n * We are attaching our custom context to webiny variable on the fastify app, so it is accessible everywhere.\n */\n app.decorate(\"webiny\", context);\n\n /**\n * We have few types of triggers:\n * * Events - EventPlugin\n * * Routes - RoutePlugin\n *\n * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.\n */\n const routePlugins = app.webiny.plugins.byType<RoutePlugin>(RoutePlugin.type);\n\n /**\n * Add routes to the system.\n */\n let routePluginName: string | undefined;\n try {\n for (const plugin of routePlugins) {\n routePluginName = plugin.name;\n plugin.cb({\n ...app.webiny.routes,\n context: app.webiny\n });\n }\n } catch (ex) {\n console.log(\n `Error while running the \"RoutePlugin\" ${\n routePluginName ? `(${routePluginName})` : \"\"\n } plugin in the beginning of the \"createHandler\" callable.`\n );\n console.log(stringifyError(ex));\n throw ex;\n }\n\n /**\n * On every request we add default headers, which can be changed later.\n * Also, if it is an options request, we skip everything after this hook and output options headers.\n */\n app.addHook(\"onRequest\", async (request, reply) => {\n /**\n * Our default headers are always set. Users can override them.\n */\n const defaultHeaders = getDefaultHeaders(definedRoutes);\n reply.headers(defaultHeaders);\n /**\n * Users can define their own custom handlers for the onRequest event - so let's run them first.\n */\n const plugins = app.webiny.plugins.byType<HandlerOnRequestPlugin>(\n HandlerOnRequestPlugin.type\n );\n\n let name: string | undefined;\n try {\n for (const plugin of plugins) {\n name = plugin.name;\n const result = await plugin.exec(request, reply);\n if (result === false) {\n return;\n }\n }\n } catch (ex) {\n console.log(\n `Error while running the \"HandlerOnRequestPlugin\" ${\n name ? `(${name})` : \"\"\n } plugin in the onRequest hook.`\n );\n console.log(stringifyError(ex));\n throw ex;\n }\n /**\n * When we receive the OPTIONS request, we end it before it goes any further as there is no need for anything to run after this - at least for our use cases.\n *\n * Users can prevent this by creating their own HandlerOnRequestPlugin and returning false as the result of the callable.\n */\n if (request.method !== \"OPTIONS\") {\n return;\n }\n\n if (reply.sent) {\n /**\n * At this point throwing an exception will not do anything with the response. So just log it.\n */\n console.log(\n JSON.stringify({\n message: `Output was already sent. Please check custom plugins of type \"HandlerOnRequestPlugin\".`,\n explanation:\n \"This error can happen if the user plugin ended the reply, but did not return false as response.\"\n })\n );\n return;\n }\n\n reply\n .headers({ ...defaultHeaders, ...OPTIONS_HEADERS })\n .code(204)\n .send(\"\")\n .hijack();\n });\n\n app.addHook(\"preParsing\", async (request, reply) => {\n app.webiny.request = request;\n app.webiny.reply = reply;\n const plugins = app.webiny.plugins.byType<ContextPlugin>(ContextPlugin.type);\n let name: string | undefined;\n try {\n for (const plugin of plugins) {\n name = plugin.name;\n await plugin.apply(app.webiny);\n }\n } catch (ex) {\n console.log(\n `Error while running the \"ContextPlugin\" ${\n name ? `(${name})` : \"\"\n } plugin in the preParsing hook.`\n );\n console.log(stringifyError(ex));\n throw ex;\n }\n });\n /**\n *\n */\n app.addHook(\"preHandler\", async () => {\n const plugins = app.webiny.plugins.byType<BeforeHandlerPlugin>(BeforeHandlerPlugin.type);\n let name: string | undefined;\n try {\n for (const plugin of plugins) {\n name = plugin.name;\n await plugin.apply(app.webiny);\n }\n } catch (ex) {\n console.log(\n `Error while running the \"BeforeHandlerPlugin\" ${\n name ? `(${name})` : \"\"\n } plugin in the preHandler hook.`\n );\n console.log(stringifyError(ex));\n throw ex;\n }\n });\n\n /**\n *\n */\n const preSerialization: preSerializationAsyncHookHandler<any> = async (_, __, payload) => {\n const plugins = app.webiny.plugins.byType<HandlerResultPlugin>(HandlerResultPlugin.type);\n let name: string | undefined;\n try {\n for (const plugin of plugins) {\n name = plugin.name;\n await plugin.handle(app.webiny, payload);\n }\n } catch (ex) {\n console.log(\n `Error while running the \"HandlerResultPlugin\" ${\n name ? `(${name})` : \"\"\n } plugin in the preSerialization hook.`\n );\n console.log(stringifyError(ex));\n throw ex;\n }\n return payload;\n };\n\n app.addHook(\"preSerialization\", preSerialization);\n\n app.setErrorHandler<WebinyError>(async (error, request, reply) => {\n return reply\n .status(500)\n .headers({\n \"Cache-Control\": \"no-store\"\n })\n .send(\n /**\n * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.\n */\n JSON.stringify({\n message: error.message,\n code: error.code,\n data: error.data\n })\n );\n });\n\n app.addHook(\"onError\", async (_, reply, error: any) => {\n const plugins = app.webiny.plugins.byType<HandlerErrorPlugin>(HandlerErrorPlugin.type);\n /**\n * Log error to cloud, as these can be extremely annoying to debug!\n */\n console.log(\"@webiny/handler\");\n console.log(stringifyError(error));\n\n reply\n .status(500)\n .headers({\n \"Cache-Control\": \"no-store\"\n })\n .send(\n /**\n * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.\n */\n JSON.stringify({\n message: error.message,\n code: error.code,\n data: error.data\n })\n );\n\n const handler = middleware(\n plugins.map(pl => {\n return (context: Context, error: Error, next: Function) => {\n return pl.handle(context, error, next);\n };\n })\n );\n await handler(app.webiny, error);\n\n return reply;\n });\n\n /**\n * With these plugins we give users possibility to do anything they want on our fastify instance.\n */\n const modifyPlugins = app.webiny.plugins.byType<ModifyFastifyPlugin>(ModifyFastifyPlugin.type);\n\n let modifyFastifyPluginName: string | undefined;\n try {\n for (const plugin of modifyPlugins) {\n modifyFastifyPluginName = plugin.name;\n plugin.modify(app);\n }\n } catch (ex) {\n console.log(\n `Error while running the \"ModifyFastifyPlugin\" ${\n modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : \"\"\n } plugin in the end of the \"createHandler\" callable.`\n );\n console.log(stringifyError(ex));\n throw ex;\n }\n\n return app;\n};\n"],"mappings":";;;;;;;;AACA;AAIA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA,MAAMA,eAAuC;EACzC,eAAe,EAAE,UAAU;EAC3B,cAAc,EAAE,iCAAiC;EACjD,6BAA6B,EAAE,GAAG;EAClC,8BAA8B,EAAE,GAAG;EACnC,8BAA8B,EAAE;AAAmC,GAChE,IAAAC,8BAAuB,GAAE,CAC/B;AAED,MAAMC,iBAAiB,GAAIC,MAA4B,IAA6B;EAChF;AACJ;AACA;EACI,MAAMC,IAAI,GAAGC,MAAM,CAACD,IAAI,CAACD,MAAM,CAAkB;EACjD,MAAMG,GAAG,GAAGF,IA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,CACL;IAEL,MAAM0D,OAAO,GAAG,IAAAkD,sBAAU,EACtBlC,OAAO,CAACmC,GAAG,CAACC,EAAE,IAAI;MACd,OAAO,CAACtC,OAAgB,EAAE7E,KAAY,EAAEoH,IAAc,KAAK;QACvD,OAAOD,EAAE,CAACN,MAAM,CAAChC,OAAO,EAAE7E,KAAK,EAAEoH,IAAI,CAAC;MAC1C,CAAC;IACL,CAAC,CAAC,CACL;IACD,MAAMrD,OAAO,CAACnB,GAAG,CAAC0C,MAAM,EAAEtF,KAAK,CAAC;IAEhC,OAAO2D,KAAK;EAChB,CAAC,CAAC;;EAEF;AACJ;AACA;EACI,MAAM0D,aAAa,GAAGzE,GAAG,CAAC0C,MAAM,CAACP,OAAO,CAACQ,MAAM,CAAsB+B,wCAAmB,CAAC5H,IAAI,CAAC;EAE9F,IAAI6H,uBAA2C;EAC/C,IAAI;IACA,KAAK,MAAM7B,MAAM,IAAI2B,aAAa,EAAE;MAChCE,uBAAuB,GAAG7B,MAAM,CAACzF,IAAI;MACrCyF,MAAM,CAAC8B,MAAM,CAAC5E,GAAG,CAAC;IACtB;EACJ,CAAC,CAAC,OAAOuC,EAAE,EAAE;IACT7C,OAAO,CAACC,GAAG,CACN,iDACGgF,uBAAuB,GAAI,IAAGA,uBAAwB,GAAE,GAAG,EAC9D,qDAAoD,CACxD;IACDjF,OAAO,CAACC,GAAG,CAACxC,cAAc,CAACoF,EAAE,CAAC,CAAC;IAC/B,MAAMA,EAAE;EACZ;EAEA,OAAOvC,GAAG;AACd,CAAC;AAAC"}
package/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
- export * from "./fastify";
2
1
  export * from "@webiny/api/plugins/ContextPlugin";
2
+ export * from "./fastify";
3
3
  export * from "./Context";
4
4
  export * from "./plugins/EventPlugin";
5
5
  export * from "./plugins/RoutePlugin";
6
6
  export * from "./plugins/BeforeHandlerPlugin";
7
7
  export * from "./plugins/HandlerErrorPlugin";
8
8
  export * from "./plugins/HandlerResultPlugin";
9
+ export * from "./plugins/HandlerOnRequestPlugin";
10
+ export * from "./plugins/ModifyFastifyPlugin";
package/index.js CHANGED
@@ -3,25 +3,25 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- var _fastify = require("./fastify");
7
- Object.keys(_fastify).forEach(function (key) {
6
+ var _ContextPlugin = require("@webiny/api/plugins/ContextPlugin");
7
+ Object.keys(_ContextPlugin).forEach(function (key) {
8
8
  if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _fastify[key]) return;
9
+ if (key in exports && exports[key] === _ContextPlugin[key]) return;
10
10
  Object.defineProperty(exports, key, {
11
11
  enumerable: true,
12
12
  get: function () {
13
- return _fastify[key];
13
+ return _ContextPlugin[key];
14
14
  }
15
15
  });
16
16
  });
17
- var _ContextPlugin = require("@webiny/api/plugins/ContextPlugin");
18
- Object.keys(_ContextPlugin).forEach(function (key) {
17
+ var _fastify = require("./fastify");
18
+ Object.keys(_fastify).forEach(function (key) {
19
19
  if (key === "default" || key === "__esModule") return;
20
- if (key in exports && exports[key] === _ContextPlugin[key]) return;
20
+ if (key in exports && exports[key] === _fastify[key]) return;
21
21
  Object.defineProperty(exports, key, {
22
22
  enumerable: true,
23
23
  get: function () {
24
- return _ContextPlugin[key];
24
+ return _fastify[key];
25
25
  }
26
26
  });
27
27
  });
@@ -90,4 +90,26 @@ Object.keys(_HandlerResultPlugin).forEach(function (key) {
90
90
  return _HandlerResultPlugin[key];
91
91
  }
92
92
  });
93
+ });
94
+ var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
95
+ Object.keys(_HandlerOnRequestPlugin).forEach(function (key) {
96
+ if (key === "default" || key === "__esModule") return;
97
+ if (key in exports && exports[key] === _HandlerOnRequestPlugin[key]) return;
98
+ Object.defineProperty(exports, key, {
99
+ enumerable: true,
100
+ get: function () {
101
+ return _HandlerOnRequestPlugin[key];
102
+ }
103
+ });
104
+ });
105
+ var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
106
+ Object.keys(_ModifyFastifyPlugin).forEach(function (key) {
107
+ if (key === "default" || key === "__esModule") return;
108
+ if (key in exports && exports[key] === _ModifyFastifyPlugin[key]) return;
109
+ Object.defineProperty(exports, key, {
110
+ enumerable: true,
111
+ get: function () {
112
+ return _ModifyFastifyPlugin[key];
113
+ }
114
+ });
93
115
  });
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"~/fastify\";\nexport * from \"@webiny/api/plugins/ContextPlugin\";\nexport * from \"~/Context\";\nexport * from \"~/plugins/EventPlugin\";\nexport * from \"~/plugins/RoutePlugin\";\nexport * from \"~/plugins/BeforeHandlerPlugin\";\nexport * from \"~/plugins/HandlerErrorPlugin\";\nexport * from \"~/plugins/HandlerResultPlugin\";\n"],"mappings":";;;;;AAAA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"@webiny/api/plugins/ContextPlugin\";\nexport * from \"~/fastify\";\nexport * from \"~/Context\";\nexport * from \"~/plugins/EventPlugin\";\nexport * from \"~/plugins/RoutePlugin\";\nexport * from \"~/plugins/BeforeHandlerPlugin\";\nexport * from \"~/plugins/HandlerErrorPlugin\";\nexport * from \"~/plugins/HandlerResultPlugin\";\nexport * from \"~/plugins/HandlerOnRequestPlugin\";\nexport * from \"~/plugins/ModifyFastifyPlugin\";\n"],"mappings":";;;;;AAAA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/handler",
3
- "version": "0.0.0-unstable.6e5425ee89",
3
+ "version": "0.0.0-unstable.7f63ea0744",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,22 +12,23 @@
12
12
  ],
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "@babel/runtime": "7.20.1",
15
+ "@babel/runtime": "7.20.13",
16
+ "@fastify/compress": "6.2.0",
16
17
  "@fastify/cookie": "8.3.0",
17
- "@webiny/api": "0.0.0-unstable.6e5425ee89",
18
- "@webiny/error": "0.0.0-unstable.6e5425ee89",
19
- "@webiny/handler-client": "0.0.0-unstable.6e5425ee89",
20
- "@webiny/plugins": "0.0.0-unstable.6e5425ee89",
21
- "@webiny/utils": "0.0.0-unstable.6e5425ee89",
22
- "fastify": "4.10.2"
18
+ "@webiny/api": "0.0.0-unstable.7f63ea0744",
19
+ "@webiny/error": "0.0.0-unstable.7f63ea0744",
20
+ "@webiny/handler-client": "0.0.0-unstable.7f63ea0744",
21
+ "@webiny/plugins": "0.0.0-unstable.7f63ea0744",
22
+ "@webiny/utils": "0.0.0-unstable.7f63ea0744",
23
+ "fastify": "4.11.0"
23
24
  },
24
25
  "devDependencies": {
25
26
  "@babel/cli": "^7.19.3",
26
27
  "@babel/core": "^7.19.3",
27
28
  "@babel/preset-env": "^7.19.4",
28
29
  "@babel/preset-typescript": "^7.18.6",
29
- "@webiny/cli": "^0.0.0-unstable.6e5425ee89",
30
- "@webiny/project-utils": "^0.0.0-unstable.6e5425ee89",
30
+ "@webiny/cli": "^0.0.0-unstable.7f63ea0744",
31
+ "@webiny/project-utils": "^0.0.0-unstable.7f63ea0744",
31
32
  "babel-plugin-lodash": "^3.3.4",
32
33
  "merge": "^1.2.1",
33
34
  "rimraf": "^3.0.2",
@@ -42,5 +43,5 @@
42
43
  "build": "yarn webiny run build",
43
44
  "watch": "yarn webiny run watch"
44
45
  },
45
- "gitHead": "1e49c5d4b72b2f00661572464188f53b42ed7745"
46
+ "gitHead": "7f63ea0744c9e31977e5dabb95538d22b4db585c"
46
47
  }
@@ -0,0 +1,20 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ import { FastifyReply, FastifyRequest } from "fastify";
3
+ /**
4
+ * If the execution of the callable returns false, no more plugins will be executed after the given one.
5
+ * Nor it will execute our default OPTIONS code.
6
+ *
7
+ * This way users can prevent stopping of the request on our built-in OPTIONS request.
8
+ */
9
+ export declare type HandlerOnRequestPluginCallableResponse = false | undefined | null | void;
10
+ interface HandlerOnRequestPluginCallable {
11
+ (request: FastifyRequest, reply: FastifyReply): Promise<HandlerOnRequestPluginCallableResponse>;
12
+ }
13
+ export declare class HandlerOnRequestPlugin extends Plugin {
14
+ static type: string;
15
+ private readonly cb;
16
+ constructor(cb: HandlerOnRequestPluginCallable);
17
+ exec(request: FastifyRequest, reply: FastifyReply): Promise<HandlerOnRequestPluginCallableResponse>;
18
+ }
19
+ export declare const createHandlerOnRequest: (cb: HandlerOnRequestPluginCallable) => HandlerOnRequestPlugin;
20
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.createHandlerOnRequest = exports.HandlerOnRequestPlugin = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _plugins = require("@webiny/plugins");
10
+ class HandlerOnRequestPlugin extends _plugins.Plugin {
11
+ constructor(cb) {
12
+ super();
13
+ (0, _defineProperty2.default)(this, "cb", void 0);
14
+ this.cb = cb;
15
+ }
16
+ async exec(request, reply) {
17
+ return this.cb(request, reply);
18
+ }
19
+ }
20
+ exports.HandlerOnRequestPlugin = HandlerOnRequestPlugin;
21
+ (0, _defineProperty2.default)(HandlerOnRequestPlugin, "type", "handler.event.onRequest");
22
+ const createHandlerOnRequest = cb => {
23
+ return new HandlerOnRequestPlugin(cb);
24
+ };
25
+ exports.createHandlerOnRequest = createHandlerOnRequest;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["HandlerOnRequestPlugin","Plugin","constructor","cb","exec","request","reply","createHandlerOnRequest"],"sources":["HandlerOnRequestPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { FastifyReply, FastifyRequest } from \"fastify\";\n\n/**\n * If the execution of the callable returns false, no more plugins will be executed after the given one.\n * Nor it will execute our default OPTIONS code.\n *\n * This way users can prevent stopping of the request on our built-in OPTIONS request.\n */\nexport type HandlerOnRequestPluginCallableResponse = false | undefined | null | void;\ninterface HandlerOnRequestPluginCallable {\n (request: FastifyRequest, reply: FastifyReply): Promise<HandlerOnRequestPluginCallableResponse>;\n}\n\nexport class HandlerOnRequestPlugin extends Plugin {\n public static override type = \"handler.event.onRequest\";\n\n private readonly cb: HandlerOnRequestPluginCallable;\n\n public constructor(cb: HandlerOnRequestPluginCallable) {\n super();\n this.cb = cb;\n }\n\n public async exec(\n request: FastifyRequest,\n reply: FastifyReply\n ): Promise<HandlerOnRequestPluginCallableResponse> {\n return this.cb(request, reply);\n }\n}\n\nexport const createHandlerOnRequest = (cb: HandlerOnRequestPluginCallable) => {\n return new HandlerOnRequestPlugin(cb);\n};\n"],"mappings":";;;;;;;;AAAA;AAcO,MAAMA,sBAAsB,SAASC,eAAM,CAAC;EAKxCC,WAAW,CAACC,EAAkC,EAAE;IACnD,KAAK,EAAE;IAAC;IACR,IAAI,CAACA,EAAE,GAAGA,EAAE;EAChB;EAEA,MAAaC,IAAI,CACbC,OAAuB,EACvBC,KAAmB,EAC4B;IAC/C,OAAO,IAAI,CAACH,EAAE,CAACE,OAAO,EAAEC,KAAK,CAAC;EAClC;AACJ;AAAC;AAAA,8BAhBYN,sBAAsB,UACD,yBAAyB;AAiBpD,MAAMO,sBAAsB,GAAIJ,EAAkC,IAAK;EAC1E,OAAO,IAAIH,sBAAsB,CAACG,EAAE,CAAC;AACzC,CAAC;AAAC"}
@@ -0,0 +1,13 @@
1
+ import { Plugin } from "@webiny/plugins/Plugin";
2
+ import { FastifyInstance } from "fastify";
3
+ interface ModifyFastifyPluginCallable {
4
+ (app: FastifyInstance): void;
5
+ }
6
+ export declare class ModifyFastifyPlugin extends Plugin {
7
+ static type: string;
8
+ private readonly cb;
9
+ constructor(cb: ModifyFastifyPluginCallable);
10
+ modify(app: FastifyInstance): void;
11
+ }
12
+ export declare const createModifyFastifyPlugin: (cb: ModifyFastifyPluginCallable) => ModifyFastifyPlugin;
13
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.createModifyFastifyPlugin = exports.ModifyFastifyPlugin = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _Plugin = require("@webiny/plugins/Plugin");
10
+ class ModifyFastifyPlugin extends _Plugin.Plugin {
11
+ constructor(cb) {
12
+ super();
13
+ (0, _defineProperty2.default)(this, "cb", void 0);
14
+ this.cb = cb;
15
+ }
16
+ modify(app) {
17
+ this.cb(app);
18
+ }
19
+ }
20
+ exports.ModifyFastifyPlugin = ModifyFastifyPlugin;
21
+ (0, _defineProperty2.default)(ModifyFastifyPlugin, "type", "handler.fastify.modify");
22
+ const createModifyFastifyPlugin = cb => {
23
+ return new ModifyFastifyPlugin(cb);
24
+ };
25
+ exports.createModifyFastifyPlugin = createModifyFastifyPlugin;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["ModifyFastifyPlugin","Plugin","constructor","cb","modify","app","createModifyFastifyPlugin"],"sources":["ModifyFastifyPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins/Plugin\";\nimport { FastifyInstance } from \"fastify\";\n\ninterface ModifyFastifyPluginCallable {\n (app: FastifyInstance): void;\n}\n\nexport class ModifyFastifyPlugin extends Plugin {\n public static override type = \"handler.fastify.modify\";\n\n private readonly cb: ModifyFastifyPluginCallable;\n\n public constructor(cb: ModifyFastifyPluginCallable) {\n super();\n this.cb = cb;\n }\n\n public modify(app: FastifyInstance): void {\n this.cb(app);\n }\n}\n\nexport const createModifyFastifyPlugin = (cb: ModifyFastifyPluginCallable) => {\n return new ModifyFastifyPlugin(cb);\n};\n"],"mappings":";;;;;;;;AAAA;AAOO,MAAMA,mBAAmB,SAASC,cAAM,CAAC;EAKrCC,WAAW,CAACC,EAA+B,EAAE;IAChD,KAAK,EAAE;IAAC;IACR,IAAI,CAACA,EAAE,GAAGA,EAAE;EAChB;EAEOC,MAAM,CAACC,GAAoB,EAAQ;IACtC,IAAI,CAACF,EAAE,CAACE,GAAG,CAAC;EAChB;AACJ;AAAC;AAAA,8BAbYL,mBAAmB,UACE,wBAAwB;AAcnD,MAAMM,yBAAyB,GAAIH,EAA+B,IAAK;EAC1E,OAAO,IAAIH,mBAAmB,CAACG,EAAE,CAAC;AACtC,CAAC;AAAC"}
package/types.d.ts CHANGED
@@ -33,6 +33,10 @@ export interface Context extends ClientContext {
33
33
  * Current request. Must be set only once!
34
34
  */
35
35
  request: FastifyRequest;
36
+ /**
37
+ * Current reply. Must be set only once!
38
+ */
39
+ reply: FastifyReply;
36
40
  /**
37
41
  * @internal
38
42
  */
package/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import {\n FastifyInstance,\n FastifyRequest,\n FastifyReply,\n HTTPMethods,\n RouteHandlerMethod\n} from \"fastify\";\n\nexport { FastifyInstance, HTTPMethods } from \"fastify\";\nimport { ClientContext } from \"@webiny/handler-client/types\";\n\nexport interface RouteMethodOptions {\n override?: boolean;\n}\n\nexport type RouteMethodPath = `/${string}` | \"*\";\nexport interface RouteMethod {\n (path: RouteMethodPath, handler: RouteHandlerMethod, options?: RouteMethodOptions): void;\n}\n\nexport type Request = FastifyRequest;\nexport type Reply = FastifyReply;\n\nexport type DefinedContextRoutes = Record<HTTPMethods, string[]>;\nexport interface ContextRoutes {\n defined: DefinedContextRoutes;\n onGet: RouteMethod;\n onPost: RouteMethod;\n onPut: RouteMethod;\n onPatch: RouteMethod;\n onDelete: RouteMethod;\n onOptions: RouteMethod;\n onAll: RouteMethod;\n onHead: RouteMethod;\n}\n\nexport interface Context extends ClientContext {\n /**\n * An instance of fastify server.\n * Use at your own risk.\n * @instance\n */\n server: FastifyInstance;\n /**\n * Current request. Must be set only once!\n */\n request: FastifyRequest;\n /**\n * @internal\n */\n routes: ContextRoutes;\n}\n\ndeclare module \"fastify\" {\n interface FastifyInstance {\n webiny: Context;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA"}
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import {\n FastifyInstance,\n FastifyRequest,\n FastifyReply,\n HTTPMethods,\n RouteHandlerMethod\n} from \"fastify\";\n\nexport { FastifyInstance, HTTPMethods } from \"fastify\";\nimport { ClientContext } from \"@webiny/handler-client/types\";\n\nexport interface RouteMethodOptions {\n override?: boolean;\n}\n\nexport type RouteMethodPath = `/${string}` | \"*\";\nexport interface RouteMethod {\n (path: RouteMethodPath, handler: RouteHandlerMethod, options?: RouteMethodOptions): void;\n}\n\nexport type Request = FastifyRequest;\nexport type Reply = FastifyReply;\n\nexport type DefinedContextRoutes = Record<HTTPMethods, string[]>;\nexport interface ContextRoutes {\n defined: DefinedContextRoutes;\n onGet: RouteMethod;\n onPost: RouteMethod;\n onPut: RouteMethod;\n onPatch: RouteMethod;\n onDelete: RouteMethod;\n onOptions: RouteMethod;\n onAll: RouteMethod;\n onHead: RouteMethod;\n}\n\nexport interface Context extends ClientContext {\n /**\n * An instance of fastify server.\n * Use at your own risk.\n * @instance\n */\n server: FastifyInstance;\n /**\n * Current request. Must be set only once!\n */\n request: FastifyRequest;\n /**\n * Current reply. Must be set only once!\n */\n reply: FastifyReply;\n /**\n * @internal\n */\n routes: ContextRoutes;\n}\n\ndeclare module \"fastify\" {\n interface FastifyInstance {\n webiny: Context;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA"}