@webiny/handler 0.0.0-mt-3 → 0.0.0-unstable.06b2ede40f

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 (86) hide show
  1. package/Context.d.ts +22 -0
  2. package/Context.js +34 -0
  3. package/Context.js.map +1 -0
  4. package/PreHandler/IPreHandler.d.ts +9 -0
  5. package/PreHandler/IPreHandler.js +13 -0
  6. package/PreHandler/IPreHandler.js.map +1 -0
  7. package/PreHandler/IfNotOptionsRequest.d.ts +9 -0
  8. package/PreHandler/IfNotOptionsRequest.js +28 -0
  9. package/PreHandler/IfNotOptionsRequest.js.map +1 -0
  10. package/PreHandler/IfOptionsRequest.d.ts +9 -0
  11. package/PreHandler/IfOptionsRequest.js +28 -0
  12. package/PreHandler/IfOptionsRequest.js.map +1 -0
  13. package/PreHandler/PreHandler.d.ts +9 -0
  14. package/PreHandler/PreHandler.js +24 -0
  15. package/PreHandler/PreHandler.js.map +1 -0
  16. package/PreHandler/ProcessBeforeHandlerPlugins.d.ts +10 -0
  17. package/PreHandler/ProcessBeforeHandlerPlugins.js +31 -0
  18. package/PreHandler/ProcessBeforeHandlerPlugins.js.map +1 -0
  19. package/PreHandler/ProcessContextPlugins.d.ts +10 -0
  20. package/PreHandler/ProcessContextPlugins.js +31 -0
  21. package/PreHandler/ProcessContextPlugins.js.map +1 -0
  22. package/PreHandler/ProcessHandlerOnRequestPlugins.d.ts +10 -0
  23. package/PreHandler/ProcessHandlerOnRequestPlugins.js +33 -0
  24. package/PreHandler/ProcessHandlerOnRequestPlugins.js.map +1 -0
  25. package/PreHandler/SendEarlyOptionsResponse.d.ts +9 -0
  26. package/PreHandler/SendEarlyOptionsResponse.js +38 -0
  27. package/PreHandler/SendEarlyOptionsResponse.js.map +1 -0
  28. package/PreHandler/SetDefaultHeaders.d.ts +9 -0
  29. package/PreHandler/SetDefaultHeaders.js +64 -0
  30. package/PreHandler/SetDefaultHeaders.js.map +1 -0
  31. package/README.md +8 -2
  32. package/ResponseHeaders.d.ts +25 -0
  33. package/ResponseHeaders.js +46 -0
  34. package/ResponseHeaders.js.map +1 -0
  35. package/fastify.d.ts +10 -0
  36. package/fastify.js +401 -0
  37. package/fastify.js.map +1 -0
  38. package/index.d.ts +15 -1
  39. package/index.js +144 -8
  40. package/index.js.map +1 -0
  41. package/package.json +18 -18
  42. package/plugins/BeforeHandlerPlugin.d.ts +8 -7
  43. package/plugins/BeforeHandlerPlugin.js +9 -14
  44. package/plugins/BeforeHandlerPlugin.js.map +1 -0
  45. package/plugins/EventPlugin.d.ts +25 -0
  46. package/plugins/EventPlugin.js +30 -0
  47. package/plugins/EventPlugin.js.map +1 -0
  48. package/plugins/HandlerErrorPlugin.d.ts +15 -0
  49. package/plugins/HandlerErrorPlugin.js +24 -0
  50. package/plugins/HandlerErrorPlugin.js.map +1 -0
  51. package/plugins/HandlerOnRequestPlugin.d.ts +21 -0
  52. package/plugins/HandlerOnRequestPlugin.js +31 -0
  53. package/plugins/HandlerOnRequestPlugin.js.map +1 -0
  54. package/plugins/HandlerResultPlugin.d.ts +12 -0
  55. package/plugins/HandlerResultPlugin.js +24 -0
  56. package/plugins/HandlerResultPlugin.js.map +1 -0
  57. package/plugins/ModifyFastifyPlugin.d.ts +13 -0
  58. package/plugins/ModifyFastifyPlugin.js +24 -0
  59. package/plugins/ModifyFastifyPlugin.js.map +1 -0
  60. package/plugins/ModifyResponseHeadersPlugin.d.ts +14 -0
  61. package/plugins/ModifyResponseHeadersPlugin.js +24 -0
  62. package/plugins/ModifyResponseHeadersPlugin.js.map +1 -0
  63. package/plugins/OnRequestResponseSendPlugin.d.ts +26 -0
  64. package/plugins/OnRequestResponseSendPlugin.js +40 -0
  65. package/plugins/OnRequestResponseSendPlugin.js.map +1 -0
  66. package/plugins/OnRequestTimeoutPlugin.d.ts +12 -0
  67. package/plugins/OnRequestTimeoutPlugin.js +24 -0
  68. package/plugins/OnRequestTimeoutPlugin.js.map +1 -0
  69. package/plugins/RoutePlugin.d.ts +23 -0
  70. package/plugins/RoutePlugin.js +21 -0
  71. package/plugins/RoutePlugin.js.map +1 -0
  72. package/stringifyError.d.ts +5 -0
  73. package/stringifyError.js +27 -0
  74. package/stringifyError.js.map +1 -0
  75. package/suppressPunycodeWarnings.d.ts +4 -0
  76. package/suppressPunycodeWarnings.js +11 -0
  77. package/suppressPunycodeWarnings.js.map +1 -0
  78. package/types.d.ts +43 -34
  79. package/types.js +11 -1
  80. package/types.js.map +1 -0
  81. package/createHandler.d.ts +0 -2
  82. package/createHandler.js +0 -70
  83. package/middleware.d.ts +0 -5
  84. package/middleware.js +0 -51
  85. package/plugins/ContextPlugin.d.ts +0 -11
  86. package/plugins/ContextPlugin.js +0 -32
