@webiny/handler 0.0.0-unstable.990c3ab1b6 → 0.0.0-unstable.d4f203fa97

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.js CHANGED
@@ -1,17 +1,19 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
6
7
  });
7
8
  exports.Context = void 0;
9
+
8
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
9
12
  var _api = require("@webiny/api");
13
+
10
14
  class Context extends _api.Context {
11
15
  // @ts-ignore
12
-
13
16
  // @ts-ignore
14
-
15
17
  constructor(params) {
16
18
  super(params);
17
19
  (0, _defineProperty2.default)(this, "server", void 0);
@@ -21,5 +23,7 @@ class Context extends _api.Context {
21
23
  this.server = params.server;
22
24
  this.routes = params.routes;
23
25
  }
26
+
24
27
  }
28
+
25
29
  exports.Context = Context;
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\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,OAAN,SAAsBC,YAAtB,CAA6D;EAGhE;EAEA;EAGOC,WAAW,CAACC,MAAD,EAAwB;IACtC,MAAMA,MAAN;IADsC;IAAA;IAAA;IAAA;IAEtC,KAAKC,MAAL,GAAcD,MAAM,CAACC,MAArB;IACA,KAAKC,MAAL,GAAcF,MAAM,CAACE,MAArB;EACH;;AAZ+D"}
package/fastify.d.ts CHANGED
@@ -5,4 +5,4 @@ export interface CreateHandlerParams {
5
5
  plugins: PluginCollection;
6
6
  options?: ServerOptions;
7
7
  }
8
- export declare const createHandler: (params: CreateHandlerParams) => import("fastify").FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<import("fastify").FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>>;
8
+ export declare const createHandler: (params: CreateHandlerParams) => import("fastify").FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<import("fastify").FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>>;
package/fastify.js CHANGED
@@ -1,23 +1,38 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
6
7
  });
7
8
  exports.createHandler = void 0;
9
+
8
10
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
+
9
12
  var _fastify = _interopRequireDefault(require("fastify"));
13
+
10
14
  var _utils = require("@webiny/utils");
15
+
11
16
  var _Context = require("./Context");
17
+
12
18
  var _error = _interopRequireDefault(require("@webiny/error"));
19
+
13
20
  var _RoutePlugin = require("./plugins/RoutePlugin");
21
+
14
22
  var _handlerClient = require("@webiny/handler-client");
23
+
15
24
  var _cookie = _interopRequireDefault(require("@fastify/cookie"));
25
+
16
26
  var _middleware = require("./middleware");
27
+
17
28
  var _api = require("@webiny/api");
29
+
18
30
  var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
31
+
19
32
  var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
33
+
20
34
  var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
35
+
21
36
  const DEFAULT_HEADERS = (0, _objectSpread2.default)({
22
37
  "Cache-Control": "no-store",
23
38
  "Content-Type": "application/json; charset=utf-8",
@@ -25,31 +40,38 @@ const DEFAULT_HEADERS = (0, _objectSpread2.default)({
25
40
  "Access-Control-Allow-Headers": "*",
26
41
  "Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH"
27
42
  }, (0, _utils.getWebinyVersionHeaders)());
43
+
28
44
  const getDefaultHeaders = routes => {
29
45
  /**
30
46
  * If we are accepting all headers, just output that one.
31
47
  */
32
48
  const keys = Object.keys(routes);
33
49
  const all = keys.every(key => routes[key].length > 0);
50
+
34
51
  if (all) {
35
52
  return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
36
53
  "Access-Control-Allow-Methods": "*"
37
54
  });
38
55
  }
56
+
39
57
  return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
40
58
  "Access-Control-Allow-Methods": keys.filter(key => {
41
59
  const type = key;
60
+
42
61
  if (!routes[type] || Array.isArray(routes[type]) === false) {
43
62
  return false;
44
63
  }
64
+
45
65
  return routes[type].length > 0;
46
66
  }).sort().join(",")
47
67
  });
48
68
  };
69
+
49
70
  const OPTIONS_HEADERS = {
50
71
  "Access-Control-Max-Age": "86400",
51
72
  "Cache-Control": "public, max-age=86400"
52
73
  };
74
+
53
75
  const createHandler = params => {
54
76
  const definedRoutes = {
55
77
  POST: [],
@@ -69,15 +91,18 @@ const createHandler = params => {
69
91
  TRACE: [],
70
92
  UNLOCK: []
71
93
  };
94
+
72
95
  const throwOnDefinedRoute = (type, path, options) => {
73
96
  if (type === "ALL") {
74
97
  const all = Object.keys(definedRoutes).some(key => {
75
98
  const routes = definedRoutes[key];
76
99
  return routes.includes(path);
77
100
  });
101
+
78
102
  if (!all) {
79
103
  return;
80
104
  }
105
+
81
106
  throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
82
107
  type,
83
108
  path
@@ -87,46 +112,59 @@ const createHandler = params => {
87
112
  } else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
88
113
  return;
89
114
  }
115
+
90
116
  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
117
  type,
92
118
  path
93
119
  });
94
120
  };
121
+
95
122
  const addDefinedRoute = (inputType, path) => {
96
123
  const type = inputType.toUpperCase();
124
+
97
125
  if (!definedRoutes[type]) {
98
126
  return;
99
127
  } else if (definedRoutes[type].includes(path)) {
100
128
  return;
101
129
  }
130
+
102
131
  definedRoutes[type].push(path);
103
132
  };
104
133
  /**
105
134
  * We must attach the server to our internal context if we want to have it accessible.
106
135
  */
136
+
137
+
107
138
  const app = (0, _fastify.default)((0, _objectSpread2.default)({}, params.options || {}));
108
139
  /**
109
140
  * We need to register routes in our system so we can output headers later on and dissallow overriding routes.
110
141
  */
142
+
111
143
  app.addHook("onRoute", route => {
112
144
  const method = route.method;
145
+
113
146
  if (Array.isArray(method)) {
114
147
  for (const m of method) {
115
148
  addDefinedRoute(m, route.path);
116
149
  }
150
+
117
151
  return;
118
152
  }
153
+
119
154
  addDefinedRoute(method, route.path);
120
155
  });
121
156
  /**
122
157
  *
123
158
  */
159
+
124
160
  app.register(_cookie.default, {
125
161
  parseOptions: {} // options for parsing cookies
162
+
126
163
  });
127
164
  /**
128
165
  * Route helpers - mostly for users.
129
166
  */
