@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
@@ -0,0 +1,5 @@
1
+ export interface CustomError extends Error {
2
+ code?: string;
3
+ data?: Record<string, any>;
4
+ }
5
+ export declare const stringifyError: (error: CustomError) => string;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.stringifyError = void 0;
7
+ const stringifyError = error => {
8
+ const {
9
+ name,
10
+ message,
11
+ code,
12
+ stack,
13
+ data
14
+ } = error;
15
+ return JSON.stringify({
16
+ ...error,
17
+ constructorName: error.constructor?.name || "UnknownError",
18
+ name: name || "No error name",
19
+ message: message || "No error message",
20
+ code: code || "NO_CODE",
21
+ data,
22
+ stack: process.env.DEBUG === "true" ? stack : "Turn on the debug flag to see the stack."
23
+ });
24
+ };
25
+ exports.stringifyError = stringifyError;
26
+
27
+ //# sourceMappingURL=stringifyError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["stringifyError","error","name","message","code","stack","data","JSON","stringify","constructorName","constructor","process","env","DEBUG","exports"],"sources":["stringifyError.ts"],"sourcesContent":["export interface CustomError extends Error {\n code?: string;\n data?: Record<string, any>;\n}\n\nexport const stringifyError = (error: CustomError) => {\n const { name, message, code, stack, data } = error;\n return JSON.stringify({\n ...error,\n constructorName: error.constructor?.name || \"UnknownError\",\n name: name || \"No error name\",\n message: message || \"No error message\",\n code: code || \"NO_CODE\",\n data,\n stack: process.env.DEBUG === \"true\" ? stack : \"Turn on the debug flag to see the stack.\"\n });\n};\n"],"mappings":";;;;;;AAKO,MAAMA,cAAc,GAAIC,KAAkB,IAAK;EAClD,MAAM;IAAEC,IAAI;IAAEC,OAAO;IAAEC,IAAI;IAAEC,KAAK;IAAEC;EAAK,CAAC,GAAGL,KAAK;EAClD,OAAOM,IAAI,CAACC,SAAS,CAAC;IAClB,GAAGP,KAAK;IACRQ,eAAe,EAAER,KAAK,CAACS,WAAW,EAAER,IAAI,IAAI,cAAc;IAC1DA,IAAI,EAAEA,IAAI,IAAI,eAAe;IAC7BC,OAAO,EAAEA,OAAO,IAAI,kBAAkB;IACtCC,IAAI,EAAEA,IAAI,IAAI,SAAS;IACvBE,IAAI;IACJD,KAAK,EAAEM,OAAO,CAACC,GAAG,CAACC,KAAK,KAAK,MAAM,GAAGR,KAAK,GAAG;EAClD,CAAC,CAAC;AACN,CAAC;AAACS,OAAA,CAAAd,cAAA,GAAAA,cAAA","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ declare const originalConsoleError: {
2
+ (...data: any[]): void;
3
+ (message?: any, ...optionalParams: any[]): void;
4
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ const originalConsoleError = console.error;
4
+ console.error = (message, ...args) => {
5
+ if (typeof message === "string" && message.includes("punycode")) {
6
+ return;
7
+ }
8
+ originalConsoleError.call(console, message, ...args);
9
+ };
10
+
11
+ //# sourceMappingURL=suppressPunycodeWarnings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["originalConsoleError","console","error","message","args","includes","call"],"sources":["suppressPunycodeWarnings.ts"],"sourcesContent":["const originalConsoleError = console.error;\nconsole.error = (message, ...args) => {\n if (typeof message === \"string\" && message.includes(\"punycode\")) {\n return;\n }\n originalConsoleError.call(console, message, ...args);\n};\n"],"mappings":";;AAAA,MAAMA,oBAAoB,GAAGC,OAAO,CAACC,KAAK;AAC1CD,OAAO,CAACC,KAAK,GAAG,CAACC,OAAO,EAAE,GAAGC,IAAI,KAAK;EAClC,IAAI,OAAOD,OAAO,KAAK,QAAQ,IAAIA,OAAO,CAACE,QAAQ,CAAC,UAAU,CAAC,EAAE;IAC7D;EACJ;EACAL,oBAAoB,CAACM,IAAI,CAACL,OAAO,EAAEE,OAAO,EAAE,GAAGC,IAAI,CAAC;AACxD,CAAC","ignoreList":[]}
package/types.d.ts CHANGED
@@ -1,37 +1,46 @@
1
- import { Plugin, PluginsContainer } from "@webiny/plugins/types";
2
- export declare type HandlerArgs = any[];
3
- export interface HandlerContext {
4
- plugins: PluginsContainer;
5
- args: HandlerArgs;
6
- readonly WEBINY_VERSION: string;
1
+ import "@fastify/cookie";
2
+ import type { FastifyRequest, FastifyReply, HTTPMethods as BaseHttpMethods, RouteHandlerMethod } from "fastify";
3
+ export { FastifyInstance } from "fastify";
4
+ import type { ClientContext } from "@webiny/handler-client/types";
5
+ export interface RouteMethodOptions {
6
+ override?: boolean;
7
7
  }
8
- export interface ContextInterface {
9
- plugins: PluginsContainer;
10
- args: HandlerArgs;
11
- readonly WEBINY_VERSION: string;
8
+ export type RouteMethodPath = `/${string}` | "*";
9
+ export interface RouteMethod {
10
+ (path: RouteMethodPath, handler: RouteHandlerMethod, options?: RouteMethodOptions): void;
12
11
  }
13
- export declare type Context<C0 = Record<string, any>, C1 = Record<string, any>, C2 = Record<string, any>, C3 = Record<string, any>, C4 = Record<string, any>, C5 = Record<string, any>, C6 = Record<string, any>, C7 = Record<string, any>, C8 = Record<string, any>, C9 = Record<string, any>> = {
14
- plugins: PluginsContainer;
15
- args: HandlerArgs;
16
- readonly WEBINY_VERSION: string;
17
- } & C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9;
18
- export interface BeforeHandlerPlugin<T extends ContextInterface = ContextInterface> extends Plugin {
19
- type: "before-handler";
20
- apply: (context: T) => Promise<void>;
12
+ export type Request = FastifyRequest;
13
+ export type Reply = FastifyReply;
14
+ export type HTTPMethods = Uppercase<BaseHttpMethods>;
15
+ export type DefinedContextRoutes = Record<HTTPMethods, string[]>;
16
+ export interface ContextRoutes {
17
+ defined: DefinedContextRoutes;
18
+ onGet: RouteMethod;
19
+ onPost: RouteMethod;
20
+ onPut: RouteMethod;
21
+ onPatch: RouteMethod;
22
+ onDelete: RouteMethod;
23
+ onOptions: RouteMethod;
24
+ onAll: RouteMethod;
25
+ onHead: RouteMethod;
26
+ }
27
+ export interface Context extends ClientContext {
28
+ /**
29
+ * Current request. Must be set only once!
30
+ */
31
+ request: FastifyRequest;
32
+ /**
33
+ * Current reply. Must be set only once!
34
+ */
35
+ reply: FastifyReply;
36
+ /**
37
+ * @internal
38
+ */
39
+ routes: ContextRoutes;
40
+ }
41
+ declare module "fastify" {
42
+ interface FastifyInstance {
43
+ webiny: Context;
44
+ __webiny_raw_result: any;
45
+ }
21
46
  }
22
- export declare type ContextPlugin<C0 = Context, C1 = Context, C2 = Context, C3 = Context, C4 = Context, C5 = Context, C6 = Context, C7 = Context, C8 = Context, C9 = Context> = Plugin & {
23
- type: "context";
24
- apply(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9): Promise<void>;
25
- };
26
- export declare type HandlerPlugin<C0 = Context, C1 = Context, C2 = Context, C3 = Context, C4 = Context, C5 = Context, C6 = Context, C7 = Context, C8 = Context, C9 = Context> = Plugin & {
27
- type: "handler";
28
- handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, next: Function): any;
29
- };
30
- export declare type HandlerResultPlugin<C0 = Context, C1 = Context, C2 = Context, C3 = Context, C4 = Context, C5 = Context, C6 = Context, C7 = Context, C8 = Context, C9 = Context> = Plugin & {
31
- type: "handler-result";
32
- handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, result: any): any;
33
- };
34
- export declare type HandlerErrorPlugin<C0 = Context, C1 = Context, C2 = Context, C3 = Context, C4 = Context, C5 = Context, C6 = Context, C7 = Context, C8 = Context, C9 = Context> = Plugin & {
35
- type: "handler-error";
36
- handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, error: any, next: Function): Promise<any>;
37
- };
package/types.js CHANGED
@@ -2,4 +2,14 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
- });
5
+ });
6
+ Object.defineProperty(exports, "FastifyInstance", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _fastify.FastifyInstance;
10
+ }
11
+ });
12
+ require("@fastify/cookie");
13
+ var _fastify = require("fastify");
14
+
15
+ //# sourceMappingURL=types.js.map
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["require","_fastify"],"sources":["types.ts"],"sourcesContent":["import \"@fastify/cookie\";\nimport type {\n FastifyRequest,\n FastifyReply,\n HTTPMethods as BaseHttpMethods,\n RouteHandlerMethod\n} from \"fastify\";\nexport { FastifyInstance } from \"fastify\";\nimport type { ClientContext } from \"@webiny/handler-client/types\";\n\nexport interface RouteMethodOptions {\n override?: boolean;\n}\n\nexport type RouteMethodPath = `/${string}` | \"*\";\nexport interface RouteMethod {\n (path: RouteMethodPath, handler: RouteHandlerMethod, options?: RouteMethodOptions): void;\n}\n\nexport type Request = FastifyRequest;\nexport type Reply = FastifyReply;\n\nexport type HTTPMethods = Uppercase<BaseHttpMethods>;\n\nexport type DefinedContextRoutes = Record<HTTPMethods, string[]>;\n\nexport interface ContextRoutes {\n defined: DefinedContextRoutes;\n onGet: RouteMethod;\n onPost: RouteMethod;\n onPut: RouteMethod;\n onPatch: RouteMethod;\n onDelete: RouteMethod;\n onOptions: RouteMethod;\n onAll: RouteMethod;\n onHead: RouteMethod;\n}\n\nexport interface Context extends ClientContext {\n /**\n * Current request. Must be set only once!\n */\n request: FastifyRequest;\n /**\n * Current reply. Must be set only once!\n */\n reply: FastifyReply;\n /**\n * @internal\n */\n routes: ContextRoutes;\n}\n\ndeclare module \"fastify\" {\n interface FastifyInstance {\n webiny: Context;\n __webiny_raw_result: any;\n }\n}\n"],"mappings":";;;;;;;;;;;AAAAA,OAAA;AAOA,IAAAC,QAAA,GAAAD,OAAA","ignoreList":[]}
@@ -1,2 +0,0 @@
1
- declare const _default: (...plugins: any[]) => (...args: any[]) => Promise<any>;
2
- export default _default;
package/createHandler.js DELETED
@@ -1,70 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = void 0;
9
-
10
- var _plugins = require("@webiny/plugins");
11
-
12
- var _middleware = _interopRequireDefault(require("./middleware"));
13
-
14
- var _default = (...plugins) => async (...args) => {
15
- const context = {
16
- plugins: new _plugins.PluginsContainer(plugins),
17
- args,
18
- // @ts-ignore
19
- // this is injected using webpack.DefinePlugin at build time
20
- WEBINY_VERSION: process.env.WEBINY_VERSION
21
- };
22
- const result = await handle(args, context);
23
- const handlerPlugins = context.plugins.byType("handler-result");
24
-
25
- for (let i = 0; i < handlerPlugins.length; i++) {
26
- if (handlerPlugins[i].apply) {
27
- await handlerPlugins[i].apply(result, context);
28
- }
29
- }
30
-
31
- return result;
32
- };
33
-
34
- exports.default = _default;
35
-
36
- async function handle(_, context) {
37
- try {
38
- const contextPlugins = context.plugins.byType("context");
39
-
40
- for (let i = 0; i < contextPlugins.length; i++) {
41
- if (contextPlugins[i].apply) {
42
- await contextPlugins[i].apply(context);
43
- }
44
- }
45
-
46
- const beforeHandlerPlugins = context.plugins.byType("before-handler");
47
-
48
- for (let i = 0; i < beforeHandlerPlugins.length; i++) {
49
- if (beforeHandlerPlugins[i].apply) {
50
- await beforeHandlerPlugins[i].apply(context);
51
- }
52
- }
53
-
54
- const handlers = context.plugins.byType("handler");
55
- const handler = (0, _middleware.default)(handlers.map(pl => pl.handle));
56
- const result = await handler(context);
57
-
58
- if (!result) {
59
- throw Error(`No result was returned from registered handlers.`);
60
- }
61
-
62
- return result;
63
- } catch (error) {
64
- // Log error to cloud, as these can be extremely annoying to debug!
65
- console.log(error);
66
- const handlers = context.plugins.byType("handler-error");
67
- const handler = (0, _middleware.default)(handlers.map(pl => pl.handle));
68
- return handler(context, error);
69
- }
70
- }
package/middleware.d.ts DELETED
@@ -1,5 +0,0 @@
1
- declare const _default: (functions?: Array<Function>) => Function;
2
- /**
3
- * Compose a single middleware from the array of middleware functions
4
- */
5
- export default _default;
package/middleware.js DELETED
@@ -1,51 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- /**
9
- * Compose a single middleware from the array of middleware functions
10
- */
11
- var _default = (functions = []) => {
12
- return (...args) => {
13
- if (!functions.length) {
14
- return Promise.resolve();
15
- } // Create a clone of function chain to prevent modifying the original array with `shift()`
16
-
17
-
18
- const chain = [...functions];
19
- return new Promise((parentResolve, parentReject) => {
20
- const next = async () => {
21
- const fn = chain.shift();
22
-
23
- if (!fn) {
24
- return Promise.resolve();
25
- }
26
-
27
- return new Promise(async (resolve, reject) => {
28
- try {
29
- const result = await fn(...args, resolve);
30
-
31
- if (typeof result !== "undefined") {
32
- return parentResolve(result);
33
- }
34
- } catch (e) {
35
- reject(e);
36
- }
37
- }).then(() => {
38
- return next();
39
- }).then(() => {
40
- parentResolve(...args);
41
- }).catch(e => {
42
- parentReject(e);
43
- });
44
- };
45
-
46
- return next();
47
- });
48
- };
49
- };
50
-
51
- exports.default = _default;
@@ -1,11 +0,0 @@
1
- import { Plugin } from "@webiny/plugins";
2
- interface Callable<TContext> {
3
- (context: TContext): void | Promise<void>;
4
- }
5
- export declare class ContextPlugin<TContext> extends Plugin {
6
- static readonly type = "context";
7
- private readonly _callable;
8
- constructor(callable?: Callable<TContext>);
9
- apply(context: TContext): void | Promise<void>;
10
- }
11
- export {};
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.ContextPlugin = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
- var _plugins = require("@webiny/plugins");
13
-
14
- class ContextPlugin extends _plugins.Plugin {
15
- constructor(callable) {
16
- super();
17
- (0, _defineProperty2.default)(this, "_callable", void 0);
18
- this._callable = callable;
19
- }
20
-
21
- apply(context) {
22
- if (typeof this._callable !== "function") {
23
- throw Error(`Missing callable in ContextPlugin! Either pass a callable to plugin constructor or extend the plugin and override the "apply" method.`);
24
- }
25
-
26
- return this._callable(context);
27
- }
28
-
29
- }
30
-
31
- exports.ContextPlugin = ContextPlugin;
32
- (0, _defineProperty2.default)(ContextPlugin, "type", "context");