@webiny/handler 6.0.0-alpha.5 → 6.0.0-rc.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.
- package/Context.d.ts +1 -1
- package/Context.js +6 -14
- package/Context.js.map +1 -1
- package/PreHandler/IPreHandler.d.ts +1 -1
- package/PreHandler/IPreHandler.js +1 -7
- package/PreHandler/IPreHandler.js.map +1 -1
- package/PreHandler/IfNotOptionsRequest.d.ts +3 -3
- package/PreHandler/IfNotOptionsRequest.js +6 -13
- package/PreHandler/IfNotOptionsRequest.js.map +1 -1
- package/PreHandler/IfOptionsRequest.d.ts +3 -3
- package/PreHandler/IfOptionsRequest.js +6 -13
- package/PreHandler/IfOptionsRequest.js.map +1 -1
- package/PreHandler/PreHandler.d.ts +3 -3
- package/PreHandler/PreHandler.js +5 -12
- package/PreHandler/PreHandler.js.map +1 -1
- package/PreHandler/ProcessBeforeHandlerPlugins.d.ts +4 -4
- package/PreHandler/ProcessBeforeHandlerPlugins.js +5 -12
- package/PreHandler/ProcessBeforeHandlerPlugins.js.map +1 -1
- package/PreHandler/ProcessContextPlugins.d.ts +4 -4
- package/PreHandler/ProcessContextPlugins.js +5 -12
- package/PreHandler/ProcessContextPlugins.js.map +1 -1
- package/PreHandler/ProcessHandlerOnRequestPlugins.d.ts +4 -4
- package/PreHandler/ProcessHandlerOnRequestPlugins.js +6 -13
- package/PreHandler/ProcessHandlerOnRequestPlugins.js.map +1 -1
- package/PreHandler/SendEarlyOptionsResponse.d.ts +3 -3
- package/PreHandler/SendEarlyOptionsResponse.js +6 -13
- package/PreHandler/SendEarlyOptionsResponse.js.map +1 -1
- package/PreHandler/SetDefaultHeaders.d.ts +3 -3
- package/PreHandler/SetDefaultHeaders.js +8 -15
- package/PreHandler/SetDefaultHeaders.js.map +1 -1
- package/README.md +10 -14
- package/ResponseHeaders.d.ts +0 -1
- package/ResponseHeaders.js +1 -8
- package/ResponseHeaders.js.map +1 -1
- package/abstractions/Reply.d.ts +5 -0
- package/abstractions/Reply.js +4 -0
- package/abstractions/Reply.js.map +1 -0
- package/abstractions/Request.d.ts +5 -0
- package/abstractions/Request.js +4 -0
- package/abstractions/Request.js.map +1 -0
- package/fastify.d.ts +5 -4
- package/fastify.js +107 -65
- package/fastify.js.map +1 -1
- package/index.d.ts +15 -13
- package/index.js +18 -149
- package/index.js.map +1 -1
- package/package.json +16 -17
- package/plugins/BeforeHandlerPlugin.d.ts +1 -1
- package/plugins/BeforeHandlerPlugin.js +3 -11
- package/plugins/BeforeHandlerPlugin.js.map +1 -1
- package/plugins/EventPlugin.d.ts +3 -3
- package/plugins/EventPlugin.js +3 -12
- package/plugins/EventPlugin.js.map +1 -1
- package/plugins/HandlerErrorPlugin.d.ts +1 -1
- package/plugins/HandlerErrorPlugin.js +3 -11
- package/plugins/HandlerErrorPlugin.js.map +1 -1
- package/plugins/HandlerOnRequestPlugin.d.ts +1 -1
- package/plugins/HandlerOnRequestPlugin.js +3 -10
- package/plugins/HandlerOnRequestPlugin.js.map +1 -1
- package/plugins/HandlerResultPlugin.d.ts +1 -1
- package/plugins/HandlerResultPlugin.js +3 -11
- package/plugins/HandlerResultPlugin.js.map +1 -1
- package/plugins/ModifyFastifyPlugin.d.ts +1 -1
- package/plugins/ModifyFastifyPlugin.js +3 -11
- package/plugins/ModifyFastifyPlugin.js.map +1 -1
- package/plugins/ModifyResponseHeadersPlugin.d.ts +3 -3
- package/plugins/ModifyResponseHeadersPlugin.js +3 -11
- package/plugins/ModifyResponseHeadersPlugin.js.map +1 -1
- package/plugins/OnRequestResponseSendPlugin.js +3 -12
- package/plugins/OnRequestResponseSendPlugin.js.map +1 -1
- package/plugins/OnRequestTimeoutPlugin.js +3 -11
- package/plugins/OnRequestTimeoutPlugin.js.map +1 -1
- package/plugins/RoutePlugin.d.ts +2 -2
- package/plugins/RoutePlugin.js +3 -11
- package/plugins/RoutePlugin.js.map +1 -1
- package/stringifyError.js +1 -8
- package/stringifyError.js.map +1 -1
- package/suppressPunycodeWarnings.d.ts +1 -4
- package/suppressPunycodeWarnings.js +1 -2
- package/suppressPunycodeWarnings.js.map +1 -1
- package/types.d.ts +2 -2
- package/types.js +1 -13
- package/types.js.map +1 -1
package/README.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
# @webiny/handler
|
|
2
|
-
|
|
3
|
-
[!
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
Or if you prefer yarn:
|
|
13
|
-
```
|
|
14
|
-
yarn add @webiny/handler
|
|
15
|
-
```
|
|
2
|
+
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
|
|
5
|
+
> It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
|
|
6
|
+
|
|
7
|
+
📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
_This README file is automatically generated during the publish process._
|
package/ResponseHeaders.d.ts
CHANGED
package/ResponseHeaders.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.ResponseHeaders = void 0;
|
|
7
1
|
// Extract known standard headers, and remove all non-string keys.
|
|
8
2
|
|
|
9
3
|
function isFunction(setter) {
|
|
10
4
|
return typeof setter === "function";
|
|
11
5
|
}
|
|
12
|
-
class ResponseHeaders {
|
|
6
|
+
export class ResponseHeaders {
|
|
13
7
|
headers = new Map();
|
|
14
8
|
constructor(initialHeaders) {
|
|
15
9
|
if (initialHeaders) {
|
|
@@ -41,6 +35,5 @@ class ResponseHeaders {
|
|
|
41
35
|
return new ResponseHeaders(initialHeaders);
|
|
42
36
|
}
|
|
43
37
|
}
|
|
44
|
-
exports.ResponseHeaders = ResponseHeaders;
|
|
45
38
|
|
|
46
39
|
//# sourceMappingURL=ResponseHeaders.js.map
|
package/ResponseHeaders.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["isFunction","setter","ResponseHeaders","headers","Map","constructor","initialHeaders","Object","keys","forEach","key","set","header","previousValue","get","newValue","merge","create","getHeaders","fromEntries"
|
|
1
|
+
{"version":3,"names":["isFunction","setter","ResponseHeaders","headers","Map","constructor","initialHeaders","Object","keys","forEach","key","set","header","previousValue","get","newValue","merge","create","getHeaders","fromEntries"],"sources":["ResponseHeaders.ts"],"sourcesContent":["import type * as http from \"http\";\n\ntype ExtraHeaders = {\n \"content-type\"?: string | undefined;\n \"x-webiny-version\"?: http.OutgoingHttpHeader | undefined;\n};\n\ntype AllHeaders = http.OutgoingHttpHeaders & ExtraHeaders;\n\nexport type StandardHeaderValue = http.OutgoingHttpHeader | undefined;\n\n// Extract known standard headers, and remove all non-string keys.\nexport type StandardHeaders = {\n [K in keyof AllHeaders as string extends K\n ? never\n : number extends K\n ? never\n : K]: http.OutgoingHttpHeaders[K];\n} & {\n [name: string]: StandardHeaderValue;\n};\n\nfunction isFunction<T>(setter: unknown): setter is (value: T) => T {\n return typeof setter === \"function\";\n}\n\ntype Setter<T> = ((value: T) => T) | T;\n\nexport class ResponseHeaders {\n private readonly headers = new Map<keyof StandardHeaders, StandardHeaderValue>();\n\n private constructor(initialHeaders?: StandardHeaders) {\n if (initialHeaders) {\n (Object.keys(initialHeaders) as Array<keyof StandardHeaders>).forEach(key => {\n this.headers.set(key, initialHeaders[key]);\n });\n }\n }\n\n set<T extends keyof StandardHeaders>(header: T, setter: Setter<StandardHeaders[T]>) {\n if (isFunction<StandardHeaders[T]>(setter)) {\n const previousValue = this.headers.get(header) as StandardHeaders[T];\n const newValue = setter(previousValue);\n this.headers.set(header, newValue);\n return this;\n }\n\n this.headers.set(header, setter);\n\n return this;\n }\n\n merge(headers: ResponseHeaders) {\n return ResponseHeaders.create({ ...this.getHeaders(), ...headers.getHeaders() });\n }\n\n getHeaders() {\n return Object.fromEntries(this.headers);\n }\n\n static create(initialHeaders?: StandardHeaders) {\n return new ResponseHeaders(initialHeaders);\n }\n}\n"],"mappings":"AAWA;;AAWA,SAASA,UAAUA,CAAIC,MAAe,EAA6B;EAC/D,OAAO,OAAOA,MAAM,KAAK,UAAU;AACvC;AAIA,OAAO,MAAMC,eAAe,CAAC;EACRC,OAAO,GAAG,IAAIC,GAAG,CAA6C,CAAC;EAExEC,WAAWA,CAACC,cAAgC,EAAE;IAClD,IAAIA,cAAc,EAAE;MACfC,MAAM,CAACC,IAAI,CAACF,cAAc,CAAC,CAAkCG,OAAO,CAACC,GAAG,IAAI;QACzE,IAAI,CAACP,OAAO,CAACQ,GAAG,CAACD,GAAG,EAAEJ,cAAc,CAACI,GAAG,CAAC,CAAC;MAC9C,CAAC,CAAC;IACN;EACJ;EAEAC,GAAGA,CAAkCC,MAAS,EAAEX,MAAkC,EAAE;IAChF,IAAID,UAAU,CAAqBC,MAAM,CAAC,EAAE;MACxC,MAAMY,aAAa,GAAG,IAAI,CAACV,OAAO,CAACW,GAAG,CAACF,MAAM,CAAuB;MACpE,MAAMG,QAAQ,GAAGd,MAAM,CAACY,aAAa,CAAC;MACtC,IAAI,CAACV,OAAO,CAACQ,GAAG,CAACC,MAAM,EAAEG,QAAQ,CAAC;MAClC,OAAO,IAAI;IACf;IAEA,IAAI,CAACZ,OAAO,CAACQ,GAAG,CAACC,MAAM,EAAEX,MAAM,CAAC;IAEhC,OAAO,IAAI;EACf;EAEAe,KAAKA,CAACb,OAAwB,EAAE;IAC5B,OAAOD,eAAe,CAACe,MAAM,CAAC;MAAE,GAAG,IAAI,CAACC,UAAU,CAAC,CAAC;MAAE,GAAGf,OAAO,CAACe,UAAU,CAAC;IAAE,CAAC,CAAC;EACpF;EAEAA,UAAUA,CAAA,EAAG;IACT,OAAOX,MAAM,CAACY,WAAW,CAAC,IAAI,CAAChB,OAAO,CAAC;EAC3C;EAEA,OAAOc,MAAMA,CAACX,cAAgC,EAAE;IAC5C,OAAO,IAAIJ,eAAe,CAACI,cAAc,CAAC;EAC9C;AACJ","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createAbstraction","Reply"],"sources":["Reply.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Reply as IReply } from \"~/types.js\";\n\nexport const Reply = createAbstraction<IReply>(\"Reply\");\n\nexport namespace Reply {\n export type Interface = IReply;\n}\n"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,qBAAqB;AAGvD,OAAO,MAAMC,KAAK,GAAGD,iBAAiB,CAAS,OAAO,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createAbstraction","Request"],"sources":["Request.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Request as IRequest } from \"~/types.js\";\n\nexport const Request = createAbstraction<IRequest>(\"Request\");\n\nexport namespace Request {\n export type Interface = IRequest;\n}\n"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,qBAAqB;AAGvD,OAAO,MAAMC,OAAO,GAAGD,iBAAiB,CAAW,SAAS,CAAC","ignoreList":[]}
|
package/fastify.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import { PluginsContainer } from "@webiny/plugins/types";
|
|
1
|
+
import type { PluginCollection } from "@webiny/plugins/types.js";
|
|
2
|
+
import { PluginsContainer } from "@webiny/plugins/types.js";
|
|
4
3
|
import type { FastifyInstance, FastifyServerOptions as ServerOptions } from "fastify";
|
|
5
4
|
export interface CreateHandlerParams {
|
|
6
5
|
plugins: PluginCollection | PluginsContainer;
|
|
7
6
|
options?: ServerOptions;
|
|
8
7
|
debug?: boolean;
|
|
9
8
|
}
|
|
10
|
-
export declare const createHandler: (params: CreateHandlerParams) => FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault
|
|
9
|
+
export declare const createHandler: (params: CreateHandlerParams) => FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>> & {
|
|
10
|
+
__linterBrands: "SafePromiseLike";
|
|
11
|
+
};
|
package/fastify.js
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var _ProcessBeforeHandlerPlugins = require("./PreHandler/ProcessBeforeHandlerPlugins");
|
|
32
|
-
var _IfOptionsRequest = require("./PreHandler/IfOptionsRequest");
|
|
33
|
-
var _SendEarlyOptionsResponse = require("./PreHandler/SendEarlyOptionsResponse");
|
|
34
|
-
var _OnRequestTimeoutPlugin = require("./plugins/OnRequestTimeoutPlugin.js");
|
|
35
|
-
var _OnRequestResponseSendPlugin = require("./plugins/OnRequestResponseSendPlugin.js");
|
|
1
|
+
import { PluginsContainer } from "@webiny/plugins/types.js";
|
|
2
|
+
import fastify from "fastify";
|
|
3
|
+
import { middleware } from "@webiny/utils";
|
|
4
|
+
import { Context } from "./Context.js";
|
|
5
|
+
import WebinyError from "@webiny/error";
|
|
6
|
+
import { RoutePlugin } from "./plugins/RoutePlugin.js";
|
|
7
|
+
import { createHandlerClient } from "@webiny/handler-client";
|
|
8
|
+
import fastifyCookie from "@fastify/cookie";
|
|
9
|
+
import fastifyCompress from "@fastify/compress";
|
|
10
|
+
import { ContextPlugin } from "@webiny/api";
|
|
11
|
+
import { BeforeHandlerPlugin } from "./plugins/BeforeHandlerPlugin.js";
|
|
12
|
+
import { HandlerResultPlugin } from "./plugins/HandlerResultPlugin.js";
|
|
13
|
+
import { HandlerErrorPlugin } from "./plugins/HandlerErrorPlugin.js";
|
|
14
|
+
import { ModifyFastifyPlugin } from "./plugins/ModifyFastifyPlugin.js";
|
|
15
|
+
import { HandlerOnRequestPlugin } from "./plugins/HandlerOnRequestPlugin.js";
|
|
16
|
+
import { ResponseHeaders } from "./ResponseHeaders.js";
|
|
17
|
+
import { ModifyResponseHeadersPlugin } from "./plugins/ModifyResponseHeadersPlugin.js";
|
|
18
|
+
import { SetDefaultHeaders } from "./PreHandler/SetDefaultHeaders.js";
|
|
19
|
+
import { PreHandler } from "./PreHandler/PreHandler.js";
|
|
20
|
+
import { stringifyError } from "./stringifyError.js";
|
|
21
|
+
import { ProcessHandlerOnRequestPlugins } from "./PreHandler/ProcessHandlerOnRequestPlugins.js";
|
|
22
|
+
import { ProcessContextPlugins } from "./PreHandler/ProcessContextPlugins.js";
|
|
23
|
+
import { IfNotOptionsRequest } from "./PreHandler/IfNotOptionsRequest.js";
|
|
24
|
+
import { ProcessBeforeHandlerPlugins } from "./PreHandler/ProcessBeforeHandlerPlugins.js";
|
|
25
|
+
import { IfOptionsRequest } from "./PreHandler/IfOptionsRequest.js";
|
|
26
|
+
import { SendEarlyOptionsResponse } from "./PreHandler/SendEarlyOptionsResponse.js";
|
|
27
|
+
import { OnRequestTimeoutPlugin } from "./plugins/OnRequestTimeoutPlugin.js";
|
|
28
|
+
import { OnRequestResponseSendPlugin } from "./plugins/OnRequestResponseSendPlugin.js";
|
|
29
|
+
import { Request } from "./abstractions/Request.js";
|
|
30
|
+
import { Reply } from "./abstractions/Reply.js";
|
|
36
31
|
const modifyResponseHeaders = (app, request, reply) => {
|
|
37
|
-
const modifyHeaders = app.webiny.plugins.byType(
|
|
32
|
+
const modifyHeaders = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
|
|
38
33
|
const replyHeaders = reply.getHeaders();
|
|
39
|
-
const headers =
|
|
34
|
+
const headers = ResponseHeaders.create(replyHeaders);
|
|
40
35
|
modifyHeaders.forEach(plugin => {
|
|
41
36
|
plugin.modify(request, headers);
|
|
42
37
|
});
|
|
43
|
-
|
|
38
|
+
|
|
39
|
+
// Exclude 'set-cookie' header to avoid duplication.
|
|
40
|
+
// Cookies are managed by @fastify/cookie and calling reply.headers() with 'set-cookie' duplicates them.
|
|
41
|
+
const headersToSet = headers.getHeaders();
|
|
42
|
+
delete headersToSet["set-cookie"];
|
|
43
|
+
reply.headers(headersToSet);
|
|
44
44
|
};
|
|
45
|
-
const createHandler = params => {
|
|
45
|
+
export const createHandler = params => {
|
|
46
46
|
const definedRoutes = {
|
|
47
47
|
POST: [],
|
|
48
48
|
GET: [],
|
|
@@ -75,7 +75,7 @@ const createHandler = params => {
|
|
|
75
75
|
}
|
|
76
76
|
console.error(`Error while registering onAll route. One of the routes is already defined.`);
|
|
77
77
|
console.error(JSON.stringify(all));
|
|
78
|
-
throw new
|
|
78
|
+
throw new WebinyError(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
|
|
79
79
|
type,
|
|
80
80
|
path
|
|
81
81
|
});
|
|
@@ -85,7 +85,7 @@ const createHandler = params => {
|
|
|
85
85
|
return;
|
|
86
86
|
}
|
|
87
87
|
console.error(`Error while trying to override route: [${type}] ${path}`);
|
|
88
|
-
throw new
|
|
88
|
+
throw new WebinyError(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
|
|
89
89
|
type,
|
|
90
90
|
path
|
|
91
91
|
});
|
|
@@ -103,10 +103,11 @@ const createHandler = params => {
|
|
|
103
103
|
/**
|
|
104
104
|
* We must attach the server to our internal context if we want to have it accessible.
|
|
105
105
|
*/
|
|
106
|
-
const app = (
|
|
106
|
+
const app = fastify({
|
|
107
107
|
bodyLimit: 536870912,
|
|
108
108
|
// 512MB
|
|
109
109
|
disableRequestLogging: true,
|
|
110
|
+
allowErrorHandlerOverride: true,
|
|
110
111
|
...(params.options || {})
|
|
111
112
|
});
|
|
112
113
|
|
|
@@ -132,7 +133,7 @@ const createHandler = params => {
|
|
|
132
133
|
*
|
|
133
134
|
* https://github.com/fastify/fastify-cookie
|
|
134
135
|
*/
|
|
135
|
-
app.register(
|
|
136
|
+
app.register(fastifyCookie, {
|
|
136
137
|
parseOptions: {} // options for parsing cookies
|
|
137
138
|
});
|
|
138
139
|
/**
|
|
@@ -140,7 +141,7 @@ const createHandler = params => {
|
|
|
140
141
|
*
|
|
141
142
|
* https://github.com/fastify/fastify-compress
|
|
142
143
|
*/
|
|
143
|
-
app.register(
|
|
144
|
+
app.register(fastifyCompress, {
|
|
144
145
|
global: true,
|
|
145
146
|
threshold: 1024,
|
|
146
147
|
onUnsupportedEncoding: (encoding, _, reply) => {
|
|
@@ -188,15 +189,15 @@ const createHandler = params => {
|
|
|
188
189
|
}
|
|
189
190
|
};
|
|
190
191
|
let context;
|
|
191
|
-
const plugins = new
|
|
192
|
+
const plugins = new PluginsContainer([
|
|
192
193
|
/**
|
|
193
194
|
* We must have handlerClient by default.
|
|
194
195
|
* And it must be one of the first context plugins applied.
|
|
195
196
|
*/
|
|
196
|
-
|
|
197
|
+
createHandlerClient()]);
|
|
197
198
|
plugins.merge(params.plugins || []);
|
|
198
199
|
try {
|
|
199
|
-
context = new
|
|
200
|
+
context = new Context({
|
|
200
201
|
plugins,
|
|
201
202
|
/**
|
|
202
203
|
* Inserted via webpack at build time.
|
|
@@ -206,7 +207,7 @@ const createHandler = params => {
|
|
|
206
207
|
});
|
|
207
208
|
} catch (ex) {
|
|
208
209
|
console.error(`Error while constructing the Context.`);
|
|
209
|
-
console.error(
|
|
210
|
+
console.error(stringifyError(ex));
|
|
210
211
|
throw ex;
|
|
211
212
|
}
|
|
212
213
|
|
|
@@ -215,6 +216,26 @@ const createHandler = params => {
|
|
|
215
216
|
*/
|
|
216
217
|
app.decorate("webiny", context);
|
|
217
218
|
|
|
219
|
+
/**
|
|
220
|
+
* To prevent Unsupported Media Type errors on OPTIONS requests with a body,
|
|
221
|
+
* we need to have a custom parser
|
|
222
|
+
*/
|
|
223
|
+
app.addContentTypeParser("application/json", {
|
|
224
|
+
parseAs: "string",
|
|
225
|
+
bodyLimit: 1024 * 1024
|
|
226
|
+
}, (req, body, done) => {
|
|
227
|
+
if (req.method === "OPTIONS") {
|
|
228
|
+
done(null, undefined);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
const json = typeof body === "string" ? body : body.toString("utf8");
|
|
233
|
+
done(null, JSON.parse(json));
|
|
234
|
+
} catch (err) {
|
|
235
|
+
done(err);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
|
|
218
239
|
/**
|
|
219
240
|
* With this we ensure that an undefined request body is not parsed on OPTIONS requests,
|
|
220
241
|
* in case there's a `content-type` header set for whatever reason.
|
|
@@ -238,20 +259,26 @@ const createHandler = params => {
|
|
|
238
259
|
app.addHook("preHandler", async (request, reply) => {
|
|
239
260
|
app.webiny.request = request;
|
|
240
261
|
app.webiny.reply = reply;
|
|
262
|
+
|
|
263
|
+
// Bind request and reply to DI container for runtime access
|
|
264
|
+
if (app.webiny.container) {
|
|
265
|
+
app.webiny.container.registerInstance(Request, request);
|
|
266
|
+
app.webiny.container.registerInstance(Reply, reply);
|
|
267
|
+
}
|
|
241
268
|
/**
|
|
242
269
|
* Default code to 200 - so we do not need to set it again.
|
|
243
270
|
* Usually we set errors manually when we use reply.send.
|
|
244
271
|
*/
|
|
245
272
|
reply.code(200);
|
|
246
|
-
const handlerOnRequestPlugins = app.webiny.plugins.byType(
|
|
247
|
-
const contextPlugins = app.webiny.plugins.byType(
|
|
248
|
-
const beforeHandlerPlugins = app.webiny.plugins.byType(
|
|
249
|
-
const modifyHeadersPlugins = app.webiny.plugins.byType(
|
|
250
|
-
const preHandler = new
|
|
273
|
+
const handlerOnRequestPlugins = app.webiny.plugins.byType(HandlerOnRequestPlugin.type);
|
|
274
|
+
const contextPlugins = app.webiny.plugins.byType(ContextPlugin.type);
|
|
275
|
+
const beforeHandlerPlugins = app.webiny.plugins.byType(BeforeHandlerPlugin.type);
|
|
276
|
+
const modifyHeadersPlugins = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
|
|
277
|
+
const preHandler = new PreHandler([new SetDefaultHeaders(definedRoutes), new ProcessHandlerOnRequestPlugins(handlerOnRequestPlugins), new IfNotOptionsRequest([new ProcessContextPlugins(app.webiny, contextPlugins), new ProcessBeforeHandlerPlugins(app.webiny, beforeHandlerPlugins)]), new IfOptionsRequest([new SendEarlyOptionsResponse(modifyHeadersPlugins)])]);
|
|
251
278
|
await preHandler.execute(request, reply, app.webiny);
|
|
252
279
|
});
|
|
253
280
|
app.addHook("preSerialization", async (_, __, payload) => {
|
|
254
|
-
const plugins = app.webiny.plugins.byType(
|
|
281
|
+
const plugins = app.webiny.plugins.byType(HandlerResultPlugin.type);
|
|
255
282
|
let name;
|
|
256
283
|
try {
|
|
257
284
|
for (const plugin of plugins) {
|
|
@@ -260,7 +287,7 @@ const createHandler = params => {
|
|
|
260
287
|
}
|
|
261
288
|
} catch (ex) {
|
|
262
289
|
console.error(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
|
|
263
|
-
console.error(
|
|
290
|
+
console.error(stringifyError(ex));
|
|
264
291
|
throw ex;
|
|
265
292
|
}
|
|
266
293
|
return payload;
|
|
@@ -273,6 +300,22 @@ const createHandler = params => {
|
|
|
273
300
|
console.warn("Reply already sent, cannot send the result (handler:setErrorHandler).");
|
|
274
301
|
return reply;
|
|
275
302
|
}
|
|
303
|
+
if (error.code?.startsWith("Authentication/")) {
|
|
304
|
+
return reply.status(401).headers({
|
|
305
|
+
"Cache-Control": "no-store"
|
|
306
|
+
}).send(JSON.stringify({
|
|
307
|
+
message: error.message,
|
|
308
|
+
code: error.code
|
|
309
|
+
}));
|
|
310
|
+
}
|
|
311
|
+
if (error.code === "Tenancy/TenantDisabled") {
|
|
312
|
+
return reply.status(503).headers({
|
|
313
|
+
"Cache-Control": "no-store"
|
|
314
|
+
}).send(JSON.stringify({
|
|
315
|
+
message: error.message,
|
|
316
|
+
code: error.code
|
|
317
|
+
}));
|
|
318
|
+
}
|
|
276
319
|
return reply.status(500).headers({
|
|
277
320
|
"Cache-Control": "no-store"
|
|
278
321
|
}).send(
|
|
@@ -286,13 +329,13 @@ const createHandler = params => {
|
|
|
286
329
|
}));
|
|
287
330
|
});
|
|
288
331
|
app.addHook("onError", async (_, reply, error) => {
|
|
289
|
-
const plugins = app.webiny.plugins.byType(
|
|
332
|
+
const plugins = app.webiny.plugins.byType(HandlerErrorPlugin.type);
|
|
290
333
|
/**
|
|
291
334
|
* Log error to cloud, as these can be extremely annoying to debug!
|
|
292
335
|
*/
|
|
293
336
|
console.error("Logging error in @webiny/handler");
|
|
294
337
|
try {
|
|
295
|
-
console.error(
|
|
338
|
+
console.error(stringifyError(error));
|
|
296
339
|
} catch (ex) {
|
|
297
340
|
console.warn("Could not stringify error:");
|
|
298
341
|
console.log(error);
|
|
@@ -316,7 +359,7 @@ const createHandler = params => {
|
|
|
316
359
|
} else {
|
|
317
360
|
console.warn("Reply already sent, cannot send the result (handler:addHook:onError).");
|
|
318
361
|
}
|
|
319
|
-
const handler =
|
|
362
|
+
const handler = middleware(plugins.map(pl => {
|
|
320
363
|
return (context, error, next) => {
|
|
321
364
|
return pl.handle(context, error, next);
|
|
322
365
|
};
|
|
@@ -330,7 +373,7 @@ const createHandler = params => {
|
|
|
330
373
|
*/
|
|
331
374
|
app.addHook("onSend", async (request, reply, input) => {
|
|
332
375
|
modifyResponseHeaders(app, request, reply);
|
|
333
|
-
const plugins = app.webiny.plugins.byType(
|
|
376
|
+
const plugins = app.webiny.plugins.byType(OnRequestResponseSendPlugin.type);
|
|
334
377
|
let payload = input;
|
|
335
378
|
for (const plugin of plugins) {
|
|
336
379
|
payload = await plugin.exec(request, reply, payload);
|
|
@@ -345,7 +388,7 @@ const createHandler = params => {
|
|
|
345
388
|
await context.benchmark.output();
|
|
346
389
|
});
|
|
347
390
|
app.addHook("onTimeout", async (request, reply) => {
|
|
348
|
-
const plugins = app.webiny.plugins.byType(
|
|
391
|
+
const plugins = app.webiny.plugins.byType(OnRequestTimeoutPlugin.type);
|
|
349
392
|
for (const plugin of plugins) {
|
|
350
393
|
await plugin.exec(request, reply);
|
|
351
394
|
}
|
|
@@ -355,7 +398,7 @@ const createHandler = params => {
|
|
|
355
398
|
/**
|
|
356
399
|
* With these plugins we give users possibility to do anything they want on our fastify instance.
|
|
357
400
|
*/
|
|
358
|
-
const modifyPlugins = app.webiny.plugins.byType(
|
|
401
|
+
const modifyPlugins = app.webiny.plugins.byType(ModifyFastifyPlugin.type);
|
|
359
402
|
let modifyFastifyPluginName;
|
|
360
403
|
try {
|
|
361
404
|
for (const plugin of modifyPlugins) {
|
|
@@ -364,7 +407,7 @@ const createHandler = params => {
|
|
|
364
407
|
}
|
|
365
408
|
} catch (ex) {
|
|
366
409
|
console.error(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
|
|
367
|
-
console.error(
|
|
410
|
+
console.error(stringifyError(ex));
|
|
368
411
|
throw ex;
|
|
369
412
|
}
|
|
370
413
|
|
|
@@ -375,7 +418,7 @@ const createHandler = params => {
|
|
|
375
418
|
*
|
|
376
419
|
* Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
|
|
377
420
|
*/
|
|
378
|
-
const routePlugins = app.webiny.plugins.byType(
|
|
421
|
+
const routePlugins = app.webiny.plugins.byType(RoutePlugin.type);
|
|
379
422
|
|
|
380
423
|
/**
|
|
381
424
|
* Add routes to the system.
|
|
@@ -391,11 +434,10 @@ const createHandler = params => {
|
|
|
391
434
|
}
|
|
392
435
|
} catch (ex) {
|
|
393
436
|
console.error(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
|
|
394
|
-
console.error(
|
|
437
|
+
console.error(stringifyError(ex));
|
|
395
438
|
throw ex;
|
|
396
439
|
}
|
|
397
440
|
return app;
|
|
398
441
|
};
|
|
399
|
-
exports.createHandler = createHandler;
|
|
400
442
|
|
|
401
443
|
//# sourceMappingURL=fastify.js.map
|