@webiny/handler 0.0.0-mt-2 → 0.0.0-unstable.1145e7667f

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 (42) hide show
  1. package/Context.d.ts +23 -0
  2. package/Context.js +40 -0
  3. package/Context.js.map +1 -0
  4. package/README.md +8 -2
  5. package/fastify.d.ts +8 -0
  6. package/fastify.js +427 -0
  7. package/fastify.js.map +1 -0
  8. package/index.d.ts +9 -1
  9. package/index.js +98 -9
  10. package/index.js.map +1 -0
  11. package/middleware.d.ts +1 -2
  12. package/middleware.js +5 -11
  13. package/middleware.js.map +1 -0
  14. package/package.json +24 -15
  15. package/plugins/BeforeHandlerPlugin.d.ts +8 -7
  16. package/plugins/BeforeHandlerPlugin.js +8 -12
  17. package/plugins/BeforeHandlerPlugin.js.map +1 -0
  18. package/plugins/EventPlugin.d.ts +25 -0
  19. package/plugins/EventPlugin.js +31 -0
  20. package/plugins/EventPlugin.js.map +1 -0
  21. package/plugins/HandlerErrorPlugin.d.ts +12 -0
  22. package/plugins/HandlerErrorPlugin.js +25 -0
  23. package/plugins/HandlerErrorPlugin.js.map +1 -0
  24. package/plugins/HandlerOnRequestPlugin.d.ts +20 -0
  25. package/plugins/HandlerOnRequestPlugin.js +25 -0
  26. package/plugins/HandlerOnRequestPlugin.js.map +1 -0
  27. package/plugins/HandlerResultPlugin.d.ts +12 -0
  28. package/plugins/HandlerResultPlugin.js +25 -0
  29. package/plugins/HandlerResultPlugin.js.map +1 -0
  30. package/plugins/ModifyFastifyPlugin.d.ts +13 -0
  31. package/plugins/ModifyFastifyPlugin.js +25 -0
  32. package/plugins/ModifyFastifyPlugin.js.map +1 -0
  33. package/plugins/RoutePlugin.d.ts +23 -0
  34. package/plugins/RoutePlugin.js +22 -0
  35. package/plugins/RoutePlugin.js.map +1 -0
  36. package/types.d.ts +46 -34
  37. package/types.js +14 -1
  38. package/types.js.map +1 -0
  39. package/createHandler.d.ts +0 -2
  40. package/createHandler.js +0 -70
  41. package/plugins/ContextPlugin.d.ts +0 -11
  42. package/plugins/ContextPlugin.js +0 -32
package/types.d.ts CHANGED
@@ -1,37 +1,49 @@
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 { FastifyInstance, FastifyRequest, FastifyReply, HTTPMethods, RouteHandlerMethod } from "fastify";
2
+ export { FastifyInstance, HTTPMethods } from "fastify";
3
+ import { ClientContext } from "@webiny/handler-client/types";
4
+ export interface RouteMethodOptions {
5
+ override?: boolean;
7
6
  }
8
- export interface ContextInterface {
9
- plugins: PluginsContainer;
10
- args: HandlerArgs;
11
- readonly WEBINY_VERSION: string;
7
+ export declare type RouteMethodPath = `/${string}` | "*";
8
+ export interface RouteMethod {
9
+ (path: RouteMethodPath, handler: RouteHandlerMethod, options?: RouteMethodOptions): void;
12
10
  }
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>;
11
+ export declare type Request = FastifyRequest;
12
+ export declare type Reply = FastifyReply;
13
+ export declare type DefinedContextRoutes = Record<HTTPMethods, string[]>;
14
+ export interface ContextRoutes {
15
+ defined: DefinedContextRoutes;
16
+ onGet: RouteMethod;
17
+ onPost: RouteMethod;
18
+ onPut: RouteMethod;
19
+ onPatch: RouteMethod;
20
+ onDelete: RouteMethod;
21
+ onOptions: RouteMethod;
22
+ onAll: RouteMethod;
23
+ onHead: RouteMethod;
24
+ }
25
+ export interface Context extends ClientContext {
26
+ /**
27
+ * An instance of fastify server.
28
+ * Use at your own risk.
29
+ * @instance
30
+ */
31
+ server: FastifyInstance;
32
+ /**
33
+ * Current request. Must be set only once!
34
+ */
35
+ request: FastifyRequest;
36
+ /**
37
+ * Current reply. Must be set only once!
38
+ */
39
+ reply: FastifyReply;
40
+ /**
41
+ * @internal
42
+ */
43
+ routes: ContextRoutes;
44
+ }
45
+ declare module "fastify" {
46
+ interface FastifyInstance {
47
+ webiny: Context;
48
+ }
21
49
  }
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,17 @@
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
+ Object.defineProperty(exports, "HTTPMethods", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _fastify.HTTPMethods;
16
+ }
17
+ });
18
+ var _fastify = require("fastify");
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import {\n FastifyInstance,\n FastifyRequest,\n FastifyReply,\n HTTPMethods,\n RouteHandlerMethod\n} from \"fastify\";\n\nexport { FastifyInstance, HTTPMethods } from \"fastify\";\nimport { 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 DefinedContextRoutes = Record<HTTPMethods, string[]>;\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 * An instance of fastify server.\n * Use at your own risk.\n * @instance\n */\n server: FastifyInstance;\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 }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA"}
@@ -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
- }
@@ -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");