package/Context.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { ContextParams as BaseContextParams, ContextPluginCallable as BaseContextPluginCallable } from "@webiny/api";
2
+ import { Context as BaseContext, ContextPlugin as BaseContextPlugin } from "@webiny/api";
3
+ import type { Context as ContextInterface } from "./types";
4
+ export interface ContextParams extends BaseContextParams {
5
+ routes: ContextInterface["routes"];
6
+ }
7
+ export declare class Context extends BaseContext implements ContextInterface {
8
+ readonly routes: ContextInterface["routes"];
9
+ handlerClient: ContextInterface["handlerClient"];
10
+ request: ContextInterface["request"];
11
+ reply: ContextInterface["reply"];
12
+ constructor(params: ContextParams);
13
+ }
14
+ /**
15
+ * We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.
16
+ *
17
+ * This can be removed when we introduce the type augmentation.
18
+ */
19
+ export type ContextPluginCallable<T extends ContextInterface = ContextInterface> = BaseContextPluginCallable<T>;
20
+ export declare class ContextPlugin<T extends ContextInterface = ContextInterface> extends BaseContextPlugin<T> {
21
+ }
22
+ export declare const createContextPlugin: <T extends ContextInterface = ContextInterface>(callable: ContextPluginCallable<T>) => BaseContextPlugin<T>;
package/Context.js ADDED
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createContextPlugin = exports.ContextPlugin = exports.Context = void 0;
7
+ var _api = require("@webiny/api");
8
+ class Context extends _api.Context {
9
+ // @ts-expect-error
10
+
11
+ // @ts-expect-error
12
+
13
+ // @ts-expect-error
14
+
15
+ constructor(params) {
16
+ super(params);
17
+ this.routes = params.routes;
18
+ }
19
+ }
20
+
21
+ /**
22
+ * We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.
23
+ *
24
+ * This can be removed when we introduce the type augmentation.
25
+ */
26
+ exports.Context = Context;
27
+ class ContextPlugin extends _api.ContextPlugin {}
28
+ exports.ContextPlugin = ContextPlugin;
29
+ const createContextPlugin = callable => {
30
+ return (0, _api.createContextPlugin)(callable);
31
+ };
32
+ exports.createContextPlugin = createContextPlugin;
33
+
34
+ //# sourceMappingURL=Context.js.map
package/Context.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_api","require","Context","BaseContext","constructor","params","routes","exports","ContextPlugin","BaseContextPlugin","createContextPlugin","callable","baseCreateContextPlugin"],"sources":["Context.ts"],"sourcesContent":["import type {\n ContextParams as BaseContextParams,\n ContextPluginCallable as BaseContextPluginCallable\n} from \"@webiny/api\";\nimport {\n Context as BaseContext,\n ContextPlugin as BaseContextPlugin,\n createContextPlugin as baseCreateContextPlugin\n} from \"@webiny/api\";\nimport type { Context as ContextInterface } from \"~/types\";\n\nexport interface ContextParams extends BaseContextParams {\n routes: ContextInterface[\"routes\"];\n}\n\nexport class Context extends BaseContext implements ContextInterface {\n public readonly routes: ContextInterface[\"routes\"];\n // @ts-expect-error\n public handlerClient: ContextInterface[\"handlerClient\"];\n // @ts-expect-error\n public request: ContextInterface[\"request\"];\n // @ts-expect-error\n public reply: ContextInterface[\"reply\"];\n\n public constructor(params: ContextParams) {\n super(params);\n this.routes = params.routes;\n }\n}\n\n/**\n * We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.\n *\n * This can be removed when we introduce the type augmentation.\n */\nexport type ContextPluginCallable<T extends ContextInterface = ContextInterface> =\n BaseContextPluginCallable<T>;\n\nexport class ContextPlugin<\n T extends ContextInterface = ContextInterface\n> extends BaseContextPlugin<T> {}\n\nexport const createContextPlugin = <T extends ContextInterface = ContextInterface>(\n callable: ContextPluginCallable<T>\n) => {\n return baseCreateContextPlugin<T>(callable);\n};\n"],"mappings":";;;;;;AAIA,IAAAA,IAAA,GAAAC,OAAA;AAWO,MAAMC,OAAO,SAASC,YAAW,CAA6B;EAEjE;;EAEA;;EAEA;;EAGOC,WAAWA,CAACC,MAAqB,EAAE;IACtC,KAAK,CAACA,MAAM,CAAC;IACb,IAAI,CAACC,MAAM,GAAGD,MAAM,CAACC,MAAM;EAC/B;AACJ;;AAEA;AACA;AACA;AACA;AACA;AAJAC,OAAA,CAAAL,OAAA,GAAAA,OAAA;AAQO,MAAMM,aAAa,SAEhBC,kBAAiB,CAAI;AAAEF,OAAA,CAAAC,aAAA,GAAAA,aAAA;AAE1B,MAAME,mBAAmB,GAC5BC,QAAkC,IACjC;EACD,OAAO,IAAAC,wBAAuB,EAAID,QAAQ,CAAC;AAC/C,CAAC;AAACJ,OAAA,CAAAG,mBAAA,GAAAA,mBAAA","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ import type { FastifyReply, FastifyRequest } from "fastify";
2
+ import type { Context } from "../types";
3
+ export declare enum Action {
4
+ CONTINUE = "continue",
5
+ DONE = "done"
6
+ }
7
+ export interface IPreHandler {
8
+ execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action> | Action;
9
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Action = void 0;
7
+ let Action = exports.Action = /*#__PURE__*/function (Action) {
8
+ Action["CONTINUE"] = "continue";
9
+ Action["DONE"] = "done";
10
+ return Action;
11
+ }({});
12
+
13
+ //# sourceMappingURL=IPreHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Action","exports"],"sources":["IPreHandler.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport type { Context } from \"~/types\";\n\nexport enum Action {\n CONTINUE = \"continue\",\n DONE = \"done\"\n}\n\nexport interface IPreHandler {\n execute(\n request: FastifyRequest,\n reply: FastifyReply,\n context: Context\n ): Promise<Action> | Action;\n}\n"],"mappings":";;;;;;IAGYA,MAAM,GAAAC,OAAA,CAAAD,MAAA,0BAANA,MAAM;EAANA,MAAM;EAANA,MAAM;EAAA,OAANA,MAAM;AAAA","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ import type { FastifyReply, FastifyRequest } from "fastify";
2
+ import type { IPreHandler } from "./IPreHandler";
3
+ import { Action } from "./IPreHandler";
4
+ import type { Context } from "../types";
5
+ export declare class IfNotOptionsRequest implements IPreHandler {
6
+ private readonly handlers;
7
+ constructor(handlers: IPreHandler[]);
8
+ execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action>;
9
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.IfNotOptionsRequest = void 0;
7
+ var _IPreHandler = require("./IPreHandler");
8
+ class IfNotOptionsRequest {
9
+ constructor(handlers) {
10
+ this.handlers = handlers;
11
+ }
12
+ async execute(request, reply, context) {
13
+ const isOptionsRequest = request.method === "OPTIONS";
14
+ if (isOptionsRequest) {
15
+ return _IPreHandler.Action.CONTINUE;
16
+ }
17
+ for (const handler of this.handlers) {
18
+ const action = await handler.execute(request, reply, context);
19
+ if (action === _IPreHandler.Action.DONE) {
20
+ return _IPreHandler.Action.DONE;
21
+ }
22
+ }
23
+ return _IPreHandler.Action.CONTINUE;
24
+ }
25
+ }
26
+ exports.IfNotOptionsRequest = IfNotOptionsRequest;
27
+
28
+ //# sourceMappingURL=IfNotOptionsRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_IPreHandler","require","IfNotOptionsRequest","constructor","handlers","execute","request","reply","context","isOptionsRequest","method","Action","CONTINUE","handler","action","DONE","exports"],"sources":["IfNotOptionsRequest.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport type { IPreHandler } from \"./IPreHandler\";\nimport { Action } from \"./IPreHandler\";\nimport type { Context } from \"~/types\";\n\nexport class IfNotOptionsRequest implements IPreHandler {\n private readonly handlers: IPreHandler[];\n\n constructor(handlers: IPreHandler[]) {\n this.handlers = handlers;\n }\n\n async execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action> {\n const isOptionsRequest = request.method === \"OPTIONS\";\n if (isOptionsRequest) {\n return Action.CONTINUE;\n }\n\n for (const handler of this.handlers) {\n const action = await handler.execute(request, reply, context);\n if (action === Action.DONE) {\n return Action.DONE;\n }\n }\n\n return Action.CONTINUE;\n }\n}\n"],"mappings":";;;;;;AAEA,IAAAA,YAAA,GAAAC,OAAA;AAGO,MAAMC,mBAAmB,CAAwB;EAGpDC,WAAWA,CAACC,QAAuB,EAAE;IACjC,IAAI,CAACA,QAAQ,GAAGA,QAAQ;EAC5B;EAEA,MAAMC,OAAOA,CAACC,OAAuB,EAAEC,KAAmB,EAAEC,OAAgB,EAAmB;IAC3F,MAAMC,gBAAgB,GAAGH,OAAO,CAACI,MAAM,KAAK,SAAS;IACrD,IAAID,gBAAgB,EAAE;MAClB,OAAOE,mBAAM,CAACC,QAAQ;IAC1B;IAEA,KAAK,MAAMC,OAAO,IAAI,IAAI,CAACT,QAAQ,EAAE;MACjC,MAAMU,MAAM,GAAG,MAAMD,OAAO,CAACR,OAAO,CAACC,OAAO,EAAEC,KAAK,EAAEC,OAAO,CAAC;MAC7D,IAAIM,MAAM,KAAKH,mBAAM,CAACI,IAAI,EAAE;QACxB,OAAOJ,mBAAM,CAACI,IAAI;MACtB;IACJ;IAEA,OAAOJ,mBAAM,CAACC,QAAQ;EAC1B;AACJ;AAACI,OAAA,CAAAd,mBAAA,GAAAA,mBAAA","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ import type { FastifyReply, FastifyRequest } from "fastify";
2
+ import type { IPreHandler } from "./IPreHandler";
3
+ import { Action } from "./IPreHandler";
4
+ import type { Context } from "../types";
5
+ export declare class IfOptionsRequest implements IPreHandler {
6
+ private readonly handlers;
7
+ constructor(handlers: IPreHandler[]);
8
+ execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action>;
9
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.IfOptionsRequest = void 0;
7
+ var _IPreHandler = require("./IPreHandler");
8
+ class IfOptionsRequest {
9
+ constructor(handlers) {
10
+ this.handlers = handlers;
11
+ }
12
+ async execute(request, reply, context) {
13
+ const isOptionsRequest = request.method === "OPTIONS";
14
+ if (!isOptionsRequest) {
15
+ return _IPreHandler.Action.CONTINUE;
16
+ }
17
+ for (const handler of this.handlers) {
18
+ const action = await handler.execute(request, reply, context);
19
+ if (action === _IPreHandler.Action.DONE) {
20
+ return _IPreHandler.Action.DONE;
21
+ }
22
+ }
23
+ return _IPreHandler.Action.CONTINUE;
24
+ }
25
+ }
26
+ exports.IfOptionsRequest = IfOptionsRequest;
27
+
28
+ //# sourceMappingURL=IfOptionsRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_IPreHandler","require","IfOptionsRequest","constructor","handlers","execute","request","reply","context","isOptionsRequest","method","Action","CONTINUE","handler","action","DONE","exports"],"sources":["IfOptionsRequest.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport type { IPreHandler } from \"./IPreHandler\";\nimport { Action } from \"./IPreHandler\";\nimport type { Context } from \"~/types\";\n\nexport class IfOptionsRequest implements IPreHandler {\n private readonly handlers: IPreHandler[];\n\n constructor(handlers: IPreHandler[]) {\n this.handlers = handlers;\n }\n\n async execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action> {\n const isOptionsRequest = request.method === \"OPTIONS\";\n if (!isOptionsRequest) {\n return Action.CONTINUE;\n }\n\n for (const handler of this.handlers) {\n const action = await handler.execute(request, reply, context);\n if (action === Action.DONE) {\n return Action.DONE;\n }\n }\n\n return Action.CONTINUE;\n }\n}\n"],"mappings":";;;;;;AAEA,IAAAA,YAAA,GAAAC,OAAA;AAGO,MAAMC,gBAAgB,CAAwB;EAGjDC,WAAWA,CAACC,QAAuB,EAAE;IACjC,IAAI,CAACA,QAAQ,GAAGA,QAAQ;EAC5B;EAEA,MAAMC,OAAOA,CAACC,OAAuB,EAAEC,KAAmB,EAAEC,OAAgB,EAAmB;IAC3F,MAAMC,gBAAgB,GAAGH,OAAO,CAACI,MAAM,KAAK,SAAS;IACrD,IAAI,CAACD,gBAAgB,EAAE;MACnB,OAAOE,mBAAM,CAACC,QAAQ;IAC1B;IAEA,KAAK,MAAMC,OAAO,IAAI,IAAI,CAACT,QAAQ,EAAE;MACjC,MAAMU,MAAM,GAAG,MAAMD,OAAO,CAACR,OAAO,CAACC,OAAO,EAAEC,KAAK,EAAEC,OAAO,CAAC;MAC7D,IAAIM,MAAM,KAAKH,mBAAM,CAACI,IAAI,EAAE;QACxB,OAAOJ,mBAAM,CAACI,IAAI;MACtB;IACJ;IAEA,OAAOJ,mBAAM,CAACC,QAAQ;EAC1B;AACJ;AAACI,OAAA,CAAAd,gBAAA,GAAAA,gBAAA","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ import type { FastifyReply, FastifyRequest } from "fastify";
2
+ import type { IPreHandler } from "./IPreHandler";
3
+ import { Action } from "./IPreHandler";
4
+ import type { Context } from "../types";
5
+ export declare class PreHandler implements IPreHandler {
6
+ private readonly handlers;
7
+ constructor(handlers: IPreHandler[]);
8
+ execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action>;
9
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.PreHandler = void 0;
7
+ var _IPreHandler = require("./IPreHandler");
8
+ class PreHandler {
9
+ constructor(handlers) {
10
+ this.handlers = handlers;
11
+ }
12
+ async execute(request, reply, context) {
13
+ for (const handler of this.handlers) {
14
+ const action = await handler.execute(request, reply, context);
15
+ if (action === _IPreHandler.Action.DONE) {
16
+ return _IPreHandler.Action.DONE;
17
+ }
18
+ }
19
+ return _IPreHandler.Action.CONTINUE;
20
+ }
21
+ }
22
+ exports.PreHandler = PreHandler;
23
+
24
+ //# sourceMappingURL=PreHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_IPreHandler","require","PreHandler","constructor","handlers","execute","request","reply","context","handler","action","Action","DONE","CONTINUE","exports"],"sources":["PreHandler.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport type { IPreHandler } from \"~/PreHandler/IPreHandler\";\nimport { Action } from \"~/PreHandler/IPreHandler\";\nimport type { Context } from \"~/types\";\n\nexport class PreHandler implements IPreHandler {\n private readonly handlers: IPreHandler[];\n\n constructor(handlers: IPreHandler[]) {\n this.handlers = handlers;\n }\n\n async execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action> {\n for (const handler of this.handlers) {\n const action = await handler.execute(request, reply, context);\n if (action === Action.DONE) {\n return Action.DONE;\n }\n }\n\n return Action.CONTINUE;\n }\n}\n"],"mappings":";;;;;;AAEA,IAAAA,YAAA,GAAAC,OAAA;AAGO,MAAMC,UAAU,CAAwB;EAG3CC,WAAWA,CAACC,QAAuB,EAAE;IACjC,IAAI,CAACA,QAAQ,GAAGA,QAAQ;EAC5B;EAEA,MAAMC,OAAOA,CAACC,OAAuB,EAAEC,KAAmB,EAAEC,OAAgB,EAAmB;IAC3F,KAAK,MAAMC,OAAO,IAAI,IAAI,CAACL,QAAQ,EAAE;MACjC,MAAMM,MAAM,GAAG,MAAMD,OAAO,CAACJ,OAAO,CAACC,OAAO,EAAEC,KAAK,EAAEC,OAAO,CAAC;MAC7D,IAAIE,MAAM,KAAKC,mBAAM,CAACC,IAAI,EAAE;QACxB,OAAOD,mBAAM,CAACC,IAAI;MACtB;IACJ;IAEA,OAAOD,mBAAM,CAACE,QAAQ;EAC1B;AACJ;AAACC,OAAA,CAAAZ,UAAA,GAAAA,UAAA","ignoreList":[]}
@@ -0,0 +1,10 @@
1
+ import type { IPreHandler } from "./IPreHandler";
2
+ import { Action } from "./IPreHandler";
3
+ import type { Context } from "../types";
4
+ import type { BeforeHandlerPlugin } from "../plugins/BeforeHandlerPlugin";
5
+ export declare class ProcessBeforeHandlerPlugins implements IPreHandler {
6
+ private readonly plugins;
7
+ private readonly context;
8
+ constructor(context: Context, plugins: BeforeHandlerPlugin[]);
9
+ execute(): Promise<Action>;
10
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ProcessBeforeHandlerPlugins = void 0;
7
+ var _IPreHandler = require("./IPreHandler");
8
+ var _stringifyError = require("../stringifyError");
9
+ class ProcessBeforeHandlerPlugins {
10
+ constructor(context, plugins) {
11
+ this.context = context;
12
+ this.plugins = plugins;
13
+ }
14
+ async execute() {
15
+ let name;
16
+ try {
17
+ for (const plugin of this.plugins) {
18
+ name = plugin.name;
19
+ await plugin.apply(this.context);
20
+ }
21
+ } catch (ex) {
22
+ console.error(`Error running BeforeHandlerPlugin "${name}".`);
23
+ console.error((0, _stringifyError.stringifyError)(ex));
24
+ throw ex;
25
+ }
26
+ return _IPreHandler.Action.CONTINUE;
27
+ }
28
+ }
29
+ exports.ProcessBeforeHandlerPlugins = ProcessBeforeHandlerPlugins;
30
+
31
+ //# sourceMappingURL=ProcessBeforeHandlerPlugins.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_IPreHandler","require","_stringifyError","ProcessBeforeHandlerPlugins","constructor","context","plugins","execute","name","plugin","apply","ex","console","error","stringifyError","Action","CONTINUE","exports"],"sources":["ProcessBeforeHandlerPlugins.ts"],"sourcesContent":["import type { IPreHandler } from \"~/PreHandler/IPreHandler\";\nimport { Action } from \"~/PreHandler/IPreHandler\";\nimport { stringifyError } from \"~/stringifyError\";\nimport type { Context } from \"~/types\";\nimport type { BeforeHandlerPlugin } from \"~/plugins/BeforeHandlerPlugin\";\n\nexport class ProcessBeforeHandlerPlugins implements IPreHandler {\n private readonly plugins: BeforeHandlerPlugin[];\n private readonly context: Context;\n\n constructor(context: Context, plugins: BeforeHandlerPlugin[]) {\n this.context = context;\n this.plugins = plugins;\n }\n\n async execute(): Promise<Action> {\n let name: string | undefined;\n try {\n for (const plugin of this.plugins) {\n name = plugin.name;\n await plugin.apply(this.context);\n }\n } catch (ex) {\n console.error(`Error running BeforeHandlerPlugin \"${name}\".`);\n console.error(stringifyError(ex));\n throw ex;\n }\n\n return Action.CONTINUE;\n }\n}\n"],"mappings":";;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AAIO,MAAME,2BAA2B,CAAwB;EAI5DC,WAAWA,CAACC,OAAgB,EAAEC,OAA8B,EAAE;IAC1D,IAAI,CAACD,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACC,OAAO,GAAGA,OAAO;EAC1B;EAEA,MAAMC,OAAOA,CAAA,EAAoB;IAC7B,IAAIC,IAAwB;IAC5B,IAAI;MACA,KAAK,MAAMC,MAAM,IAAI,IAAI,CAACH,OAAO,EAAE;QAC/BE,IAAI,GAAGC,MAAM,CAACD,IAAI;QAClB,MAAMC,MAAM,CAACC,KAAK,CAAC,IAAI,CAACL,OAAO,CAAC;MACpC;IACJ,CAAC,CAAC,OAAOM,EAAE,EAAE;MACTC,OAAO,CAACC,KAAK,CAAC,sCAAsCL,IAAI,IAAI,CAAC;MAC7DI,OAAO,CAACC,KAAK,CAAC,IAAAC,8BAAc,EAACH,EAAE,CAAC,CAAC;MACjC,MAAMA,EAAE;IACZ;IAEA,OAAOI,mBAAM,CAACC,QAAQ;EAC1B;AACJ;AAACC,OAAA,CAAAd,2BAAA,GAAAA,2BAAA","ignoreList":[]}
@@ -0,0 +1,10 @@
1
+ import type { IPreHandler } from "./IPreHandler";
2
+ import { Action } from "./IPreHandler";
3
+ import type { ContextPlugin } from "../Context";
4
+ import type { Context } from "../types";
5
+ export declare class ProcessContextPlugins implements IPreHandler {
6
+ private readonly plugins;
7
+ private readonly context;
8
+ constructor(context: Context, plugins: ContextPlugin[]);
9
+ execute(): Promise<Action>;
10
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ProcessContextPlugins = void 0;
7
+ var _IPreHandler = require("./IPreHandler");
8
+ var _stringifyError = require("../stringifyError");
9
+ class ProcessContextPlugins {
10
+ constructor(context, plugins) {
11
+ this.context = context;
12
+ this.plugins = plugins;
13
+ }
14
+ async execute() {
15
+ let name;
16
+ try {
17
+ for (const plugin of this.plugins) {
18
+ name = plugin.name;
19
+ await plugin.apply(this.context);
20
+ }
21
+ } catch (ex) {
22
+ console.error(`Error running ContextPlugin "${name}".`);
23
+ console.error((0, _stringifyError.stringifyError)(ex));
24
+ throw ex;
25
+ }
26
+ return _IPreHandler.Action.CONTINUE;
27
+ }
28
+ }
29
+ exports.ProcessContextPlugins = ProcessContextPlugins;
30
+
31
+ //# sourceMappingURL=ProcessContextPlugins.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_IPreHandler","require","_stringifyError","ProcessContextPlugins","constructor","context","plugins","execute","name","plugin","apply","ex","console","error","stringifyError","Action","CONTINUE","exports"],"sources":["ProcessContextPlugins.ts"],"sourcesContent":["import type { IPreHandler } from \"~/PreHandler/IPreHandler\";\nimport { Action } from \"~/PreHandler/IPreHandler\";\nimport { stringifyError } from \"~/stringifyError\";\nimport type { ContextPlugin } from \"~/Context\";\nimport type { Context } from \"~/types\";\n\nexport class ProcessContextPlugins implements IPreHandler {\n private readonly plugins: ContextPlugin[];\n private readonly context: Context;\n\n constructor(context: Context, plugins: ContextPlugin[]) {\n this.context = context;\n this.plugins = plugins;\n }\n\n async execute(): Promise<Action> {\n let name: string | undefined;\n try {\n for (const plugin of this.plugins) {\n name = plugin.name;\n await plugin.apply(this.context);\n }\n } catch (ex) {\n console.error(`Error running ContextPlugin \"${name}\".`);\n console.error(stringifyError(ex));\n throw ex;\n }\n\n return Action.CONTINUE;\n }\n}\n"],"mappings":";;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AAIO,MAAME,qBAAqB,CAAwB;EAItDC,WAAWA,CAACC,OAAgB,EAAEC,OAAwB,EAAE;IACpD,IAAI,CAACD,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACC,OAAO,GAAGA,OAAO;EAC1B;EAEA,MAAMC,OAAOA,CAAA,EAAoB;IAC7B,IAAIC,IAAwB;IAC5B,IAAI;MACA,KAAK,MAAMC,MAAM,IAAI,IAAI,CAACH,OAAO,EAAE;QAC/BE,IAAI,GAAGC,MAAM,CAACD,IAAI;QAClB,MAAMC,MAAM,CAACC,KAAK,CAAC,IAAI,CAACL,OAAO,CAAC;MACpC;IACJ,CAAC,CAAC,OAAOM,EAAE,EAAE;MACTC,OAAO,CAACC,KAAK,CAAC,gCAAgCL,IAAI,IAAI,CAAC;MACvDI,OAAO,CAACC,KAAK,CAAC,IAAAC,8BAAc,EAACH,EAAE,CAAC,CAAC;MACjC,MAAMA,EAAE;IACZ;IAEA,OAAOI,mBAAM,CAACC,QAAQ;EAC1B;AACJ;AAACC,OAAA,CAAAd,qBAAA,GAAAA,qBAAA","ignoreList":[]}
@@ -0,0 +1,10 @@
1
+ import type { FastifyReply, FastifyRequest } from "fastify";
2
+ import type { IPreHandler } from "./IPreHandler";
3
+ import { Action } from "./IPreHandler";
4
+ import type { HandlerOnRequestPlugin } from "../plugins/HandlerOnRequestPlugin";
5
+ import type { Context } from "../types";
6
+ export declare class ProcessHandlerOnRequestPlugins implements IPreHandler {
7
+ private readonly plugins;
8
+ constructor(plugins: HandlerOnRequestPlugin[]);
9
+ execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action>;
10
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ProcessHandlerOnRequestPlugins = void 0;
7
+ var _IPreHandler = require("./IPreHandler");
8
+ var _stringifyError = require("../stringifyError");
9
+ class ProcessHandlerOnRequestPlugins {
10
+ constructor(plugins) {
11
+ this.plugins = plugins;
12
+ }
13
+ async execute(request, reply, context) {
14
+ let name;
15
+ try {
16
+ for (const plugin of this.plugins) {
17
+ name = plugin.name;
18
+ const result = await plugin.exec(request, reply, context);
19
+ if (result === false) {
20
+ return _IPreHandler.Action.DONE;
21
+ }
22
+ }
23
+ } catch (ex) {
24
+ console.error(`Error while running the "HandlerOnRequestPlugin" ${name ? `(${name})` : ""} plugin in the onRequest hook.`);
25
+ console.error((0, _stringifyError.stringifyError)(ex));
26
+ throw ex;
27
+ }
28
+ return _IPreHandler.Action.CONTINUE;
29
+ }
30
+ }
31
+ exports.ProcessHandlerOnRequestPlugins = ProcessHandlerOnRequestPlugins;
32
+
33
+ //# sourceMappingURL=ProcessHandlerOnRequestPlugins.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_IPreHandler","require","_stringifyError","ProcessHandlerOnRequestPlugins","constructor","plugins","execute","request","reply","context","name","plugin","result","exec","Action","DONE","ex","console","error","stringifyError","CONTINUE","exports"],"sources":["ProcessHandlerOnRequestPlugins.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport type { IPreHandler } from \"~/PreHandler/IPreHandler\";\nimport { Action } from \"~/PreHandler/IPreHandler\";\nimport type { HandlerOnRequestPlugin } from \"~/plugins/HandlerOnRequestPlugin\";\nimport { stringifyError } from \"~/stringifyError\";\nimport type { Context } from \"~/types\";\n\nexport class ProcessHandlerOnRequestPlugins implements IPreHandler {\n private readonly plugins: HandlerOnRequestPlugin[];\n\n constructor(plugins: HandlerOnRequestPlugin[]) {\n this.plugins = plugins;\n }\n\n async execute(request: FastifyRequest, reply: FastifyReply, context: Context): Promise<Action> {\n let name: string | undefined;\n try {\n for (const plugin of this.plugins) {\n name = plugin.name;\n const result = await plugin.exec(request, reply, context);\n if (result === false) {\n return Action.DONE;\n }\n }\n } catch (ex) {\n console.error(\n `Error while running the \"HandlerOnRequestPlugin\" ${\n name ? `(${name})` : \"\"\n } plugin in the onRequest hook.`\n );\n console.error(stringifyError(ex));\n throw ex;\n }\n\n return Action.CONTINUE;\n }\n}\n"],"mappings":";;;;;;AAEA,IAAAA,YAAA,GAAAC,OAAA;AAEA,IAAAC,eAAA,GAAAD,OAAA;AAGO,MAAME,8BAA8B,CAAwB;EAG/DC,WAAWA,CAACC,OAAiC,EAAE;IAC3C,IAAI,CAACA,OAAO,GAAGA,OAAO;EAC1B;EAEA,MAAMC,OAAOA,CAACC,OAAuB,EAAEC,KAAmB,EAAEC,OAAgB,EAAmB;IAC3F,IAAIC,IAAwB;IAC5B,IAAI;MACA,KAAK,MAAMC,MAAM,IAAI,IAAI,CAACN,OAAO,EAAE;QAC/BK,IAAI,GAAGC,MAAM,CAACD,IAAI;QAClB,MAAME,MAAM,GAAG,MAAMD,MAAM,CAACE,IAAI,CAACN,OAAO,EAAEC,KAAK,EAAEC,OAAO,CAAC;QACzD,IAAIG,MAAM,KAAK,KAAK,EAAE;UAClB,OAAOE,mBAAM,CAACC,IAAI;QACtB;MACJ;IACJ,CAAC,CAAC,OAAOC,EAAE,EAAE;MACTC,OAAO,CAACC,KAAK,CACT,oDACIR,IAAI,GAAG,IAAIA,IAAI,GAAG,GAAG,EAAE,gCAE/B,CAAC;MACDO,OAAO,CAACC,KAAK,CAAC,IAAAC,8BAAc,EAACH,EAAE,CAAC,CAAC;MACjC,MAAMA,EAAE;IACZ;IAEA,OAAOF,mBAAM,CAACM,QAAQ;EAC1B;AACJ;AAACC,OAAA,CAAAlB,8BAAA,GAAAA,8BAAA","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ import type { FastifyReply, FastifyRequest } from "fastify";
2
+ import type { IPreHandler } from "./IPreHandler";
3
+ import { Action } from "./IPreHandler";
4
+ import type { ModifyResponseHeadersPlugin } from "../plugins/ModifyResponseHeadersPlugin";
5
+ export declare class SendEarlyOptionsResponse implements IPreHandler {
6
+ private readonly plugins;
7
+ constructor(plugins: ModifyResponseHeadersPlugin[]);
8
+ execute(request: FastifyRequest, reply: FastifyReply): Promise<Action>;
9
+ }
@@ -0,0 +1,38 @@
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");
9
+ 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;
26
+ }
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
+ }
36
+ exports.SendEarlyOptionsResponse = SendEarlyOptionsResponse;
37
+
38
+ //# sourceMappingURL=SendEarlyOptionsResponse.js.map
@@ -0,0 +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":[]}
@@ -0,0 +1,9 @@
1
+ import type { FastifyReply, FastifyRequest } from "fastify";
2
+ import type { DefinedContextRoutes } from "../types";
3
+ import type { IPreHandler } from "./IPreHandler";
4
+ import { Action } from "./IPreHandler";
5
+ export declare class SetDefaultHeaders implements IPreHandler {
6
+ private readonly definedRoutes;
7
+ constructor(definedRoutes: DefinedContextRoutes);
8
+ execute(request: FastifyRequest, reply: FastifyReply): Action;
9
+ }
@@ -0,0 +1,64 @@
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");
10
+ 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
+ });
19
+ }
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;
46
+ };
47
+ 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
+ }
61
+ }
62
+ exports.SetDefaultHeaders = SetDefaultHeaders;
63
+
64
+ //# sourceMappingURL=SetDefaultHeaders.js.map
@@ -0,0 +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":[]}
package/README.md CHANGED
@@ -4,6 +4,12 @@
4
4
  [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
5
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
6
 
7
- A tiny, universal, and plugin-based framework for creating (serverless) handlers, used within services like AWS Lambda, Azure Serverless Functions, and others.
7
+ ## Install
8
+ ```
9
+ npm install --save @webiny/handler
10
+ ```
8
11
 
9
- A handler can be anything from a simple event-handler, to a fully-fledged GraphQL server. It all depends on the additional plugins that are passed upon the creating the handler.
12
+ Or if you prefer yarn:
13
+ ```
14
+ yarn add @webiny/handler
15
+ ```
@@ -0,0 +1,25 @@
1
+ /// <reference types="node" />
2
+ import type * as http from "http";
3
+ type ExtraHeaders = {
4
+ "content-type"?: string | undefined;
5
+ "x-webiny-version"?: http.OutgoingHttpHeader | undefined;
6
+ };
7
+ type AllHeaders = http.OutgoingHttpHeaders & ExtraHeaders;
8
+ export type StandardHeaderValue = http.OutgoingHttpHeader | undefined;
9
+ export type StandardHeaders = {
10
+ [K in keyof AllHeaders as string extends K ? never : number extends K ? never : K]: http.OutgoingHttpHeaders[K];
11
+ } & {
12
+ [name: string]: StandardHeaderValue;
13
+ };
14
+ type Setter<T> = ((value: T) => T) | T;
15
+ export declare class ResponseHeaders {
16
+ private readonly headers;
17
+ private constructor();
18
+ set<T extends keyof StandardHeaders>(header: T, setter: Setter<StandardHeaders[T]>): this;
19
+ merge(headers: ResponseHeaders): ResponseHeaders;
20
+ getHeaders(): {
21
+ [k: string]: StandardHeaderValue;
22
+ };
23
+ static create(initialHeaders?: StandardHeaders): ResponseHeaders;
24
+ }
25
+ export {};
@@ -0,0 +1,46 @@
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
+ function isFunction(setter) {
10
+ return typeof setter === "function";
11
+ }
12
+ 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
+ });
19
+ }
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;
27
+ }
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
+ }
44
+ exports.ResponseHeaders = ResponseHeaders;
45
+
46
+ //# sourceMappingURL=ResponseHeaders.js.map
@@ -0,0 +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":[]}