@webiny/handler 5.20.0-beta.2 → 5.22.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/createHandler.js +56 -22
- package/index.d.ts +6 -0
- package/index.js +88 -1
- package/package.json +10 -9
- package/plugins/BeforeHandlerPlugin.d.ts +7 -7
- package/plugins/Context.d.ts +20 -0
- package/plugins/Context.js +56 -0
- package/plugins/ContextPlugin.d.ts +6 -6
- package/plugins/HandlerErrorPlugin.d.ts +11 -0
- package/plugins/HandlerErrorPlugin.js +28 -0
- package/plugins/HandlerPlugin.d.ts +11 -0
- package/plugins/HandlerPlugin.js +28 -0
- package/plugins/HandlerResultPlugin.d.ts +11 -0
- package/plugins/HandlerResultPlugin.js +28 -0
- package/types.d.ts +56 -15
package/createHandler.js
CHANGED
|
@@ -7,25 +7,39 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.default = void 0;
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _HandlerPlugin = require("./plugins/HandlerPlugin");
|
|
11
|
+
|
|
12
|
+
var _ContextPlugin = require("./plugins/ContextPlugin");
|
|
11
13
|
|
|
12
14
|
var _middleware = _interopRequireDefault(require("./middleware"));
|
|
13
15
|
|
|
16
|
+
var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
|
|
17
|
+
|
|
18
|
+
var _Context = require("./plugins/Context");
|
|
19
|
+
|
|
20
|
+
var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
|
|
21
|
+
|
|
22
|
+
var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
|
|
23
|
+
|
|
14
24
|
var _default = (...plugins) => async (...args) => {
|
|
15
|
-
const context = {
|
|
16
|
-
plugins
|
|
25
|
+
const context = new _Context.Context({
|
|
26
|
+
plugins,
|
|
17
27
|
args,
|
|
18
|
-
|
|
19
|
-
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Inserted via webpack on build time.
|
|
31
|
+
*/
|
|
20
32
|
WEBINY_VERSION: process.env.WEBINY_VERSION
|
|
21
|
-
};
|
|
22
|
-
const result = await handle(
|
|
23
|
-
const handlerPlugins = context.plugins.byType(
|
|
33
|
+
});
|
|
34
|
+
const result = await handle(context);
|
|
35
|
+
const handlerPlugins = context.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
|
|
24
36
|
|
|
25
37
|
for (let i = 0; i < handlerPlugins.length; i++) {
|
|
26
|
-
if (handlerPlugins[i].
|
|
27
|
-
|
|
38
|
+
if (!handlerPlugins[i].handle) {
|
|
39
|
+
continue;
|
|
28
40
|
}
|
|
41
|
+
|
|
42
|
+
await handlerPlugins[i].handle(context, result);
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
return result;
|
|
@@ -33,38 +47,58 @@ var _default = (...plugins) => async (...args) => {
|
|
|
33
47
|
|
|
34
48
|
exports.default = _default;
|
|
35
49
|
|
|
36
|
-
async function handle(
|
|
50
|
+
async function handle(context) {
|
|
37
51
|
try {
|
|
38
|
-
const contextPlugins = context.plugins.byType(
|
|
52
|
+
const contextPlugins = context.plugins.byType(_ContextPlugin.ContextPlugin.type);
|
|
39
53
|
|
|
40
54
|
for (let i = 0; i < contextPlugins.length; i++) {
|
|
41
|
-
if (contextPlugins[i].apply) {
|
|
42
|
-
|
|
55
|
+
if (!contextPlugins[i].apply) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await contextPlugins[i].apply(context);
|
|
60
|
+
|
|
61
|
+
if (context.hasResult()) {
|
|
62
|
+
return context.getResult();
|
|
43
63
|
}
|
|
44
64
|
}
|
|
45
65
|
|
|
46
|
-
const beforeHandlerPlugins = context.plugins.byType(
|
|
66
|
+
const beforeHandlerPlugins = context.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
|
|
47
67
|
|
|
48
68
|
for (let i = 0; i < beforeHandlerPlugins.length; i++) {
|
|
49
|
-
if (beforeHandlerPlugins[i].apply) {
|
|
50
|
-
|
|
69
|
+
if (!beforeHandlerPlugins[i].apply) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
await beforeHandlerPlugins[i].apply(context);
|
|
74
|
+
|
|
75
|
+
if (context.hasResult()) {
|
|
76
|
+
return context.getResult();
|
|
51
77
|
}
|
|
52
78
|
}
|
|
53
79
|
|
|
54
|
-
const handlers = context.plugins.byType(
|
|
55
|
-
const handler = (0, _middleware.default)(handlers.map(pl =>
|
|
80
|
+
const handlers = context.plugins.byType(_HandlerPlugin.HandlerPlugin.type);
|
|
81
|
+
const handler = (0, _middleware.default)(handlers.map(pl => {
|
|
82
|
+
return (context, next) => {
|
|
83
|
+
return pl.handle(context, next);
|
|
84
|
+
};
|
|
85
|
+
}));
|
|
56
86
|
const result = await handler(context);
|
|
57
87
|
|
|
58
88
|
if (!result) {
|
|
59
|
-
throw Error(`No result was returned from registered handlers.`);
|
|
89
|
+
throw new Error(`No result was returned from registered handlers.`);
|
|
60
90
|
}
|
|
61
91
|
|
|
62
92
|
return result;
|
|
63
93
|
} catch (error) {
|
|
64
94
|
// Log error to cloud, as these can be extremely annoying to debug!
|
|
65
95
|
console.log(error);
|
|
66
|
-
const handlers = context.plugins.byType(
|
|
67
|
-
const handler = (0, _middleware.default)(handlers.map(pl =>
|
|
96
|
+
const handlers = context.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
|
|
97
|
+
const handler = (0, _middleware.default)(handlers.map(pl => {
|
|
98
|
+
return (context, error, next) => {
|
|
99
|
+
return pl.handle(context, error, next);
|
|
100
|
+
};
|
|
101
|
+
}));
|
|
68
102
|
return handler(context, error);
|
|
69
103
|
}
|
|
70
104
|
}
|
package/index.d.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
1
|
export { default as createHandler } from "./createHandler";
|
|
2
|
+
export * from "./plugins/ContextPlugin";
|
|
3
|
+
export * from "./plugins/Context";
|
|
4
|
+
export * from "./plugins/HandlerPlugin";
|
|
5
|
+
export * from "./plugins/HandlerErrorPlugin";
|
|
6
|
+
export * from "./plugins/HandlerResultPlugin";
|
|
7
|
+
export * from "./plugins/BeforeHandlerPlugin";
|
package/index.js
CHANGED
|
@@ -5,6 +5,9 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
+
var _exportNames = {
|
|
9
|
+
createHandler: true
|
|
10
|
+
};
|
|
8
11
|
Object.defineProperty(exports, "createHandler", {
|
|
9
12
|
enumerable: true,
|
|
10
13
|
get: function () {
|
|
@@ -12,4 +15,88 @@ Object.defineProperty(exports, "createHandler", {
|
|
|
12
15
|
}
|
|
13
16
|
});
|
|
14
17
|
|
|
15
|
-
var _createHandler = _interopRequireDefault(require("./createHandler"));
|
|
18
|
+
var _createHandler = _interopRequireDefault(require("./createHandler"));
|
|
19
|
+
|
|
20
|
+
var _ContextPlugin = require("./plugins/ContextPlugin");
|
|
21
|
+
|
|
22
|
+
Object.keys(_ContextPlugin).forEach(function (key) {
|
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
|
24
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
25
|
+
if (key in exports && exports[key] === _ContextPlugin[key]) return;
|
|
26
|
+
Object.defineProperty(exports, key, {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () {
|
|
29
|
+
return _ContextPlugin[key];
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
var _Context = require("./plugins/Context");
|
|
35
|
+
|
|
36
|
+
Object.keys(_Context).forEach(function (key) {
|
|
37
|
+
if (key === "default" || key === "__esModule") return;
|
|
38
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
39
|
+
if (key in exports && exports[key] === _Context[key]) return;
|
|
40
|
+
Object.defineProperty(exports, key, {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () {
|
|
43
|
+
return _Context[key];
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
var _HandlerPlugin = require("./plugins/HandlerPlugin");
|
|
49
|
+
|
|
50
|
+
Object.keys(_HandlerPlugin).forEach(function (key) {
|
|
51
|
+
if (key === "default" || key === "__esModule") return;
|
|
52
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
53
|
+
if (key in exports && exports[key] === _HandlerPlugin[key]) return;
|
|
54
|
+
Object.defineProperty(exports, key, {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () {
|
|
57
|
+
return _HandlerPlugin[key];
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
|
|
63
|
+
|
|
64
|
+
Object.keys(_HandlerErrorPlugin).forEach(function (key) {
|
|
65
|
+
if (key === "default" || key === "__esModule") return;
|
|
66
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
67
|
+
if (key in exports && exports[key] === _HandlerErrorPlugin[key]) return;
|
|
68
|
+
Object.defineProperty(exports, key, {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
get: function () {
|
|
71
|
+
return _HandlerErrorPlugin[key];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
|
|
77
|
+
|
|
78
|
+
Object.keys(_HandlerResultPlugin).forEach(function (key) {
|
|
79
|
+
if (key === "default" || key === "__esModule") return;
|
|
80
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
81
|
+
if (key in exports && exports[key] === _HandlerResultPlugin[key]) return;
|
|
82
|
+
Object.defineProperty(exports, key, {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
get: function () {
|
|
85
|
+
return _HandlerResultPlugin[key];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
|
|
91
|
+
|
|
92
|
+
Object.keys(_BeforeHandlerPlugin).forEach(function (key) {
|
|
93
|
+
if (key === "default" || key === "__esModule") return;
|
|
94
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
95
|
+
if (key in exports && exports[key] === _BeforeHandlerPlugin[key]) return;
|
|
96
|
+
Object.defineProperty(exports, key, {
|
|
97
|
+
enumerable: true,
|
|
98
|
+
get: function () {
|
|
99
|
+
return _BeforeHandlerPlugin[key];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/handler",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.22.0-beta.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -14,16 +14,17 @@
|
|
|
14
14
|
"Adrian Smijulj <adrian@webiny.com>"
|
|
15
15
|
],
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@babel/runtime": "7.16.
|
|
18
|
-
"@webiny/plugins": "5.
|
|
17
|
+
"@babel/runtime": "7.16.7",
|
|
18
|
+
"@webiny/plugins": "5.22.0-beta.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@babel/cli": "^7.
|
|
22
|
-
"@babel/core": "^7.
|
|
23
|
-
"@babel/preset-env": "^7.
|
|
24
|
-
"@webiny/cli": "^5.
|
|
25
|
-
"@webiny/project-utils": "^5.
|
|
21
|
+
"@babel/cli": "^7.16.0",
|
|
22
|
+
"@babel/core": "^7.16.0",
|
|
23
|
+
"@babel/preset-env": "^7.16.4",
|
|
24
|
+
"@webiny/cli": "^5.22.0-beta.0",
|
|
25
|
+
"@webiny/project-utils": "^5.22.0-beta.0",
|
|
26
26
|
"rimraf": "^3.0.2",
|
|
27
|
+
"ttypescript": "^1.5.13",
|
|
27
28
|
"typescript": "^4.1.3"
|
|
28
29
|
},
|
|
29
30
|
"publishConfig": {
|
|
@@ -34,5 +35,5 @@
|
|
|
34
35
|
"build": "yarn webiny run build",
|
|
35
36
|
"watch": "yarn webiny run watch"
|
|
36
37
|
},
|
|
37
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "487330472a07000c4845340f3b3dfa6b3ae98944"
|
|
38
39
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Plugin } from "@webiny/plugins";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { Context } from "../types";
|
|
3
|
+
export interface BeforeHandlerCallable<T extends Context = Context> {
|
|
4
|
+
(context: T): void | Promise<void>;
|
|
4
5
|
}
|
|
5
|
-
export declare class BeforeHandlerPlugin<
|
|
6
|
-
static readonly type
|
|
6
|
+
export declare class BeforeHandlerPlugin<T extends Context = Context> extends Plugin {
|
|
7
|
+
static readonly type: string;
|
|
7
8
|
private readonly _callable;
|
|
8
|
-
constructor(callable?:
|
|
9
|
-
apply(context:
|
|
9
|
+
constructor(callable?: BeforeHandlerCallable<T>);
|
|
10
|
+
apply(context: T): void | Promise<void>;
|
|
10
11
|
}
|
|
11
|
-
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Context as ContextInterface, HandlerArgs } from "../types";
|
|
2
|
+
import { PluginsContainer } from "@webiny/plugins";
|
|
3
|
+
export interface Params {
|
|
4
|
+
args: HandlerArgs;
|
|
5
|
+
plugins?: Plugin | Plugin[] | Plugin[][] | PluginsContainer;
|
|
6
|
+
WEBINY_VERSION: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class Context implements ContextInterface {
|
|
9
|
+
_result: any;
|
|
10
|
+
private readonly _plugins;
|
|
11
|
+
private readonly _args;
|
|
12
|
+
private readonly _version;
|
|
13
|
+
get plugins(): PluginsContainer;
|
|
14
|
+
get args(): HandlerArgs;
|
|
15
|
+
get WEBINY_VERSION(): string;
|
|
16
|
+
constructor(params: Params);
|
|
17
|
+
getResult(): any;
|
|
18
|
+
hasResult(): boolean;
|
|
19
|
+
setResult(value: any): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.Context = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _plugins = require("@webiny/plugins");
|
|
13
|
+
|
|
14
|
+
class Context {
|
|
15
|
+
get plugins() {
|
|
16
|
+
return this._plugins;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get args() {
|
|
20
|
+
return this._args;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get WEBINY_VERSION() {
|
|
24
|
+
return this._version;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
constructor(params) {
|
|
28
|
+
(0, _defineProperty2.default)(this, "_result", void 0);
|
|
29
|
+
(0, _defineProperty2.default)(this, "_plugins", void 0);
|
|
30
|
+
(0, _defineProperty2.default)(this, "_args", void 0);
|
|
31
|
+
(0, _defineProperty2.default)(this, "_version", void 0);
|
|
32
|
+
const {
|
|
33
|
+
plugins,
|
|
34
|
+
args,
|
|
35
|
+
WEBINY_VERSION
|
|
36
|
+
} = params;
|
|
37
|
+
this._plugins = new _plugins.PluginsContainer(plugins || []);
|
|
38
|
+
this._args = args;
|
|
39
|
+
this._version = WEBINY_VERSION;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getResult() {
|
|
43
|
+
return this._result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
hasResult() {
|
|
47
|
+
return !!this._result;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
setResult(value) {
|
|
51
|
+
this._result = value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.Context = Context;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Plugin } from "@webiny/plugins";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { Context } from "../types";
|
|
3
|
+
export interface ContextCallable<T extends Context = Context> {
|
|
4
|
+
(context: T): void | Promise<void>;
|
|
4
5
|
}
|
|
5
|
-
export declare class ContextPlugin<
|
|
6
|
+
export declare class ContextPlugin<T extends Context = Context> extends Plugin {
|
|
6
7
|
static readonly type = "context";
|
|
7
8
|
private readonly _callable;
|
|
8
|
-
constructor(callable?:
|
|
9
|
-
apply(context:
|
|
9
|
+
constructor(callable?: ContextCallable<T>);
|
|
10
|
+
apply(context: T): void | Promise<void>;
|
|
10
11
|
}
|
|
11
|
-
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Plugin } from "@webiny/plugins";
|
|
2
|
+
import { Context } from "../types";
|
|
3
|
+
export interface HandlerErrorCallable<T extends Context = Context> {
|
|
4
|
+
(context: T, error: Error, next: Function): Promise<any>;
|
|
5
|
+
}
|
|
6
|
+
export declare class HandlerErrorPlugin<T extends Context = Context> extends Plugin {
|
|
7
|
+
static readonly type: string;
|
|
8
|
+
private readonly _callable;
|
|
9
|
+
constructor(callable: HandlerErrorCallable<T>);
|
|
10
|
+
handle(context: T, error: Error, next: Function): Promise<any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.HandlerErrorPlugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _plugins = require("@webiny/plugins");
|
|
13
|
+
|
|
14
|
+
class HandlerErrorPlugin extends _plugins.Plugin {
|
|
15
|
+
constructor(callable) {
|
|
16
|
+
super();
|
|
17
|
+
(0, _defineProperty2.default)(this, "_callable", void 0);
|
|
18
|
+
this._callable = callable;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async handle(context, error, next) {
|
|
22
|
+
return this._callable(context, error, next);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.HandlerErrorPlugin = HandlerErrorPlugin;
|
|
28
|
+
(0, _defineProperty2.default)(HandlerErrorPlugin, "type", "handler-error");
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Plugin } from "@webiny/plugins";
|
|
2
|
+
import { Context } from "../types";
|
|
3
|
+
export interface HandlerCallable<T extends Context = Context> {
|
|
4
|
+
(context: T, next: Function): Promise<any>;
|
|
5
|
+
}
|
|
6
|
+
export declare class HandlerPlugin<T extends Context = Context> extends Plugin {
|
|
7
|
+
static readonly type: string;
|
|
8
|
+
private readonly _callable;
|
|
9
|
+
constructor(callable: HandlerCallable<T>);
|
|
10
|
+
handle(context: T, next: Function): Promise<any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.HandlerPlugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _plugins = require("@webiny/plugins");
|
|
13
|
+
|
|
14
|
+
class HandlerPlugin extends _plugins.Plugin {
|
|
15
|
+
constructor(callable) {
|
|
16
|
+
super();
|
|
17
|
+
(0, _defineProperty2.default)(this, "_callable", void 0);
|
|
18
|
+
this._callable = callable;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async handle(context, next) {
|
|
22
|
+
return this._callable(context, next);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.HandlerPlugin = HandlerPlugin;
|
|
28
|
+
(0, _defineProperty2.default)(HandlerPlugin, "type", "handler");
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Plugin } from "@webiny/plugins";
|
|
2
|
+
import { Context } from "../types";
|
|
3
|
+
export interface HandlerResultCallable<T extends Context = Context> {
|
|
4
|
+
(context: T, result: any): Promise<any>;
|
|
5
|
+
}
|
|
6
|
+
export declare class HandlerResultPlugin<T extends Context = Context> extends Plugin {
|
|
7
|
+
static readonly type: string;
|
|
8
|
+
private readonly _callable;
|
|
9
|
+
constructor(callable: HandlerResultCallable<T>);
|
|
10
|
+
handle(context: T, result: any): Promise<any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.HandlerResultPlugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _plugins = require("@webiny/plugins");
|
|
13
|
+
|
|
14
|
+
class HandlerResultPlugin extends _plugins.Plugin {
|
|
15
|
+
constructor(callable) {
|
|
16
|
+
super();
|
|
17
|
+
(0, _defineProperty2.default)(this, "_callable", void 0);
|
|
18
|
+
this._callable = callable;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async handle(context, result) {
|
|
22
|
+
return this._callable(context, result);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.HandlerResultPlugin = HandlerResultPlugin;
|
|
28
|
+
(0, _defineProperty2.default)(HandlerResultPlugin, "type", "handler-result");
|
package/types.d.ts
CHANGED
|
@@ -1,36 +1,77 @@
|
|
|
1
1
|
import { Plugin, PluginsContainer } from "@webiny/plugins/types";
|
|
2
2
|
export declare type HandlerArgs = any[];
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Left for backwards compatibility.
|
|
5
|
+
* @deprecated
|
|
6
|
+
*/
|
|
7
|
+
export declare type HandlerContext = Context;
|
|
8
|
+
/**
|
|
9
|
+
* The main context which is constructed on every request.
|
|
10
|
+
* All other contexts should extend this one.
|
|
11
|
+
*/
|
|
12
|
+
export interface Context {
|
|
4
13
|
plugins: PluginsContainer;
|
|
5
14
|
args: HandlerArgs;
|
|
6
15
|
readonly WEBINY_VERSION: string;
|
|
16
|
+
/**
|
|
17
|
+
* Not to be used outside of Webiny internal code.
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
hasResult: () => boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Not to be used outside of Webiny internal code.
|
|
23
|
+
* @internal
|
|
24
|
+
*
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
_result?: any;
|
|
28
|
+
/**
|
|
29
|
+
* Not to be used outside of Webiny internal code.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
setResult: (value: any) => void;
|
|
33
|
+
/**
|
|
34
|
+
* Not to be used outside of Webiny internal code.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
getResult: () => void;
|
|
7
38
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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>;
|
|
21
|
-
}
|
|
39
|
+
/**
|
|
40
|
+
* Left for backwards-compatibility.
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
* @deprecated
|
|
44
|
+
*/
|
|
22
45
|
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
46
|
type: "context";
|
|
24
47
|
apply(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9): Promise<void>;
|
|
25
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* Left for backwards-compatibility.
|
|
51
|
+
*
|
|
52
|
+
* @internal
|
|
53
|
+
* @deprecated
|
|
54
|
+
*/
|
|
26
55
|
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
56
|
type: "handler";
|
|
28
57
|
handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, next: Function): any;
|
|
29
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* Left for backwards-compatibility.
|
|
61
|
+
*
|
|
62
|
+
* @internal
|
|
63
|
+
* @deprecated
|
|
64
|
+
*/
|
|
30
65
|
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
66
|
type: "handler-result";
|
|
32
67
|
handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, result: any): any;
|
|
33
68
|
};
|
|
69
|
+
/**
|
|
70
|
+
* Left for backwards-compatibility.
|
|
71
|
+
*
|
|
72
|
+
* @internal
|
|
73
|
+
* @deprecated
|
|
74
|
+
*/
|
|
34
75
|
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
76
|
type: "handler-error";
|
|
36
77
|
handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, error: any, next: Function): Promise<any>;
|