@medusajs/workflows-sdk 0.1.1 → 0.1.2-next-20240124093206
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/dist/helper/workflow-export.d.ts +16 -1
- package/dist/helper/workflow-export.js +109 -91
- package/dist/helper/workflow-export.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/types/common.d.ts +18 -0
- package/dist/types/common.js +3 -0
- package/dist/types/common.js.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +20 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/mutations.d.ts +7 -0
- package/dist/types/mutations.js +3 -0
- package/dist/types/mutations.js.map +1 -0
- package/dist/types/service.d.ts +53 -0
- package/dist/types/service.js +3 -0
- package/dist/types/service.js.map +1 -0
- package/dist/utils/_playground.js +12 -0
- package/dist/utils/_playground.js.map +1 -1
- package/dist/utils/composer/create-step.d.ts +3 -3
- package/dist/utils/composer/create-step.js +15 -6
- package/dist/utils/composer/create-step.js.map +1 -1
- package/dist/utils/composer/create-workflow.d.ts +15 -5
- package/dist/utils/composer/create-workflow.js +15 -5
- package/dist/utils/composer/create-workflow.js.map +1 -1
- package/dist/utils/composer/type.d.ts +8 -6
- package/package.json +5 -5
|
@@ -33,6 +33,21 @@ export type ExportedWorkflow<TData = unknown, TResult = unknown, TDataOverride =
|
|
|
33
33
|
registerStepSuccess: (args?: FlowRegisterStepSuccessOptions<TDataOverride extends undefined ? TData : TDataOverride>) => Promise<WorkflowResult<TResultOverride extends undefined ? TResult : TResultOverride>>;
|
|
34
34
|
registerStepFailure: (args?: FlowRegisterStepFailureOptions<TDataOverride extends undefined ? TData : TDataOverride>) => Promise<WorkflowResult<TResultOverride extends undefined ? TResult : TResultOverride>>;
|
|
35
35
|
};
|
|
36
|
+
export type MainExportedWorkflow<TData = unknown, TResult = unknown> = {
|
|
37
|
+
<TDataOverride = undefined, TResultOverride = undefined>(container?: LoadedModule[] | MedusaContainer): Omit<LocalWorkflow, "run" | "registerStepSuccess" | "registerStepFailure"> & ExportedWorkflow<TData, TResult, TDataOverride, TResultOverride>;
|
|
38
|
+
/**
|
|
39
|
+
* You can also directly call run, registerStepSuccess and registerStepFailure on the exported workflow
|
|
40
|
+
*/
|
|
41
|
+
run<TDataOverride = undefined, TResultOverride = undefined>(args?: FlowRunOptions<TDataOverride extends undefined ? TData : TDataOverride> & {
|
|
42
|
+
container?: LoadedModule[] | MedusaContainer;
|
|
43
|
+
}): Promise<WorkflowResult<TResultOverride extends undefined ? TResult : TResultOverride>>;
|
|
44
|
+
registerStepSuccess<TDataOverride = undefined, TResultOverride = undefined>(args?: FlowRegisterStepSuccessOptions<TDataOverride extends undefined ? TData : TDataOverride> & {
|
|
45
|
+
container?: LoadedModule[] | MedusaContainer;
|
|
46
|
+
}): Promise<WorkflowResult<TResultOverride extends undefined ? TResult : TResultOverride>>;
|
|
47
|
+
registerStepFailure<TDataOverride = undefined, TResultOverride = undefined>(args?: FlowRegisterStepFailureOptions<TDataOverride extends undefined ? TData : TDataOverride> & {
|
|
48
|
+
container?: LoadedModule[] | MedusaContainer;
|
|
49
|
+
}): Promise<WorkflowResult<TResultOverride extends undefined ? TResult : TResultOverride>>;
|
|
50
|
+
};
|
|
36
51
|
export declare const exportWorkflow: <TData = unknown, TResult = unknown>(workflowId: string, defaultResult?: string | Symbol, dataPreparation?: ((data: TData) => Promise<unknown>) | undefined, options?: {
|
|
37
52
|
wrappedInput?: boolean;
|
|
38
|
-
}) => <
|
|
53
|
+
}) => MainExportedWorkflow<TData, TResult>;
|
|
@@ -3,106 +3,124 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.exportWorkflow = void 0;
|
|
4
4
|
const orchestration_1 = require("@medusajs/orchestration");
|
|
5
5
|
const modules_sdk_1 = require("@medusajs/modules-sdk");
|
|
6
|
-
const utils_1 = require("@medusajs/utils");
|
|
7
6
|
const os_1 = require("os");
|
|
8
7
|
const ulid_1 = require("ulid");
|
|
9
8
|
const medusa_workflow_1 = require("../medusa-workflow");
|
|
10
9
|
const composer_1 = require("../utils/composer");
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
function createContextualWorkflowRunner({ workflowId, defaultResult, dataPreparation, options, container, }) {
|
|
11
|
+
if (!container) {
|
|
12
|
+
container = modules_sdk_1.MedusaModule.getLoadedModules().map((mod) => Object.values(mod)[0]);
|
|
13
|
+
}
|
|
14
|
+
const flow = new orchestration_1.LocalWorkflow(workflowId, container);
|
|
15
|
+
const originalRun = flow.run.bind(flow);
|
|
16
|
+
const originalRegisterStepSuccess = flow.registerStepSuccess.bind(flow);
|
|
17
|
+
const originalRegisterStepFailure = flow.registerStepFailure.bind(flow);
|
|
18
|
+
const originalExecution = async (method, { throwOnError, resultFrom }, ...args) => {
|
|
19
|
+
const transaction = await method.apply(method, args);
|
|
20
|
+
const errors = transaction.getErrors(orchestration_1.TransactionHandlerType.INVOKE);
|
|
21
|
+
const failedStatus = [orchestration_1.TransactionState.FAILED, orchestration_1.TransactionState.REVERTED];
|
|
22
|
+
if (failedStatus.includes(transaction.getState()) && throwOnError) {
|
|
23
|
+
const errorMessage = errors
|
|
24
|
+
?.map((err) => `${err.error?.message}${os_1.EOL}${err.error?.stack}`)
|
|
25
|
+
?.join(`${os_1.EOL}`);
|
|
26
|
+
throw new Error(errorMessage);
|
|
15
27
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
?.join(`${os_1.EOL}`);
|
|
28
|
-
throw new Error(errorMessage);
|
|
29
|
-
}
|
|
30
|
-
let result = undefined;
|
|
31
|
-
const resFrom = resultFrom?.__type === utils_1.OrchestrationUtils.SymbolWorkflowStep
|
|
32
|
-
? resultFrom.__step__
|
|
33
|
-
: resultFrom;
|
|
34
|
-
if (resFrom) {
|
|
35
|
-
if (Array.isArray(resFrom)) {
|
|
36
|
-
result = resFrom.map((from) => {
|
|
37
|
-
const res = transaction.getContext().invoke?.[from];
|
|
38
|
-
return res?.__type === utils_1.OrchestrationUtils.SymbolWorkflowWorkflowData
|
|
39
|
-
? res.output
|
|
40
|
-
: res;
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
const res = transaction.getContext().invoke?.[resFrom];
|
|
45
|
-
result =
|
|
46
|
-
res?.__type === utils_1.OrchestrationUtils.SymbolWorkflowWorkflowData
|
|
47
|
-
? res.output
|
|
48
|
-
: res;
|
|
49
|
-
}
|
|
50
|
-
const ret = result || resFrom;
|
|
51
|
-
result = options?.wrappedInput
|
|
52
|
-
? await (0, composer_1.resolveValue)(ret, transaction.getContext())
|
|
53
|
-
: ret;
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
errors,
|
|
57
|
-
transaction,
|
|
58
|
-
result,
|
|
59
|
-
};
|
|
28
|
+
let result;
|
|
29
|
+
if (options?.wrappedInput) {
|
|
30
|
+
result = await (0, composer_1.resolveValue)(resultFrom, transaction.getContext());
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
result = transaction.getContext().invoke?.[resultFrom];
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
errors,
|
|
37
|
+
transaction,
|
|
38
|
+
result,
|
|
60
39
|
};
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
errors: [err],
|
|
78
|
-
};
|
|
40
|
+
};
|
|
41
|
+
const newRun = async ({ input, context, throwOnError, resultFrom, events } = {
|
|
42
|
+
throwOnError: true,
|
|
43
|
+
resultFrom: defaultResult,
|
|
44
|
+
}) => {
|
|
45
|
+
resultFrom ?? (resultFrom = defaultResult);
|
|
46
|
+
throwOnError ?? (throwOnError = true);
|
|
47
|
+
if (typeof dataPreparation === "function") {
|
|
48
|
+
try {
|
|
49
|
+
const copyInput = input ? JSON.parse(JSON.stringify(input)) : input;
|
|
50
|
+
input = await dataPreparation(copyInput);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
if (throwOnError) {
|
|
54
|
+
throw new Error(`Data preparation failed: ${err.message}${os_1.EOL}${err.stack}`);
|
|
79
55
|
}
|
|
56
|
+
return {
|
|
57
|
+
errors: [err],
|
|
58
|
+
};
|
|
80
59
|
}
|
|
81
|
-
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
|
|
60
|
+
}
|
|
61
|
+
return await originalExecution(originalRun, { throwOnError, resultFrom }, context?.transactionId ?? (0, ulid_1.ulid)(), input, context, events);
|
|
62
|
+
};
|
|
63
|
+
flow.run = newRun;
|
|
64
|
+
const newRegisterStepSuccess = async ({ response, idempotencyKey, context, throwOnError, resultFrom, events, } = {
|
|
65
|
+
idempotencyKey: "",
|
|
66
|
+
throwOnError: true,
|
|
67
|
+
resultFrom: defaultResult,
|
|
68
|
+
}) => {
|
|
69
|
+
resultFrom ?? (resultFrom = defaultResult);
|
|
70
|
+
throwOnError ?? (throwOnError = true);
|
|
71
|
+
return await originalExecution(originalRegisterStepSuccess, { throwOnError, resultFrom }, idempotencyKey, response, context, events);
|
|
72
|
+
};
|
|
73
|
+
flow.registerStepSuccess = newRegisterStepSuccess;
|
|
74
|
+
const newRegisterStepFailure = async ({ response, idempotencyKey, context, throwOnError, resultFrom, events, } = {
|
|
75
|
+
idempotencyKey: "",
|
|
76
|
+
throwOnError: true,
|
|
77
|
+
resultFrom: defaultResult,
|
|
78
|
+
}) => {
|
|
79
|
+
resultFrom ?? (resultFrom = defaultResult);
|
|
80
|
+
throwOnError ?? (throwOnError = true);
|
|
81
|
+
return await originalExecution(originalRegisterStepFailure, { throwOnError, resultFrom }, idempotencyKey, response, context, events);
|
|
82
|
+
};
|
|
83
|
+
flow.registerStepFailure = newRegisterStepFailure;
|
|
84
|
+
return flow;
|
|
85
|
+
}
|
|
86
|
+
const exportWorkflow = (workflowId, defaultResult, dataPreparation, options) => {
|
|
87
|
+
function exportedWorkflow(container) {
|
|
88
|
+
return createContextualWorkflowRunner({
|
|
89
|
+
workflowId,
|
|
90
|
+
defaultResult,
|
|
91
|
+
dataPreparation,
|
|
92
|
+
options,
|
|
93
|
+
container,
|
|
94
|
+
});
|
|
105
95
|
}
|
|
96
|
+
const buildRunnerFn = (action, container) => {
|
|
97
|
+
const contextualRunner = createContextualWorkflowRunner({
|
|
98
|
+
workflowId,
|
|
99
|
+
defaultResult,
|
|
100
|
+
dataPreparation,
|
|
101
|
+
options,
|
|
102
|
+
container,
|
|
103
|
+
});
|
|
104
|
+
return contextualRunner[action];
|
|
105
|
+
};
|
|
106
|
+
exportedWorkflow.run = async (args) => {
|
|
107
|
+
const container = args?.container;
|
|
108
|
+
delete args?.container;
|
|
109
|
+
const inputArgs = { ...args };
|
|
110
|
+
return await buildRunnerFn("run", container)(inputArgs);
|
|
111
|
+
};
|
|
112
|
+
exportedWorkflow.registerStepSuccess = async (args) => {
|
|
113
|
+
const container = args?.container;
|
|
114
|
+
delete args?.container;
|
|
115
|
+
const inputArgs = { ...args };
|
|
116
|
+
return await buildRunnerFn("registerStepSuccess", container)(inputArgs);
|
|
117
|
+
};
|
|
118
|
+
exportedWorkflow.registerStepFailure = async (args) => {
|
|
119
|
+
const container = args?.container;
|
|
120
|
+
delete args?.container;
|
|
121
|
+
const inputArgs = { ...args };
|
|
122
|
+
return await buildRunnerFn("registerStepFailure", container)(inputArgs);
|
|
123
|
+
};
|
|
106
124
|
medusa_workflow_1.MedusaWorkflow.registerWorkflow(workflowId, exportedWorkflow);
|
|
107
125
|
return exportedWorkflow;
|
|
108
126
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workflow-export.js","sourceRoot":"","sources":["../../src/helper/workflow-export.ts"],"names":[],"mappings":";;;AAAA,2DAOgC;AAGhC,uDAAoD;AACpD,
|
|
1
|
+
{"version":3,"file":"workflow-export.js","sourceRoot":"","sources":["../../src/helper/workflow-export.ts"],"names":[],"mappings":";;;AAAA,2DAOgC;AAGhC,uDAAoD;AACpD,2BAAwB;AACxB,+BAA2B;AAC3B,wDAAmD;AACnD,gDAAgD;AAwHhD,SAAS,8BAA8B,CAKrC,EACA,UAAU,EACV,aAAa,EACb,eAAe,EACf,OAAO,EACP,SAAS,GASV;IAEC,IAAI,CAAC,SAAS,EAAE;QACd,SAAS,GAAG,0BAAY,CAAC,gBAAgB,EAAE,CAAC,GAAG,CAC7C,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAC/B,CAAA;KACF;IAED,MAAM,IAAI,GAAG,IAAI,6BAAa,CAAC,UAAU,EAAE,SAAS,CAAC,CAAA;IAErD,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACvC,MAAM,2BAA2B,GAAG,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACvE,MAAM,2BAA2B,GAAG,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAEvE,MAAM,iBAAiB,GAAG,KAAK,EAC7B,MAAM,EACN,EAAE,YAAY,EAAE,UAAU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K,EACL,SAAS,CACV,CAAC,SAAS,CAAC,CAAA;IACd,CAAC,CAAA;IAED,gBAAgB,CAAC,mBAAmB,GAAG,KAAK,EAI1C,IAIC,EAKD,EAAE;QACF,MAAM,SAAS,GAAG,IAAI,EAAE,SAAS,CAAA;QACjC,OAAO,IAAI,EAAE,SAAS,CAAA;QACtB,MAAM,SAAS,GAAG,EAAE,GAAG,IAAI,EAE1B,CAAA;QAED,OAAO,MAAM,aAAa,CAKxB,qBAAqB,EACrB,SAAS,CACV,CAAC,SAAS,CAAC,CAAA;IACd,CAAC,CAAA;IAED,gBAAgB,CAAC,mBAAmB,GAAG,KAAK,EAI1C,IAIC,EAKD,EAAE;QACF,MAAM,SAAS,GAAG,IAAI,EAAE,SAAS,CAAA;QACjC,OAAO,IAAI,EAAE,SAAS,CAAA;QACtB,MAAM,SAAS,GAAG,EAAE,GAAG,IAAI,EAE1B,CAAA;QAED,OAAO,MAAM,aAAa,CAKxB,qBAAqB,EACrB,SAAS,CACV,CAAC,SAAS,CAAC,CAAA;IACd,CAAC,CAAA;IAED,gCAAc,CAAC,gBAAgB,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAA;IAC7D,OAAO,gBAAgB,CAAA;AACzB,CAAC,CAAA;AArJY,QAAA,cAAc,kBAqJ1B"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -26,9 +26,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
26
26
|
return result;
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.Composer = void 0;
|
|
29
|
+
exports.Composer = exports.WorkflowOrchestratorTypes = void 0;
|
|
30
30
|
__exportStar(require("./helper"), exports);
|
|
31
31
|
__exportStar(require("./medusa-workflow"), exports);
|
|
32
|
+
exports.WorkflowOrchestratorTypes = __importStar(require("./types"));
|
|
32
33
|
__exportStar(require("./utils/composer"), exports);
|
|
33
34
|
exports.Composer = __importStar(require("./utils/composer"));
|
|
34
35
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,oDAAiC;AACjC,mDAAgC;AAChC,6DAA4C"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,oDAAiC;AACjC,qEAAoD;AACpD,mDAAgC;AAChC,6DAA4C"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BaseFilterable } from "@medusajs/types";
|
|
2
|
+
export interface WorkflowExecutionDTO {
|
|
3
|
+
id: string;
|
|
4
|
+
workflow_id: string;
|
|
5
|
+
transaction_id: string;
|
|
6
|
+
execution: string;
|
|
7
|
+
context: string;
|
|
8
|
+
state: any;
|
|
9
|
+
created_at: Date;
|
|
10
|
+
updated_at: Date;
|
|
11
|
+
deleted_at: Date;
|
|
12
|
+
}
|
|
13
|
+
export interface FilterableWorkflowExecutionProps extends BaseFilterable<FilterableWorkflowExecutionProps> {
|
|
14
|
+
id?: string[];
|
|
15
|
+
workflow_id?: string[];
|
|
16
|
+
transaction_id?: string[];
|
|
17
|
+
state?: any[];
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/types/common.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./common"), exports);
|
|
18
|
+
__exportStar(require("./mutations"), exports);
|
|
19
|
+
__exportStar(require("./service"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,8CAA2B;AAC3B,4CAAyB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/types/mutations.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ContainerLike, Context, FindConfig, IModuleService } from "@medusajs/types";
|
|
2
|
+
import { ReturnWorkflow, UnwrapWorkflowInputDataType } from "../utils/composer";
|
|
3
|
+
import { FilterableWorkflowExecutionProps, WorkflowExecutionDTO } from "./common";
|
|
4
|
+
type FlowRunOptions<TData = unknown> = {
|
|
5
|
+
input?: TData;
|
|
6
|
+
context?: Context;
|
|
7
|
+
resultFrom?: string | string[] | Symbol;
|
|
8
|
+
throwOnError?: boolean;
|
|
9
|
+
events?: Record<string, Function>;
|
|
10
|
+
};
|
|
11
|
+
export interface WorkflowOrchestratorRunDTO<T = unknown> extends FlowRunOptions<T> {
|
|
12
|
+
transactionId?: string;
|
|
13
|
+
container?: ContainerLike;
|
|
14
|
+
}
|
|
15
|
+
export type IdempotencyKeyParts = {
|
|
16
|
+
workflowId: string;
|
|
17
|
+
transactionId: string;
|
|
18
|
+
stepId: string;
|
|
19
|
+
action: "invoke" | "compensate";
|
|
20
|
+
};
|
|
21
|
+
export interface IWorkflowsModuleService extends IModuleService {
|
|
22
|
+
listWorkflowExecution(filters?: FilterableWorkflowExecutionProps, config?: FindConfig<WorkflowExecutionDTO>, sharedContext?: Context): Promise<WorkflowExecutionDTO[]>;
|
|
23
|
+
listAndCountWorkflowExecution(filters?: FilterableWorkflowExecutionProps, config?: FindConfig<WorkflowExecutionDTO>, sharedContext?: Context): Promise<[WorkflowExecutionDTO[], number]>;
|
|
24
|
+
run<TWorkflow extends ReturnWorkflow<any, any, any> = ReturnWorkflow<any, any, any>, TData = UnwrapWorkflowInputDataType<TWorkflow>>(workflowId: string, options?: WorkflowOrchestratorRunDTO, sharedContext?: Context): Promise<{
|
|
25
|
+
errors: Error[];
|
|
26
|
+
transaction: object;
|
|
27
|
+
result: any;
|
|
28
|
+
acknowledgement: object;
|
|
29
|
+
}>;
|
|
30
|
+
getRunningTransaction(workflowId: string, transactionId: string, options?: Record<string, any>, sharedContext?: Context): Promise<unknown>;
|
|
31
|
+
setStepSuccess({ idempotencyKey, stepResponse, options, }: {
|
|
32
|
+
idempotencyKey: string | IdempotencyKeyParts;
|
|
33
|
+
stepResponse: unknown;
|
|
34
|
+
options?: Record<string, any>;
|
|
35
|
+
}, sharedContext?: Context): any;
|
|
36
|
+
setStepFailure({ idempotencyKey, stepResponse, options, }: {
|
|
37
|
+
idempotencyKey: string | object;
|
|
38
|
+
stepResponse: unknown;
|
|
39
|
+
options?: Record<string, any>;
|
|
40
|
+
}, sharedContext?: Context): any;
|
|
41
|
+
subscribe(args: {
|
|
42
|
+
workflowId: string;
|
|
43
|
+
transactionId?: string;
|
|
44
|
+
subscriber: Function;
|
|
45
|
+
subscriberId?: string;
|
|
46
|
+
}, sharedContext?: Context): Promise<void>;
|
|
47
|
+
unsubscribe(args: {
|
|
48
|
+
workflowId: string;
|
|
49
|
+
transactionId?: string;
|
|
50
|
+
subscriberOrId: string | Function;
|
|
51
|
+
}, sharedContext?: Context): any;
|
|
52
|
+
}
|
|
53
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/types/service.ts"],"names":[],"mappings":""}
|
|
@@ -20,4 +20,16 @@ workflow()
|
|
|
20
20
|
.then((res) => {
|
|
21
21
|
console.log(res.result); // result: { step2: { test: "test", test2: "step1" } }
|
|
22
22
|
});
|
|
23
|
+
/*type type0 = typeof workflow extends ReturnWorkflow<infer T, infer R, infer X>
|
|
24
|
+
? T
|
|
25
|
+
: never
|
|
26
|
+
|
|
27
|
+
function run<
|
|
28
|
+
TWorkflow extends ReturnWorkflow<any, any, any>,
|
|
29
|
+
TData = TWorkflow extends ReturnWorkflow<infer T, infer R, infer X>
|
|
30
|
+
? T
|
|
31
|
+
: never
|
|
32
|
+
>(name: string, options: FlowRunOptions<TData>) {}
|
|
33
|
+
|
|
34
|
+
const test = run<typeof workflow>("workflow", { input: "string" })*/
|
|
23
35
|
//# sourceMappingURL=_playground.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_playground.js","sourceRoot":"","sources":["../../src/utils/_playground.ts"],"names":[],"mappings":";;AAAA,yCAAqE;AAErE,MAAM,KAAK,GAAG,IAAA,qBAAU,EAAC,OAAO,EAAE,KAAK,EAAE,KAAS,EAAE,OAAO,EAAE,EAAE;IAC7D,OAAO,IAAI,uBAAY,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAA;AAC7C,CAAC,CAAC,CAAA;AAEF,MAAM,KAAK,GAAG,IAAA,qBAAU,EACtB,OAAO,EACP,KAAK,EAAE,KAAsC,EAAE,OAAO,EAAE,EAAE;IACxD,OAAO,IAAI,uBAAY,CAAC,EAAE,KAAK,EAAE,K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IAAI,uBAAY,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAA;AAC7C,CAAC,CAAC,CAAA;AAEF,MAAM,QAAQ,GAAG,IAAA,yBAAc,EAAC,UAAU,EAAE;IAC1C,MAAM,QAAQ,GAAG,KAAK,EAAE,CAAA;IACxB,KAAK,EAAE,CAAA;IACP,OAAO,KAAK,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;AACvD,CAAC,CAAC,CAAA;AAEF,QAAQ,EAAE;KACP,GAAG,CAAC,EAAE,CAAC;KACP,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;IACZ,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA,CAAC,sDAAsD;AAChF,CAAC,CAAC,CAAA;AAEJ;;;;;;;;;;;oEAWoE"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { TransactionStepsDefinition } from "@medusajs/orchestration";
|
|
1
2
|
import { StepResponse } from "./helpers";
|
|
2
3
|
import { StepExecutionContext, StepFunction } from "./type";
|
|
3
|
-
import { TransactionStepsDefinition } from "@medusajs/orchestration";
|
|
4
4
|
/**
|
|
5
5
|
* The type of invocation function passed to a step.
|
|
6
6
|
*
|
|
@@ -86,11 +86,11 @@ context: StepExecutionContext) => unknown | Promise<unknown>;
|
|
|
86
86
|
*/
|
|
87
87
|
export declare function createStep<TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput>(
|
|
88
88
|
/**
|
|
89
|
-
* The name of the step or its configuration
|
|
89
|
+
* The name of the step or its configuration.
|
|
90
90
|
*/
|
|
91
91
|
nameOrConfig: string | ({
|
|
92
92
|
name: string;
|
|
93
|
-
} &
|
|
93
|
+
} & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">),
|
|
94
94
|
/**
|
|
95
95
|
* An invocation function that will be executed when the workflow is executed. The function must return an instance of {@link StepResponse}. The constructor of {@link StepResponse}
|
|
96
96
|
* accepts the output of the step as a first argument, and optionally as a second argument the data to be passed to the compensation function as a parameter.
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createStep = void 0;
|
|
4
|
+
const orchestration_1 = require("@medusajs/orchestration");
|
|
5
|
+
const utils_1 = require("@medusajs/utils");
|
|
6
|
+
const ulid_1 = require("ulid");
|
|
4
7
|
const helpers_1 = require("./helpers");
|
|
5
8
|
const proxy_1 = require("./helpers/proxy");
|
|
6
|
-
const utils_1 = require("@medusajs/utils");
|
|
7
9
|
/**
|
|
8
10
|
* @internal
|
|
9
11
|
*
|
|
@@ -61,17 +63,24 @@ function applyStep({ stepName, stepConfig = {}, input, invokeFn, compensateFn, }
|
|
|
61
63
|
}
|
|
62
64
|
: undefined,
|
|
63
65
|
};
|
|
66
|
+
stepConfig.uuid = (0, ulid_1.ulid)();
|
|
64
67
|
stepConfig.noCompensation = !compensateFn;
|
|
65
68
|
this.flow.addAction(stepName, stepConfig);
|
|
66
|
-
this.handlers.
|
|
69
|
+
if (!this.handlers.has(stepName)) {
|
|
70
|
+
this.handlers.set(stepName, handler);
|
|
71
|
+
}
|
|
67
72
|
const ret = {
|
|
68
73
|
__type: utils_1.OrchestrationUtils.SymbolWorkflowStep,
|
|
69
74
|
__step__: stepName,
|
|
70
|
-
config: (
|
|
71
|
-
|
|
75
|
+
config: (localConfig) => {
|
|
76
|
+
const newStepName = localConfig.name ?? stepName;
|
|
77
|
+
delete localConfig.name;
|
|
78
|
+
this.handlers.set(newStepName, handler);
|
|
79
|
+
this.flow.replaceAction(stepConfig.uuid, newStepName, {
|
|
72
80
|
...stepConfig,
|
|
73
|
-
...
|
|
81
|
+
...localConfig,
|
|
74
82
|
});
|
|
83
|
+
orchestration_1.WorkflowManager.update(this.workflowId, this.flow, this.handlers);
|
|
75
84
|
return (0, proxy_1.proxify)(ret);
|
|
76
85
|
},
|
|
77
86
|
};
|
|
@@ -128,7 +137,7 @@ function applyStep({ stepName, stepConfig = {}, input, invokeFn, compensateFn, }
|
|
|
128
137
|
*/
|
|
129
138
|
function createStep(
|
|
130
139
|
/**
|
|
131
|
-
* The name of the step or its configuration
|
|
140
|
+
* The name of the step or its configuration.
|
|
132
141
|
*/
|
|
133
142
|
nameOrConfig,
|
|
134
143
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-step.js","sourceRoot":"","sources":["../../../src/utils/composer/create-step.ts"],"names":[],"mappings":";;;AAAA,uCAAsD;
|
|
1
|
+
{"version":3,"file":"create-step.js","sourceRoot":"","sources":["../../../src/utils/composer/create-step.ts"],"names":[],"mappings":";;;AAAA,2DAGgC;AAChC,2CAA8D;AAC9D,+BAA2B;AAC3B,uCAAsD;AACtD,2CAAyC;AA4EzC;;;;;;;;;;;GAWG;AACH,SAAS,SAAS,CAOhB,EACA,QAAQ,EACR,UAAU,GAAG,EAAE,EACf,KAAK,EACL,QAAQ,EACR,YAAY,GAMb;IACC,OAAO;QACL,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACpB,MAAM,IAAI,KAAK,CACb,4DAA4D,CAC7D,CAAA;SACF;QAED,MAAM,OAAO,GAAG;YACd,MAAM,EAAE,KAAK,EAAE,kBAAkB,EAAE,EAAE;gBACnC,MAAM,gBAAgB,GAAyB;oBAC7C,SAAS,EAAE,kBAAkB,CAAC,SAAS;oBACvC,QAAQ,EAAE,kBAAkB,CAAC,QAAQ;oBACrC,OAAO,EAAE,kBAAkB,CAAC,OAAO;iBACpC,CAAA;gBAED,MAAM,QAAQ,GAAG,KAAK;oBACpB,CAAC,CAAC,MAAM,IAAA,sBAAY,EAAC,KAAK,EAAE,kBAAkB,CAAC;oBAC/C,CAAC,CAAC,EAAE,CAAA;gBACN,MAAM,YAAY,GAA2B,MAAM,QAAQ,CAAC,KAAK,CAC/D,IAAI,EACJ,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAC7B,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AAC,IAAI,CAAA;IAC9E,MAAM,MAAM,GAAG,IAAA,gBAAQ,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,CAAA;IAEzD,MAAM,QAAQ,GAAG,UACf,KAIa;QAEb,IAAI,CAAC,MAAM,CAAC,0BAAkB,CAAC,mCAAmC,CAAC,EAAE;YACnE,MAAM,IAAI,KAAK,CACb,4DAA4D,CAC7D,CAAA;SACF;QAED,MAAM,UAAU,GACd,MAAM,CACJ,0BAAkB,CAAC,mCAAmC,CAEzD,CAAC,UAAU,CAAA;QAEZ,OAAO,UAAU,CACf,SAAS,CAKP;YACA,QAAQ;YACR,UAAU,EAAE,MAAM;YAClB,KAAK;YACL,QAAQ;YACR,YAAY;SACb,CAAC,CACH,CAAA;IACH,CAAoD,CAAA;IAEpD,QAAQ,CAAC,MAAM,GAAG,0BAAkB,CAAC,sBAAsB,CAAA;IAC3D,QAAQ,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAE5B,OAAO,QAAQ,CAAA;AACjB,CAAC;AA1ED,gCA0EC"}
|
|
@@ -49,11 +49,20 @@ import { WorkflowData, WorkflowDataProperties } from "./type";
|
|
|
49
49
|
* }
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
|
-
type ReturnWorkflow<TData, TResult, THooks extends Record<string, Function>> = {
|
|
52
|
+
export type ReturnWorkflow<TData, TResult, THooks extends Record<string, Function>> = {
|
|
53
53
|
<TDataOverride = undefined, TResultOverride = undefined>(container?: LoadedModule[] | MedusaContainer): Omit<LocalWorkflow, "run" | "registerStepSuccess" | "registerStepFailure"> & ExportedWorkflow<TData, TResult, TDataOverride, TResultOverride>;
|
|
54
54
|
} & THooks & {
|
|
55
55
|
getName: () => string;
|
|
56
|
+
} & {
|
|
57
|
+
config: (config: TransactionModelOptions) => void;
|
|
56
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* Extract the raw type of the expected input data of a workflow.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* type WorkflowInputData = UnwrapWorkflowInputDataType<typeof myWorkflow>
|
|
64
|
+
*/
|
|
65
|
+
export type UnwrapWorkflowInputDataType<T extends ReturnWorkflow<any, any, any>> = T extends ReturnWorkflow<infer TData, infer R, infer THooks> ? TData : never;
|
|
57
66
|
/**
|
|
58
67
|
* This function creates a workflow with the provided name and a constructor function.
|
|
59
68
|
* The constructor function builds the workflow from steps created by the {@link createStep} function.
|
|
@@ -110,9 +119,11 @@ type ReturnWorkflow<TData, TResult, THooks extends Record<string, Function>> = {
|
|
|
110
119
|
*/
|
|
111
120
|
export declare function createWorkflow<TData, TResult, THooks extends Record<string, Function> = Record<string, Function>>(
|
|
112
121
|
/**
|
|
113
|
-
* The name of the workflow.
|
|
122
|
+
* The name of the workflow or its configuration.
|
|
114
123
|
*/
|
|
115
|
-
|
|
124
|
+
nameOrConfig: string | ({
|
|
125
|
+
name: string;
|
|
126
|
+
} & TransactionModelOptions),
|
|
116
127
|
/**
|
|
117
128
|
* The constructor function that is executed when the `run` method in {@link ReturnWorkflow} is used.
|
|
118
129
|
* The function can't be an arrow function or an asynchronus function. It also can't directly manipulate data.
|
|
@@ -120,5 +131,4 @@ name: string,
|
|
|
120
131
|
*/
|
|
121
132
|
composer: (input: WorkflowData<TData>) => void | WorkflowData<TResult> | {
|
|
122
133
|
[K in keyof TResult]: WorkflowData<TResult[K]> | WorkflowDataProperties<TResult[K]>;
|
|
123
|
-
}
|
|
124
|
-
export {};
|
|
134
|
+
}): ReturnWorkflow<TData, TResult, THooks>;
|
|
@@ -62,15 +62,17 @@ global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerContext] = null;
|
|
|
62
62
|
*/
|
|
63
63
|
function createWorkflow(
|
|
64
64
|
/**
|
|
65
|
-
* The name of the workflow.
|
|
65
|
+
* The name of the workflow or its configuration.
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
nameOrConfig,
|
|
68
68
|
/**
|
|
69
69
|
* The constructor function that is executed when the `run` method in {@link ReturnWorkflow} is used.
|
|
70
70
|
* The function can't be an arrow function or an asynchronus function. It also can't directly manipulate data.
|
|
71
71
|
* You'll have to use the {@link transform} function if you need to directly manipulate data.
|
|
72
72
|
*/
|
|
73
|
-
composer
|
|
73
|
+
composer) {
|
|
74
|
+
const name = (0, utils_1.isString)(nameOrConfig) ? nameOrConfig : nameOrConfig.name;
|
|
75
|
+
const options = (0, utils_1.isString)(nameOrConfig) ? {} : nameOrConfig;
|
|
74
76
|
const handlers = new Map();
|
|
75
77
|
if (orchestration_1.WorkflowManager.getWorkflow(name)) {
|
|
76
78
|
orchestration_1.WorkflowManager.unregister(name);
|
|
@@ -97,16 +99,24 @@ composer, options) {
|
|
|
97
99
|
const inputPlaceHolder = (0, proxy_1.proxify)({
|
|
98
100
|
__type: utils_1.OrchestrationUtils.SymbolInputReference,
|
|
99
101
|
__step__: "",
|
|
102
|
+
config: () => {
|
|
103
|
+
// TODO: config default value?
|
|
104
|
+
throw new Error("Config is not available for the input object.");
|
|
105
|
+
},
|
|
100
106
|
});
|
|
101
107
|
const returnedStep = composer.apply(context, [inputPlaceHolder]);
|
|
102
108
|
delete global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerContext];
|
|
103
|
-
orchestration_1.WorkflowManager.update(name, context.flow, handlers);
|
|
109
|
+
orchestration_1.WorkflowManager.update(name, context.flow, handlers, options);
|
|
104
110
|
const workflow = (0, helper_1.exportWorkflow)(name, returnedStep, undefined, {
|
|
105
111
|
wrappedInput: true,
|
|
106
112
|
});
|
|
107
113
|
const mainFlow = (container) => {
|
|
108
114
|
const workflow_ = workflow(container);
|
|
109
|
-
|
|
115
|
+
const expandedFlow = workflow_;
|
|
116
|
+
expandedFlow.config = (config) => {
|
|
117
|
+
workflow_.setOptions(config);
|
|
118
|
+
};
|
|
119
|
+
return expandedFlow;
|
|
110
120
|
};
|
|
111
121
|
let shouldRegisterHookHandler = true;
|
|
112
122
|
for (const hook of context.hooks_) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-workflow.js","sourceRoot":"","sources":["../../../src/utils/composer/create-workflow.ts"],"names":[],"mappings":";;;AAAA,2DAKgC;AAEhC,
|
|
1
|
+
{"version":3,"file":"create-workflow.js","sourceRoot":"","sources":["../../../src/utils/composer/create-workflow.ts"],"names":[],"mappings":";;;AAAA,2DAKgC;AAEhC,2CAA8D;AAC9D,yCAA+D;AAC/D,2CAAyC;AAOzC,MAAM,CAAC,0BAAkB,CAAC,mCAAmC,CAAC,GAAG,IAAI,CAAA;AA6ErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AAEH,SAAgB,cAAc;AAK5B;;GAEG;AACH,YAAmE;AACnE;;;;GAIG;AACH,QAOK;IAEL,MAAM,IAAI,GAAG,IAAA,gBAAQ,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAA;IACtE,MAAM,OAAO,GAAG,IAAA,gBAAQ,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,CAAA;IAE1D,MAAM,QAAQ,GAAoB,IAAI,GAAG,EAAE,CAAA;IAE3C,IAAI,+BAAe,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE;QACrC,+BAAe,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;KACjC;IAED,+BAAe,CAAC,QAAQ,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAA;IAE5D,MAAM,OAAO,GAAkC;QAC7C,UAAU,EAAE,IAAI;QAChB,IAAI,EAAE,+BAAe,CAAC,wBAAwB,CAAC,IAAI,CAAC;QACpD,QAAQ;QACR,MAAM,EAAE,EAAE;QACV,cAAc,EAAE,EAAE;QAClB,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE;YACvB,OAAO,CAAC,MAAM,CAAC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AM,CAAC,CAAA;QAC9B,CAAC,CAAA;QAED,OAAO,YAAY,CAAA;IACrB,CAAC,CAAA;IAED,IAAI,yBAAyB,GAAG,IAAI,CAAA;IAEpC,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,MAAM,EAAE;QACjC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE;;YACtB,MAAA,OAAO,CAAC,cAAc,EAAC,IAAI,SAAJ,IAAI,IAAM,EAAE,EAAA;YAEnC,IAAI,CAAC,yBAAyB,EAAE;gBAC9B,OAAO,CAAC,IAAI,CACV,sDAAsD,IAAI,0DAA0D,CACrH,CAAA;gBACD,OAAM;aACP;YAED,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YACrC,yBAAyB,GAAG,KAAK,CAAA;QACnC,CAAC,CAAA;KACF;IAED,QAAQ,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC,IAAI,CAAA;IAE7B,OAAO,QAAkD,CAAA;AAC3D,CAAC;AA/GD,wCA+GC"}
|
|
@@ -21,11 +21,7 @@ export type StepFunction<TInput, TOutput = unknown> = (keyof TInput extends [] ?
|
|
|
21
21
|
}>;
|
|
22
22
|
}) & WorkflowDataProperties<{
|
|
23
23
|
[K in keyof TOutput]: TOutput[K];
|
|
24
|
-
}> & {
|
|
25
|
-
config(config: Pick<TransactionStepsDefinition, "maxRetries">): WorkflowData<{
|
|
26
|
-
[K in keyof TOutput]: TOutput[K];
|
|
27
|
-
}>;
|
|
28
|
-
} & WorkflowDataProperties<{
|
|
24
|
+
}> & WorkflowDataProperties<{
|
|
29
25
|
[K in keyof TOutput]: TOutput[K];
|
|
30
26
|
}>;
|
|
31
27
|
export type WorkflowDataProperties<T = unknown> = {
|
|
@@ -39,7 +35,13 @@ export type WorkflowDataProperties<T = unknown> = {
|
|
|
39
35
|
*/
|
|
40
36
|
export type WorkflowData<T = unknown> = (T extends object ? {
|
|
41
37
|
[Key in keyof T]: WorkflowData<T[Key]>;
|
|
42
|
-
} : WorkflowDataProperties<T>) & WorkflowDataProperties<T
|
|
38
|
+
} : WorkflowDataProperties<T>) & WorkflowDataProperties<T> & {
|
|
39
|
+
config(config: {
|
|
40
|
+
name?: string;
|
|
41
|
+
} & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">): T extends object ? WorkflowData<T extends object ? {
|
|
42
|
+
[K in keyof T]: T[K];
|
|
43
|
+
} : T> : T;
|
|
44
|
+
};
|
|
43
45
|
export type CreateWorkflowComposerContext = {
|
|
44
46
|
hooks_: string[];
|
|
45
47
|
hooksCallback_: Record<string, Function[]>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medusajs/workflows-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2-next-20240124093206",
|
|
4
4
|
"description": "Set of workflows tooling for Medusa",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"author": "Medusa",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@medusajs/types": "
|
|
24
|
+
"@medusajs/types": "1.11.11-next-20240124093206",
|
|
25
25
|
"cross-env": "^5.2.1",
|
|
26
26
|
"jest": "^29.6.3",
|
|
27
27
|
"rimraf": "^5.0.1",
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"typescript": "^5.1.6"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@medusajs/modules-sdk": "
|
|
33
|
-
"@medusajs/orchestration": "
|
|
34
|
-
"@medusajs/utils": "
|
|
32
|
+
"@medusajs/modules-sdk": "1.12.7-next-20240124093206",
|
|
33
|
+
"@medusajs/orchestration": "0.5.3-next-20240124093206",
|
|
34
|
+
"@medusajs/utils": "1.11.4-next-20240124093206",
|
|
35
35
|
"awilix": "^8.0.1",
|
|
36
36
|
"ulid": "^2.3.0"
|
|
37
37
|
},
|