@webiny/handler 0.0.0-unstable.9bd236cf5e → 0.0.0-unstable.9f53ea597d
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 +10 -31
- package/Context.js.map +1 -1
- package/PreHandler/IPreHandler.d.ts +1 -1
- package/PreHandler/IPreHandler.js +5 -10
- package/PreHandler/IPreHandler.js.map +1 -1
- package/PreHandler/IfNotOptionsRequest.d.ts +3 -3
- package/PreHandler/IfNotOptionsRequest.js +12 -22
- package/PreHandler/IfNotOptionsRequest.js.map +1 -1
- package/PreHandler/IfOptionsRequest.d.ts +3 -3
- package/PreHandler/IfOptionsRequest.js +12 -22
- package/PreHandler/IfOptionsRequest.js.map +1 -1
- package/PreHandler/PreHandler.d.ts +3 -3
- package/PreHandler/PreHandler.js +11 -19
- package/PreHandler/PreHandler.js.map +1 -1
- package/PreHandler/ProcessBeforeHandlerPlugins.d.ts +4 -4
- package/PreHandler/ProcessBeforeHandlerPlugins.js +20 -26
- package/PreHandler/ProcessBeforeHandlerPlugins.js.map +1 -1
- package/PreHandler/ProcessContextPlugins.d.ts +4 -4
- package/PreHandler/ProcessContextPlugins.js +20 -26
- package/PreHandler/ProcessContextPlugins.js.map +1 -1
- package/PreHandler/ProcessHandlerOnRequestPlugins.d.ts +4 -4
- package/PreHandler/ProcessHandlerOnRequestPlugins.js +19 -27
- package/PreHandler/ProcessHandlerOnRequestPlugins.js.map +1 -1
- package/PreHandler/RegisterExtensions.d.ts +10 -0
- package/PreHandler/RegisterExtensions.js +13 -0
- package/PreHandler/RegisterExtensions.js.map +1 -0
- package/PreHandler/SendEarlyOptionsResponse.d.ts +3 -3
- package/PreHandler/SendEarlyOptionsResponse.js +21 -33
- package/PreHandler/SendEarlyOptionsResponse.js.map +1 -1
- package/PreHandler/SetDefaultHeaders.d.ts +3 -3
- package/PreHandler/SetDefaultHeaders.js +39 -57
- package/PreHandler/SetDefaultHeaders.js.map +1 -1
- package/README.md +10 -14
- package/ResponseHeaders.d.ts +0 -1
- package/ResponseHeaders.js +28 -38
- package/ResponseHeaders.js.map +1 -1
- package/abstractions/Reply.d.ts +5 -0
- package/abstractions/Reply.js +5 -0
- package/abstractions/Reply.js.map +1 -0
- package/abstractions/Request.d.ts +5 -0
- package/abstractions/Request.js +5 -0
- package/abstractions/Request.js.map +1 -0
- package/abstractions/Route.d.ts +25 -0
- package/abstractions/Route.js +30 -0
- package/abstractions/Route.js.map +1 -0
- package/exports/api.d.ts +1 -0
- package/exports/api.js +1 -0
- package/fastify.d.ts +5 -4
- package/fastify.js +308 -388
- package/fastify.js.map +1 -1
- package/index.d.ts +17 -13
- package/index.js +19 -151
- package/package.json +23 -20
- package/plugins/BeforeHandlerPlugin.d.ts +1 -1
- package/plugins/BeforeHandlerPlugin.js +14 -23
- package/plugins/BeforeHandlerPlugin.js.map +1 -1
- package/plugins/EventPlugin.d.ts +3 -3
- package/plugins/EventPlugin.js +11 -27
- package/plugins/EventPlugin.js.map +1 -1
- package/plugins/HandlerErrorPlugin.d.ts +1 -1
- package/plugins/HandlerErrorPlugin.js +14 -21
- package/plugins/HandlerErrorPlugin.js.map +1 -1
- package/plugins/HandlerOnRequestPlugin.d.ts +1 -1
- package/plugins/HandlerOnRequestPlugin.js +14 -28
- package/plugins/HandlerOnRequestPlugin.js.map +1 -1
- package/plugins/HandlerResultPlugin.d.ts +1 -1
- package/plugins/HandlerResultPlugin.js +14 -21
- package/plugins/HandlerResultPlugin.js.map +1 -1
- package/plugins/ModifyFastifyPlugin.d.ts +1 -1
- package/plugins/ModifyFastifyPlugin.js +14 -21
- package/plugins/ModifyFastifyPlugin.js.map +1 -1
- package/plugins/ModifyResponseHeadersPlugin.d.ts +3 -3
- package/plugins/ModifyResponseHeadersPlugin.js +14 -19
- package/plugins/ModifyResponseHeadersPlugin.js.map +1 -1
- package/plugins/OnRequestResponseSendPlugin.js +14 -37
- package/plugins/OnRequestResponseSendPlugin.js.map +1 -1
- package/plugins/OnRequestTimeoutPlugin.js +14 -21
- package/plugins/OnRequestTimeoutPlugin.js.map +1 -1
- package/plugins/RegisterExtensionPlugin.d.ts +12 -0
- package/plugins/RegisterExtensionPlugin.js +16 -0
- package/plugins/RegisterExtensionPlugin.js.map +1 -0
- package/plugins/RoutePlugin.d.ts +2 -2
- package/plugins/RoutePlugin.js +11 -18
- package/plugins/RoutePlugin.js.map +1 -1
- package/stringifyError.js +12 -24
- package/stringifyError.js.map +1 -1
- package/suppressPunycodeWarnings.d.ts +1 -4
- package/suppressPunycodeWarnings.js +3 -7
- package/suppressPunycodeWarnings.js.map +1 -1
- package/types.d.ts +3 -3
- package/types.js +1 -15
- package/index.js.map +0 -1
- package/types.js.map +0 -1
|
@@ -1,38 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.SendEarlyOptionsResponse = void 0;
|
|
7
|
-
var _IPreHandler = require("./IPreHandler");
|
|
8
|
-
var _ResponseHeaders = require("../ResponseHeaders");
|
|
1
|
+
import { Action } from "./IPreHandler.js";
|
|
2
|
+
import { ResponseHeaders } from "../ResponseHeaders.js";
|
|
9
3
|
class SendEarlyOptionsResponse {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
4
|
+
constructor(plugins){
|
|
5
|
+
this.plugins = plugins;
|
|
6
|
+
}
|
|
7
|
+
async execute(request, reply) {
|
|
8
|
+
if (reply.sent) {
|
|
9
|
+
console.error(JSON.stringify({
|
|
10
|
+
message: 'Output was already sent. Please check custom plugins of type "HandlerOnRequestPlugin".',
|
|
11
|
+
explanation: "This error can happen if the user plugin ended the reply, but did not return false as response."
|
|
12
|
+
}));
|
|
13
|
+
return Action.DONE;
|
|
14
|
+
}
|
|
15
|
+
const headers = ResponseHeaders.create(reply.getHeaders());
|
|
16
|
+
this.plugins.forEach((plugin)=>{
|
|
17
|
+
plugin.modify(request, headers);
|
|
18
|
+
});
|
|
19
|
+
reply.headers(headers.getHeaders());
|
|
20
|
+
reply.code(204).send("").hijack();
|
|
21
|
+
return Action.DONE;
|
|
26
22
|
}
|
|
27
|
-
const headers = _ResponseHeaders.ResponseHeaders.create(reply.getHeaders());
|
|
28
|
-
this.plugins.forEach(plugin => {
|
|
29
|
-
plugin.modify(request, headers);
|
|
30
|
-
});
|
|
31
|
-
reply.headers(headers.getHeaders());
|
|
32
|
-
reply.code(204).send("").hijack();
|
|
33
|
-
return _IPreHandler.Action.DONE;
|
|
34
|
-
}
|
|
35
23
|
}
|
|
36
|
-
|
|
24
|
+
export { SendEarlyOptionsResponse };
|
|
37
25
|
|
|
38
26
|
//# sourceMappingURL=SendEarlyOptionsResponse.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"PreHandler/SendEarlyOptionsResponse.js","sources":["../../src/PreHandler/SendEarlyOptionsResponse.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport type { IPreHandler } from \"~/PreHandler/IPreHandler.js\";\nimport { Action } from \"~/PreHandler/IPreHandler.js\";\nimport type { ModifyResponseHeadersPlugin } from \"~/plugins/ModifyResponseHeadersPlugin.js\";\nimport type { StandardHeaders } from \"~/ResponseHeaders.js\";\nimport { ResponseHeaders } from \"~/ResponseHeaders.js\";\n\nexport class SendEarlyOptionsResponse implements IPreHandler {\n private readonly plugins: ModifyResponseHeadersPlugin[];\n\n constructor(plugins: ModifyResponseHeadersPlugin[]) {\n this.plugins = plugins;\n }\n\n async execute(request: FastifyRequest, reply: FastifyReply): Promise<Action> {\n /**\n * IMPORTANT! Do not send anything if reply was already sent.\n */\n if (reply.sent) {\n /**\n * At this point throwing an exception will not do anything with the response. So just log it.\n */\n console.error(\n JSON.stringify({\n message: `Output was already sent. Please check custom plugins of type \"HandlerOnRequestPlugin\".`,\n explanation:\n \"This error can happen if the user plugin ended the reply, but did not return false as response.\"\n })\n );\n return Action.DONE;\n }\n\n const headers = ResponseHeaders.create(reply.getHeaders() as StandardHeaders);\n\n this.plugins.forEach(plugin => {\n plugin.modify(request, headers);\n });\n\n reply.headers(headers.getHeaders());\n\n reply.code(204).send(\"\").hijack();\n\n return Action.DONE;\n }\n}\n"],"names":["SendEarlyOptionsResponse","plugins","request","reply","console","JSON","Action","headers","ResponseHeaders","plugin"],"mappings":";;AAOO,MAAMA;IAGT,YAAYC,OAAsC,CAAE;QAChD,IAAI,CAAC,OAAO,GAAGA;IACnB;IAEA,MAAM,QAAQC,OAAuB,EAAEC,KAAmB,EAAmB;QAIzE,IAAIA,MAAM,IAAI,EAAE;YAIZC,QAAQ,KAAK,CACTC,KAAK,SAAS,CAAC;gBACX,SAAS;gBACT,aACI;YACR;YAEJ,OAAOC,OAAO,IAAI;QACtB;QAEA,MAAMC,UAAUC,gBAAgB,MAAM,CAACL,MAAM,UAAU;QAEvD,IAAI,CAAC,OAAO,CAAC,OAAO,CAACM,CAAAA;YACjBA,OAAO,MAAM,CAACP,SAASK;QAC3B;QAEAJ,MAAM,OAAO,CAACI,QAAQ,UAAU;QAEhCJ,MAAM,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,MAAM;QAE/B,OAAOG,OAAO,IAAI;IACtB;AACJ"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { FastifyReply, FastifyRequest } from "fastify";
|
|
2
|
-
import type { DefinedContextRoutes } from "../types";
|
|
3
|
-
import type { IPreHandler } from "
|
|
4
|
-
import { Action } from "
|
|
2
|
+
import type { DefinedContextRoutes } from "../types.js";
|
|
3
|
+
import type { IPreHandler } from "../PreHandler/IPreHandler.js";
|
|
4
|
+
import { Action } from "../PreHandler/IPreHandler.js";
|
|
5
5
|
export declare class SetDefaultHeaders implements IPreHandler {
|
|
6
6
|
private readonly definedRoutes;
|
|
7
7
|
constructor(definedRoutes: DefinedContextRoutes);
|
|
@@ -1,64 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.SetDefaultHeaders = void 0;
|
|
7
|
-
var _utils = require("@webiny/utils");
|
|
8
|
-
var _ResponseHeaders = require("../ResponseHeaders");
|
|
9
|
-
var _IPreHandler = require("./IPreHandler");
|
|
1
|
+
import { getWebinyVersionHeaders } from "@webiny/utils";
|
|
2
|
+
import { ResponseHeaders } from "../ResponseHeaders.js";
|
|
3
|
+
import { Action } from "./IPreHandler.js";
|
|
10
4
|
function createDefaultHeaders() {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
return ResponseHeaders.create({
|
|
6
|
+
"content-type": "application/json; charset=utf-8",
|
|
7
|
+
"cache-control": "no-store",
|
|
8
|
+
"access-control-allow-origin": "*",
|
|
9
|
+
"access-control-allow-headers": "*",
|
|
10
|
+
"access-control-allow-methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH",
|
|
11
|
+
...getWebinyVersionHeaders()
|
|
12
|
+
});
|
|
19
13
|
}
|
|
20
|
-
const getDefaultOptionsHeaders = ()
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const allowedMethods = keys.filter(type => {
|
|
38
|
-
if (!routes[type] || !Array.isArray(routes[type])) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
return routes[type].length > 0;
|
|
42
|
-
}).sort().join(",");
|
|
43
|
-
headers.set("access-control-allow-methods", allowedMethods);
|
|
44
|
-
}
|
|
45
|
-
return headers;
|
|
14
|
+
const getDefaultOptionsHeaders = ()=>ResponseHeaders.create({
|
|
15
|
+
"access-control-max-age": "86400",
|
|
16
|
+
"cache-control": "public, max-age=86400"
|
|
17
|
+
});
|
|
18
|
+
const getDefaultHeaders = (routes)=>{
|
|
19
|
+
const headers = createDefaultHeaders();
|
|
20
|
+
const keys = Object.keys(routes);
|
|
21
|
+
const all = keys.every((key)=>routes[key].length > 0);
|
|
22
|
+
if (all) headers.set("access-control-allow-methods", "*");
|
|
23
|
+
else {
|
|
24
|
+
const allowedMethods = keys.filter((type)=>{
|
|
25
|
+
if (!routes[type] || !Array.isArray(routes[type])) return false;
|
|
26
|
+
return routes[type].length > 0;
|
|
27
|
+
}).sort().join(",");
|
|
28
|
+
headers.set("access-control-allow-methods", allowedMethods);
|
|
29
|
+
}
|
|
30
|
+
return headers;
|
|
46
31
|
};
|
|
47
32
|
class SetDefaultHeaders {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
reply.headers(initialHeaders.getHeaders());
|
|
59
|
-
return _IPreHandler.Action.CONTINUE;
|
|
60
|
-
}
|
|
33
|
+
constructor(definedRoutes){
|
|
34
|
+
this.definedRoutes = definedRoutes;
|
|
35
|
+
}
|
|
36
|
+
execute(request, reply) {
|
|
37
|
+
const isOptionsRequest = "OPTIONS" === request.method;
|
|
38
|
+
const defaultHeaders = getDefaultHeaders(this.definedRoutes);
|
|
39
|
+
const initialHeaders = isOptionsRequest ? defaultHeaders.merge(getDefaultOptionsHeaders()) : defaultHeaders;
|
|
40
|
+
reply.headers(initialHeaders.getHeaders());
|
|
41
|
+
return Action.CONTINUE;
|
|
42
|
+
}
|
|
61
43
|
}
|
|
62
|
-
|
|
44
|
+
export { SetDefaultHeaders };
|
|
63
45
|
|
|
64
46
|
//# sourceMappingURL=SetDefaultHeaders.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"PreHandler/SetDefaultHeaders.js","sources":["../../src/PreHandler/SetDefaultHeaders.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport { getWebinyVersionHeaders } from \"@webiny/utils\";\nimport { ResponseHeaders } from \"~/ResponseHeaders.js\";\nimport type { DefinedContextRoutes, HTTPMethods } from \"~/types.js\";\nimport type { IPreHandler } from \"~/PreHandler/IPreHandler.js\";\nimport { Action } from \"~/PreHandler/IPreHandler.js\";\n\nfunction createDefaultHeaders() {\n return ResponseHeaders.create({\n \"content-type\": \"application/json; charset=utf-8\",\n \"cache-control\": \"no-store\",\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}\n\nconst getDefaultOptionsHeaders = () => {\n return ResponseHeaders.create({\n \"access-control-max-age\": \"86400\",\n \"cache-control\": \"public, max-age=86400\"\n });\n};\n\nconst getDefaultHeaders = (routes: DefinedContextRoutes): ResponseHeaders => {\n const headers = createDefaultHeaders();\n\n /**\n * If we are accepting all headers, just output that one.\n */\n const keys = Object.keys(routes) as HTTPMethods[];\n const all = keys.every(key => routes[key].length > 0);\n if (all) {\n headers.set(\"access-control-allow-methods\", \"*\");\n } else {\n const allowedMethods = keys\n .filter(type => {\n if (!routes[type] || !Array.isArray(routes[type])) {\n return false;\n }\n return routes[type].length > 0;\n })\n .sort()\n .join(\",\");\n\n headers.set(\"access-control-allow-methods\", allowedMethods);\n }\n\n return headers;\n};\n\nexport class SetDefaultHeaders implements IPreHandler {\n private readonly definedRoutes: DefinedContextRoutes;\n\n constructor(definedRoutes: DefinedContextRoutes) {\n this.definedRoutes = definedRoutes;\n }\n\n execute(request: FastifyRequest, reply: FastifyReply) {\n const isOptionsRequest = request.method === \"OPTIONS\";\n /**\n * Our default headers are always set. Users can override them.\n */\n const defaultHeaders = getDefaultHeaders(this.definedRoutes);\n\n const initialHeaders = isOptionsRequest\n ? defaultHeaders.merge(getDefaultOptionsHeaders())\n : defaultHeaders;\n\n reply.headers(initialHeaders.getHeaders());\n\n return Action.CONTINUE;\n }\n}\n"],"names":["createDefaultHeaders","ResponseHeaders","getWebinyVersionHeaders","getDefaultOptionsHeaders","getDefaultHeaders","routes","headers","keys","Object","all","key","allowedMethods","type","Array","SetDefaultHeaders","definedRoutes","request","reply","isOptionsRequest","defaultHeaders","initialHeaders","Action"],"mappings":";;;AAOA,SAASA;IACL,OAAOC,gBAAgB,MAAM,CAAC;QAC1B,gBAAgB;QAChB,iBAAiB;QACjB,+BAA+B;QAC/B,gCAAgC;QAChC,gCAAgC;QAChC,GAAGC,yBAAyB;IAChC;AACJ;AAEA,MAAMC,2BAA2B,IACtBF,gBAAgB,MAAM,CAAC;QAC1B,0BAA0B;QAC1B,iBAAiB;IACrB;AAGJ,MAAMG,oBAAoB,CAACC;IACvB,MAAMC,UAAUN;IAKhB,MAAMO,OAAOC,OAAO,IAAI,CAACH;IACzB,MAAMI,MAAMF,KAAK,KAAK,CAACG,CAAAA,MAAOL,MAAM,CAACK,IAAI,CAAC,MAAM,GAAG;IACnD,IAAID,KACAH,QAAQ,GAAG,CAAC,gCAAgC;SACzC;QACH,MAAMK,iBAAiBJ,KAClB,MAAM,CAACK,CAAAA;YACJ,IAAI,CAACP,MAAM,CAACO,KAAK,IAAI,CAACC,MAAM,OAAO,CAACR,MAAM,CAACO,KAAK,GAC5C,OAAO;YAEX,OAAOP,MAAM,CAACO,KAAK,CAAC,MAAM,GAAG;QACjC,GACC,IAAI,GACJ,IAAI,CAAC;QAEVN,QAAQ,GAAG,CAAC,gCAAgCK;IAChD;IAEA,OAAOL;AACX;AAEO,MAAMQ;IAGT,YAAYC,aAAmC,CAAE;QAC7C,IAAI,CAAC,aAAa,GAAGA;IACzB;IAEA,QAAQC,OAAuB,EAAEC,KAAmB,EAAE;QAClD,MAAMC,mBAAmBF,AAAmB,cAAnBA,QAAQ,MAAM;QAIvC,MAAMG,iBAAiBf,kBAAkB,IAAI,CAAC,aAAa;QAE3D,MAAMgB,iBAAiBF,mBACjBC,eAAe,KAAK,CAAChB,8BACrBgB;QAENF,MAAM,OAAO,CAACG,eAAe,UAAU;QAEvC,OAAOC,OAAO,QAAQ;IAC1B;AACJ"}
|
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,46 +1,36 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.ResponseHeaders = void 0;
|
|
7
|
-
// Extract known standard headers, and remove all non-string keys.
|
|
8
|
-
|
|
9
1
|
function isFunction(setter) {
|
|
10
|
-
|
|
2
|
+
return "function" == typeof setter;
|
|
11
3
|
}
|
|
12
4
|
class ResponseHeaders {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
constructor(initialHeaders){
|
|
6
|
+
this.headers = new Map();
|
|
7
|
+
if (initialHeaders) Object.keys(initialHeaders).forEach((key)=>{
|
|
8
|
+
this.headers.set(key, initialHeaders[key]);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
set(header, setter) {
|
|
12
|
+
if (isFunction(setter)) {
|
|
13
|
+
const previousValue = this.headers.get(header);
|
|
14
|
+
const newValue = setter(previousValue);
|
|
15
|
+
this.headers.set(header, newValue);
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
this.headers.set(header, setter);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
merge(headers) {
|
|
22
|
+
return ResponseHeaders.create({
|
|
23
|
+
...this.getHeaders(),
|
|
24
|
+
...headers.getHeaders()
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
getHeaders() {
|
|
28
|
+
return Object.fromEntries(this.headers);
|
|
19
29
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (isFunction(setter)) {
|
|
23
|
-
const previousValue = this.headers.get(header);
|
|
24
|
-
const newValue = setter(previousValue);
|
|
25
|
-
this.headers.set(header, newValue);
|
|
26
|
-
return this;
|
|
30
|
+
static create(initialHeaders) {
|
|
31
|
+
return new ResponseHeaders(initialHeaders);
|
|
27
32
|
}
|
|
28
|
-
this.headers.set(header, setter);
|
|
29
|
-
return this;
|
|
30
|
-
}
|
|
31
|
-
merge(headers) {
|
|
32
|
-
return ResponseHeaders.create({
|
|
33
|
-
...this.getHeaders(),
|
|
34
|
-
...headers.getHeaders()
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
getHeaders() {
|
|
38
|
-
return Object.fromEntries(this.headers);
|
|
39
|
-
}
|
|
40
|
-
static create(initialHeaders) {
|
|
41
|
-
return new ResponseHeaders(initialHeaders);
|
|
42
|
-
}
|
|
43
33
|
}
|
|
44
|
-
|
|
34
|
+
export { ResponseHeaders };
|
|
45
35
|
|
|
46
36
|
//# sourceMappingURL=ResponseHeaders.js.map
|
package/ResponseHeaders.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"ResponseHeaders.js","sources":["../src/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"],"names":["isFunction","setter","ResponseHeaders","initialHeaders","Map","Object","key","header","previousValue","newValue","headers"],"mappings":"AAsBA,SAASA,WAAcC,MAAe;IAClC,OAAO,AAAkB,cAAlB,OAAOA;AAClB;AAIO,MAAMC;IAGT,YAAoBC,cAAgC,CAAE;aAFrC,OAAO,GAAG,IAAIC;QAG3B,IAAID,gBACCE,OAAO,IAAI,CAACF,gBAAiD,OAAO,CAACG,CAAAA;YAClE,IAAI,CAAC,OAAO,CAAC,GAAG,CAACA,KAAKH,cAAc,CAACG,IAAI;QAC7C;IAER;IAEA,IAAqCC,MAAS,EAAEN,MAAkC,EAAE;QAChF,IAAID,WAA+BC,SAAS;YACxC,MAAMO,gBAAgB,IAAI,CAAC,OAAO,CAAC,GAAG,CAACD;YACvC,MAAME,WAAWR,OAAOO;YACxB,IAAI,CAAC,OAAO,CAAC,GAAG,CAACD,QAAQE;YACzB,OAAO,IAAI;QACf;QAEA,IAAI,CAAC,OAAO,CAAC,GAAG,CAACF,QAAQN;QAEzB,OAAO,IAAI;IACf;IAEA,MAAMS,OAAwB,EAAE;QAC5B,OAAOR,gBAAgB,MAAM,CAAC;YAAE,GAAG,IAAI,CAAC,UAAU,EAAE;YAAE,GAAGQ,QAAQ,UAAU,EAAE;QAAC;IAClF;IAEA,aAAa;QACT,OAAOL,OAAO,WAAW,CAAC,IAAI,CAAC,OAAO;IAC1C;IAEA,OAAO,OAAOF,cAAgC,EAAE;QAC5C,OAAO,IAAID,gBAAgBC;IAC/B;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstractions/Reply.js","sources":["../../src/abstractions/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"],"names":["Reply","createAbstraction"],"mappings":";AAGO,MAAMA,QAAQC,kBAA0B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstractions/Request.js","sources":["../../src/abstractions/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"],"names":["Request","createAbstraction"],"mappings":";AAGO,MAAMA,UAAUC,kBAA4B"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { FastifyRequest, FastifyReply } from "fastify";
|
|
2
|
+
export interface IRouteRequest {
|
|
3
|
+
body: unknown;
|
|
4
|
+
headers: Record<string, string | string[] | undefined>;
|
|
5
|
+
method: string;
|
|
6
|
+
url: string;
|
|
7
|
+
params: unknown;
|
|
8
|
+
query: unknown;
|
|
9
|
+
}
|
|
10
|
+
export interface IRouteReply {
|
|
11
|
+
code(statusCode: number): this;
|
|
12
|
+
send(data?: unknown): void;
|
|
13
|
+
header(key: string, value: unknown): this;
|
|
14
|
+
}
|
|
15
|
+
export interface IRoute {
|
|
16
|
+
execute(request: IRouteRequest, reply: IRouteReply): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export declare const Route: import("@webiny/di").Abstraction<IRoute>;
|
|
19
|
+
export declare namespace Route {
|
|
20
|
+
type Interface = IRoute;
|
|
21
|
+
type Request = IRouteRequest;
|
|
22
|
+
type Reply = IRouteReply;
|
|
23
|
+
}
|
|
24
|
+
export declare function toRouteRequest(req: FastifyRequest): IRouteRequest;
|
|
25
|
+
export declare function toRouteReply(reply: FastifyReply): IRouteReply;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createAbstraction } from "@webiny/feature/api";
|
|
2
|
+
const Route = createAbstraction("Route");
|
|
3
|
+
function toRouteRequest(req) {
|
|
4
|
+
return {
|
|
5
|
+
body: req.body,
|
|
6
|
+
headers: req.headers,
|
|
7
|
+
method: req.method,
|
|
8
|
+
url: req.url,
|
|
9
|
+
params: req.params ?? {},
|
|
10
|
+
query: req.query ?? {}
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function toRouteReply(reply) {
|
|
14
|
+
return {
|
|
15
|
+
code (statusCode) {
|
|
16
|
+
reply.code(statusCode);
|
|
17
|
+
return this;
|
|
18
|
+
},
|
|
19
|
+
send (data) {
|
|
20
|
+
reply.send(data);
|
|
21
|
+
},
|
|
22
|
+
header (key, value) {
|
|
23
|
+
reply.header(key, value);
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export { Route, toRouteReply, toRouteRequest };
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=Route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstractions/Route.js","sources":["../../src/abstractions/Route.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { FastifyRequest, FastifyReply } from \"fastify\";\n\nexport interface IRouteRequest {\n body: unknown;\n headers: Record<string, string | string[] | undefined>;\n method: string;\n url: string;\n params: unknown;\n query: unknown;\n}\n\nexport interface IRouteReply {\n code(statusCode: number): this;\n send(data?: unknown): void;\n header(key: string, value: unknown): this;\n}\n\nexport interface IRoute {\n execute(request: IRouteRequest, reply: IRouteReply): Promise<void>;\n}\n\nexport const Route = createAbstraction<IRoute>(\"Route\");\n\nexport namespace Route {\n export type Interface = IRoute;\n export type Request = IRouteRequest;\n export type Reply = IRouteReply;\n}\n\nexport function toRouteRequest(req: FastifyRequest): IRouteRequest {\n return {\n body: req.body,\n headers: req.headers as Record<string, string | string[] | undefined>,\n method: req.method,\n url: req.url,\n params: (req.params as Record<string, string>) ?? {},\n query: (req.query as Record<string, string | string[]>) ?? {}\n };\n}\n\nexport function toRouteReply(reply: FastifyReply): IRouteReply {\n return {\n code(statusCode) {\n reply.code(statusCode);\n return this;\n },\n send(data) {\n reply.send(data);\n },\n header(key, value) {\n reply.header(key, value as string);\n return this;\n }\n };\n}\n"],"names":["Route","createAbstraction","toRouteRequest","req","toRouteReply","reply","statusCode","data","key","value"],"mappings":";AAsBO,MAAMA,QAAQC,kBAA0B;AAQxC,SAASC,eAAeC,GAAmB;IAC9C,OAAO;QACH,MAAMA,IAAI,IAAI;QACd,SAASA,IAAI,OAAO;QACpB,QAAQA,IAAI,MAAM;QAClB,KAAKA,IAAI,GAAG;QACZ,QAASA,IAAI,MAAM,IAA+B,CAAC;QACnD,OAAQA,IAAI,KAAK,IAA0C,CAAC;IAChE;AACJ;AAEO,SAASC,aAAaC,KAAmB;IAC5C,OAAO;QACH,MAAKC,UAAU;YACXD,MAAM,IAAI,CAACC;YACX,OAAO,IAAI;QACf;QACA,MAAKC,IAAI;YACLF,MAAM,IAAI,CAACE;QACf;QACA,QAAOC,GAAG,EAAEC,KAAK;YACbJ,MAAM,MAAM,CAACG,KAAKC;YAClB,OAAO,IAAI;QACf;IACJ;AACJ"}
|
package/exports/api.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Route } from "../abstractions/Route.js";
|
package/exports/api.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Route } from "../abstractions/Route.js";
|
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
|
+
};
|