@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.
Files changed (94) hide show
  1. package/Context.d.ts +1 -1
  2. package/Context.js +10 -31
  3. package/Context.js.map +1 -1
  4. package/PreHandler/IPreHandler.d.ts +1 -1
  5. package/PreHandler/IPreHandler.js +5 -10
  6. package/PreHandler/IPreHandler.js.map +1 -1
  7. package/PreHandler/IfNotOptionsRequest.d.ts +3 -3
  8. package/PreHandler/IfNotOptionsRequest.js +12 -22
  9. package/PreHandler/IfNotOptionsRequest.js.map +1 -1
  10. package/PreHandler/IfOptionsRequest.d.ts +3 -3
  11. package/PreHandler/IfOptionsRequest.js +12 -22
  12. package/PreHandler/IfOptionsRequest.js.map +1 -1
  13. package/PreHandler/PreHandler.d.ts +3 -3
  14. package/PreHandler/PreHandler.js +11 -19
  15. package/PreHandler/PreHandler.js.map +1 -1
  16. package/PreHandler/ProcessBeforeHandlerPlugins.d.ts +4 -4
  17. package/PreHandler/ProcessBeforeHandlerPlugins.js +20 -26
  18. package/PreHandler/ProcessBeforeHandlerPlugins.js.map +1 -1
  19. package/PreHandler/ProcessContextPlugins.d.ts +4 -4
  20. package/PreHandler/ProcessContextPlugins.js +20 -26
  21. package/PreHandler/ProcessContextPlugins.js.map +1 -1
  22. package/PreHandler/ProcessHandlerOnRequestPlugins.d.ts +4 -4
  23. package/PreHandler/ProcessHandlerOnRequestPlugins.js +19 -27
  24. package/PreHandler/ProcessHandlerOnRequestPlugins.js.map +1 -1
  25. package/PreHandler/RegisterExtensions.d.ts +10 -0
  26. package/PreHandler/RegisterExtensions.js +13 -0
  27. package/PreHandler/RegisterExtensions.js.map +1 -0
  28. package/PreHandler/SendEarlyOptionsResponse.d.ts +3 -3
  29. package/PreHandler/SendEarlyOptionsResponse.js +21 -33
  30. package/PreHandler/SendEarlyOptionsResponse.js.map +1 -1
  31. package/PreHandler/SetDefaultHeaders.d.ts +3 -3
  32. package/PreHandler/SetDefaultHeaders.js +39 -57
  33. package/PreHandler/SetDefaultHeaders.js.map +1 -1
  34. package/README.md +10 -14
  35. package/ResponseHeaders.d.ts +0 -1
  36. package/ResponseHeaders.js +28 -38
  37. package/ResponseHeaders.js.map +1 -1
  38. package/abstractions/Reply.d.ts +5 -0
  39. package/abstractions/Reply.js +5 -0
  40. package/abstractions/Reply.js.map +1 -0
  41. package/abstractions/Request.d.ts +5 -0
  42. package/abstractions/Request.js +5 -0
  43. package/abstractions/Request.js.map +1 -0
  44. package/abstractions/Route.d.ts +25 -0
  45. package/abstractions/Route.js +30 -0
  46. package/abstractions/Route.js.map +1 -0
  47. package/exports/api.d.ts +1 -0
  48. package/exports/api.js +1 -0
  49. package/fastify.d.ts +5 -4
  50. package/fastify.js +308 -388
  51. package/fastify.js.map +1 -1
  52. package/index.d.ts +17 -13
  53. package/index.js +19 -151
  54. package/package.json +23 -20
  55. package/plugins/BeforeHandlerPlugin.d.ts +1 -1
  56. package/plugins/BeforeHandlerPlugin.js +14 -23
  57. package/plugins/BeforeHandlerPlugin.js.map +1 -1
  58. package/plugins/EventPlugin.d.ts +3 -3
  59. package/plugins/EventPlugin.js +11 -27
  60. package/plugins/EventPlugin.js.map +1 -1
  61. package/plugins/HandlerErrorPlugin.d.ts +1 -1
  62. package/plugins/HandlerErrorPlugin.js +14 -21
  63. package/plugins/HandlerErrorPlugin.js.map +1 -1
  64. package/plugins/HandlerOnRequestPlugin.d.ts +1 -1
  65. package/plugins/HandlerOnRequestPlugin.js +14 -28
  66. package/plugins/HandlerOnRequestPlugin.js.map +1 -1
  67. package/plugins/HandlerResultPlugin.d.ts +1 -1
  68. package/plugins/HandlerResultPlugin.js +14 -21
  69. package/plugins/HandlerResultPlugin.js.map +1 -1
  70. package/plugins/ModifyFastifyPlugin.d.ts +1 -1
  71. package/plugins/ModifyFastifyPlugin.js +14 -21
  72. package/plugins/ModifyFastifyPlugin.js.map +1 -1
  73. package/plugins/ModifyResponseHeadersPlugin.d.ts +3 -3
  74. package/plugins/ModifyResponseHeadersPlugin.js +14 -19
  75. package/plugins/ModifyResponseHeadersPlugin.js.map +1 -1
  76. package/plugins/OnRequestResponseSendPlugin.js +14 -37
  77. package/plugins/OnRequestResponseSendPlugin.js.map +1 -1
  78. package/plugins/OnRequestTimeoutPlugin.js +14 -21
  79. package/plugins/OnRequestTimeoutPlugin.js.map +1 -1
  80. package/plugins/RegisterExtensionPlugin.d.ts +12 -0
  81. package/plugins/RegisterExtensionPlugin.js +16 -0
  82. package/plugins/RegisterExtensionPlugin.js.map +1 -0
  83. package/plugins/RoutePlugin.d.ts +2 -2
  84. package/plugins/RoutePlugin.js +11 -18
  85. package/plugins/RoutePlugin.js.map +1 -1
  86. package/stringifyError.js +12 -24
  87. package/stringifyError.js.map +1 -1
  88. package/suppressPunycodeWarnings.d.ts +1 -4
  89. package/suppressPunycodeWarnings.js +3 -7
  90. package/suppressPunycodeWarnings.js.map +1 -1
  91. package/types.d.ts +3 -3
  92. package/types.js +1 -15
  93. package/index.js.map +0 -1
  94. package/types.js.map +0 -1
@@ -1,38 +1,26 @@
1
- "use strict";
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
- constructor(plugins) {
11
- this.plugins = plugins;
12
- }
13
- async execute(request, reply) {
14
- /**
15
- * IMPORTANT! Do not send anything if reply was already sent.
16
- */
17
- if (reply.sent) {
18
- /**
19
- * At this point throwing an exception will not do anything with the response. So just log it.
20
- */
21
- console.error(JSON.stringify({
22
- message: `Output was already sent. Please check custom plugins of type "HandlerOnRequestPlugin".`,
23
- explanation: "This error can happen if the user plugin ended the reply, but did not return false as response."
24
- }));
25
- return _IPreHandler.Action.DONE;
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
- exports.SendEarlyOptionsResponse = SendEarlyOptionsResponse;
24
+ export { SendEarlyOptionsResponse };
37
25
 
38
26
  //# sourceMappingURL=SendEarlyOptionsResponse.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_IPreHandler","require","_ResponseHeaders","SendEarlyOptionsResponse","constructor","plugins","execute","request","reply","sent","console","error","JSON","stringify","message","explanation","Action","DONE","headers","ResponseHeaders","create","getHeaders","forEach","plugin","modify","code","send","hijack","exports"],"sources":["SendEarlyOptionsResponse.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport type { IPreHandler } from \"~/PreHandler/IPreHandler\";\nimport { Action } from \"~/PreHandler/IPreHandler\";\nimport type { ModifyResponseHeadersPlugin } from \"~/plugins/ModifyResponseHeadersPlugin\";\nimport type { StandardHeaders } from \"~/ResponseHeaders\";\nimport { ResponseHeaders } from \"~/ResponseHeaders\";\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"],"mappings":";;;;;;AAEA,IAAAA,YAAA,GAAAC,OAAA;AAGA,IAAAC,gBAAA,GAAAD,OAAA;AAEO,MAAME,wBAAwB,CAAwB;EAGzDC,WAAWA,CAACC,OAAsC,EAAE;IAChD,IAAI,CAACA,OAAO,GAAGA,OAAO;EAC1B;EAEA,MAAMC,OAAOA,CAACC,OAAuB,EAAEC,KAAmB,EAAmB;IACzE;AACR;AACA;IACQ,IAAIA,KAAK,CAACC,IAAI,EAAE;MACZ;AACZ;AACA;MACYC,OAAO,CAACC,KAAK,CACTC,IAAI,CAACC,SAAS,CAAC;QACXC,OAAO,EAAE,wFAAwF;QACjGC,WAAW,EACP;MACR,CAAC,CACL,CAAC;MACD,OAAOC,mBAAM,CAACC,IAAI;IACtB;IAEA,MAAMC,OAAO,GAAGC,gCAAe,CAACC,MAAM,CAACZ,KAAK,CAACa,UAAU,CAAC,CAAoB,CAAC;IAE7E,IAAI,CAAChB,OAAO,CAACiB,OAAO,CAACC,MAAM,IAAI;MAC3BA,MAAM,CAACC,MAAM,CAACjB,OAAO,EAAEW,OAAO,CAAC;IACnC,CAAC,CAAC;IAEFV,KAAK,CAACU,OAAO,CAACA,OAAO,CAACG,UAAU,CAAC,CAAC,CAAC;IAEnCb,KAAK,CAACiB,IAAI,CAAC,GAAG,CAAC,CAACC,IAAI,CAAC,EAAE,CAAC,CAACC,MAAM,CAAC,CAAC;IAEjC,OAAOX,mBAAM,CAACC,IAAI;EACtB;AACJ;AAACW,OAAA,CAAAzB,wBAAA,GAAAA,wBAAA","ignoreList":[]}
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 "./IPreHandler";
4
- import { Action } from "./IPreHandler";
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
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
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
- return _ResponseHeaders.ResponseHeaders.create({
12
- "content-type": "application/json; charset=utf-8",
13
- "cache-control": "no-store",
14
- "access-control-allow-origin": "*",
15
- "access-control-allow-headers": "*",
16
- "access-control-allow-methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH",
17
- ...(0, _utils.getWebinyVersionHeaders)()
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
- return _ResponseHeaders.ResponseHeaders.create({
22
- "access-control-max-age": "86400",
23
- "cache-control": "public, max-age=86400"
24
- });
25
- };
26
- const getDefaultHeaders = routes => {
27
- const headers = createDefaultHeaders();
28
-
29
- /**
30
- * If we are accepting all headers, just output that one.
31
- */
32
- const keys = Object.keys(routes);
33
- const all = keys.every(key => routes[key].length > 0);
34
- if (all) {
35
- headers.set("access-control-allow-methods", "*");
36
- } else {
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
- constructor(definedRoutes) {
49
- this.definedRoutes = definedRoutes;
50
- }
51
- execute(request, reply) {
52
- const isOptionsRequest = request.method === "OPTIONS";
53
- /**
54
- * Our default headers are always set. Users can override them.
55
- */
56
- const defaultHeaders = getDefaultHeaders(this.definedRoutes);
57
- const initialHeaders = isOptionsRequest ? defaultHeaders.merge(getDefaultOptionsHeaders()) : defaultHeaders;
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
- exports.SetDefaultHeaders = SetDefaultHeaders;
44
+ export { SetDefaultHeaders };
63
45
 
64
46
  //# sourceMappingURL=SetDefaultHeaders.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_utils","require","_ResponseHeaders","_IPreHandler","createDefaultHeaders","ResponseHeaders","create","getWebinyVersionHeaders","getDefaultOptionsHeaders","getDefaultHeaders","routes","headers","keys","Object","all","every","key","length","set","allowedMethods","filter","type","Array","isArray","sort","join","SetDefaultHeaders","constructor","definedRoutes","execute","request","reply","isOptionsRequest","method","defaultHeaders","initialHeaders","merge","getHeaders","Action","CONTINUE","exports"],"sources":["SetDefaultHeaders.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport { getWebinyVersionHeaders } from \"@webiny/utils\";\nimport { ResponseHeaders } from \"~/ResponseHeaders\";\nimport type { DefinedContextRoutes, HTTPMethods } from \"~/types\";\nimport type { IPreHandler } from \"~/PreHandler/IPreHandler\";\nimport { Action } from \"~/PreHandler/IPreHandler\";\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"],"mappings":";;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,gBAAA,GAAAD,OAAA;AAGA,IAAAE,YAAA,GAAAF,OAAA;AAEA,SAASG,oBAAoBA,CAAA,EAAG;EAC5B,OAAOC,gCAAe,CAACC,MAAM,CAAC;IAC1B,cAAc,EAAE,iCAAiC;IACjD,eAAe,EAAE,UAAU;IAC3B,6BAA6B,EAAE,GAAG;IAClC,8BAA8B,EAAE,GAAG;IACnC,8BAA8B,EAAE,mCAAmC;IACnE,GAAG,IAAAC,8BAAuB,EAAC;EAC/B,CAAC,CAAC;AACN;AAEA,MAAMC,wBAAwB,GAAGA,CAAA,KAAM;EACnC,OAAOH,gCAAe,CAACC,MAAM,CAAC;IAC1B,wBAAwB,EAAE,OAAO;IACjC,eAAe,EAAE;EACrB,CAAC,CAAC;AACN,CAAC;AAED,MAAMG,iBAAiB,GAAIC,MAA4B,IAAsB;EACzE,MAAMC,OAAO,GAAGP,oBAAoB,CAAC,CAAC;;EAEtC;AACJ;AACA;EACI,MAAMQ,IAAI,GAAGC,MAAM,CAACD,IAAI,CAACF,MAAM,CAAkB;EACjD,MAAMI,GAAG,GAAGF,IAAI,CAACG,KAAK,CAACC,GAAG,IAAIN,MAAM,CAACM,GAAG,CAAC,CAACC,MAAM,GAAG,CAAC,CAAC;EACrD,IAAIH,GAAG,EAAE;IACLH,OAAO,CAACO,GAAG,CAAC,8BAA8B,EAAE,GAAG,CAAC;EACpD,CAAC,MAAM;IACH,MAAMC,cAAc,GAAGP,IAAI,CACtBQ,MAAM,CAACC,IAAI,IAAI;MACZ,IAAI,CAACX,MAAM,CAACW,IAAI,CAAC,IAAI,CAACC,KAAK,CAACC,OAAO,CAACb,MAAM,CAACW,IAAI,CAAC,CAAC,EAAE;QAC/C,OAAO,KAAK;MAChB;MACA,OAAOX,MAAM,CAACW,IAAI,CAAC,CAACJ,MAAM,GAAG,CAAC;IAClC,CAAC,CAAC,CACDO,IAAI,CAAC,CAAC,CACNC,IAAI,CAAC,GAAG,CAAC;IAEdd,OAAO,CAACO,GAAG,CAAC,8BAA8B,EAAEC,cAAc,CAAC;EAC/D;EAEA,OAAOR,OAAO;AAClB,CAAC;AAEM,MAAMe,iBAAiB,CAAwB;EAGlDC,WAAWA,CAACC,aAAmC,EAAE;IAC7C,IAAI,CAACA,aAAa,GAAGA,aAAa;EACtC;EAEAC,OAAOA,CAACC,OAAuB,EAAEC,KAAmB,EAAE;IAClD,MAAMC,gBAAgB,GAAGF,OAAO,CAACG,MAAM,KAAK,SAAS;IACrD;AACR;AACA;IACQ,MAAMC,cAAc,GAAGzB,iBAAiB,CAAC,IAAI,CAACmB,aAAa,CAAC;IAE5D,MAAMO,cAAc,GAAGH,gBAAgB,GACjCE,cAAc,CAACE,KAAK,CAAC5B,wBAAwB,CAAC,CAAC,CAAC,GAChD0B,cAAc;IAEpBH,KAAK,CAACpB,OAAO,CAACwB,cAAc,CAACE,UAAU,CAAC,CAAC,CAAC;IAE1C,OAAOC,mBAAM,CAACC,QAAQ;EAC1B;AACJ;AAACC,OAAA,CAAAd,iBAAA,GAAAA,iBAAA","ignoreList":[]}
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
- [![](https://img.shields.io/npm/dw/@webiny/handler.svg)](https://www.npmjs.com/package/@webiny/handler)
3
- [![](https://img.shields.io/npm/v/@webiny/handler.svg)](https://www.npmjs.com/package/@webiny/handler)
4
- [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
- [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
-
7
- ## Install
8
- ```
9
- npm install --save @webiny/handler
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._
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type * as http from "http";
3
2
  type ExtraHeaders = {
4
3
  "content-type"?: string | undefined;
@@ -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
- return typeof setter === "function";
2
+ return "function" == typeof setter;
11
3
  }
12
4
  class ResponseHeaders {
13
- headers = new Map();
14
- constructor(initialHeaders) {
15
- if (initialHeaders) {
16
- Object.keys(initialHeaders).forEach(key => {
17
- this.headers.set(key, initialHeaders[key]);
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
- set(header, setter) {
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
- exports.ResponseHeaders = ResponseHeaders;
34
+ export { ResponseHeaders };
45
35
 
46
36
  //# sourceMappingURL=ResponseHeaders.js.map
@@ -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","exports"],"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;AAIO,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;AAACc,OAAA,CAAAlB,eAAA,GAAAA,eAAA","ignoreList":[]}
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,5 @@
1
+ import type { Reply as IReply } from "../types.js";
2
+ export declare const Reply: import("@webiny/di").Abstraction<IReply>;
3
+ export declare namespace Reply {
4
+ type Interface = IReply;
5
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const Reply = createAbstraction("Reply");
3
+ export { Reply };
4
+
5
+ //# sourceMappingURL=Reply.js.map
@@ -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,5 @@
1
+ import type { Request as IRequest } from "../types.js";
2
+ export declare const Request: import("@webiny/di").Abstraction<IRequest>;
3
+ export declare namespace Request {
4
+ type Interface = IRequest;
5
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const Request = createAbstraction("Request");
3
+ export { Request };
4
+
5
+ //# sourceMappingURL=Request.js.map
@@ -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"}
@@ -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
- /// <reference types="node" />
2
- import type { PluginCollection } from "@webiny/plugins/types";
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
+ };