@8medusa/workflows-sdk 2.7.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/dist/helper/index.d.ts +3 -0
- package/dist/helper/index.d.ts.map +1 -0
- package/dist/helper/index.js +19 -0
- package/dist/helper/index.js.map +1 -0
- package/dist/helper/type.d.ts +66 -0
- package/dist/helper/type.d.ts.map +1 -0
- package/dist/helper/type.js +3 -0
- package/dist/helper/type.js.map +1 -0
- package/dist/helper/workflow-export.d.ts +6 -0
- package/dist/helper/workflow-export.d.ts.map +1 -0
- package/dist/helper/workflow-export.js +253 -0
- package/dist/helper/workflow-export.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/medusa-workflow.d.ts +3 -0
- package/dist/medusa-workflow.d.ts.map +1 -0
- package/dist/medusa-workflow.js +19 -0
- package/dist/medusa-workflow.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/_playground.d.ts +2 -0
- package/dist/utils/_playground.d.ts.map +1 -0
- package/dist/utils/_playground.js +79 -0
- package/dist/utils/_playground.js.map +1 -0
- package/dist/utils/composer/create-hook.d.ts +60 -0
- package/dist/utils/composer/create-hook.d.ts.map +1 -0
- package/dist/utils/composer/create-hook.js +93 -0
- package/dist/utils/composer/create-hook.js.map +1 -0
- package/dist/utils/composer/create-step.d.ts +134 -0
- package/dist/utils/composer/create-step.d.ts.map +1 -0
- package/dist/utils/composer/create-step.js +237 -0
- package/dist/utils/composer/create-step.js.map +1 -0
- package/dist/utils/composer/create-workflow.d.ts +71 -0
- package/dist/utils/composer/create-workflow.d.ts.map +1 -0
- package/dist/utils/composer/create-workflow.js +209 -0
- package/dist/utils/composer/create-workflow.js.map +1 -0
- package/dist/utils/composer/helpers/create-step-handler.d.ts +20 -0
- package/dist/utils/composer/helpers/create-step-handler.d.ts.map +1 -0
- package/dist/utils/composer/helpers/create-step-handler.js +70 -0
- package/dist/utils/composer/helpers/create-step-handler.js.map +1 -0
- package/dist/utils/composer/helpers/index.d.ts +3 -0
- package/dist/utils/composer/helpers/index.d.ts.map +1 -0
- package/dist/utils/composer/helpers/index.js +19 -0
- package/dist/utils/composer/helpers/index.js.map +1 -0
- package/dist/utils/composer/helpers/proxy.d.ts +3 -0
- package/dist/utils/composer/helpers/proxy.d.ts.map +1 -0
- package/dist/utils/composer/helpers/proxy.js +25 -0
- package/dist/utils/composer/helpers/proxy.js.map +1 -0
- package/dist/utils/composer/helpers/resolve-value.d.ts +5 -0
- package/dist/utils/composer/helpers/resolve-value.d.ts.map +1 -0
- package/dist/utils/composer/helpers/resolve-value.js +64 -0
- package/dist/utils/composer/helpers/resolve-value.js.map +1 -0
- package/dist/utils/composer/helpers/step-response.d.ts +126 -0
- package/dist/utils/composer/helpers/step-response.d.ts.map +1 -0
- package/dist/utils/composer/helpers/step-response.js +166 -0
- package/dist/utils/composer/helpers/step-response.js.map +1 -0
- package/dist/utils/composer/helpers/workflow-response.d.ts +20 -0
- package/dist/utils/composer/helpers/workflow-response.d.ts.map +1 -0
- package/dist/utils/composer/helpers/workflow-response.js +16 -0
- package/dist/utils/composer/helpers/workflow-response.js.map +1 -0
- package/dist/utils/composer/index.d.ts +11 -0
- package/dist/utils/composer/index.d.ts.map +1 -0
- package/dist/utils/composer/index.js +27 -0
- package/dist/utils/composer/index.js.map +1 -0
- package/dist/utils/composer/parallelize.d.ts +43 -0
- package/dist/utils/composer/parallelize.d.ts.map +1 -0
- package/dist/utils/composer/parallelize.js +57 -0
- package/dist/utils/composer/parallelize.js.map +1 -0
- package/dist/utils/composer/transform.d.ts +79 -0
- package/dist/utils/composer/transform.d.ts.map +1 -0
- package/dist/utils/composer/transform.js +40 -0
- package/dist/utils/composer/transform.js.map +1 -0
- package/dist/utils/composer/type.d.ts +227 -0
- package/dist/utils/composer/type.d.ts.map +1 -0
- package/dist/utils/composer/type.js +3 -0
- package/dist/utils/composer/type.js.map +1 -0
- package/dist/utils/composer/when.d.ts +58 -0
- package/dist/utils/composer/when.d.ts.map +1 -0
- package/dist/utils/composer/when.js +56 -0
- package/dist/utils/composer/when.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHook = createHook;
|
|
4
|
+
const utils_1 = require("@8medusa/utils");
|
|
5
|
+
const create_step_1 = require("./create-step");
|
|
6
|
+
const create_step_handler_1 = require("./helpers/create-step-handler");
|
|
7
|
+
const helpers_1 = require("./helpers");
|
|
8
|
+
const NOOP_RESULT = Symbol.for("NOOP");
|
|
9
|
+
/**
|
|
10
|
+
* Expose a hook in your workflow where you can inject custom functionality as a step function.
|
|
11
|
+
*
|
|
12
|
+
* A handler hook can later be registered to consume the hook and perform custom functionality.
|
|
13
|
+
*
|
|
14
|
+
* Learn more in [this documentation](https://docs.medusajs.com/learn/fundamentals/workflows/workflow-hooks).
|
|
15
|
+
*
|
|
16
|
+
* @param name - The hook's name. This is used when the hook handler is registered to consume the workflow.
|
|
17
|
+
* @param input - The input to pass to the hook handler.
|
|
18
|
+
* @returns A workflow hook.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import {
|
|
22
|
+
* createStep,
|
|
23
|
+
* createHook,
|
|
24
|
+
* createWorkflow,
|
|
25
|
+
* WorkflowResponse,
|
|
26
|
+
* } from "@8medusa/framework/workflows-sdk"
|
|
27
|
+
* import { createProductStep } from "./steps/create-product"
|
|
28
|
+
*
|
|
29
|
+
* export const myWorkflow = createWorkflow(
|
|
30
|
+
* "my-workflow",
|
|
31
|
+
* function (input) {
|
|
32
|
+
* const product = createProductStep(input)
|
|
33
|
+
* const productCreatedHook = createHook(
|
|
34
|
+
* "productCreated",
|
|
35
|
+
* { productId: product.id }
|
|
36
|
+
* )
|
|
37
|
+
*
|
|
38
|
+
* return new WorkflowResponse(product, {
|
|
39
|
+
* hooks: [productCreatedHook],
|
|
40
|
+
* })
|
|
41
|
+
* }
|
|
42
|
+
* )
|
|
43
|
+
*/
|
|
44
|
+
function createHook(name, input, options = {}) {
|
|
45
|
+
const context = global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerContext];
|
|
46
|
+
const getHookResultStep = (0, create_step_1.createStep)(`get-${name}-result`, (_, context) => {
|
|
47
|
+
const result = context[" getStepResult"](name);
|
|
48
|
+
if (result === NOOP_RESULT) {
|
|
49
|
+
return new helpers_1.StepResponse(undefined);
|
|
50
|
+
}
|
|
51
|
+
if (options.resultValidator) {
|
|
52
|
+
return options.resultValidator.parse(result);
|
|
53
|
+
}
|
|
54
|
+
if (result === undefined) {
|
|
55
|
+
return new helpers_1.StepResponse(undefined);
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}, () => void 0);
|
|
59
|
+
context.hookBinder(name, function () {
|
|
60
|
+
/**
|
|
61
|
+
* We start by registering a new step within the workflow. This will be a noop
|
|
62
|
+
* step that can be replaced (optionally) by the workflow consumer.
|
|
63
|
+
*/
|
|
64
|
+
(0, create_step_1.createStep)(name, (_) => new helpers_1.StepResponse(NOOP_RESULT), () => void 0)(input);
|
|
65
|
+
function hook(invokeFn, compensateFn) {
|
|
66
|
+
const handlers = create_step_handler_1.createStepHandler.bind(this)({
|
|
67
|
+
stepName: name,
|
|
68
|
+
input,
|
|
69
|
+
invokeFn,
|
|
70
|
+
compensateFn,
|
|
71
|
+
});
|
|
72
|
+
if (this.hooks_.registered.includes(name)) {
|
|
73
|
+
throw new Error(`Cannot define multiple hook handlers for the ${name} hook`);
|
|
74
|
+
}
|
|
75
|
+
this.hooks_.registered.push(name);
|
|
76
|
+
this.handlers.set(name, handlers);
|
|
77
|
+
}
|
|
78
|
+
return hook;
|
|
79
|
+
});
|
|
80
|
+
return {
|
|
81
|
+
__type: utils_1.OrchestrationUtils.SymbolWorkflowHook,
|
|
82
|
+
name,
|
|
83
|
+
getResult() {
|
|
84
|
+
if ("cachedResult" in this) {
|
|
85
|
+
return this.cachedResult;
|
|
86
|
+
}
|
|
87
|
+
const result = getHookResultStep();
|
|
88
|
+
this["cachedResult"] = result;
|
|
89
|
+
return result;
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=create-hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-hook.js","sourceRoot":"","sources":["../../../src/utils/composer/create-hook.ts"],"names":[],"mappings":";;AAkEA,gCA2EC;AA5ID,0CAAmD;AAEnD,+CAAkE;AAClE,uEAAiE;AACjE,uCAAwC;AAExC,MAAM,WAAW,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AAwBtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,SAAgB,UAAU,CACxB,IAAU,EACV,KAAmB,EACnB,UAEI,EAAE;IAEN,MAAM,OAAO,GAAG,MAAM,CACpB,0BAAkB,CAAC,mCAAmC,CACtB,CAAA;IAElC,MAAM,iBAAiB,GAAG,IAAA,wBAAU,EAClC,OAAO,IAAI,SAAS,EACpB,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE;QACb,MAAM,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,CAAA;QAC9C,IAAI,MAAM,KAAK,WAAW,EAAE,CAAC;YAC3B,OAAO,IAAI,sBAAY,CAAC,SAAS,CAAC,CAAA;QACpC,CAAC;QACD,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;YAC5B,OAAO,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;QAC9C,CAAC;QACD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,OAAO,IAAI,sBAAY,CAAC,SAAS,CAAC,CAAA;QACpC,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC,EACD,GAAG,EAAE,CAAC,KA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
|
+
import { StepExecutionContext, StepFunction, StepFunctionResult, WorkflowData } from "./type";
|
|
4
|
+
/**
|
|
5
|
+
* The type of invocation function passed to a step.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam TInput - The type of the input that the function expects.
|
|
8
|
+
* @typeParam TOutput - The type of the output that the function returns.
|
|
9
|
+
* @typeParam TCompensateInput - The type of the input that the compensation function expects.
|
|
10
|
+
*
|
|
11
|
+
* @returns The expected output based on the type parameter `TOutput`.
|
|
12
|
+
*/
|
|
13
|
+
export type InvokeFn<TInput, TOutput, TCompensateInput> = (
|
|
14
|
+
/**
|
|
15
|
+
* The input of the step.
|
|
16
|
+
*/
|
|
17
|
+
input: TInput,
|
|
18
|
+
/**
|
|
19
|
+
* The step's context.
|
|
20
|
+
*/
|
|
21
|
+
context: StepExecutionContext) => void | StepResponse<TOutput, TCompensateInput extends undefined ? TOutput : TCompensateInput> | Promise<void | StepResponse<TOutput, TCompensateInput extends undefined ? TOutput : TCompensateInput>>;
|
|
22
|
+
/**
|
|
23
|
+
* The type of compensation function passed to a step.
|
|
24
|
+
*
|
|
25
|
+
* @typeParam T -
|
|
26
|
+
* The type of the argument passed to the compensation function. If not specified, then it will be the same type as the invocation function's output.
|
|
27
|
+
*
|
|
28
|
+
* @returns There's no expected type to be returned by the compensation function.
|
|
29
|
+
*/
|
|
30
|
+
export type CompensateFn<T> = (
|
|
31
|
+
/**
|
|
32
|
+
* The argument passed to the compensation function.
|
|
33
|
+
*/
|
|
34
|
+
input: T | undefined,
|
|
35
|
+
/**
|
|
36
|
+
* The step's context.
|
|
37
|
+
*/
|
|
38
|
+
context: StepExecutionContext) => unknown | Promise<unknown>;
|
|
39
|
+
export type LocalStepConfig = {
|
|
40
|
+
name?: string;
|
|
41
|
+
} & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">;
|
|
42
|
+
export interface ApplyStepOptions<TStepInputs extends {
|
|
43
|
+
[K in keyof TInvokeInput]: WorkflowData<TInvokeInput[K]>;
|
|
44
|
+
}, TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput> {
|
|
45
|
+
stepName: string;
|
|
46
|
+
stepConfig?: TransactionStepsDefinition;
|
|
47
|
+
input?: TStepInputs;
|
|
48
|
+
invokeFn: InvokeFn<TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput>;
|
|
49
|
+
compensateFn?: CompensateFn<TInvokeResultCompensateInput>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @internal
|
|
53
|
+
*
|
|
54
|
+
* Internal function to create the invoke and compensate handler for a step.
|
|
55
|
+
* This is where the inputs and context are passed to the underlying invoke and compensate function.
|
|
56
|
+
*
|
|
57
|
+
* @param stepName
|
|
58
|
+
* @param stepConfig
|
|
59
|
+
* @param input
|
|
60
|
+
* @param invokeFn
|
|
61
|
+
* @param compensateFn
|
|
62
|
+
*/
|
|
63
|
+
export declare function applyStep<TInvokeInput, TStepInput extends {
|
|
64
|
+
[K in keyof TInvokeInput]: WorkflowData<TInvokeInput[K]>;
|
|
65
|
+
}, TInvokeResultOutput, TInvokeResultCompensateInput>({ stepName, stepConfig, input, invokeFn, compensateFn, }: ApplyStepOptions<TStepInput, TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput>): StepFunctionResult<TInvokeResultOutput>;
|
|
66
|
+
/**
|
|
67
|
+
* This function creates a {@link StepFunction} that can be used as a step in a workflow constructed by the {@link createWorkflow} function.
|
|
68
|
+
*
|
|
69
|
+
* @typeParam TInvokeInput - The type of the expected input parameter to the invocation function.
|
|
70
|
+
* @typeParam TInvokeResultOutput - The type of the expected output parameter of the invocation function.
|
|
71
|
+
* @typeParam TInvokeResultCompensateInput - The type of the expected input parameter to the compensation function.
|
|
72
|
+
*
|
|
73
|
+
* @returns A step function to be used in a workflow.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* import {
|
|
77
|
+
* createStep,
|
|
78
|
+
* StepResponse
|
|
79
|
+
* } from "@8medusa/framework/workflows-sdk"
|
|
80
|
+
*
|
|
81
|
+
* interface CreateProductInput {
|
|
82
|
+
* title: string
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* export const createProductStep = createStep(
|
|
86
|
+
* "createProductStep",
|
|
87
|
+
* async function (
|
|
88
|
+
* input: CreateProductInput,
|
|
89
|
+
* { container }
|
|
90
|
+
* ) {
|
|
91
|
+
* const productModuleService = container.resolve(
|
|
92
|
+
* "product"
|
|
93
|
+
* )
|
|
94
|
+
* const product = await productModuleService.createProducts(input)
|
|
95
|
+
* return new StepResponse({
|
|
96
|
+
* product
|
|
97
|
+
* }, {
|
|
98
|
+
* product_id: product.id
|
|
99
|
+
* })
|
|
100
|
+
* },
|
|
101
|
+
* async function (
|
|
102
|
+
* input,
|
|
103
|
+
* { container }
|
|
104
|
+
* ) {
|
|
105
|
+
* if (!input) {
|
|
106
|
+
* return
|
|
107
|
+
* }
|
|
108
|
+
* const productModuleService = container.resolve(
|
|
109
|
+
* "product"
|
|
110
|
+
* )
|
|
111
|
+
* await productModuleService.deleteProducts([input.product_id])
|
|
112
|
+
* }
|
|
113
|
+
* )
|
|
114
|
+
*/
|
|
115
|
+
export declare function createStep<TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput>(
|
|
116
|
+
/**
|
|
117
|
+
* The name of the step or its configuration.
|
|
118
|
+
*/
|
|
119
|
+
nameOrConfig: string | ({
|
|
120
|
+
name: string;
|
|
121
|
+
} & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">),
|
|
122
|
+
/**
|
|
123
|
+
* 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}
|
|
124
|
+
* 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.
|
|
125
|
+
*/
|
|
126
|
+
invokeFn: InvokeFn<TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput>,
|
|
127
|
+
/**
|
|
128
|
+
* A compensation function that's executed if an error occurs in the workflow. It's used to roll-back actions when errors occur.
|
|
129
|
+
* It accepts as a parameter the second argument passed to the constructor of the {@link StepResponse} instance returned by the invocation function. If the
|
|
130
|
+
* invocation function doesn't pass the second argument to `StepResponse` constructor, the compensation function receives the first argument
|
|
131
|
+
* passed to the `StepResponse` constructor instead.
|
|
132
|
+
*/
|
|
133
|
+
compensateFn?: CompensateFn<TInvokeResultCompensateInput>): StepFunction<TInvokeInput, TInvokeResultOutput>;
|
|
134
|
+
//# sourceMappingURL=create-step.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-step.d.ts","sourceRoot":"","sources":["../../../src/utils/composer/create-step.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,0BAA0B,EAI3B,MAAM,wBAAwB,CAAA;AAG/B,OAAO,EAAgB,YAAY,EAAE,MAAM,WAAW,CAAA;AAGtD,OAAO,EAEL,oBAAoB,EACpB,YAAY,EACZ,kBAAkB,EAClB,YAAY,EACb,MAAM,QAAQ,CAAA;AAEf;;;;;;;;GAQG;AACH,MAAM,MAAM,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,gBAAgB,IAAI;AACxD;;GAEG;AACH,KAAK,EAAE,MAAM;AACb;;GAEG;AACH,OAAO,EAAE,oBAAoB,KAE3B,IAAI,GACJ,YAAY,CACV,OAAO,EACP,gBAAgB,SAAS,SAAS,GAAG,OAAO,GAAG,gBAAgB,CAChE,GACD,OAAO,CAAC,IAAI,GAAG,YAAY,CACzB,OAAO,EACP,gBAAgB,SAAS,SAAS,GAAG,OAAO,GAAG,gBAAgB,CAChE,CAAC,CAAA;AAEN;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI;AAC5B;;GAEG;AACH,KAAK,EAAE,CAAC,GAAG,SAAS;AACpB;;GAEG;AACH,OAAO,EAAE,oBAAoB,KAC1B,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;AAE/B,MAAM,MAAM,eAAe,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,C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B,EACnB,4BAA4B,CAC7B;AACD;;;;;GAKG;AACH,YAAY,CAAC,EAAE,YAAY,CAAC,4BAA4B,CAAC,GACxD,YAAY,CAAC,YAAY,EAAE,mBAAmB,CAAC,CA0CjD"}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applyStep = applyStep;
|
|
4
|
+
exports.createStep = createStep;
|
|
5
|
+
const orchestration_1 = require("@8medusa/orchestration");
|
|
6
|
+
const utils_1 = require("@8medusa/utils");
|
|
7
|
+
const ulid_1 = require("ulid");
|
|
8
|
+
const helpers_1 = require("./helpers");
|
|
9
|
+
const create_step_handler_1 = require("./helpers/create-step-handler");
|
|
10
|
+
const proxy_1 = require("./helpers/proxy");
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*
|
|
14
|
+
* Internal function to create the invoke and compensate handler for a step.
|
|
15
|
+
* This is where the inputs and context are passed to the underlying invoke and compensate function.
|
|
16
|
+
*
|
|
17
|
+
* @param stepName
|
|
18
|
+
* @param stepConfig
|
|
19
|
+
* @param input
|
|
20
|
+
* @param invokeFn
|
|
21
|
+
* @param compensateFn
|
|
22
|
+
*/
|
|
23
|
+
function applyStep({ stepName, stepConfig = {}, input, invokeFn, compensateFn, }) {
|
|
24
|
+
return function () {
|
|
25
|
+
if (!this.workflowId) {
|
|
26
|
+
throw new Error("createStep must be used inside a createWorkflow definition");
|
|
27
|
+
}
|
|
28
|
+
const handler = create_step_handler_1.createStepHandler.bind(this)({
|
|
29
|
+
stepName,
|
|
30
|
+
input,
|
|
31
|
+
invokeFn,
|
|
32
|
+
compensateFn,
|
|
33
|
+
});
|
|
34
|
+
wrapAsyncHandler(stepConfig, handler);
|
|
35
|
+
stepConfig.uuid = (0, ulid_1.ulid)();
|
|
36
|
+
stepConfig.noCompensation = !compensateFn;
|
|
37
|
+
this.flow.addAction(stepName, stepConfig);
|
|
38
|
+
this.isAsync ||= !!(stepConfig.async || stepConfig.compensateAsync);
|
|
39
|
+
if (!this.handlers.has(stepName)) {
|
|
40
|
+
this.handlers.set(stepName, handler);
|
|
41
|
+
}
|
|
42
|
+
const ret = {
|
|
43
|
+
__type: utils_1.OrchestrationUtils.SymbolWorkflowStep,
|
|
44
|
+
__step__: stepName,
|
|
45
|
+
};
|
|
46
|
+
const refRet = (0, proxy_1.proxify)(ret);
|
|
47
|
+
refRet.config = (localConfig) => {
|
|
48
|
+
const newStepName = localConfig.name ?? stepName;
|
|
49
|
+
const newConfig = {
|
|
50
|
+
async: false,
|
|
51
|
+
compensateAsync: false,
|
|
52
|
+
...stepConfig,
|
|
53
|
+
...localConfig,
|
|
54
|
+
};
|
|
55
|
+
delete localConfig.name;
|
|
56
|
+
const handler = create_step_handler_1.createStepHandler.bind(this)({
|
|
57
|
+
stepName: newStepName,
|
|
58
|
+
input,
|
|
59
|
+
invokeFn,
|
|
60
|
+
compensateFn,
|
|
61
|
+
});
|
|
62
|
+
wrapAsyncHandler(stepConfig, handler);
|
|
63
|
+
this.handlers.set(newStepName, handler);
|
|
64
|
+
this.flow.replaceAction(stepConfig.uuid, newStepName, newConfig);
|
|
65
|
+
this.isAsync ||= !!(newConfig.async || newConfig.compensateAsync);
|
|
66
|
+
ret.__step__ = newStepName;
|
|
67
|
+
orchestration_1.WorkflowManager.update(this.workflowId, this.flow, this.handlers);
|
|
68
|
+
if (global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerCondition]) {
|
|
69
|
+
const flagSteps = global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerCondition].steps;
|
|
70
|
+
const idx = flagSteps.findIndex((a) => a.__step__ === ret.__step__);
|
|
71
|
+
if (idx > -1) {
|
|
72
|
+
flagSteps.splice(idx, 1);
|
|
73
|
+
}
|
|
74
|
+
flagSteps.push(refRet);
|
|
75
|
+
}
|
|
76
|
+
return refRet;
|
|
77
|
+
};
|
|
78
|
+
refRet.if = (input, condition) => {
|
|
79
|
+
if (typeof condition !== "function") {
|
|
80
|
+
throw new Error("Condition must be a function");
|
|
81
|
+
}
|
|
82
|
+
wrapConditionalStep(input, condition, handler);
|
|
83
|
+
this.handlers.set(ret.__step__, handler);
|
|
84
|
+
return refRet;
|
|
85
|
+
};
|
|
86
|
+
if (global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerCondition]) {
|
|
87
|
+
global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerCondition].steps.push(refRet);
|
|
88
|
+
}
|
|
89
|
+
return refRet;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @internal
|
|
94
|
+
*
|
|
95
|
+
* Internal function to handle async steps to be automatically marked as completed after they are executed.
|
|
96
|
+
*
|
|
97
|
+
* @param stepConfig
|
|
98
|
+
* @param handle
|
|
99
|
+
*/
|
|
100
|
+
function wrapAsyncHandler(stepConfig, handle) {
|
|
101
|
+
if (stepConfig.async) {
|
|
102
|
+
if (typeof handle.invoke === "function") {
|
|
103
|
+
const originalInvoke = handle.invoke;
|
|
104
|
+
handle.invoke = async (stepArguments) => {
|
|
105
|
+
const response = (await originalInvoke(stepArguments));
|
|
106
|
+
if (response?.output?.__type !==
|
|
107
|
+
utils_1.OrchestrationUtils.SymbolWorkflowStepResponse) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
stepArguments.step.definition.backgroundExecution = true;
|
|
111
|
+
return response;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (stepConfig.compensateAsync) {
|
|
116
|
+
if (typeof handle.compensate === "function") {
|
|
117
|
+
const originalCompensate = handle.compensate;
|
|
118
|
+
handle.compensate = async (stepArguments) => {
|
|
119
|
+
const response = (await originalCompensate(stepArguments));
|
|
120
|
+
if (response?.output?.__type !==
|
|
121
|
+
utils_1.OrchestrationUtils.SymbolWorkflowStepResponse) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
stepArguments.step.definition.backgroundExecution = true;
|
|
125
|
+
return response;
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* @internal
|
|
132
|
+
*
|
|
133
|
+
* Internal function to handle conditional steps.
|
|
134
|
+
*
|
|
135
|
+
* @param condition
|
|
136
|
+
* @param handle
|
|
137
|
+
*/
|
|
138
|
+
function wrapConditionalStep(input, condition, handle) {
|
|
139
|
+
const originalInvoke = handle.invoke;
|
|
140
|
+
handle.invoke = async (stepArguments) => {
|
|
141
|
+
const args = await (0, helpers_1.resolveValue)(input, stepArguments);
|
|
142
|
+
const canContinue = await condition(args, stepArguments);
|
|
143
|
+
if (stepArguments.step.definition?.async) {
|
|
144
|
+
stepArguments.step.definition.backgroundExecution = true;
|
|
145
|
+
}
|
|
146
|
+
if (!canContinue) {
|
|
147
|
+
return helpers_1.StepResponse.skip();
|
|
148
|
+
}
|
|
149
|
+
return await originalInvoke(stepArguments);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* This function creates a {@link StepFunction} that can be used as a step in a workflow constructed by the {@link createWorkflow} function.
|
|
154
|
+
*
|
|
155
|
+
* @typeParam TInvokeInput - The type of the expected input parameter to the invocation function.
|
|
156
|
+
* @typeParam TInvokeResultOutput - The type of the expected output parameter of the invocation function.
|
|
157
|
+
* @typeParam TInvokeResultCompensateInput - The type of the expected input parameter to the compensation function.
|
|
158
|
+
*
|
|
159
|
+
* @returns A step function to be used in a workflow.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* import {
|
|
163
|
+
* createStep,
|
|
164
|
+
* StepResponse
|
|
165
|
+
* } from "@8medusa/framework/workflows-sdk"
|
|
166
|
+
*
|
|
167
|
+
* interface CreateProductInput {
|
|
168
|
+
* title: string
|
|
169
|
+
* }
|
|
170
|
+
*
|
|
171
|
+
* export const createProductStep = createStep(
|
|
172
|
+
* "createProductStep",
|
|
173
|
+
* async function (
|
|
174
|
+
* input: CreateProductInput,
|
|
175
|
+
* { container }
|
|
176
|
+
* ) {
|
|
177
|
+
* const productModuleService = container.resolve(
|
|
178
|
+
* "product"
|
|
179
|
+
* )
|
|
180
|
+
* const product = await productModuleService.createProducts(input)
|
|
181
|
+
* return new StepResponse({
|
|
182
|
+
* product
|
|
183
|
+
* }, {
|
|
184
|
+
* product_id: product.id
|
|
185
|
+
* })
|
|
186
|
+
* },
|
|
187
|
+
* async function (
|
|
188
|
+
* input,
|
|
189
|
+
* { container }
|
|
190
|
+
* ) {
|
|
191
|
+
* if (!input) {
|
|
192
|
+
* return
|
|
193
|
+
* }
|
|
194
|
+
* const productModuleService = container.resolve(
|
|
195
|
+
* "product"
|
|
196
|
+
* )
|
|
197
|
+
* await productModuleService.deleteProducts([input.product_id])
|
|
198
|
+
* }
|
|
199
|
+
* )
|
|
200
|
+
*/
|
|
201
|
+
function createStep(
|
|
202
|
+
/**
|
|
203
|
+
* The name of the step or its configuration.
|
|
204
|
+
*/
|
|
205
|
+
nameOrConfig,
|
|
206
|
+
/**
|
|
207
|
+
* 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}
|
|
208
|
+
* 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.
|
|
209
|
+
*/
|
|
210
|
+
invokeFn,
|
|
211
|
+
/**
|
|
212
|
+
* A compensation function that's executed if an error occurs in the workflow. It's used to roll-back actions when errors occur.
|
|
213
|
+
* It accepts as a parameter the second argument passed to the constructor of the {@link StepResponse} instance returned by the invocation function. If the
|
|
214
|
+
* invocation function doesn't pass the second argument to `StepResponse` constructor, the compensation function receives the first argument
|
|
215
|
+
* passed to the `StepResponse` constructor instead.
|
|
216
|
+
*/
|
|
217
|
+
compensateFn) {
|
|
218
|
+
const stepName = ((0, utils_1.isString)(nameOrConfig) ? nameOrConfig : nameOrConfig.name) ?? invokeFn.name;
|
|
219
|
+
const config = (0, utils_1.isString)(nameOrConfig) ? {} : nameOrConfig;
|
|
220
|
+
const returnFn = function (input) {
|
|
221
|
+
const context = global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerContext];
|
|
222
|
+
if (!context) {
|
|
223
|
+
throw new Error("createStep must be used inside a createWorkflow definition");
|
|
224
|
+
}
|
|
225
|
+
return context.stepBinder(applyStep({
|
|
226
|
+
stepName,
|
|
227
|
+
stepConfig: config,
|
|
228
|
+
input,
|
|
229
|
+
invokeFn,
|
|
230
|
+
compensateFn,
|
|
231
|
+
}));
|
|
232
|
+
};
|
|
233
|
+
returnFn.__type = utils_1.OrchestrationUtils.SymbolWorkflowStepBind;
|
|
234
|
+
returnFn.__step__ = stepName;
|
|
235
|
+
return returnFn;
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=create-step.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-step.js","sourceRoot":"","sources":["../../../src/utils/composer/create-step.ts"],"names":[],"mappings":";;AAuGA,8BA8HC;AA4ID,gCAwEC;AAzbD,0DAK+B;AAC/B,0CAA6D;AAC7D,+BAA2B;AAC3B,uCAAsD;AACtD,uEAAiE;AACjE,2CAAyC;AAiFzC;;;;;;;;;;;GAWG;AACH,SAAgB,SAAS,CAOvB,EACA,QAAQ,EACR,UAAU,GAAG,EAAE,EACf,KAAK,EACL,QAAQ,EACR,YAAY,GAMb;IACC,OAAO;QACL,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CACb,4DAA4D,CAC7D,CAAA;QACH,CAAC;QAED,MAAM,OAAO,GAAG,uCAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC3C,QAAQ;YACR,KAAK;YACL,QAAQ;YACR,YAAY;SACb,CAAC,CAAA;QAEF,gBAAgB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;QAErC,UAAU,CAAC,IAAI,GAAG,IAAA,WAAI,GAAE,CAAA;QACxB,UAAU,CAAC,cAAc,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,KAAK,CACb,4DAA4D,CAC7D,CAAA;QACH,CAAC;QAED,OAAO,OAAO,CAAC,UAAU,CACvB,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"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { TransactionModelOptions } from "@8medusa/orchestration";
|
|
2
|
+
import { WorkflowResponse } from "./helpers/workflow-response";
|
|
3
|
+
import { ReturnWorkflow, WorkflowData } from "./type";
|
|
4
|
+
/**
|
|
5
|
+
* This function creates a workflow with the provided name and a constructor function.
|
|
6
|
+
* The constructor function builds the workflow from steps created by the {@link createStep} function.
|
|
7
|
+
* The returned workflow is an exported workflow of type {@link ReturnWorkflow}, meaning it's not executed right away. To execute it,
|
|
8
|
+
* invoke the exported workflow, then run its `run` method.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam TData - The type of the input passed to the composer function.
|
|
11
|
+
* @typeParam TResult - The type of the output returned by the composer function.
|
|
12
|
+
* @typeParam THooks - The type of hooks defined in the workflow.
|
|
13
|
+
*
|
|
14
|
+
* @returns The created workflow. You can later execute the workflow by invoking it, then using its `run` method.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* import {
|
|
18
|
+
* createWorkflow,
|
|
19
|
+
* WorkflowResponse
|
|
20
|
+
* } from "@8medusa/framework/workflows-sdk"
|
|
21
|
+
* import { MedusaRequest, MedusaResponse } from "@8medusa/framework/http"
|
|
22
|
+
* import {
|
|
23
|
+
* createProductStep,
|
|
24
|
+
* getProductStep,
|
|
25
|
+
* } from "./steps"
|
|
26
|
+
*
|
|
27
|
+
* interface WorkflowInput {
|
|
28
|
+
* title: string
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* const myWorkflow = createWorkflow(
|
|
32
|
+
* "my-workflow",
|
|
33
|
+
* (input: WorkflowInput) => {
|
|
34
|
+
* // Everything here will be executed and resolved later
|
|
35
|
+
* // during the execution. Including the data access.
|
|
36
|
+
*
|
|
37
|
+
* const product = createProductStep(input)
|
|
38
|
+
* return new WorkflowResponse(getProductStep(product.id))
|
|
39
|
+
* }
|
|
40
|
+
* )
|
|
41
|
+
*
|
|
42
|
+
* export async function GET(
|
|
43
|
+
* req: MedusaRequest,
|
|
44
|
+
* res: MedusaResponse
|
|
45
|
+
* ) {
|
|
46
|
+
* const { result: product } = await myWorkflow(req.scope)
|
|
47
|
+
* .run({
|
|
48
|
+
* input: {
|
|
49
|
+
* title: "Shirt"
|
|
50
|
+
* }
|
|
51
|
+
* })
|
|
52
|
+
*
|
|
53
|
+
* res.json({
|
|
54
|
+
* product
|
|
55
|
+
* })
|
|
56
|
+
* }
|
|
57
|
+
*/
|
|
58
|
+
export declare function createWorkflow<TData, TResult, THooks extends any[]>(
|
|
59
|
+
/**
|
|
60
|
+
* The name of the workflow or its configuration.
|
|
61
|
+
*/
|
|
62
|
+
nameOrConfig: string | ({
|
|
63
|
+
name: string;
|
|
64
|
+
} & TransactionModelOptions),
|
|
65
|
+
/**
|
|
66
|
+
* The constructor function that is executed when the `run` method in {@link ReturnWorkflow} is used.
|
|
67
|
+
* The function can't be an arrow function or an asynchronus function. It also can't directly manipulate data.
|
|
68
|
+
* You'll have to use the {@link transform} function if you need to directly manipulate data.
|
|
69
|
+
*/
|
|
70
|
+
composer: (input: WorkflowData<TData>) => void | WorkflowResponse<TResult, THooks>): ReturnWorkflow<TData, TResult, THooks>;
|
|
71
|
+
//# sourceMappingURL=create-workflow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-workflow.d.ts","sourceRoot":"","sources":["../../../src/utils/composer/create-workflow.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EAGxB,MAAM,wBAAwB,CAAA;AAiB/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAC9D,OAAO,EAGL,cAAc,EAEd,YAAY,EACb,MAAM,QAAQ,CAAA;AAIf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AAEH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,GAAG,EAAE;AACjE;;GAEG;AACH,YAAY,EAAE,MAAM,GAAG,CAAC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,uBAAuB,CAAC;AACnE;;;;GAIG;AACH,QAAQ,EAAE,CACR,KAAK,EAAE,YAAY,CAAC,KAAK,CAAC,KACvB,IAAI,GAAG,gBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,GAC5C,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,CAwKxC"}
|