@webiny/handler 0.0.0-mt-1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # @webiny/handler
2
+ [![](https://img.shields.io/npm/dw/@webiny/handler.svg)](https://www.npmjs.com/package/@webiny/handler)
3
+ [![](https://img.shields.io/npm/v/@webiny/handler.svg)](https://www.npmjs.com/package/@webiny/handler)
4
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
+
7
+ A tiny, universal, and plugin-based framework for creating (serverless) handlers, used within services like AWS Lambda, Azure Serverless Functions, and others.
8
+
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.
@@ -0,0 +1,2 @@
1
+ declare const _default: (...plugins: any[]) => (...args: any[]) => Promise<any>;
2
+ export default _default;
@@ -0,0 +1,70 @@
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/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { default as createHandler } from "./createHandler";
package/index.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ Object.defineProperty(exports, "createHandler", {
9
+ enumerable: true,
10
+ get: function () {
11
+ return _createHandler.default;
12
+ }
13
+ });
14
+
15
+ var _createHandler = _interopRequireDefault(require("./createHandler"));
@@ -0,0 +1,5 @@
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 ADDED
@@ -0,0 +1,51 @@
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;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@webiny/handler",
3
+ "version": "0.0.0-mt-1",
4
+ "main": "index.js",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/webiny/webiny-js.git"
9
+ },
10
+ "description": "A pluginable function for the cloud.",
11
+ "contributors": [
12
+ "Pavel Denisjuk <pavel@webiny.com>",
13
+ "Sven Al Hamad <sven@webiny.com>",
14
+ "Adrian Smijulj <adrian@webiny.com>"
15
+ ],
16
+ "dependencies": {
17
+ "@babel/runtime": "7.15.4",
18
+ "@webiny/plugins": "0.0.0-mt-1"
19
+ },
20
+ "devDependencies": {
21
+ "@babel/cli": "^7.5.5",
22
+ "@babel/core": "^7.5.5",
23
+ "@babel/preset-env": "^7.5.5",
24
+ "@webiny/cli": "^0.0.0-mt-1",
25
+ "@webiny/project-utils": "^0.0.0-mt-1",
26
+ "rimraf": "^3.0.2",
27
+ "typescript": "^4.1.3"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public",
31
+ "directory": "dist"
32
+ },
33
+ "scripts": {
34
+ "build": "yarn webiny run build",
35
+ "watch": "yarn webiny run watch"
36
+ },
37
+ "gitHead": "37736d8456a6ecb342a6c3645060bd9a3f2d4bb0"
38
+ }
@@ -0,0 +1,11 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ interface Callable<TContext> {
3
+ (context: TContext): void | Promise<void>;
4
+ }
5
+ export declare class BeforeHandlerPlugin<TContext> extends Plugin {
6
+ static readonly type = "before-handler";
7
+ private readonly _callable;
8
+ constructor(callable?: Callable<TContext>);
9
+ apply(context: TContext): void | Promise<void>;
10
+ }
11
+ export {};
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.BeforeHandlerPlugin = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _plugins = require("@webiny/plugins");
13
+
14
+ class BeforeHandlerPlugin 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 BeforeHandlerPlugin! 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.BeforeHandlerPlugin = BeforeHandlerPlugin;
32
+ (0, _defineProperty2.default)(BeforeHandlerPlugin, "type", "before-handler");
@@ -0,0 +1,11 @@
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 {};
@@ -0,0 +1,32 @@
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");
package/types.d.ts ADDED
@@ -0,0 +1,37 @@
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;
7
+ }
8
+ export interface ContextInterface {
9
+ plugins: PluginsContainer;
10
+ args: HandlerArgs;
11
+ readonly WEBINY_VERSION: string;
12
+ }
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>;
21
+ }
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 ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });