@webiny/handler 5.30.0 → 5.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/Context.d.ts +13 -0
  2. package/Context.js +29 -0
  3. package/Context.js.map +1 -0
  4. package/README.md +8 -2
  5. package/fastify.d.ts +8 -0
  6. package/fastify.js +312 -0
  7. package/fastify.js.map +1 -0
  8. package/index.d.ts +6 -5
  9. package/index.js +43 -36
  10. package/index.js.map +1 -1
  11. package/middleware.d.ts +1 -2
  12. package/middleware.js +3 -3
  13. package/middleware.js.map +1 -1
  14. package/package.json +18 -11
  15. package/plugins/BeforeHandlerPlugin.d.ts +1 -0
  16. package/plugins/BeforeHandlerPlugin.js +8 -2
  17. package/plugins/BeforeHandlerPlugin.js.map +1 -1
  18. package/plugins/EventPlugin.d.ts +25 -0
  19. package/plugins/EventPlugin.js +38 -0
  20. package/plugins/EventPlugin.js.map +1 -0
  21. package/plugins/HandlerErrorPlugin.d.ts +1 -0
  22. package/plugins/HandlerErrorPlugin.js +8 -2
  23. package/plugins/HandlerErrorPlugin.js.map +1 -1
  24. package/plugins/HandlerResultPlugin.d.ts +1 -0
  25. package/plugins/HandlerResultPlugin.js +8 -2
  26. package/plugins/HandlerResultPlugin.js.map +1 -1
  27. package/plugins/RoutePlugin.d.ts +23 -0
  28. package/plugins/RoutePlugin.js +30 -0
  29. package/plugins/RoutePlugin.js.map +1 -0
  30. package/types.d.ts +38 -71
  31. package/types.js +15 -1
  32. package/types.js.map +1 -1
  33. package/createHandler.d.ts +0 -3
  34. package/createHandler.js +0 -105
  35. package/createHandler.js.map +0 -1
  36. package/plugins/Context.d.ts +0 -20
  37. package/plugins/Context.js +0 -151
  38. package/plugins/Context.js.map +0 -1
  39. package/plugins/ContextPlugin.d.ts +0 -11
  40. package/plugins/ContextPlugin.js +0 -32
  41. package/plugins/ContextPlugin.js.map +0 -1
  42. package/plugins/HandlerPlugin.d.ts +0 -11
  43. package/plugins/HandlerPlugin.js +0 -28
  44. package/plugins/HandlerPlugin.js.map +0 -1
