@webiny/handler 0.0.0-unstable.1e66d121db → 0.0.0-unstable.611c5af35e
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 +2 -6
- package/Context.js.map +1 -1
- package/fastify.js +6 -58
- package/fastify.js.map +1 -1
- package/index.js +0 -16
- package/index.js.map +1 -1
- package/middleware.js +2 -8
- package/middleware.js.map +1 -1
- package/package.json +11 -11
- package/plugins/BeforeHandlerPlugin.js +0 -10
- package/plugins/BeforeHandlerPlugin.js.map +1 -1
- package/plugins/EventPlugin.js +1 -8
- package/plugins/EventPlugin.js.map +1 -1
- package/plugins/HandlerErrorPlugin.js +0 -9
- package/plugins/HandlerErrorPlugin.js.map +1 -1
- package/plugins/HandlerResultPlugin.js +0 -9
- package/plugins/HandlerResultPlugin.js.map +1 -1
- package/plugins/RoutePlugin.js +0 -8
- package/plugins/RoutePlugin.js.map +1 -1
- package/types.js +0 -1
- package/types.js.map +1 -1
package/Context.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.Context = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _api = require("@webiny/api");
|
|
13
|
-
|
|
14
10
|
class Context extends _api.Context {
|
|
15
11
|
// @ts-ignore
|
|
12
|
+
|
|
16
13
|
// @ts-ignore
|
|
14
|
+
|
|
17
15
|
constructor(params) {
|
|
18
16
|
super(params);
|
|
19
17
|
(0, _defineProperty2.default)(this, "server", void 0);
|
|
@@ -23,7 +21,5 @@ class Context extends _api.Context {
|
|
|
23
21
|
this.server = params.server;
|
|
24
22
|
this.routes = params.routes;
|
|
25
23
|
}
|
|
26
|
-
|
|
27
24
|
}
|
|
28
|
-
|
|
29
25
|
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":"
|
|
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"}
|
package/fastify.js
CHANGED
|
@@ -1,38 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createHandler = void 0;
|
|
9
|
-
|
|
10
8
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
-
|
|
12
9
|
var _fastify = _interopRequireDefault(require("fastify"));
|
|
13
|
-
|
|
14
10
|
var _utils = require("@webiny/utils");
|
|
15
|
-
|
|
16
11
|
var _Context = require("./Context");
|
|
17
|
-
|
|
18
12
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
19
|
-
|
|
20
13
|
var _RoutePlugin = require("./plugins/RoutePlugin");
|
|
21
|
-
|
|
22
14
|
var _handlerClient = require("@webiny/handler-client");
|
|
23
|
-
|
|
24
15
|
var _cookie = _interopRequireDefault(require("@fastify/cookie"));
|
|
25
|
-
|
|
26
16
|
var _middleware = require("./middleware");
|
|
27
|
-
|
|
28
17
|
var _api = require("@webiny/api");
|
|
29
|
-
|
|
30
18
|
var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
|
|
31
|
-
|
|
32
19
|
var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
|
|
33
|
-
|
|
34
20
|
var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
|
|
35
|
-
|
|
36
21
|
const DEFAULT_HEADERS = (0, _objectSpread2.default)({
|
|
37
22
|
"Cache-Control": "no-store",
|
|
38
23
|
"Content-Type": "application/json; charset=utf-8",
|
|
@@ -40,38 +25,31 @@ const DEFAULT_HEADERS = (0, _objectSpread2.default)({
|
|
|
40
25
|
"Access-Control-Allow-Headers": "*",
|
|
41
26
|
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH"
|
|
42
27
|
}, (0, _utils.getWebinyVersionHeaders)());
|
|
43
|
-
|
|
44
28
|
const getDefaultHeaders = routes => {
|
|
45
29
|
/**
|
|
46
30
|
* If we are accepting all headers, just output that one.
|
|
47
31
|
*/
|
|
48
32
|
const keys = Object.keys(routes);
|
|
49
33
|
const all = keys.every(key => routes[key].length > 0);
|
|
50
|
-
|
|
51
34
|
if (all) {
|
|
52
35
|
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
53
36
|
"Access-Control-Allow-Methods": "*"
|
|
54
37
|
});
|
|
55
38
|
}
|
|
56
|
-
|
|
57
39
|
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
58
40
|
"Access-Control-Allow-Methods": keys.filter(key => {
|
|
59
41
|
const type = key;
|
|
60
|
-
|
|
61
42
|
if (!routes[type] || Array.isArray(routes[type]) === false) {
|
|
62
43
|
return false;
|
|
63
44
|
}
|
|
64
|
-
|
|
65
45
|
return routes[type].length > 0;
|
|
66
46
|
}).sort().join(",")
|
|
67
47
|
});
|
|
68
48
|
};
|
|
69
|
-
|
|
70
49
|
const OPTIONS_HEADERS = {
|
|
71
50
|
"Access-Control-Max-Age": "86400",
|
|
72
51
|
"Cache-Control": "public, max-age=86400"
|
|
73
52
|
};
|
|
74
|
-
|
|
75
53
|
const createHandler = params => {
|
|
76
54
|
const definedRoutes = {
|
|
77
55
|
POST: [],
|
|
@@ -91,18 +69,15 @@ const createHandler = params => {
|
|
|
91
69
|
TRACE: [],
|
|
92
70
|
UNLOCK: []
|
|
93
71
|
};
|
|
94
|
-
|
|
95
72
|
const throwOnDefinedRoute = (type, path, options) => {
|
|
96
73
|
if (type === "ALL") {
|
|
97
74
|
const all = Object.keys(definedRoutes).some(key => {
|
|
98
75
|
const routes = definedRoutes[key];
|
|
99
76
|
return routes.includes(path);
|
|
100
77
|
});
|
|
101
|
-
|
|
102
78
|
if (!all) {
|
|
103
79
|
return;
|
|
104
80
|
}
|
|
105
|
-
|
|
106
81
|
throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
|
|
107
82
|
type,
|
|
108
83
|
path
|
|
@@ -112,59 +87,46 @@ const createHandler = params => {
|
|
|
112
87
|
} else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
|
|
113
88
|
return;
|
|
114
89
|
}
|
|
115
|
-
|
|
116
90
|
throw new _error.default(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
|
|
117
91
|
type,
|
|
118
92
|
path
|
|
119
93
|
});
|
|
120
94
|
};
|
|
121
|
-
|
|
122
95
|
const addDefinedRoute = (inputType, path) => {
|
|
123
96
|
const type = inputType.toUpperCase();
|
|
124
|
-
|
|
125
97
|
if (!definedRoutes[type]) {
|
|
126
98
|
return;
|
|
127
99
|
} else if (definedRoutes[type].includes(path)) {
|
|
128
100
|
return;
|
|
129
101
|
}
|
|
130
|
-
|
|
131
102
|
definedRoutes[type].push(path);
|
|
132
103
|
};
|
|
133
104
|
/**
|
|
134
105
|
* We must attach the server to our internal context if we want to have it accessible.
|
|
135
106
|
*/
|
|
136
|
-
|
|
137
|
-
|
|
138
107
|
const app = (0, _fastify.default)((0, _objectSpread2.default)({}, params.options || {}));
|
|
139
108
|
/**
|
|
140
109
|
* We need to register routes in our system so we can output headers later on and dissallow overriding routes.
|
|
141
110
|
*/
|
|
142
|
-
|
|
143
111
|
app.addHook("onRoute", route => {
|
|
144
112
|
const method = route.method;
|
|
145
|
-
|
|
146
113
|
if (Array.isArray(method)) {
|
|
147
114
|
for (const m of method) {
|
|
148
115
|
addDefinedRoute(m, route.path);
|
|
149
116
|
}
|
|
150
|
-
|
|
151
117
|
return;
|
|
152
118
|
}
|
|
153
|
-
|
|
154
119
|
addDefinedRoute(method, route.path);
|
|
155
120
|
});
|
|
156
121
|
/**
|
|
157
122
|
*
|
|
158
123
|
*/
|
|
159
|
-
|
|
160
124
|
app.register(_cookie.default, {
|
|
161
125
|
parseOptions: {} // options for parsing cookies
|
|
162
|
-
|
|
163
126
|
});
|
|
164
127
|
/**
|
|
165
128
|
* Route helpers - mostly for users.
|
|
166
129
|
*/
|
|
167
|
-
|
|
168
130
|
const routes = {
|
|
169
131
|
defined: definedRoutes,
|
|
170
132
|
onPost: (path, handler, options) => {
|
|
@@ -207,7 +169,6 @@ const createHandler = params => {
|
|
|
207
169
|
* And it must be one of the first context plugins applied.
|
|
208
170
|
*/
|
|
209
171
|
(0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
|
|
210
|
-
|
|
211
172
|
/**
|
|
212
173
|
* Inserted via webpack on build time.
|
|
213
174
|
*/
|
|
@@ -218,8 +179,8 @@ const createHandler = params => {
|
|
|
218
179
|
/**
|
|
219
180
|
* We are attaching our custom context to webiny variable on the fastify app so it is accessible everywhere
|
|
220
181
|
*/
|
|
221
|
-
|
|
222
182
|
app.decorate("webiny", context);
|
|
183
|
+
|
|
223
184
|
/**
|
|
224
185
|
* We have few types of triggers:
|
|
225
186
|
* * Events - EventPlugin
|
|
@@ -227,44 +188,37 @@ const createHandler = params => {
|
|
|
227
188
|
*
|
|
228
189
|
* Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
|
|
229
190
|
*/
|
|
230
|
-
|
|
231
191
|
const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
|
|
192
|
+
|
|
232
193
|
/**
|
|
233
194
|
* Add routes to the system.
|
|
234
195
|
*/
|
|
235
|
-
|
|
236
196
|
for (const plugin of routePlugins) {
|
|
237
197
|
plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
|
|
238
198
|
context: app.webiny
|
|
239
199
|
}));
|
|
240
200
|
}
|
|
201
|
+
|
|
241
202
|
/**
|
|
242
203
|
* On every request we add default headers, which can be changed later.
|
|
243
204
|
* Also, if it is an options request, we skip everything after this hook and output options headers.
|
|
244
205
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
206
|
app.addHook("onRequest", async (request, reply) => {
|
|
248
207
|
const defaultHeaders = getDefaultHeaders(definedRoutes);
|
|
249
208
|
reply.headers(defaultHeaders);
|
|
250
|
-
|
|
251
209
|
if (request.method !== "OPTIONS") {
|
|
252
210
|
return;
|
|
253
211
|
}
|
|
254
|
-
|
|
255
212
|
const raw = reply.code(204).hijack().raw;
|
|
256
213
|
const headers = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS);
|
|
257
|
-
|
|
258
214
|
for (const key in headers) {
|
|
259
215
|
raw.setHeader(key, headers[key]);
|
|
260
216
|
}
|
|
261
|
-
|
|
262
217
|
raw.end("");
|
|
263
218
|
});
|
|
264
219
|
app.addHook("preParsing", async request => {
|
|
265
220
|
app.webiny.request = request;
|
|
266
221
|
const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
|
|
267
|
-
|
|
268
222
|
for (const plugin of plugins) {
|
|
269
223
|
await plugin.apply(app.webiny);
|
|
270
224
|
}
|
|
@@ -272,32 +226,27 @@ const createHandler = params => {
|
|
|
272
226
|
/**
|
|
273
227
|
*
|
|
274
228
|
*/
|
|
275
|
-
|
|
276
229
|
app.addHook("preHandler", async () => {
|
|
277
230
|
const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
|
|
278
|
-
|
|
279
231
|
for (const plugin of plugins) {
|
|
280
232
|
await plugin.apply(app.webiny);
|
|
281
233
|
}
|
|
282
234
|
});
|
|
235
|
+
|
|
283
236
|
/**
|
|
284
237
|
*
|
|
285
238
|
*/
|
|
286
|
-
|
|
287
239
|
const preSerialization = async (_, __, payload) => {
|
|
288
240
|
const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
|
|
289
|
-
|
|
290
241
|
for (const plugin of plugins) {
|
|
291
242
|
await plugin.handle(app.webiny, payload);
|
|
292
243
|
}
|
|
293
|
-
|
|
294
244
|
return payload;
|
|
295
245
|
};
|
|
296
|
-
|
|
297
246
|
app.addHook("preSerialization", preSerialization);
|
|
298
247
|
app.addHook("onError", async (_, reply, error) => {
|
|
299
|
-
const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
|
|
300
|
-
|
|
248
|
+
const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
|
|
249
|
+
// Log error to cloud, as these can be extremely annoying to debug!
|
|
301
250
|
console.log("@webiny/handler");
|
|
302
251
|
console.log(JSON.stringify((0, _objectSpread2.default)((0, _objectSpread2.default)({}, error || {}), {}, {
|
|
303
252
|
message: error === null || error === void 0 ? void 0 : error.message,
|
|
@@ -315,5 +264,4 @@ const createHandler = params => {
|
|
|
315
264
|
});
|
|
316
265
|
return app;
|
|
317
266
|
};
|
|
318
|
-
|
|
319
267
|
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,iBAAiB,UADwB;EAEzC,gBAAgB,iCAFyB;EAGzC,+BAA+B,G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IAC9C,MAAM/B,OAAO,GAAG3B,GAAG,CAACmC,MAAJ,CAAWR,OAAX,CAAmBS,MAAnB,CAA8CuB,sCAAA,CAAmB/F,IAAjE,CAAhB,CAD8C,CAE9C;;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"}
|
|
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;\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OAAOmD,CAAC,EAAEZ,KAAK,EAAEiB,KAAK,KAAK;IAC9C,MAAM/B,OAAO,GAAG3B,GAAG,CAACmC,MAAM,CAACR,OAAO,CAACS,MAAM,CAAqBuB,sCAAkB,CAAC/F,IAAI,CAAC;IACtF;IACAgG,OAAO,CAACC,GAAG,CAAC,iBAAiB,CAAC;IAC9BD,OAAO,CAACC,GAAG,CACPC,IAAI,CAACC,SAAS,6DACNL,KAAK,IAAI,CAAC,CAAC;MACfM,OAAO,EAAEN,KAAK,aAALA,KAAK,uBAALA,KAAK,CAAEM,OAAO;MACvBnB,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"}
|
package/index.js
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
7
6
|
var _fastify = require("./fastify");
|
|
8
|
-
|
|
9
7
|
Object.keys(_fastify).forEach(function (key) {
|
|
10
8
|
if (key === "default" || key === "__esModule") return;
|
|
11
9
|
if (key in exports && exports[key] === _fastify[key]) return;
|
|
@@ -16,9 +14,7 @@ Object.keys(_fastify).forEach(function (key) {
|
|
|
16
14
|
}
|
|
17
15
|
});
|
|
18
16
|
});
|
|
19
|
-
|
|
20
17
|
var _ContextPlugin = require("@webiny/api/plugins/ContextPlugin");
|
|
21
|
-
|
|
22
18
|
Object.keys(_ContextPlugin).forEach(function (key) {
|
|
23
19
|
if (key === "default" || key === "__esModule") return;
|
|
24
20
|
if (key in exports && exports[key] === _ContextPlugin[key]) return;
|
|
@@ -29,9 +25,7 @@ Object.keys(_ContextPlugin).forEach(function (key) {
|
|
|
29
25
|
}
|
|
30
26
|
});
|
|
31
27
|
});
|
|
32
|
-
|
|
33
28
|
var _Context = require("./Context");
|
|
34
|
-
|
|
35
29
|
Object.keys(_Context).forEach(function (key) {
|
|
36
30
|
if (key === "default" || key === "__esModule") return;
|
|
37
31
|
if (key in exports && exports[key] === _Context[key]) return;
|
|
@@ -42,9 +36,7 @@ Object.keys(_Context).forEach(function (key) {
|
|
|
42
36
|
}
|
|
43
37
|
});
|
|
44
38
|
});
|
|
45
|
-
|
|
46
39
|
var _EventPlugin = require("./plugins/EventPlugin");
|
|
47
|
-
|
|
48
40
|
Object.keys(_EventPlugin).forEach(function (key) {
|
|
49
41
|
if (key === "default" || key === "__esModule") return;
|
|
50
42
|
if (key in exports && exports[key] === _EventPlugin[key]) return;
|
|
@@ -55,9 +47,7 @@ Object.keys(_EventPlugin).forEach(function (key) {
|
|
|
55
47
|
}
|
|
56
48
|
});
|
|
57
49
|
});
|
|
58
|
-
|
|
59
50
|
var _RoutePlugin = require("./plugins/RoutePlugin");
|
|
60
|
-
|
|
61
51
|
Object.keys(_RoutePlugin).forEach(function (key) {
|
|
62
52
|
if (key === "default" || key === "__esModule") return;
|
|
63
53
|
if (key in exports && exports[key] === _RoutePlugin[key]) return;
|
|
@@ -68,9 +58,7 @@ Object.keys(_RoutePlugin).forEach(function (key) {
|
|
|
68
58
|
}
|
|
69
59
|
});
|
|
70
60
|
});
|
|
71
|
-
|
|
72
61
|
var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
|
|
73
|
-
|
|
74
62
|
Object.keys(_BeforeHandlerPlugin).forEach(function (key) {
|
|
75
63
|
if (key === "default" || key === "__esModule") return;
|
|
76
64
|
if (key in exports && exports[key] === _BeforeHandlerPlugin[key]) return;
|
|
@@ -81,9 +69,7 @@ Object.keys(_BeforeHandlerPlugin).forEach(function (key) {
|
|
|
81
69
|
}
|
|
82
70
|
});
|
|
83
71
|
});
|
|
84
|
-
|
|
85
72
|
var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
|
|
86
|
-
|
|
87
73
|
Object.keys(_HandlerErrorPlugin).forEach(function (key) {
|
|
88
74
|
if (key === "default" || key === "__esModule") return;
|
|
89
75
|
if (key in exports && exports[key] === _HandlerErrorPlugin[key]) return;
|
|
@@ -94,9 +80,7 @@ Object.keys(_HandlerErrorPlugin).forEach(function (key) {
|
|
|
94
80
|
}
|
|
95
81
|
});
|
|
96
82
|
});
|
|
97
|
-
|
|
98
83
|
var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
|
|
99
|
-
|
|
100
84
|
Object.keys(_HandlerResultPlugin).forEach(function (key) {
|
|
101
85
|
if (key === "default" || key === "__esModule") return;
|
|
102
86
|
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":"
|
|
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,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.middleware = void 0;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* Compose a single middleware from the array of middleware functions
|
|
10
9
|
*/
|
|
@@ -12,22 +11,19 @@ const middleware = (functions = []) => {
|
|
|
12
11
|
return (...args) => {
|
|
13
12
|
if (!functions.length) {
|
|
14
13
|
return Promise.resolve();
|
|
15
|
-
}
|
|
16
|
-
|
|
14
|
+
}
|
|
17
15
|
|
|
16
|
+
// Create a clone of function chain to prevent modifying the original array with `shift()`
|
|
18
17
|
const chain = [...functions];
|
|
19
18
|
return new Promise((parentResolve, parentReject) => {
|
|
20
19
|
const next = async () => {
|
|
21
20
|
const fn = chain.shift();
|
|
22
|
-
|
|
23
21
|
if (!fn) {
|
|
24
22
|
return Promise.resolve();
|
|
25
23
|
}
|
|
26
|
-
|
|
27
24
|
return new Promise(async (resolve, reject) => {
|
|
28
25
|
try {
|
|
29
26
|
const result = await fn(...args, resolve);
|
|
30
|
-
|
|
31
27
|
if (typeof result !== "undefined") {
|
|
32
28
|
return parentResolve(result);
|
|
33
29
|
}
|
|
@@ -42,10 +38,8 @@ const middleware = (functions = []) => {
|
|
|
42
38
|
parentReject(e);
|
|
43
39
|
});
|
|
44
40
|
};
|
|
45
|
-
|
|
46
41
|
return next();
|
|
47
42
|
});
|
|
48
43
|
};
|
|
49
44
|
};
|
|
50
|
-
|
|
51
45
|
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":"
|
|
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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/handler",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.611c5af35e",
|
|
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.
|
|
15
|
+
"@babel/runtime": "7.20.1",
|
|
16
16
|
"@fastify/cookie": "8.3.0",
|
|
17
|
-
"@webiny/api": "0.0.0-unstable.
|
|
18
|
-
"@webiny/error": "0.0.0-unstable.
|
|
19
|
-
"@webiny/handler-client": "0.0.0-unstable.
|
|
20
|
-
"@webiny/plugins": "0.0.0-unstable.
|
|
21
|
-
"@webiny/utils": "0.0.0-unstable.
|
|
22
|
-
"fastify": "4.
|
|
17
|
+
"@webiny/api": "0.0.0-unstable.611c5af35e",
|
|
18
|
+
"@webiny/error": "0.0.0-unstable.611c5af35e",
|
|
19
|
+
"@webiny/handler-client": "0.0.0-unstable.611c5af35e",
|
|
20
|
+
"@webiny/plugins": "0.0.0-unstable.611c5af35e",
|
|
21
|
+
"@webiny/utils": "0.0.0-unstable.611c5af35e",
|
|
22
|
+
"fastify": "4.10.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.
|
|
30
|
-
"@webiny/project-utils": "^0.0.0-unstable.
|
|
29
|
+
"@webiny/cli": "^0.0.0-unstable.611c5af35e",
|
|
30
|
+
"@webiny/project-utils": "^0.0.0-unstable.611c5af35e",
|
|
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": "
|
|
45
|
+
"gitHead": "cdeed09317c2c0e07cb0d4d88f9ac38a214ddc22"
|
|
46
46
|
}
|
|
@@ -1,38 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createBeforeHandlerPlugin = exports.BeforeHandlerPlugin = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _plugins = require("@webiny/plugins");
|
|
13
|
-
|
|
14
10
|
class BeforeHandlerPlugin extends _plugins.Plugin {
|
|
15
11
|
constructor(callable) {
|
|
16
12
|
super();
|
|
17
13
|
(0, _defineProperty2.default)(this, "_callable", void 0);
|
|
18
14
|
this._callable = callable;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
async apply(context) {
|
|
22
17
|
if (typeof this._callable !== "function") {
|
|
23
18
|
throw Error(`Missing callable in BeforeHandlerPlugin! Either pass a callable to plugin constructor or extend the plugin and override the "apply" method.`);
|
|
24
19
|
}
|
|
25
|
-
|
|
26
20
|
return this._callable(context);
|
|
27
21
|
}
|
|
28
|
-
|
|
29
22
|
}
|
|
30
|
-
|
|
31
23
|
exports.BeforeHandlerPlugin = BeforeHandlerPlugin;
|
|
32
24
|
(0, _defineProperty2.default)(BeforeHandlerPlugin, "type", "before-handler");
|
|
33
|
-
|
|
34
25
|
const createBeforeHandlerPlugin = callable => {
|
|
35
26
|
return new BeforeHandlerPlugin(callable);
|
|
36
27
|
};
|
|
37
|
-
|
|
38
28
|
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":"
|
|
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"}
|
package/plugins/EventPlugin.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createEvent = exports.EventPlugin = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _Plugin = require("@webiny/plugins/Plugin");
|
|
13
|
-
|
|
14
10
|
/**
|
|
15
11
|
* EventPlugin must be handled in the package which implements fastify for certain cloud.
|
|
16
12
|
* There is no standard input for AWS Lambda, Google Cloud Functions and MS Azure Functions so we let that
|
|
@@ -19,20 +15,17 @@ var _Plugin = require("@webiny/plugins/Plugin");
|
|
|
19
15
|
* Note that only one EventPlugin can be defined per fastify initialisation.
|
|
20
16
|
* If more is needed, check ~/fastify.ts and implement that possibility.
|
|
21
17
|
*/
|
|
18
|
+
|
|
22
19
|
class EventPlugin extends _Plugin.Plugin {
|
|
23
20
|
constructor(cb) {
|
|
24
21
|
super();
|
|
25
22
|
(0, _defineProperty2.default)(this, "cb", void 0);
|
|
26
23
|
this.cb = cb;
|
|
27
24
|
}
|
|
28
|
-
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
exports.EventPlugin = EventPlugin;
|
|
32
27
|
(0, _defineProperty2.default)(EventPlugin, "type", "handler.fastify.event");
|
|
33
|
-
|
|
34
28
|
const createEvent = cb => {
|
|
35
29
|
return new EventPlugin(cb);
|
|
36
30
|
};
|
|
37
|
-
|
|
38
31
|
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":"
|
|
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,34 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createHandlerErrorPlugin = exports.HandlerErrorPlugin = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _plugins = require("@webiny/plugins");
|
|
13
|
-
|
|
14
10
|
class HandlerErrorPlugin extends _plugins.Plugin {
|
|
15
11
|
constructor(callable) {
|
|
16
12
|
super();
|
|
17
13
|
(0, _defineProperty2.default)(this, "_callable", void 0);
|
|
18
14
|
this._callable = callable;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
async handle(context, error, next) {
|
|
22
17
|
return this._callable(context, error, next);
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
}
|
|
26
|
-
|
|
27
20
|
exports.HandlerErrorPlugin = HandlerErrorPlugin;
|
|
28
21
|
(0, _defineProperty2.default)(HandlerErrorPlugin, "type", "handler-error");
|
|
29
|
-
|
|
30
22
|
const createHandlerErrorPlugin = callable => {
|
|
31
23
|
return new HandlerErrorPlugin(callable);
|
|
32
24
|
};
|
|
33
|
-
|
|
34
25
|
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":"
|
|
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,34 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createHandlerResultPlugin = exports.HandlerResultPlugin = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _plugins = require("@webiny/plugins");
|
|
13
|
-
|
|
14
10
|
class HandlerResultPlugin extends _plugins.Plugin {
|
|
15
11
|
constructor(callable) {
|
|
16
12
|
super();
|
|
17
13
|
(0, _defineProperty2.default)(this, "_callable", void 0);
|
|
18
14
|
this._callable = callable;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
async handle(context, result) {
|
|
22
17
|
return this._callable(context, result);
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
}
|
|
26
|
-
|
|
27
20
|
exports.HandlerResultPlugin = HandlerResultPlugin;
|
|
28
21
|
(0, _defineProperty2.default)(HandlerResultPlugin, "type", "handler-result");
|
|
29
|
-
|
|
30
22
|
const createHandlerResultPlugin = callable => {
|
|
31
23
|
return new HandlerResultPlugin(callable);
|
|
32
24
|
};
|
|
33
|
-
|
|
34
25
|
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":"
|
|
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"}
|
package/plugins/RoutePlugin.js
CHANGED
|
@@ -1,30 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createRoute = exports.RoutePlugin = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _Plugin = require("@webiny/plugins/Plugin");
|
|
13
|
-
|
|
14
10
|
class RoutePlugin extends _Plugin.Plugin {
|
|
15
11
|
constructor(cb) {
|
|
16
12
|
super();
|
|
17
13
|
(0, _defineProperty2.default)(this, "cb", void 0);
|
|
18
14
|
this.cb = cb;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
}
|
|
22
|
-
|
|
23
17
|
exports.RoutePlugin = RoutePlugin;
|
|
24
18
|
(0, _defineProperty2.default)(RoutePlugin, "type", "handler.fastify.route");
|
|
25
|
-
|
|
26
19
|
const createRoute = cb => {
|
|
27
20
|
return new RoutePlugin(cb);
|
|
28
21
|
};
|
|
29
|
-
|
|
30
22
|
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":"
|
|
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"}
|
package/types.js
CHANGED
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":"
|
|
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"}
|