167
+
130
168
  const routes = {
131
169
  defined: definedRoutes,
132
170
  onPost: (path, handler, options) => {
@@ -169,6 +207,7 @@ const createHandler = params => {
169
207
  * And it must be one of the first context plugins applied.
170
208
  */
171
209
  (0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
210
+
172
211
  /**
173
212
  * Inserted via webpack on build time.
174
213
  */
@@ -179,8 +218,8 @@ const createHandler = params => {
179
218
  /**
180
219
  * We are attaching our custom context to webiny variable on the fastify app so it is accessible everywhere
181
220
  */
182
- app.decorate("webiny", context);
183
221
 
222
+ app.decorate("webiny", context);
184
223
  /**
185
224
  * We have few types of triggers:
186
225
  * * Events - EventPlugin
@@ -188,37 +227,44 @@ const createHandler = params => {
188
227
  *
189
228
  * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
190
229
  */
191
- const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
192
230
 
231
+ const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
193
232
  /**
194
233
  * Add routes to the system.
195
234
  */
235
+
196
236
  for (const plugin of routePlugins) {
197
237
  plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
198
238
  context: app.webiny
199
239
  }));
200
240
  }
201
-
202
241
  /**
203
242
  * On every request we add default headers, which can be changed later.
204
243
  * Also, if it is an options request, we skip everything after this hook and output options headers.
205
244
  */
245
+
246
+
206
247
  app.addHook("onRequest", async (request, reply) => {
207
248
  const defaultHeaders = getDefaultHeaders(definedRoutes);
208
249
  reply.headers(defaultHeaders);
250
+
209
251
  if (request.method !== "OPTIONS") {
210
252
  return;
211
253
  }
254
+
212
255
  const raw = reply.code(204).hijack().raw;
213
256
  const headers = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS);
257
+
214
258
  for (const key in headers) {
215
259
  raw.setHeader(key, headers[key]);
216
260
  }
261
+
217
262
  raw.end("");
218
263
  });
219
264
  app.addHook("preParsing", async request => {
220
265
  app.webiny.request = request;
221
266
  const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
267
+
222
268
  for (const plugin of plugins) {
223
269
  await plugin.apply(app.webiny);
224
270
  }
@@ -226,27 +272,32 @@ const createHandler = params => {
226
272
  /**
227
273
  *
228
274
  */
275
+
229
276
  app.addHook("preHandler", async () => {
230
277
  const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
278
+
231
279
  for (const plugin of plugins) {
232
280
  await plugin.apply(app.webiny);
233
281
  }
234
282
  });
235
-
236
283
  /**
237
284
  *
238
285
  */
286
+
239
287
  const preSerialization = async (_, __, payload) => {
240
288
  const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
289
+
241
290
  for (const plugin of plugins) {
242
291
  await plugin.handle(app.webiny, payload);
243
292
  }
293
+
244
294
  return payload;
245
295
  };
296
+
246
297
  app.addHook("preSerialization", preSerialization);
247
298
  app.addHook("onError", async (_, reply, error) => {
248
- const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
249
- // Log error to cloud, as these can be extremely annoying to debug!
299
+ const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type); // Log error to cloud, as these can be extremely annoying to debug!
300
+
250
301
  console.log("@webiny/handler");
251
302
  console.log(JSON.stringify((0, _objectSpread2.default)((0, _objectSpread2.default)({}, error || {}), {}, {
252
303
  message: error === null || error === void 0 ? void 0 : error.message,
@@ -264,4 +315,5 @@ const createHandler = params => {
264
315
  });
265
316
  return app;
266
317
  };
318
+
267
319
  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,C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BnB,IAAI,EAAEa,KAAK,aAALA,KAAK,uBAALA,KAAK,CAAEb;IAAI,GACnB,CACL;IACD,MAAMlC,OAAO,GAAG,IAAAsD,sBAAU,EACtBtC,OAAO,CAACuC,GAAG,CAACC,EAAE,IAAI;MACd,OAAO,CAAC1C,OAAgB,EAAEiC,KAAY,EAAEU,IAAc,KAAK;QACvD,OAAOD,EAAE,CAACV,MAAM,CAAChC,OAAO,EAAEiC,KAAK,EAAEU,IAAI,CAAC;MAC1C,CAAC;IACL,CAAC,CAAC,CACL;IACD,MAAMzD,OAAO,CAACX,GAAG,CAACmC,MAAM,EAAEuB,KAAK,CAAC;IAEhC,OAAOjB,KAAK,CACPE,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","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,iBAAiB,UADwB;EAEzC,gBAAgB,iCAFyB;EAGzC,+BAA+B,GAHU;EAIzC,gCAAgC,GAJS;EAKzC,gCAAgC;AALS,GAMtC,IAAAC,8BAAA,GANsC,CAA7C;;AASA,MAAMC,iBAAiB,GAAIC,MAAD,IAA0D;EAChF;AAC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C;;IACAgG,OAAO,CAACC,GAAR,CAAY,iBAAZ;IACAD,OAAO,CAACC,GAAR,CACIC,IAAI,CAACC,SAAL,6DACQL,KAAK,IAAI,EADjB;MAEIM,OAAO,EAAEN,KAAF,aAAEA,KAAF,uBAAEA,KAAK,CAAEM,OAFpB;MAGInB,IAAI,EAAEa,KAAF,aAAEA,KAAF,uBAAEA,KAAK,CAAEb;IAHjB,GADJ;IAOA,MAAMlC,OAAO,GAAG,IAAAsD,sBAAA,EACZtC,OAAO,CAACuC,GAAR,CAAYC,EAAE,IAAI;MACd,OAAO,CAAC1C,OAAD,EAAmBiC,KAAnB,EAAiCU,IAAjC,KAAoD;QACvD,OAAOD,EAAE,CAACV,MAAH,CAAUhC,OAAV,EAAmBiC,KAAnB,EAA0BU,IAA1B,CAAP;MACH,CAFD;IAGH,CAJD,CADY,CAAhB;IAOA,MAAMzD,OAAO,CAACX,GAAG,CAACmC,MAAL,EAAauB,KAAb,CAAb;IAEA,OAAOjB,KAAK,CACPE,OADE,CACM;MACL,iBAAiB;IADZ,CADN,EAIF0B,MAJE,CAIK,GAJL,CAAP;EAKH,CAzBD;EA2BA,OAAOrE,GAAP;AACH,CArPM"}
package/index.js CHANGED
@@ -3,7 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+
6
7
  var _fastify = require("./fastify");
8
+
7
9
  Object.keys(_fastify).forEach(function (key) {
8
10
  if (key === "default" || key === "__esModule") return;
9
11
  if (key in exports && exports[key] === _fastify[key]) return;
@@ -14,7 +16,9 @@ Object.keys(_fastify).forEach(function (key) {
14
16
  }
15
17
  });
16
18
  });
19
+
17
20
  var _ContextPlugin = require("@webiny/api/plugins/ContextPlugin");
21
+
18
22
  Object.keys(_ContextPlugin).forEach(function (key) {
19
23
  if (key === "default" || key === "__esModule") return;
20
24
  if (key in exports && exports[key] === _ContextPlugin[key]) return;
@@ -25,7 +29,9 @@ Object.keys(_ContextPlugin).forEach(function (key) {
25
29
  }
26
30
  });
27
31
  });
32
+
28
33
  var _Context = require("./Context");
34
+
29
35
  Object.keys(_Context).forEach(function (key) {
30
36
  if (key === "default" || key === "__esModule") return;
31
37
  if (key in exports && exports[key] === _Context[key]) return;
@@ -36,7 +42,9 @@ Object.keys(_Context).forEach(function (key) {
36
42
  }
37
43
  });
38
44
  });
45
+
39
46
  var _EventPlugin = require("./plugins/EventPlugin");
47
+
40
48
  Object.keys(_EventPlugin).forEach(function (key) {
41
49
  if (key === "default" || key === "__esModule") return;
42
50
  if (key in exports && exports[key] === _EventPlugin[key]) return;
@@ -47,7 +55,9 @@ Object.keys(_EventPlugin).forEach(function (key) {
47
55
  }
48
56
  });
49
57
  });
58
+
50
59
  var _RoutePlugin = require("./plugins/RoutePlugin");
60
+
51
61
  Object.keys(_RoutePlugin).forEach(function (key) {
52
62
  if (key === "default" || key === "__esModule") return;
53
63
  if (key in exports && exports[key] === _RoutePlugin[key]) return;
@@ -58,7 +68,9 @@ Object.keys(_RoutePlugin).forEach(function (key) {
58
68
  }
59
69
  });
60
70
  });
71
+
61
72
  var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
73
+
62
74
  Object.keys(_BeforeHandlerPlugin).forEach(function (key) {
63
75
  if (key === "default" || key === "__esModule") return;
64
76
  if (key in exports && exports[key] === _BeforeHandlerPlugin[key]) return;
@@ -69,7 +81,9 @@ Object.keys(_BeforeHandlerPlugin).forEach(function (key) {
69
81
  }
70
82
  });
71
83
  });
84
+
72
85
  var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
86
+
73
87
  Object.keys(_HandlerErrorPlugin).forEach(function (key) {
74
88
  if (key === "default" || key === "__esModule") return;
75
89
  if (key in exports && exports[key] === _HandlerErrorPlugin[key]) return;
@@ -80,7 +94,9 @@ Object.keys(_HandlerErrorPlugin).forEach(function (key) {
80
94
  }
81
95
  });
82
96
  });
97
+
83
98
  var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
99
+
84
100
  Object.keys(_HandlerResultPlugin).forEach(function (key) {
85
101
  if (key === "default" || key === "__esModule") return;
86
102
  if (key in exports && exports[key] === _HandlerResultPlugin[key]) return;
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 \"~/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"}
package/middleware.js CHANGED
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.middleware = void 0;
7
+
7
8
  /**
8
9
  * Compose a single middleware from the array of middleware functions
9
10
  */
@@ -11,19 +12,22 @@ const middleware = (functions = []) => {
11
12
  return (...args) => {
12
13
  if (!functions.length) {
13
14
  return Promise.resolve();
14
- }
15
+ } // Create a clone of function chain to prevent modifying the original array with `shift()`
16
+
15
17
 
16
- // Create a clone of function chain to prevent modifying the original array with `shift()`
17
18
  const chain = [...functions];
18
19
  return new Promise((parentResolve, parentReject) => {
19
20
  const next = async () => {
20
21
  const fn = chain.shift();
22
+
21
23
  if (!fn) {
22
24
  return Promise.resolve();
23
25
  }
26
+
24
27
  return new Promise(async (resolve, reject) => {
25
28
  try {
26
29
  const result = await fn(...args, resolve);
30
+
27
31
  if (typeof result !== "undefined") {
28
32
  return parentResolve(result);
29
33
  }
@@ -38,8 +42,10 @@ const middleware = (functions = []) => {
38
42
  parentReject(e);
39
43
  });
40
44
  };
45
+
41
46
  return next();
42
47
  });
43
48
  };
44
49
  };
50
+
45
51
  exports.middleware = middleware;
package/middleware.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["middleware","functions","args","length","Promise","resolve","chain","parentResolve","parentReject","next","fn","shift","reject","result","e","then","catch"],"sources":["middleware.ts"],"sourcesContent":["/**\n * Compose a single middleware from the array of middleware functions\n */\nexport const middleware = (functions: Function[] = []): Function => {\n return (...args: string[]): Promise<any> => {\n if (!functions.length) {\n return Promise.resolve();\n }\n\n // Create a clone of function chain to prevent modifying the original array with `shift()`\n const chain = [...functions];\n return new Promise((parentResolve: any, parentReject) => {\n const next = async (): Promise<any> => {\n const fn = chain.shift();\n if (!fn) {\n return Promise.resolve();\n }\n\n return new Promise(async (resolve, reject) => {\n try {\n const result = await fn(...args, resolve);\n if (typeof result !== \"undefined\") {\n return parentResolve(result);\n }\n } catch (e) {\n reject(e);\n }\n })\n .then(() => {\n return next();\n })\n .then(() => {\n parentResolve(...args);\n })\n .catch(e => {\n parentReject(e);\n });\n };\n\n return next();\n });\n };\n};\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACO,MAAMA,UAAU,GAAG,CAACC,SAAqB,GAAG,EAAE,KAAe;EAChE,OAAO,CAAC,GAAGC,IAAc,KAAmB;IACxC,IAAI,CAACD,SAAS,CAACE,MAAM,EAAE;MACnB,OAAOC,OAAO,CAACC,OAAO,EAAE;IAC5B;;IAEA;IACA,MAAMC,KAAK,GAAG,CAAC,GAAGL,SAAS,CAAC;IAC5B,OAAO,IAAIG,OAAO,CAAC,CAACG,aAAkB,EAAEC,YAAY,KAAK;MACrD,MAAMC,IAAI,GAAG,YAA0B;QACnC,MAAMC,EAAE,GAAGJ,KAAK,CAACK,KAAK,EAAE;QACxB,IAAI,CAACD,EAAE,EAAE;UACL,OAAON,OAAO,CAACC,OAAO,EAAE;QAC5B;QAEA,OAAO,IAAID,OAAO,CAAC,OAAOC,OAAO,EAAEO,MAAM,KAAK;UAC1C,IAAI;YACA,MAAMC,MAAM,GAAG,MAAMH,EAAE,CAAC,GAAGR,IAAI,EAAEG,OAAO,CAAC;YACzC,IAAI,OAAOQ,MAAM,KAAK,WAAW,EAAE;cAC/B,OAAON,aAAa,CAACM,MAAM,CAAC;YAChC;UACJ,CAAC,CAAC,OAAOC,CAAC,EAAE;YACRF,MAAM,CAACE,CAAC,CAAC;UACb;QACJ,CAAC,CAAC,CACGC,IAAI,CAAC,MAAM;UACR,OAAON,IAAI,EAAE;QACjB,CAAC,CAAC,CACDM,IAAI,CAAC,MAAM;UACRR,aAAa,CAAC,GAAGL,IAAI,CAAC;QAC1B,CAAC,CAAC,CACDc,KAAK,CAACF,CAAC,IAAI;UACRN,YAAY,CAACM,CAAC,CAAC;QACnB,CAAC,CAAC;MACV,CAAC;MAED,OAAOL,IAAI,EAAE;IACjB,CAAC,CAAC;EACN,CAAC;AACL,CAAC;AAAC"}
1
+ {"version":3,"names":["middleware","functions","args","length","Promise","resolve","chain","parentResolve","parentReject","next","fn","shift","reject","result","e","then","catch"],"sources":["middleware.ts"],"sourcesContent":["/**\n * Compose a single middleware from the array of middleware functions\n */\nexport const middleware = (functions: Function[] = []): Function => {\n return (...args: string[]): Promise<any> => {\n if (!functions.length) {\n return Promise.resolve();\n }\n\n // Create a clone of function chain to prevent modifying the original array with `shift()`\n const chain = [...functions];\n return new Promise((parentResolve: any, parentReject) => {\n const next = async (): Promise<any> => {\n const fn = chain.shift();\n if (!fn) {\n return Promise.resolve();\n }\n\n return new Promise(async (resolve, reject) => {\n try {\n const result = await fn(...args, resolve);\n if (typeof result !== \"undefined\") {\n return parentResolve(result);\n }\n } catch (e) {\n reject(e);\n }\n })\n .then(() => {\n return next();\n })\n .then(() => {\n parentResolve(...args);\n })\n .catch(e => {\n parentReject(e);\n });\n };\n\n return next();\n });\n };\n};\n"],"mappings":";;;;;;;AAAA;AACA;AACA;AACO,MAAMA,UAAU,GAAG,CAACC,SAAqB,GAAG,EAAzB,KAA0C;EAChE,OAAO,CAAC,GAAGC,IAAJ,KAAqC;IACxC,IAAI,CAACD,SAAS,CAACE,MAAf,EAAuB;MACnB,OAAOC,OAAO,CAACC,OAAR,EAAP;IACH,CAHuC,CAKxC;;;IACA,MAAMC,KAAK,GAAG,CAAC,GAAGL,SAAJ,CAAd;IACA,OAAO,IAAIG,OAAJ,CAAY,CAACG,aAAD,EAAqBC,YAArB,KAAsC;MACrD,MAAMC,IAAI,GAAG,YAA0B;QACnC,MAAMC,EAAE,GAAGJ,KAAK,CAACK,KAAN,EAAX;;QACA,IAAI,CAACD,EAAL,EAAS;UACL,OAAON,OAAO,CAACC,OAAR,EAAP;QACH;;QAED,OAAO,IAAID,OAAJ,CAAY,OAAOC,OAAP,EAAgBO,MAAhB,KAA2B;UAC1C,IAAI;YACA,MAAMC,MAAM,GAAG,MAAMH,EAAE,CAAC,GAAGR,IAAJ,EAAUG,OAAV,CAAvB;;YACA,IAAI,OAAOQ,MAAP,KAAkB,WAAtB,EAAmC;cAC/B,OAAON,aAAa,CAACM,MAAD,CAApB;YACH;UACJ,CALD,CAKE,OAAOC,CAAP,EAAU;YACRF,MAAM,CAACE,CAAD,CAAN;UACH;QACJ,CATM,EAUFC,IAVE,CAUG,MAAM;UACR,OAAON,IAAI,EAAX;QACH,CAZE,EAaFM,IAbE,CAaG,MAAM;UACRR,aAAa,CAAC,GAAGL,IAAJ,CAAb;QACH,CAfE,EAgBFc,KAhBE,CAgBIF,CAAC,IAAI;UACRN,YAAY,CAACM,CAAD,CAAZ;QACH,CAlBE,CAAP;MAmBH,CAzBD;;MA2BA,OAAOL,IAAI,EAAX;IACH,CA7BM,CAAP;EA8BH,CArCD;AAsCH,CAvCM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/handler",
3
- "version": "0.0.0-unstable.990c3ab1b6",
3
+ "version": "0.0.0-unstable.d4f203fa97",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,22 +12,22 @@
12
12
  ],
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "@babel/runtime": "7.20.1",
15
+ "@babel/runtime": "7.19.0",
16
16
  "@fastify/cookie": "8.3.0",
17
- "@webiny/api": "0.0.0-unstable.990c3ab1b6",
18
- "@webiny/error": "0.0.0-unstable.990c3ab1b6",
19
- "@webiny/handler-client": "0.0.0-unstable.990c3ab1b6",
20
- "@webiny/plugins": "0.0.0-unstable.990c3ab1b6",
21
- "@webiny/utils": "0.0.0-unstable.990c3ab1b6",
22
- "fastify": "4.10.2"
17
+ "@webiny/api": "0.0.0-unstable.d4f203fa97",
18
+ "@webiny/error": "0.0.0-unstable.d4f203fa97",
19
+ "@webiny/handler-client": "0.0.0-unstable.d4f203fa97",
20
+ "@webiny/plugins": "0.0.0-unstable.d4f203fa97",
21
+ "@webiny/utils": "0.0.0-unstable.d4f203fa97",
22
+ "fastify": "4.9.2"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@babel/cli": "^7.19.3",
26
26
  "@babel/core": "^7.19.3",
27
27
  "@babel/preset-env": "^7.19.4",
28
28
  "@babel/preset-typescript": "^7.18.6",
29
- "@webiny/cli": "^0.0.0-unstable.990c3ab1b6",
30
- "@webiny/project-utils": "^0.0.0-unstable.990c3ab1b6",
29
+ "@webiny/cli": "^0.0.0-unstable.d4f203fa97",
30
+ "@webiny/project-utils": "^0.0.0-unstable.d4f203fa97",
31
31
  "babel-plugin-lodash": "^3.3.4",
32
32
  "merge": "^1.2.1",
33
33
  "rimraf": "^3.0.2",
@@ -42,5 +42,5 @@
42
42
  "build": "yarn webiny run build",
43
43
  "watch": "yarn webiny run watch"
44
44
  },
45
- "gitHead": "87e7b4d0a643f65b31d029d6bf2a81902fb940a8"
45
+ "gitHead": "d4f203fa97feba84cd90ccb002963fca40d9b677"
46
46
  }