package/Context.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { Context as BaseContext, ContextParams as BaseContextParams } from "@webiny/api";
2
+ import { Context as BaseContextType } from "./types";
3
+ export interface ContextParams extends BaseContextParams {
4
+ server: BaseContextType["server"];
5
+ routes: BaseContextType["routes"];
6
+ }
7
+ export declare class Context extends BaseContext implements BaseContextType {
8
+ readonly server: BaseContextType["server"];
9
+ readonly routes: BaseContextType["routes"];
10
+ handlerClient: BaseContextType["handlerClient"];
11
+ request: BaseContextType["request"];
12
+ constructor(params: ContextParams);
13
+ }
package/Context.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Context = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _api = require("@webiny/api");
13
+
14
+ class Context extends _api.Context {
15
+ // @ts-ignore
16
+ // @ts-ignore
17
+ constructor(params) {
18
+ super(params);
19
+ (0, _defineProperty2.default)(this, "server", void 0);
20
+ (0, _defineProperty2.default)(this, "routes", void 0);
21
+ (0, _defineProperty2.default)(this, "handlerClient", void 0);
22
+ (0, _defineProperty2.default)(this, "request", void 0);
23
+ this.server = params.server;
24
+ this.routes = params.routes;
25
+ }
26
+
27
+ }
28
+
29
+ exports.Context = Context;
package/Context.js.map ADDED
@@ -0,0 +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,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/README.md CHANGED
@@ -4,6 +4,12 @@
4
4
  [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
5
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
6
 
7
- A tiny, universal, and plugin-based framework for creating (serverless) handlers, used within services like AWS Lambda, Azure Serverless Functions, and others.
7
+ ## Install
8
+ ```
9
+ npm install --save @webiny/handler
10
+ ```
8
11
 
9
- A handler can be anything from a simple event-handler, to a fully-fledged GraphQL server. It all depends on the additional plugins that are passed upon the creating the handler.
12
+ Or if you prefer yarn:
13
+ ```
14
+ yarn add @webiny/handler
15
+ ```
package/fastify.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /// <reference types="node" />
2
+ import { PluginCollection } from "@webiny/plugins/types";
3
+ import { FastifyServerOptions as ServerOptions } from "fastify";
4
+ export interface CreateHandlerParams {
5
+ plugins: PluginCollection;
6
+ options?: ServerOptions;
7
+ }
8
+ export declare const createHandler: (params: CreateHandlerParams) => import("fastify").FastifyInstance<import("http").Server, import("http").IncomingMessage, import("http").ServerResponse, import("fastify").FastifyLoggerInstance, import("fastify").FastifyTypeProviderDefault> & PromiseLike<import("fastify").FastifyInstance<import("http").Server, import("http").IncomingMessage, import("http").ServerResponse, import("fastify").FastifyLoggerInstance, import("fastify").FastifyTypeProviderDefault>>;
package/fastify.js ADDED
@@ -0,0 +1,312 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createHandler = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _fastify = _interopRequireDefault(require("fastify"));
13
+
14
+ var _utils = require("@webiny/utils");
15
+
16
+ var _Context = require("./Context");
17
+
18
+ var _error = _interopRequireDefault(require("@webiny/error"));
19
+
20
+ var _RoutePlugin = require("./plugins/RoutePlugin");
21
+
22
+ var _handlerClient = require("@webiny/handler-client");
23
+
24
+ var _cookie = _interopRequireDefault(require("@fastify/cookie"));
25
+
26
+ var _middleware = require("./middleware");
27
+
28
+ var _api = require("@webiny/api");
29
+
30
+ var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
31
+
32
+ var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
33
+
34
+ var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
35
+
36
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
37
+
38
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
39
+
40
+ const DEFAULT_HEADERS = _objectSpread({
41
+ "Cache-Control": "no-store",
42
+ "Content-Type": "application/json; charset=utf-8",
43
+ "Access-Control-Allow-Origin": "*",
44
+ "Access-Control-Allow-Headers": "*",
45
+ "Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH"
46
+ }, (0, _utils.getWebinyVersionHeaders)());
47
+
48
+ const getDefaultHeaders = routes => {
49
+ /**
50
+ * If we are accepting all headers, just output that one.
51
+ */
52
+ const keys = Object.keys(routes);
53
+ const all = keys.every(key => routes[key].length > 0);
54
+
55
+ if (all) {
56
+ return _objectSpread(_objectSpread({}, DEFAULT_HEADERS), {}, {
57
+ "Access-Control-Allow-Methods": "*"
58
+ });
59
+ }
60
+
61
+ return _objectSpread(_objectSpread({}, DEFAULT_HEADERS), {}, {
62
+ "Access-Control-Allow-Methods": keys.filter(key => {
63
+ const type = key;
64
+
65
+ if (!routes[type] || Array.isArray(routes[type]) === false) {
66
+ return false;
67
+ }
68
+
69
+ return routes[type].length > 0;
70
+ }).sort().join(",")
71
+ });
72
+ };
73
+
74
+ const OPTIONS_HEADERS = {
75
+ "Access-Control-Max-Age": "86400",
76
+ "Cache-Control": "public, max-age=86400"
77
+ };
78
+
79
+ const createHandler = params => {
80
+ const definedRoutes = {
81
+ POST: [],
82
+ GET: [],
83
+ OPTIONS: [],
84
+ DELETE: [],
85
+ PATCH: [],
86
+ PUT: [],
87
+ HEAD: []
88
+ };
89
+
90
+ const throwOnDefinedRoute = (type, path, options) => {
91
+ if (type === "ALL") {
92
+ const all = Object.keys(definedRoutes).some(key => {
93
+ const routes = definedRoutes[key];
94
+ return routes.includes(path);
95
+ });
96
+
97
+ if (!all) {
98
+ return;
99
+ }
100
+
101
+ throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
102
+ type,
103
+ path
104
+ });
105
+ } else if (definedRoutes[type].includes(path) === false) {
106
+ return;
107
+ } else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
108
+ return;
109
+ }
110
+
111
+ throw new _error.default(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
112
+ type,
113
+ path
114
+ });
115
+ };
116
+
117
+ const addDefinedRoute = (inputType, path) => {
118
+ const type = inputType.toUpperCase();
119
+
120
+ if (!definedRoutes[type]) {
121
+ return;
122
+ } else if (definedRoutes[type].includes(path)) {
123
+ return;
124
+ }
125
+
126
+ definedRoutes[type].push(path);
127
+ };
128
+ /**
129
+ * We must attach the server to our internal context if we want to have it accessible.
130
+ */
131
+
132
+
133
+ const app = (0, _fastify.default)(_objectSpread({}, params.options || {}));
134
+ /**
135
+ * We need to register routes in our system so we can output headers later on and dissallow overriding routes.
136
+ */
137
+
138
+ app.addHook("onRoute", route => {
139
+ const method = route.method;
140
+
141
+ if (Array.isArray(method)) {
142
+ for (const m of method) {
143
+ addDefinedRoute(m, route.path);
144
+ }
145
+
146
+ return;
147
+ }
148
+
149
+ addDefinedRoute(method, route.path);
150
+ });
151
+ /**
152
+ *
153
+ */
154
+
155
+ app.register(_cookie.default, {
156
+ parseOptions: {} // options for parsing cookies
157
+
158
+ });
159
+ /**
160
+ * Route helpers - mostly for users.
161
+ */
162
+
163
+ const routes = {
164
+ defined: definedRoutes,
165
+ onPost: (path, handler, options) => {
166
+ throwOnDefinedRoute("POST", path, options);
167
+ app.post(path, handler);
168
+ },
169
+ onGet: (path, handler, options) => {
170
+ throwOnDefinedRoute("GET", path, options);
171
+ app.get(path, handler);
172
+ },
173
+ onOptions: (path, handler, options) => {
174
+ throwOnDefinedRoute("OPTIONS", path, options);
175
+ app.options(path, handler);
176
+ },
177
+ onDelete: (path, handler, options) => {
178
+ throwOnDefinedRoute("DELETE", path, options);
179
+ app.delete(path, handler);
180
+ },
181
+ onPatch: (path, handler, options) => {
182
+ throwOnDefinedRoute("PATCH", path, options);
183
+ app.patch(path, handler);
184
+ },
185
+ onPut: (path, handler, options) => {
186
+ throwOnDefinedRoute("PUT", path, options);
187
+ app.put(path, handler);
188
+ },
189
+ onAll: (path, handler, options) => {
190
+ throwOnDefinedRoute("ALL", path, options);
191
+ app.all(path, handler);
192
+ },
193
+ onHead: (path, handler, options) => {
194
+ throwOnDefinedRoute("HEAD", path, options);
195
+ app.head(path, handler);
196
+ }
197
+ };
198
+ const context = new _Context.Context({
199
+ plugins: [
200
+ /**
201
+ * We must have handlerClient by default.
202
+ * And it must be one of the first context plugins applied.
203
+ */
204
+ (0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
205
+
206
+ /**
207
+ * Inserted via webpack on build time.
208
+ */
209
+ WEBINY_VERSION: process.env.WEBINY_VERSION,
210
+ server: app,
211
+ routes
212
+ });
213
+ /**
214
+ * We are attaching our custom context to webiny variable on the fastify app so it is accessible everywhere
215
+ */
216
+
217
+ app.decorate("webiny", context);
218
+ /**
219
+ * We have few types of triggers:
220
+ * * Events - EventPlugin
221
+ * * Routes - RoutePlugin
222
+ *
223
+ * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
224
+ */
225
+
226
+ const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
227
+ /**
228
+ * Add routes to the system.
229
+ */
230
+
231
+ for (const plugin of routePlugins) {
232
+ plugin.cb(_objectSpread(_objectSpread({}, app.webiny.routes), {}, {
233
+ context: app.webiny
234
+ }));
235
+ }
236
+ /**
237
+ * On every request we add default headers, which can be changed later.
238
+ * Also, if it is an options request, we skip everything after this hook and output options headers.
239
+ */
240
+
241
+
242
+ app.addHook("onRequest", async (request, reply) => {
243
+ const defaultHeaders = getDefaultHeaders(definedRoutes);
244
+ reply.headers(defaultHeaders);
245
+
246
+ if (request.method !== "OPTIONS") {
247
+ return;
248
+ }
249
+
250
+ const raw = reply.code(204).hijack().raw;
251
+
252
+ const headers = _objectSpread(_objectSpread({}, defaultHeaders), OPTIONS_HEADERS);
253
+
254
+ for (const key in headers) {
255
+ raw.setHeader(key, headers[key]);
256
+ }
257
+
258
+ raw.end("");
259
+ });
260
+ app.addHook("preParsing", async request => {
261
+ app.webiny.request = request;
262
+ const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
263
+
264
+ for (const plugin of plugins) {
265
+ await plugin.apply(app.webiny);
266
+ }
267
+ });
268
+ /**
269
+ *
270
+ */
271
+
272
+ app.addHook("preHandler", async () => {
273
+ const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
274
+
275
+ for (const plugin of plugins) {
276
+ await plugin.apply(app.webiny);
277
+ }
278
+ });
279
+ /**
280
+ *
281
+ */
282
+
283
+ const preSerialization = async (_, __, payload) => {
284
+ const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
285
+
286
+ for (const plugin of plugins) {
287
+ await plugin.handle(app.webiny, payload);
288
+ }
289
+
290
+ return payload;
291
+ };
292
+
293
+ app.addHook("preSerialization", preSerialization);
294
+ app.addHook("onError", async (_, reply, error) => {
295
+ const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type); // Log error to cloud, as these can be extremely annoying to debug!
296
+
297
+ console.log("@webiny/handler");
298
+ console.log(JSON.stringify(error || {}));
299
+ const handler = (0, _middleware.middleware)(plugins.map(pl => {
300
+ return (context, error, next) => {
301
+ return pl.handle(context, error, next);
302
+ };
303
+ }));
304
+ await handler(app.webiny, error);
305
+ return reply.headers({
306
+ "Cache-Control": "no-store"
307
+ }).status(500);
308
+ });
309
+ return app;
310
+ };
311
+
312
+ exports.createHandler = createHandler;
package/fastify.js.map ADDED
@@ -0,0 +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","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","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, RouteTypes } 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 RouteTypes].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 RouteTypes;\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: ContextRoutes[\"defined\"] = {\n POST: [],\n GET: [],\n OPTIONS: [],\n DELETE: [],\n PATCH: [],\n PUT: [],\n HEAD: []\n };\n\n const throwOnDefinedRoute = (\n type: RouteTypes | \"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 RouteTypes];\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: RouteTypes, path: string): void => {\n const type = (inputType as string).toUpperCase() as RouteTypes;\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(JSON.stringify(error || {}));\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;;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C,CAE9C;;IACAuF,OAAO,CAACC,GAAR,CAAY,iBAAZ;IACAD,OAAO,CAACC,GAAR,CAAYC,IAAI,CAACC,SAAL,CAAeL,KAAK,IAAI,EAAxB,CAAZ;IACA,MAAM/C,OAAO,GAAG,IAAAqD,sBAAA,EACZrC,OAAO,CAACsC,GAAR,CAAYC,EAAE,IAAI;MACd,OAAO,CAACzC,OAAD,EAAmBiC,KAAnB,EAAiCS,IAAjC,KAAoD;QACvD,OAAOD,EAAE,CAACT,MAAH,CAAUhC,OAAV,EAAmBiC,KAAnB,EAA0BS,IAA1B,CAAP;MACH,CAFD;IAGH,CAJD,CADY,CAAhB;IAOA,MAAMxD,OAAO,CAACX,GAAG,CAACmC,MAAL,EAAauB,KAAb,CAAb;IAEA,OAAOjB,KAAK,CACPE,OADE,CACM;MACL,iBAAiB;IADZ,CADN,EAIFyB,MAJE,CAIK,GAJL,CAAP;EAKH,CAnBD;EAqBA,OAAOpE,GAAP;AACH,CAtOM"}
package/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- export { default as createHandler } from "./createHandler";
2
- export * from "./plugins/ContextPlugin";
3
- export * from "./plugins/Context";
4
- export * from "./plugins/HandlerPlugin";
1
+ export * from "./fastify";
2
+ export * from "@webiny/api/plugins/ContextPlugin";
3
+ export * from "./Context";
4
+ export * from "./plugins/EventPlugin";
5
+ export * from "./plugins/RoutePlugin";
6
+ export * from "./plugins/BeforeHandlerPlugin";
5
7
  export * from "./plugins/HandlerErrorPlugin";
