@moostjs/event-wf 0.2.34

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # @moostjs/event-wf
package/dist/index.cjs ADDED
@@ -0,0 +1,156 @@
1
+ 'use strict';
2
+
3
+ var moost = require('moost');
4
+ var eventWf = require('@wooksjs/event-wf');
5
+ var eventCore = require('@wooksjs/event-core');
6
+ require('wooks');
7
+
8
+ /******************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+ /* global Reflect, Promise */
23
+
24
+
25
+ function __awaiter(thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ }
34
+
35
+ function getWfMate() {
36
+ return moost.getMoostMate();
37
+ }
38
+
39
+ const LOGGER_TITLE = 'moost-wf';
40
+ const CONTEXT_TYPE = 'WF';
41
+ class MoostWf {
42
+ constructor(opts) {
43
+ this.opts = opts;
44
+ const wfAppOpts = opts === null || opts === void 0 ? void 0 : opts.wooksWf;
45
+ if (wfAppOpts && wfAppOpts instanceof eventWf.WooksWf) {
46
+ this.wfApp = wfAppOpts;
47
+ }
48
+ else if (wfAppOpts) {
49
+ this.wfApp = eventWf.createWfApp(wfAppOpts);
50
+ }
51
+ else {
52
+ this.wfApp = eventWf.createWfApp();
53
+ }
54
+ if (!(opts === null || opts === void 0 ? void 0 : opts.debug)) {
55
+ moost.getMoostInfact().silent(true);
56
+ }
57
+ }
58
+ onNotFound() {
59
+ var _a;
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const response = yield moost.defineMoostEventHandler({
62
+ loggerTitle: LOGGER_TITLE,
63
+ getIterceptorHandler: () => { var _a; return (_a = this.moost) === null || _a === void 0 ? void 0 : _a.getGlobalInterceptorHandler(); },
64
+ getControllerInstance: () => this.moost,
65
+ callControllerMethod: () => undefined,
66
+ logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
67
+ })();
68
+ return response;
69
+ });
70
+ }
71
+ onInit(moost) {
72
+ this.moost = moost;
73
+ }
74
+ start(schemaId, inputContext, input) {
75
+ return this.wfApp.start(schemaId, inputContext, input, () => {
76
+ const scopeId = eventCore.useEventId().getId();
77
+ moost.getMoostInfact().unregisterScope(scopeId);
78
+ });
79
+ }
80
+ resume(schemaId, inputContext, indexes, input) {
81
+ return this.wfApp.resume(schemaId, inputContext, indexes, input, () => {
82
+ const scopeId = eventCore.useEventId().getId();
83
+ moost.getMoostInfact().unregisterScope(scopeId);
84
+ });
85
+ }
86
+ bindHandler(opts) {
87
+ var _a, _b;
88
+ let fn;
89
+ for (const handler of opts.handlers) {
90
+ if (!(['WF_STEP', 'WF_FLOW'].includes(handler.type)))
91
+ continue;
92
+ const schemaId = handler.path;
93
+ const path = typeof schemaId === 'string'
94
+ ? schemaId
95
+ : typeof opts.method === 'string'
96
+ ? opts.method
97
+ : '';
98
+ const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`; // explicit double slash "//" -> force url to end with slash
99
+ if (!fn) {
100
+ fn = moost.defineMoostEventHandler({
101
+ contextType: CONTEXT_TYPE,
102
+ loggerTitle: LOGGER_TITLE,
103
+ getIterceptorHandler: opts.getIterceptorHandler,
104
+ getControllerInstance: opts.getInstance,
105
+ controllerMethod: opts.method,
106
+ resolveArgs: opts.resolveArgs,
107
+ manualUnscope: true,
108
+ });
109
+ }
110
+ let routerBinding;
111
+ if (handler.type === 'WF_STEP') {
112
+ routerBinding = this.wfApp.step(targetPath, {
113
+ handler: fn,
114
+ });
115
+ }
116
+ else {
117
+ const mate = getWfMate();
118
+ let wfSchema = (_a = mate.read(opts.fakeInstance, opts.method)) === null || _a === void 0 ? void 0 : _a.wfSchema;
119
+ if (!wfSchema) {
120
+ wfSchema = (_b = mate.read(opts.fakeInstance)) === null || _b === void 0 ? void 0 : _b.wfSchema;
121
+ }
122
+ routerBinding = this.wfApp.flow(targetPath, wfSchema || []);
123
+ }
124
+ opts.logHandler(`${''}(${handler.type})${''}${targetPath}`);
125
+ const args = routerBinding.getArgs();
126
+ const params = {};
127
+ args.forEach(a => params[a] = `{${a}}`);
128
+ opts.register(handler, routerBinding.getPath(params), args);
129
+ }
130
+ }
131
+ }
132
+
133
+ function WfStep(path) {
134
+ return getWfMate().decorate('handlers', { path, type: 'WF_STEP' }, true);
135
+ }
136
+ function WfFlow(path) {
137
+ return getWfMate().decorate('handlers', { path, type: 'WF_STEP' }, true);
138
+ }
139
+ function WfSchema(schema) {
140
+ return getWfMate().decorate('wfSchema', schema);
141
+ }
142
+
143
+ /**
144
+ * Wrapper on top of useEventContext that provides
145
+ * proper context types for CLI event
146
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
147
+ */
148
+ function useCliContext() {
149
+ return eventCore.useEventContext('CLI');
150
+ }
151
+
152
+ exports.MoostWf = MoostWf;
153
+ exports.WfFlow = WfFlow;
154
+ exports.WfSchema = WfSchema;
155
+ exports.WfStep = WfStep;
156
+ exports.useCliContext = useCliContext;
@@ -0,0 +1,45 @@
1
+ import { Moost } from 'moost';
2
+ import { TFlowOutput } from '@prostojs/wf';
3
+ import { TMoostAdapter } from 'moost';
4
+ import { TMoostAdapterOptions } from 'moost';
5
+ import { TWooksWfOptions } from '@wooksjs/event-wf';
6
+ import { TWorkflowSchema } from '@prostojs/wf';
7
+ import { useCliContext } from '@wooksjs/event-cli';
8
+ import { WooksWf } from '@wooksjs/event-wf';
9
+
10
+ export declare class MoostWf<T> implements TMoostAdapter<TWfHandlerMeta> {
11
+ protected opts?: TMoostWfOpts<T> | undefined;
12
+ protected wfApp: WooksWf<T>;
13
+ constructor(opts?: TMoostWfOpts<T> | undefined);
14
+ onNotFound(): Promise<unknown>;
15
+ protected moost?: Moost;
16
+ onInit(moost: Moost): void;
17
+ start<I>(schemaId: string, inputContext: T, input?: I): Promise<TFlowOutput<T, I>>;
18
+ resume<I>(schemaId: string, inputContext: T, indexes: number[], input?: I): Promise<TFlowOutput<T, I>>;
19
+ bindHandler<T extends object = object>(opts: TMoostAdapterOptions<TWfHandlerMeta, T>): void;
20
+ }
21
+
22
+ export declare interface TMoostWfOpts<T> {
23
+ /**
24
+ * wooksWf options or instance
25
+ */
26
+ wooksWf?: WooksWf<T> | TWooksWfOptions;
27
+ /**
28
+ * more internal logs are printed when true
29
+ */
30
+ debug?: boolean;
31
+ }
32
+
33
+ export declare interface TWfHandlerMeta {
34
+ path: string;
35
+ }
36
+
37
+ export { useCliContext }
38
+
39
+ export declare function WfFlow(path?: string): MethodDecorator;
40
+
41
+ export declare function WfSchema<T>(schema: TWorkflowSchema<T>): MethodDecorator;
42
+
43
+ export declare function WfStep(path?: string): MethodDecorator;
44
+
45
+ export { }
package/dist/index.mjs ADDED
@@ -0,0 +1,150 @@
1
+ import { getMoostMate, getMoostInfact, defineMoostEventHandler } from 'moost';
2
+ import { WooksWf, createWfApp } from '@wooksjs/event-wf';
3
+ import { useEventId, useEventContext } from '@wooksjs/event-core';
4
+ import 'wooks';
5
+
6
+ /******************************************************************************
7
+ Copyright (c) Microsoft Corporation.
8
+
9
+ Permission to use, copy, modify, and/or distribute this software for any
10
+ purpose with or without fee is hereby granted.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
+ PERFORMANCE OF THIS SOFTWARE.
19
+ ***************************************************************************** */
20
+ /* global Reflect, Promise */
21
+
22
+
23
+ function __awaiter(thisArg, _arguments, P, generator) {
24
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
25
+ return new (P || (P = Promise))(function (resolve, reject) {
26
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
27
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ }
32
+
33
+ function getWfMate() {
34
+ return getMoostMate();
35
+ }
36
+
37
+ const LOGGER_TITLE = 'moost-wf';
38
+ const CONTEXT_TYPE = 'WF';
39
+ class MoostWf {
40
+ constructor(opts) {
41
+ this.opts = opts;
42
+ const wfAppOpts = opts === null || opts === void 0 ? void 0 : opts.wooksWf;
43
+ if (wfAppOpts && wfAppOpts instanceof WooksWf) {
44
+ this.wfApp = wfAppOpts;
45
+ }
46
+ else if (wfAppOpts) {
47
+ this.wfApp = createWfApp(wfAppOpts);
48
+ }
49
+ else {
50
+ this.wfApp = createWfApp();
51
+ }
52
+ if (!(opts === null || opts === void 0 ? void 0 : opts.debug)) {
53
+ getMoostInfact().silent(true);
54
+ }
55
+ }
56
+ onNotFound() {
57
+ var _a;
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ const response = yield defineMoostEventHandler({
60
+ loggerTitle: LOGGER_TITLE,
61
+ getIterceptorHandler: () => { var _a; return (_a = this.moost) === null || _a === void 0 ? void 0 : _a.getGlobalInterceptorHandler(); },
62
+ getControllerInstance: () => this.moost,
63
+ callControllerMethod: () => undefined,
64
+ logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
65
+ })();
66
+ return response;
67
+ });
68
+ }
69
+ onInit(moost) {
70
+ this.moost = moost;
71
+ }
72
+ start(schemaId, inputContext, input) {
73
+ return this.wfApp.start(schemaId, inputContext, input, () => {
74
+ const scopeId = useEventId().getId();
75
+ getMoostInfact().unregisterScope(scopeId);
76
+ });
77
+ }
78
+ resume(schemaId, inputContext, indexes, input) {
79
+ return this.wfApp.resume(schemaId, inputContext, indexes, input, () => {
80
+ const scopeId = useEventId().getId();
81
+ getMoostInfact().unregisterScope(scopeId);
82
+ });
83
+ }
84
+ bindHandler(opts) {
85
+ var _a, _b;
86
+ let fn;
87
+ for (const handler of opts.handlers) {
88
+ if (!(['WF_STEP', 'WF_FLOW'].includes(handler.type)))
89
+ continue;
90
+ const schemaId = handler.path;
91
+ const path = typeof schemaId === 'string'
92
+ ? schemaId
93
+ : typeof opts.method === 'string'
94
+ ? opts.method
95
+ : '';
96
+ const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`; // explicit double slash "//" -> force url to end with slash
97
+ if (!fn) {
98
+ fn = defineMoostEventHandler({
99
+ contextType: CONTEXT_TYPE,
100
+ loggerTitle: LOGGER_TITLE,
101
+ getIterceptorHandler: opts.getIterceptorHandler,
102
+ getControllerInstance: opts.getInstance,
103
+ controllerMethod: opts.method,
104
+ resolveArgs: opts.resolveArgs,
105
+ manualUnscope: true,
106
+ });
107
+ }
108
+ let routerBinding;
109
+ if (handler.type === 'WF_STEP') {
110
+ routerBinding = this.wfApp.step(targetPath, {
111
+ handler: fn,
112
+ });
113
+ }
114
+ else {
115
+ const mate = getWfMate();
116
+ let wfSchema = (_a = mate.read(opts.fakeInstance, opts.method)) === null || _a === void 0 ? void 0 : _a.wfSchema;
117
+ if (!wfSchema) {
118
+ wfSchema = (_b = mate.read(opts.fakeInstance)) === null || _b === void 0 ? void 0 : _b.wfSchema;
119
+ }
120
+ routerBinding = this.wfApp.flow(targetPath, wfSchema || []);
121
+ }
122
+ opts.logHandler(`${''}(${handler.type})${''}${targetPath}`);
123
+ const args = routerBinding.getArgs();
124
+ const params = {};
125
+ args.forEach(a => params[a] = `{${a}}`);
126
+ opts.register(handler, routerBinding.getPath(params), args);
127
+ }
128
+ }
129
+ }
130
+
131
+ function WfStep(path) {
132
+ return getWfMate().decorate('handlers', { path, type: 'WF_STEP' }, true);
133
+ }
134
+ function WfFlow(path) {
135
+ return getWfMate().decorate('handlers', { path, type: 'WF_STEP' }, true);
136
+ }
137
+ function WfSchema(schema) {
138
+ return getWfMate().decorate('wfSchema', schema);
139
+ }
140
+
141
+ /**
142
+ * Wrapper on top of useEventContext that provides
143
+ * proper context types for CLI event
144
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
145
+ */
146
+ function useCliContext() {
147
+ return useEventContext('CLI');
148
+ }
149
+
150
+ export { MoostWf, WfFlow, WfSchema, WfStep, useCliContext };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@moostjs/event-wf",
3
+ "version": "0.2.34",
4
+ "description": "@moostjs/event-wf",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "sideEffects": false,
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/moostjs/moostjs.git",
22
+ "directory": "packages/event-wf"
23
+ },
24
+ "keywords": [
25
+ "moost",
26
+ "moostjs",
27
+ "composables",
28
+ "framework",
29
+ "wooksjs",
30
+ "prostojs"
31
+ ],
32
+ "author": "Artem Maltsev",
33
+ "license": "MIT",
34
+ "bugs": {
35
+ "url": "https://github.com/moostjs/moostjs/issues"
36
+ },
37
+ "peerDependencies": {},
38
+ "dependencies": {
39
+ "moost": "0.2.34",
40
+ "wooks": "^0.3.11",
41
+ "@wooksjs/event-core": "^0.3.11",
42
+ "@wooksjs/event-wf": "^0.3.11"
43
+ },
44
+ "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-wf#readme"
45
+ }