@@ -1,28 +1,38 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
6
7
  });
7
8
  exports.createBeforeHandlerPlugin = exports.BeforeHandlerPlugin = void 0;
9
+
8
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
9
12
  var _plugins = require("@webiny/plugins");
13
+
10
14
  class BeforeHandlerPlugin extends _plugins.Plugin {
11
15
  constructor(callable) {
12
16
  super();
13
17
  (0, _defineProperty2.default)(this, "_callable", void 0);
14
18
  this._callable = callable;
15
19
  }
20
+
16
21
  async apply(context) {
17
22
  if (typeof this._callable !== "function") {
18
23
  throw Error(`Missing callable in BeforeHandlerPlugin! Either pass a callable to plugin constructor or extend the plugin and override the "apply" method.`);
19
24
  }
25
+
20
26
  return this._callable(context);
21
27
  }
28
+
22
29
  }
30
+
23
31
  exports.BeforeHandlerPlugin = BeforeHandlerPlugin;
24
32
  (0, _defineProperty2.default)(BeforeHandlerPlugin, "type", "before-handler");
33
+
25
34
  const createBeforeHandlerPlugin = callable => {
26
35
  return new BeforeHandlerPlugin(callable);
27
36
  };
37
+
28
38
  exports.createBeforeHandlerPlugin = createBeforeHandlerPlugin;
@@ -1 +1 @@
1
- {"version":3,"names":["BeforeHandlerPlugin","Plugin","constructor","callable","_callable","apply","context","Error","createBeforeHandlerPlugin"],"sources":["BeforeHandlerPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Context } from \"~/types\";\n\nexport interface BeforeHandlerCallable<T extends Context = Context> {\n (context: T): void | Promise<void>;\n}\n\nexport class BeforeHandlerPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"before-handler\";\n private readonly _callable: BeforeHandlerCallable<T>;\n\n constructor(callable: BeforeHandlerCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async apply(context: T): Promise<void> {\n if (typeof this._callable !== \"function\") {\n throw Error(\n `Missing callable in BeforeHandlerPlugin! Either pass a callable to plugin constructor or extend the plugin and override the \"apply\" method.`\n );\n }\n\n return this._callable(context);\n }\n}\n\nexport const createBeforeHandlerPlugin = <T extends Context = Context>(\n callable: BeforeHandlerCallable<T>\n): BeforeHandlerPlugin<T> => {\n return new BeforeHandlerPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;AAAA;AAOO,MAAMA,mBAAmB,SAAsCC,eAAM,CAAC;EAIzEC,WAAW,CAACC,QAAkC,EAAE;IAC5C,KAAK,EAAE;IAAC;IACR,IAAI,CAACC,SAAS,GAAGD,QAAQ;EAC7B;EAEA,MAAaE,KAAK,CAACC,OAAU,EAAiB;IAC1C,IAAI,OAAO,IAAI,CAACF,SAAS,KAAK,UAAU,EAAE;MACtC,MAAMG,KAAK,CACN,6IAA4I,CAChJ;IACL;IAEA,OAAO,IAAI,CAACH,SAAS,CAACE,OAAO,CAAC;EAClC;AACJ;AAAC;AAAA,8BAlBYN,mBAAmB,UACmB,gBAAgB;AAmB5D,MAAMQ,yBAAyB,GAClCL,QAAkC,IACT;EACzB,OAAO,IAAIH,mBAAmB,CAAIG,QAAQ,CAAC;AAC/C,CAAC;AAAC"}
1
+ {"version":3,"names":["BeforeHandlerPlugin","Plugin","constructor","callable","_callable","apply","context","Error","createBeforeHandlerPlugin"],"sources":["BeforeHandlerPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Context } from \"~/types\";\n\nexport interface BeforeHandlerCallable<T extends Context = Context> {\n (context: T): void | Promise<void>;\n}\n\nexport class BeforeHandlerPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"before-handler\";\n private readonly _callable: BeforeHandlerCallable<T>;\n\n constructor(callable: BeforeHandlerCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async apply(context: T): Promise<void> {\n if (typeof this._callable !== \"function\") {\n throw Error(\n `Missing callable in BeforeHandlerPlugin! Either pass a callable to plugin constructor or extend the plugin and override the \"apply\" method.`\n );\n }\n\n return this._callable(context);\n }\n}\n\nexport const createBeforeHandlerPlugin = <T extends Context = Context>(\n callable: BeforeHandlerCallable<T>\n): BeforeHandlerPlugin<T> => {\n return new BeforeHandlerPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AAOO,MAAMA,mBAAN,SAA+DC,eAA/D,CAAsE;EAIzEC,WAAW,CAACC,QAAD,EAAqC;IAC5C;IAD4C;IAE5C,KAAKC,SAAL,GAAiBD,QAAjB;EACH;;EAEiB,MAALE,KAAK,CAACC,OAAD,EAA4B;IAC1C,IAAI,OAAO,KAAKF,SAAZ,KAA0B,UAA9B,EAA0C;MACtC,MAAMG,KAAK,CACN,6IADM,CAAX;IAGH;;IAED,OAAO,KAAKH,SAAL,CAAeE,OAAf,CAAP;EACH;;AAjBwE;;;8BAAhEN,mB,UACsC,gB;;AAmB5C,MAAMQ,yBAAyB,GAClCL,QADqC,IAEZ;EACzB,OAAO,IAAIH,mBAAJ,CAA2BG,QAA3B,CAAP;AACH,CAJM"}
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
6
7
  });