6
8
  export * from "./plugins/HandlerResultPlugin";
7
- export * from "./plugins/BeforeHandlerPlugin";
package/index.js CHANGED
@@ -1,27 +1,26 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
- var _exportNames = {
9
- createHandler: true
10
- };
11
- Object.defineProperty(exports, "createHandler", {
12
- enumerable: true,
13
- get: function () {
14
- return _createHandler.default;
15
- }
16
- });
17
6
 
18
- var _createHandler = _interopRequireDefault(require("./createHandler"));
7
+ var _fastify = require("./fastify");
19
8
 
20
- var _ContextPlugin = require("./plugins/ContextPlugin");
9
+ Object.keys(_fastify).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _fastify[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _fastify[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ var _ContextPlugin = require("@webiny/api/plugins/ContextPlugin");
21
21
 
22
22
  Object.keys(_ContextPlugin).forEach(function (key) {
23
23
  if (key === "default" || key === "__esModule") return;
24
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
25
24
  if (key in exports && exports[key] === _ContextPlugin[key]) return;
26
25
  Object.defineProperty(exports, key, {
27
26
  enumerable: true,
@@ -31,11 +30,10 @@ Object.keys(_ContextPlugin).forEach(function (key) {
31
30
  });
32
31
  });
33
32
 
34
- var _Context = require("./plugins/Context");
33
+ var _Context = require("./Context");
35
34
 
36
35
  Object.keys(_Context).forEach(function (key) {
37
36
  if (key === "default" || key === "__esModule") return;
38
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
39
37
  if (key in exports && exports[key] === _Context[key]) return;
40
38
  Object.defineProperty(exports, key, {
41
39
  enumerable: true,
@@ -45,58 +43,67 @@ Object.keys(_Context).forEach(function (key) {
45
43
  });
46
44
  });
47
45
 
48
- var _HandlerPlugin = require("./plugins/HandlerPlugin");
46
+ var _EventPlugin = require("./plugins/EventPlugin");
49
47
 
50
- Object.keys(_HandlerPlugin).forEach(function (key) {
48
+ Object.keys(_EventPlugin).forEach(function (key) {
51
49
  if (key === "default" || key === "__esModule") return;
52
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
53
- if (key in exports && exports[key] === _HandlerPlugin[key]) return;
50
+ if (key in exports && exports[key] === _EventPlugin[key]) return;
54
51
  Object.defineProperty(exports, key, {
55
52
  enumerable: true,
56
53
  get: function () {
57
- return _HandlerPlugin[key];
54
+ return _EventPlugin[key];
58
55
  }
59
56
  });
60
57
  });
61
58
 
62
- var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
59
+ var _RoutePlugin = require("./plugins/RoutePlugin");
63
60
 
64
- Object.keys(_HandlerErrorPlugin).forEach(function (key) {
61
+ Object.keys(_RoutePlugin).forEach(function (key) {
65
62
  if (key === "default" || key === "__esModule") return;
66
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
67
- if (key in exports && exports[key] === _HandlerErrorPlugin[key]) return;
63
+ if (key in exports && exports[key] === _RoutePlugin[key]) return;
68
64
  Object.defineProperty(exports, key, {
69
65
  enumerable: true,
70
66
  get: function () {
71
- return _HandlerErrorPlugin[key];
67
+ return _RoutePlugin[key];
72
68
  }
73
69
  });
74
70
  });
75
71
 
76
- var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
72
+ var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
77
73
 
78
- Object.keys(_HandlerResultPlugin).forEach(function (key) {
74
+ Object.keys(_BeforeHandlerPlugin).forEach(function (key) {
79
75
  if (key === "default" || key === "__esModule") return;
80
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
81
- if (key in exports && exports[key] === _HandlerResultPlugin[key]) return;
76
+ if (key in exports && exports[key] === _BeforeHandlerPlugin[key]) return;
82
77
  Object.defineProperty(exports, key, {
83
78
  enumerable: true,
84
79
  get: function () {
85
- return _HandlerResultPlugin[key];
80
+ return _BeforeHandlerPlugin[key];
86
81
  }
87
82
  });
88
83
  });
89
84
 
90
- var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
85
+ var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
91
86
 
92
- Object.keys(_BeforeHandlerPlugin).forEach(function (key) {
87
+ Object.keys(_HandlerErrorPlugin).forEach(function (key) {
93
88
  if (key === "default" || key === "__esModule") return;
94
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
95
- if (key in exports && exports[key] === _BeforeHandlerPlugin[key]) return;
89
+ if (key in exports && exports[key] === _HandlerErrorPlugin[key]) return;
96
90
  Object.defineProperty(exports, key, {
97
91
  enumerable: true,
98
92
  get: function () {
99
- return _BeforeHandlerPlugin[key];
93
+ return _HandlerErrorPlugin[key];
94
+ }
95
+ });
96
+ });
97
+
98
+ var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
99
+
100
+ Object.keys(_HandlerResultPlugin).forEach(function (key) {
101
+ if (key === "default" || key === "__esModule") return;
102
+ if (key in exports && exports[key] === _HandlerResultPlugin[key]) return;
103
+ Object.defineProperty(exports, key, {
104
+ enumerable: true,
105
+ get: function () {
106
+ return _HandlerResultPlugin[key];
100
107
  }
101
108
  });
102
109
  });
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export { default as createHandler } from \"~/createHandler\";\nexport * from \"~/plugins/ContextPlugin\";\nexport * from \"~/plugins/Context\";\nexport * from \"~/plugins/HandlerPlugin\";\nexport * from \"~/plugins/HandlerErrorPlugin\";\nexport * from \"~/plugins/HandlerResultPlugin\";\nexport * from \"~/plugins/BeforeHandlerPlugin\";\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;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.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  /**
2
2
  * Compose a single middleware from the array of middleware functions
3
3
  */
4
- declare const _default: (functions?: Array<Function>) => Function;
5
- export default _default;
4
+ export declare const middleware: (functions?: Function[]) => Function;
package/middleware.js CHANGED
@@ -3,12 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.middleware = void 0;
7
7
 
8
8
  /**
9
9
  * Compose a single middleware from the array of middleware functions
10
10
  */
11
- var _default = (functions = []) => {
11
+ const middleware = (functions = []) => {
12
12
  return (...args) => {
13
13
  if (!functions.length) {
14
14
  return Promise.resolve();
@@ -48,4 +48,4 @@ var _default = (functions = []) => {
48
48
  };
49
49
  };
50
50
 
51
- exports.default = _default;
51
+ exports.middleware = middleware;
package/middleware.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["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 default (functions: Array<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;eACe,CAACA,SAA0B,GAAG,EAA9B,KAA+C;EAC1D,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,C"}
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"}