7
8
  exports.createEvent = exports.EventPlugin = void 0;
9
+
8
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
9
12
  var _Plugin = require("@webiny/plugins/Plugin");
13
+
10
14
  /**
11
15
  * EventPlugin must be handled in the package which implements fastify for certain cloud.
12
16
  * There is no standard input for AWS Lambda, Google Cloud Functions and MS Azure Functions so we let that
@@ -15,17 +19,20 @@ var _Plugin = require("@webiny/plugins/Plugin");
15
19
  * Note that only one EventPlugin can be defined per fastify initialisation.
16
20
  * If more is needed, check ~/fastify.ts and implement that possibility.
17
21
  */
18
-
19
22
  class EventPlugin extends _Plugin.Plugin {
20
23
  constructor(cb) {
21
24
  super();
22
25
  (0, _defineProperty2.default)(this, "cb", void 0);
23
26
  this.cb = cb;
24
27
  }
28
+
25
29
  }
30
+
26
31
  exports.EventPlugin = EventPlugin;
27
32
  (0, _defineProperty2.default)(EventPlugin, "type", "handler.fastify.event");
33
+
28
34
  const createEvent = cb => {
29
35
  return new EventPlugin(cb);
30
36
  };
37
+
31
38
  exports.createEvent = createEvent;
@@ -1 +1 @@
1
- {"version":3,"names":["EventPlugin","Plugin","constructor","cb","createEvent"],"sources":["EventPlugin.ts"],"sourcesContent":["/**\n * EventPlugin must be handled in the package which implements fastify for certain cloud.\n * There is no standard input for AWS Lambda, Google Cloud Functions and MS Azure Functions so we let that\n * to be sorted out in the implementation package.\n *\n * Note that only one EventPlugin can be defined per fastify initialisation.\n * If more is needed, check ~/fastify.ts and implement that possibility.\n */\nimport { Plugin } from \"@webiny/plugins/Plugin\";\nimport { Request, Context as BaseContext, Reply } from \"~/types\";\n\nexport interface EventPluginCallableParams<Payload, Context extends BaseContext> {\n context: Context;\n payload: Payload;\n request: Request;\n reply: Reply;\n}\n\nexport interface EventPluginCallable<Payload, Context extends BaseContext, Response> {\n (params: EventPluginCallableParams<Payload, Context>): Promise<Response | Reply>;\n}\n\nexport class EventPlugin<\n Payload = any,\n Context extends BaseContext = BaseContext,\n Response = any\n> extends Plugin {\n public static override type = \"handler.fastify.event\";\n\n public readonly cb: EventPluginCallable<Payload, Context, Response>;\n\n public constructor(cb: EventPluginCallable<Payload, Context, Response>) {\n super();\n this.cb = cb;\n }\n}\n\nexport const createEvent = <\n Payload = any,\n Context extends BaseContext = BaseContext,\n Response = any\n>(\n cb: EventPluginCallable<Payload, Context, Response>\n) => {\n return new EventPlugin<Payload, Context, Response>(cb);\n};\n"],"mappings":";;;;;;;;AAQA;AARA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAeO,MAAMA,WAAW,SAIdC,cAAM,CAAC;EAKNC,WAAW,CAACC,EAAmD,EAAE;IACpE,KAAK,EAAE;IAAC;IACR,IAAI,CAACA,EAAE,GAAGA,EAAE;EAChB;AACJ;AAAC;AAAA,8BAbYH,WAAW,UAKU,uBAAuB;AAUlD,MAAMI,WAAW,GAKpBD,EAAmD,IAClD;EACD,OAAO,IAAIH,WAAW,CAA6BG,EAAE,CAAC;AAC1D,CAAC;AAAC"}
1
+ {"version":3,"names":["EventPlugin","Plugin","constructor","cb","createEvent"],"sources":["EventPlugin.ts"],"sourcesContent":["/**\n * EventPlugin must be handled in the package which implements fastify for certain cloud.\n * There is no standard input for AWS Lambda, Google Cloud Functions and MS Azure Functions so we let that\n * to be sorted out in the implementation package.\n *\n * Note that only one EventPlugin can be defined per fastify initialisation.\n * If more is needed, check ~/fastify.ts and implement that possibility.\n */\nimport { Plugin } from \"@webiny/plugins/Plugin\";\nimport { Request, Context as BaseContext, Reply } from \"~/types\";\n\nexport interface EventPluginCallableParams<Payload, Context extends BaseContext> {\n context: Context;\n payload: Payload;\n request: Request;\n reply: Reply;\n}\n\nexport interface EventPluginCallable<Payload, Context extends BaseContext, Response> {\n (params: EventPluginCallableParams<Payload, Context>): Promise<Response | Reply>;\n}\n\nexport class EventPlugin<\n Payload = any,\n Context extends BaseContext = BaseContext,\n Response = any\n> extends Plugin {\n public static override type = \"handler.fastify.event\";\n\n public readonly cb: EventPluginCallable<Payload, Context, Response>;\n\n public constructor(cb: EventPluginCallable<Payload, Context, Response>) {\n super();\n this.cb = cb;\n }\n}\n\nexport const createEvent = <\n Payload = any,\n Context extends BaseContext = BaseContext,\n Response = any\n>(\n cb: EventPluginCallable<Payload, Context, Response>\n) => {\n return new EventPlugin<Payload, Context, Response>(cb);\n};\n"],"mappings":";;;;;;;;;;;AAQA;;AARA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAeO,MAAMA,WAAN,SAIGC,cAJH,CAIU;EAKNC,WAAW,CAACC,EAAD,EAAsD;IACpE;IADoE;IAEpE,KAAKA,EAAL,GAAUA,EAAV;EACH;;AARY;;;8BAJJH,W,UAKqB,uB;;AAU3B,MAAMI,WAAW,GAKpBD,EALuB,IAMtB;EACD,OAAO,IAAIH,WAAJ,CAA4CG,EAA5C,CAAP;AACH,CARM"}
@@ -1,25 +1,34 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
6
7
  });
7
8
  exports.createHandlerErrorPlugin = exports.HandlerErrorPlugin = void 0;
9
+
8
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
9
12
  var _plugins = require("@webiny/plugins");
13
+
10
14
  class HandlerErrorPlugin extends _plugins.Plugin {
11
15
  constructor(callable) {
12
16
  super();
13
17
  (0, _defineProperty2.default)(this, "_callable", void 0);
14
18
  this._callable = callable;
15
19
  }
20
+
16
21
  async handle(context, error, next) {
17
22
  return this._callable(context, error, next);
18
23
  }
24
+
19
25
  }
26
+
20
27
  exports.HandlerErrorPlugin = HandlerErrorPlugin;
21
28
  (0, _defineProperty2.default)(HandlerErrorPlugin, "type", "handler-error");
29
+
22
30
  const createHandlerErrorPlugin = callable => {
23
31
  return new HandlerErrorPlugin(callable);
24
32
  };
33
+
25
34
  exports.createHandlerErrorPlugin = createHandlerErrorPlugin;
@@ -1 +1 @@
1
- {"version":3,"names":["HandlerErrorPlugin","Plugin","constructor","callable","_callable","handle","context","error","next","createHandlerErrorPlugin"],"sources":["HandlerErrorPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Context } from \"~/types\";\n\nexport interface HandlerErrorCallable<T extends Context = Context> {\n (context: T, error: Error, next: Function): Promise<any>;\n}\n\nexport class HandlerErrorPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"handler-error\";\n\n private readonly _callable: HandlerErrorCallable<T>;\n\n public constructor(callable: HandlerErrorCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async handle(context: T, error: Error, next: Function): Promise<any> {\n return this._callable(context, error, next);\n }\n}\n\nexport const createHandlerErrorPlugin = <T extends Context = Context>(\n callable: HandlerErrorCallable<T>\n): HandlerErrorPlugin<T> => {\n return new HandlerErrorPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;AAAA;AAOO,MAAMA,kBAAkB,SAAsCC,eAAM,CAAC;EAKjEC,WAAW,CAACC,QAAiC,EAAE;IAClD,KAAK,EAAE;IAAC;IACR,IAAI,CAACC,SAAS,GAAGD,QAAQ;EAC7B;EAEA,MAAaE,MAAM,CAACC,OAAU,EAAEC,KAAY,EAAEC,IAAc,EAAgB;IACxE,OAAO,IAAI,CAACJ,SAAS,CAACE,OAAO,EAAEC,KAAK,EAAEC,IAAI,CAAC;EAC/C;AACJ;AAAC;AAAA,8BAbYR,kBAAkB,UACoB,eAAe;AAc3D,MAAMS,wBAAwB,GACjCN,QAAiC,IACT;EACxB,OAAO,IAAIH,kBAAkB,CAAIG,QAAQ,CAAC;AAC9C,CAAC;AAAC"}
1
+ {"version":3,"names":["HandlerErrorPlugin","Plugin","constructor","callable","_callable","handle","context","error","next","createHandlerErrorPlugin"],"sources":["HandlerErrorPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Context } from \"~/types\";\n\nexport interface HandlerErrorCallable<T extends Context = Context> {\n (context: T, error: Error, next: Function): Promise<any>;\n}\n\nexport class HandlerErrorPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"handler-error\";\n\n private readonly _callable: HandlerErrorCallable<T>;\n\n public constructor(callable: HandlerErrorCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async handle(context: T, error: Error, next: Function): Promise<any> {\n return this._callable(context, error, next);\n }\n}\n\nexport const createHandlerErrorPlugin = <T extends Context = Context>(\n callable: HandlerErrorCallable<T>\n): HandlerErrorPlugin<T> => {\n return new HandlerErrorPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AAOO,MAAMA,kBAAN,SAA8DC,eAA9D,CAAqE;EAKjEC,WAAW,CAACC,QAAD,EAAoC;IAClD;IADkD;IAElD,KAAKC,SAAL,GAAiBD,QAAjB;EACH;;EAEkB,MAANE,MAAM,CAACC,OAAD,EAAaC,KAAb,EAA2BC,IAA3B,EAAyD;IACxE,OAAO,KAAKJ,SAAL,CAAeE,OAAf,EAAwBC,KAAxB,EAA+BC,IAA/B,CAAP;EACH;;AAZuE;;;8BAA/DR,kB,UACsC,e;;AAc5C,MAAMS,wBAAwB,GACjCN,QADoC,IAEZ;EACxB,OAAO,IAAIH,kBAAJ,CAA0BG,QAA1B,CAAP;AACH,CAJM"}
@@ -1,25 +1,34 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
6
7
  });
7
8
  exports.createHandlerResultPlugin = exports.HandlerResultPlugin = void 0;
9
+
8
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
9
12
  var _plugins = require("@webiny/plugins");
13
+
10
14
  class HandlerResultPlugin extends _plugins.Plugin {
11
15
  constructor(callable) {
12
16
  super();
13
17
  (0, _defineProperty2.default)(this, "_callable", void 0);
14
18
  this._callable = callable;
15
19
  }
20
+
16
21
  async handle(context, result) {
17
22
  return this._callable(context, result);
18
23
  }
24
+
19
25
  }
26
+
20
27
  exports.HandlerResultPlugin = HandlerResultPlugin;
21
28
  (0, _defineProperty2.default)(HandlerResultPlugin, "type", "handler-result");
29
+
22
30
  const createHandlerResultPlugin = callable => {
23
31
  return new HandlerResultPlugin(callable);
24
32
  };
33
+
25
34
  exports.createHandlerResultPlugin = createHandlerResultPlugin;
@@ -1 +1 @@
1
- {"version":3,"names":["HandlerResultPlugin","Plugin","constructor","callable","_callable","handle","context","result","createHandlerResultPlugin"],"sources":["HandlerResultPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Context } from \"~/types\";\n\nexport interface HandlerResultCallable<T extends Context = Context> {\n (context: T, result: any): Promise<any>;\n}\n\nexport class HandlerResultPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"handler-result\";\n\n private readonly _callable: HandlerResultCallable<T>;\n\n public constructor(callable: HandlerResultCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async handle(context: T, result: any): Promise<any> {\n return this._callable(context, result);\n }\n}\n\nexport const createHandlerResultPlugin = <T extends Context = Context>(\n callable: HandlerResultCallable<T>\n): HandlerResultPlugin<T> => {\n return new HandlerResultPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;AAAA;AAOO,MAAMA,mBAAmB,SAAsCC,eAAM,CAAC;EAKlEC,WAAW,CAACC,QAAkC,EAAE;IACnD,KAAK,EAAE;IAAC;IACR,IAAI,CAACC,SAAS,GAAGD,QAAQ;EAC7B;EAEA,MAAaE,MAAM,CAACC,OAAU,EAAEC,MAAW,EAAgB;IACvD,OAAO,IAAI,CAACH,SAAS,CAACE,OAAO,EAAEC,MAAM,CAAC;EAC1C;AACJ;AAAC;AAAA,8BAbYP,mBAAmB,UACmB,gBAAgB;AAc5D,MAAMQ,yBAAyB,GAClCL,QAAkC,IACT;EACzB,OAAO,IAAIH,mBAAmB,CAAIG,QAAQ,CAAC;AAC/C,CAAC;AAAC"}
1
+ {"version":3,"names":["HandlerResultPlugin","Plugin","constructor","callable","_callable","handle","context","result","createHandlerResultPlugin"],"sources":["HandlerResultPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport { Context } from \"~/types\";\n\nexport interface HandlerResultCallable<T extends Context = Context> {\n (context: T, result: any): Promise<any>;\n}\n\nexport class HandlerResultPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"handler-result\";\n\n private readonly _callable: HandlerResultCallable<T>;\n\n public constructor(callable: HandlerResultCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async handle(context: T, result: any): Promise<any> {\n return this._callable(context, result);\n }\n}\n\nexport const createHandlerResultPlugin = <T extends Context = Context>(\n callable: HandlerResultCallable<T>\n): HandlerResultPlugin<T> => {\n return new HandlerResultPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AAOO,MAAMA,mBAAN,SAA+DC,eAA/D,CAAsE;EAKlEC,WAAW,CAACC,QAAD,EAAqC;IACnD;IADmD;IAEnD,KAAKC,SAAL,GAAiBD,QAAjB;EACH;;EAEkB,MAANE,MAAM,CAACC,OAAD,EAAaC,MAAb,EAAwC;IACvD,OAAO,KAAKH,SAAL,CAAeE,OAAf,EAAwBC,MAAxB,CAAP;EACH;;AAZwE;;;8BAAhEP,mB,UACsC,gB;;AAc5C,MAAMQ,yBAAyB,GAClCL,QADqC,IAEZ;EACzB,OAAO,IAAIH,mBAAJ,CAA2BG,QAA3B,CAAP;AACH,CAJM"}
@@ -1,22 +1,30 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
6
7
  });
7
8
  exports.createRoute = exports.RoutePlugin = void 0;
9
+
8
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
9
12
  var _Plugin = require("@webiny/plugins/Plugin");
13
+
10
14
  class RoutePlugin extends _Plugin.Plugin {
11
15
  constructor(cb) {
12
16
  super();
13
17
  (0, _defineProperty2.default)(this, "cb", void 0);
14
18
  this.cb = cb;
15
19
  }
20
+
16
21
  }
22
+
17
23
  exports.RoutePlugin = RoutePlugin;
18
24
  (0, _defineProperty2.default)(RoutePlugin, "type", "handler.fastify.route");
25
+
19
26
  const createRoute = cb => {
20
27
  return new RoutePlugin(cb);
21
28
  };
29
+
22
30
  exports.createRoute = createRoute;
@@ -1 +1 @@
1
- {"version":3,"names":["RoutePlugin","Plugin","constructor","cb","createRoute"],"sources":["RoutePlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins/Plugin\";\nimport { Context, RouteMethod } from \"~/types\";\n\ninterface RoutePluginCbParams<T extends Context> {\n context: T;\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}\nexport interface RoutePluginCb<T extends Context> {\n (params: RoutePluginCbParams<T>): void;\n}\n\nexport class RoutePlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"handler.fastify.route\";\n\n public readonly cb: RoutePluginCb<T>;\n\n public constructor(cb: RoutePluginCb<T>) {\n super();\n this.cb = cb;\n }\n}\n\nexport const createRoute = <T extends Context = Context>(cb: RoutePluginCb<T>): RoutePlugin<T> => {\n return new RoutePlugin<T>(cb);\n};\n"],"mappings":";;;;;;;;AAAA;AAkBO,MAAMA,WAAW,SAAsCC,cAAM,CAAC;EAK1DC,WAAW,CAACC,EAAoB,EAAE;IACrC,KAAK,EAAE;IAAC;IACR,IAAI,CAACA,EAAE,GAAGA,EAAE;EAChB;AACJ;AAAC;AAAA,8BATYH,WAAW,UAC2B,uBAAuB;AAUnE,MAAMI,WAAW,GAAiCD,EAAoB,IAAqB;EAC9F,OAAO,IAAIH,WAAW,CAAIG,EAAE,CAAC;AACjC,CAAC;AAAC"}
1
+ {"version":3,"names":["RoutePlugin","Plugin","constructor","cb","createRoute"],"sources":["RoutePlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins/Plugin\";\nimport { Context, RouteMethod } from \"~/types\";\n\ninterface RoutePluginCbParams<T extends Context> {\n context: T;\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}\nexport interface RoutePluginCb<T extends Context> {\n (params: RoutePluginCbParams<T>): void;\n}\n\nexport class RoutePlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"handler.fastify.route\";\n\n public readonly cb: RoutePluginCb<T>;\n\n public constructor(cb: RoutePluginCb<T>) {\n super();\n this.cb = cb;\n }\n}\n\nexport const createRoute = <T extends Context = Context>(cb: RoutePluginCb<T>): RoutePlugin<T> => {\n return new RoutePlugin<T>(cb);\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AAkBO,MAAMA,WAAN,SAAuDC,cAAvD,CAA8D;EAK1DC,WAAW,CAACC,EAAD,EAAuB;IACrC;IADqC;IAErC,KAAKA,EAAL,GAAUA,EAAV;EACH;;AARgE;;;8BAAxDH,W,UACsC,uB;;AAU5C,MAAMI,WAAW,GAAiCD,EAA9B,IAAuE;EAC9F,OAAO,IAAIH,WAAJ,CAAmBG,EAAnB,CAAP;AACH,CAFM"}
package/types.js CHANGED
@@ -15,4 +15,5 @@ Object.defineProperty(exports, "HTTPMethods", {
15
15
  return _fastify.HTTPMethods;
16
16
  }
17
17
  });
18
+
18
19
  var _fastify = require("fastify");
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 * @internal\n */\n routes: ContextRoutes;\n}\n\ndeclare module \"fastify\" {\n interface FastifyInstance {\n webiny: Context;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA"}