@medusajs/workflows-sdk 0.2.0-next-20231124140416
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/empty-handler.d.ts +1 -0
- package/dist/helper/empty-handler.js +6 -0
- package/dist/helper/empty-handler.js.map +1 -0
- package/dist/helper/index.d.ts +4 -0
- package/dist/helper/index.js +21 -0
- package/dist/helper/index.js.map +1 -0
- package/dist/helper/merge-data.d.ts +8 -0
- package/dist/helper/merge-data.js +47 -0
- package/dist/helper/merge-data.js.map +1 -0
- package/dist/helper/pipe.d.ts +52 -0
- package/dist/helper/pipe.js +80 -0
- package/dist/helper/pipe.js.map +1 -0
- package/dist/helper/workflow-export.d.ts +16 -0
- package/dist/helper/workflow-export.js +69 -0
- package/dist/helper/workflow-export.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/composer/create-step.d.ts +105 -0
- package/dist/utils/composer/create-step.js +153 -0
- package/dist/utils/composer/create-step.js.map +1 -0
- package/dist/utils/composer/create-workflow.d.ts +124 -0
- package/dist/utils/composer/create-workflow.js +136 -0
- package/dist/utils/composer/create-workflow.js.map +1 -0
- package/dist/utils/composer/helpers/index.d.ts +3 -0
- package/dist/utils/composer/helpers/index.js +20 -0
- package/dist/utils/composer/helpers/index.js.map +1 -0
- package/dist/utils/composer/helpers/proxy.d.ts +2 -0
- package/dist/utils/composer/helpers/proxy.js +27 -0
- package/dist/utils/composer/helpers/proxy.js.map +1 -0
- package/dist/utils/composer/helpers/resolve-value.d.ts +4 -0
- package/dist/utils/composer/helpers/resolve-value.js +59 -0
- package/dist/utils/composer/helpers/resolve-value.js.map +1 -0
- package/dist/utils/composer/helpers/step-response.d.ts +48 -0
- package/dist/utils/composer/helpers/step-response.js +80 -0
- package/dist/utils/composer/helpers/step-response.js.map +1 -0
- package/dist/utils/composer/helpers/symbol.d.ts +8 -0
- package/dist/utils/composer/helpers/symbol.js +12 -0
- package/dist/utils/composer/helpers/symbol.js.map +1 -0
- package/dist/utils/composer/hook.d.ts +94 -0
- package/dist/utils/composer/hook.js +125 -0
- package/dist/utils/composer/hook.js.map +1 -0
- package/dist/utils/composer/index.d.ts +9 -0
- package/dist/utils/composer/index.js +26 -0
- package/dist/utils/composer/index.js.map +1 -0
- package/dist/utils/composer/parallelize.d.ts +42 -0
- package/dist/utils/composer/parallelize.js +58 -0
- package/dist/utils/composer/parallelize.js.map +1 -0
- package/dist/utils/composer/transform.d.ts +83 -0
- package/dist/utils/composer/transform.js +29 -0
- package/dist/utils/composer/transform.js.map +1 -0
- package/dist/utils/composer/type.d.ts +70 -0
- package/dist/utils/composer/type.js +3 -0
- package/dist/utils/composer/type.js.map +1 -0
- package/package.json +41 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hook = void 0;
|
|
4
|
+
const helpers_1 = require("./helpers");
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @ignore
|
|
8
|
+
*
|
|
9
|
+
* This function allows you to add hooks in your workflow that provide access to some data. Then, consumers of that workflow can add a handler function that performs
|
|
10
|
+
* an action with the provided data or modify it.
|
|
11
|
+
*
|
|
12
|
+
* For example, in a "create product" workflow, you may add a hook after the product is created, providing access to the created product.
|
|
13
|
+
* Then, developers using that workflow can hook into that point to access the product, modify its attributes, then return the updated product.
|
|
14
|
+
*
|
|
15
|
+
* @typeParam TOutput - The expected output of the hook's handler function.
|
|
16
|
+
* @returns The output of handler functions of this hook. If there are no handler functions, the output is `undefined`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import {
|
|
20
|
+
* createWorkflow,
|
|
21
|
+
* StepExecutionContext,
|
|
22
|
+
* hook,
|
|
23
|
+
* transform
|
|
24
|
+
* } from "@medusajs/workflows-sdk"
|
|
25
|
+
* import {
|
|
26
|
+
* createProductStep,
|
|
27
|
+
* getProductStep,
|
|
28
|
+
* createPricesStep
|
|
29
|
+
* } from "./steps"
|
|
30
|
+
* import {
|
|
31
|
+
* MedusaRequest,
|
|
32
|
+
* MedusaResponse,
|
|
33
|
+
* Product, ProductService
|
|
34
|
+
* } from "@medusajs/medusa"
|
|
35
|
+
*
|
|
36
|
+
* interface WorkflowInput {
|
|
37
|
+
* title: string
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* const myWorkflow = createWorkflow<
|
|
41
|
+
* WorkflowInput,
|
|
42
|
+
* Product
|
|
43
|
+
* >("my-workflow",
|
|
44
|
+
* function (input) {
|
|
45
|
+
* const product = createProductStep(input)
|
|
46
|
+
*
|
|
47
|
+
* const hookProduct = hook<Product>("createdProductHook", product)
|
|
48
|
+
*
|
|
49
|
+
* const newProduct = transform({
|
|
50
|
+
* product,
|
|
51
|
+
* hookProduct
|
|
52
|
+
* }, (input) => {
|
|
53
|
+
* return input.hookProduct || input.product
|
|
54
|
+
* })
|
|
55
|
+
*
|
|
56
|
+
* const prices = createPricesStep(newProduct)
|
|
57
|
+
*
|
|
58
|
+
* return getProductStep(product.id)
|
|
59
|
+
* }
|
|
60
|
+
* )
|
|
61
|
+
*
|
|
62
|
+
* myWorkflow.createdProductHook(
|
|
63
|
+
* async (product, context: StepExecutionContext) => {
|
|
64
|
+
* const productService: ProductService = context.container.resolve("productService")
|
|
65
|
+
*
|
|
66
|
+
* const updatedProduct = await productService.update(product.id, {
|
|
67
|
+
* description: "a cool shirt"
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* return updatedProduct
|
|
71
|
+
* })
|
|
72
|
+
*
|
|
73
|
+
* export async function POST(
|
|
74
|
+
* req: MedusaRequest,
|
|
75
|
+
* res: MedusaResponse
|
|
76
|
+
* ) {
|
|
77
|
+
* const { result: product } = await myWorkflow(req.scope)
|
|
78
|
+
* .run({
|
|
79
|
+
* input: {
|
|
80
|
+
* title: req.body.title
|
|
81
|
+
* }
|
|
82
|
+
* })
|
|
83
|
+
*
|
|
84
|
+
* res.json({
|
|
85
|
+
* product
|
|
86
|
+
* })
|
|
87
|
+
* }
|
|
88
|
+
*/
|
|
89
|
+
function hook(
|
|
90
|
+
/**
|
|
91
|
+
* The name of the hook. This will be used by the consumer to add a handler method for the hook.
|
|
92
|
+
*/
|
|
93
|
+
name,
|
|
94
|
+
/**
|
|
95
|
+
* The data that a handler function receives as a parameter.
|
|
96
|
+
*/
|
|
97
|
+
value) {
|
|
98
|
+
const hookBinder = global[helpers_1.SymbolMedusaWorkflowComposerContext].hookBinder;
|
|
99
|
+
return hookBinder(name, function (context) {
|
|
100
|
+
return {
|
|
101
|
+
__value: async function (transactionContext) {
|
|
102
|
+
const executionContext = {
|
|
103
|
+
container: transactionContext.container,
|
|
104
|
+
metadata: transactionContext.metadata,
|
|
105
|
+
context: transactionContext.context,
|
|
106
|
+
};
|
|
107
|
+
const allValues = await (0, helpers_1.resolveValue)(value, transactionContext);
|
|
108
|
+
const stepValue = allValues
|
|
109
|
+
? JSON.parse(JSON.stringify(allValues))
|
|
110
|
+
: allValues;
|
|
111
|
+
let finalResult;
|
|
112
|
+
const functions = context.hooksCallback_[name];
|
|
113
|
+
for (let i = 0; i < functions.length; i++) {
|
|
114
|
+
const fn = functions[i];
|
|
115
|
+
const arg = i === 0 ? stepValue : finalResult;
|
|
116
|
+
finalResult = await fn.apply(fn, [arg, executionContext]);
|
|
117
|
+
}
|
|
118
|
+
return finalResult;
|
|
119
|
+
},
|
|
120
|
+
__type: helpers_1.SymbolWorkflowHook,
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
exports.hook = hook;
|
|
125
|
+
//# sourceMappingURL=hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hook.js","sourceRoot":"","sources":["../../../src/utils/composer/hook.ts"],"names":[],"mappings":";;;AAAA,uCAIkB;AAOlB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmFG;AACH,SAAgB,IAAI;AAClB;;GAEG;AACH,IAAY;AACZ;;GAEG;AACH,KAAU;IAEV,MAAM,UAAU,GACd,MAAM,CAAC,6CAAmC,CAC3C,CAAC,UAAU,CAAA;IAEZ,OAAO,UAAU,CAAC,IAAI,EAAE,UAAU,OAAO;QACvC,OAAO;YACL,OAAO,EAAE,KAAK,WAAW,kBAAkB;gBACzC,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,SAAS,GAAG,MAAM,IAAA,sBAAY,EAAC,KAAK,EAAE,kBAAkB,CAAC,CAAA;gBAC/D,MAAM,SAAS,GAAG,SAAS;oBACzB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;oBACvC,CAAC,CAAC,SAAS,CAAA;gBAEb,IAAI,WAAW,CAAA;gBACf,MAAM,SAAS,GAAG,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAA;gBAC9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;oBACzC,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,CAAA;oBACvB,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAA;oBAC7C,WAAW,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC,CAAA;iBAC1D;gBACD,OAAO,WAAW,CAAA;YACpB,CAAC;YACD,MAAM,EAAE,4BAAkB;SAC3B,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAxCD,oBAwCC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./create-step";
|
|
2
|
+
export * from "./create-workflow";
|
|
3
|
+
export * from "./hook";
|
|
4
|
+
export * from "./parallelize";
|
|
5
|
+
export * from "./helpers/resolve-value";
|
|
6
|
+
export * from "./helpers/symbol";
|
|
7
|
+
export * from "./helpers/step-response";
|
|
8
|
+
export * from "./transform";
|
|
9
|
+
export * from "./type";
|
|
@@ -0,0 +1,26 @@
|
|
|
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("./create-step"), exports);
|
|
18
|
+
__exportStar(require("./create-workflow"), exports);
|
|
19
|
+
__exportStar(require("./hook"), exports);
|
|
20
|
+
__exportStar(require("./parallelize"), exports);
|
|
21
|
+
__exportStar(require("./helpers/resolve-value"), exports);
|
|
22
|
+
__exportStar(require("./helpers/symbol"), exports);
|
|
23
|
+
__exportStar(require("./helpers/step-response"), exports);
|
|
24
|
+
__exportStar(require("./transform"), exports);
|
|
25
|
+
__exportStar(require("./type"), exports);
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/composer/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA6B;AAC7B,oDAAiC;AACjC,yCAAsB;AACtB,gDAA6B;AAC7B,0DAAuC;AACvC,mDAAgC;AAChC,0DAAuC;AACvC,8CAA2B;AAC3B,yCAAsB"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { WorkflowData } from "./type";
|
|
2
|
+
/**
|
|
3
|
+
* This function is used to run multiple steps in parallel. The result of each step will be returned as part of the result array.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam TResult - The type of the expected result.
|
|
6
|
+
*
|
|
7
|
+
* @returns The step results. The results are ordered in the array by the order they're passed in the function's parameter.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {
|
|
12
|
+
* createWorkflow,
|
|
13
|
+
* parallelize
|
|
14
|
+
* } from "@medusajs/workflows-sdk"
|
|
15
|
+
* import {
|
|
16
|
+
* createProductStep,
|
|
17
|
+
* getProductStep,
|
|
18
|
+
* createPricesStep,
|
|
19
|
+
* attachProductToSalesChannelStep
|
|
20
|
+
* } from "./steps"
|
|
21
|
+
*
|
|
22
|
+
* interface WorkflowInput {
|
|
23
|
+
* title: string
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* const myWorkflow = createWorkflow<
|
|
27
|
+
* WorkflowInput,
|
|
28
|
+
* Product
|
|
29
|
+
* >("my-workflow", (input) => {
|
|
30
|
+
* const product = createProductStep(input)
|
|
31
|
+
*
|
|
32
|
+
* const [prices, productSalesChannel] = parallelize(
|
|
33
|
+
* createPricesStep(product),
|
|
34
|
+
* attachProductToSalesChannelStep(product)
|
|
35
|
+
* )
|
|
36
|
+
*
|
|
37
|
+
* const id = product.id
|
|
38
|
+
* return getProductStep(product.id)
|
|
39
|
+
* }
|
|
40
|
+
* )
|
|
41
|
+
*/
|
|
42
|
+
export declare function parallelize<TResult extends WorkflowData[]>(...steps: TResult): TResult;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parallelize = void 0;
|
|
4
|
+
const helpers_1 = require("./helpers");
|
|
5
|
+
/**
|
|
6
|
+
* This function is used to run multiple steps in parallel. The result of each step will be returned as part of the result array.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam TResult - The type of the expected result.
|
|
9
|
+
*
|
|
10
|
+
* @returns The step results. The results are ordered in the array by the order they're passed in the function's parameter.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {
|
|
15
|
+
* createWorkflow,
|
|
16
|
+
* parallelize
|
|
17
|
+
* } from "@medusajs/workflows-sdk"
|
|
18
|
+
* import {
|
|
19
|
+
* createProductStep,
|
|
20
|
+
* getProductStep,
|
|
21
|
+
* createPricesStep,
|
|
22
|
+
* attachProductToSalesChannelStep
|
|
23
|
+
* } from "./steps"
|
|
24
|
+
*
|
|
25
|
+
* interface WorkflowInput {
|
|
26
|
+
* title: string
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* const myWorkflow = createWorkflow<
|
|
30
|
+
* WorkflowInput,
|
|
31
|
+
* Product
|
|
32
|
+
* >("my-workflow", (input) => {
|
|
33
|
+
* const product = createProductStep(input)
|
|
34
|
+
*
|
|
35
|
+
* const [prices, productSalesChannel] = parallelize(
|
|
36
|
+
* createPricesStep(product),
|
|
37
|
+
* attachProductToSalesChannelStep(product)
|
|
38
|
+
* )
|
|
39
|
+
*
|
|
40
|
+
* const id = product.id
|
|
41
|
+
* return getProductStep(product.id)
|
|
42
|
+
* }
|
|
43
|
+
* )
|
|
44
|
+
*/
|
|
45
|
+
function parallelize(...steps) {
|
|
46
|
+
if (!global[helpers_1.SymbolMedusaWorkflowComposerContext]) {
|
|
47
|
+
throw new Error("parallelize must be used inside a createWorkflow definition");
|
|
48
|
+
}
|
|
49
|
+
const parallelizeBinder = global[helpers_1.SymbolMedusaWorkflowComposerContext].parallelizeBinder;
|
|
50
|
+
const resultSteps = steps.map((step) => step);
|
|
51
|
+
return parallelizeBinder(function () {
|
|
52
|
+
const stepOntoMerge = steps.shift();
|
|
53
|
+
this.flow.mergeActions(stepOntoMerge.__step__, ...steps.map((step) => step.__step__));
|
|
54
|
+
return resultSteps;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
exports.parallelize = parallelize;
|
|
58
|
+
//# sourceMappingURL=parallelize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parallelize.js","sourceRoot":"","sources":["../../../src/utils/composer/parallelize.ts"],"names":[],"mappings":";;;AACA,uCAA+D;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,SAAgB,WAAW,CACzB,GAAG,KAAc;IAEjB,IAAI,CAAC,MAAM,CAAC,6CAAmC,CAAC,EAAE;QAChD,MAAM,IAAI,KAAK,CACb,6DAA6D,CAC9D,CAAA;KACF;IAED,MAAM,iBAAiB,GACrB,MAAM,CAAC,6CAAmC,CAC3C,CAAC,iBAAiB,CAAA;IAEnB,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,CAAA;IAE7C,OAAO,iBAAiB,CAAU;QAGhC,MAAM,aAAa,GAAG,KAAK,CAAC,KAAK,EAAG,CAAA;QACpC,IAAI,CAAC,IAAI,CAAC,YAAY,CACpB,aAAa,CAAC,QAAQ,EACtB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CACtC,CAAA;QAED,OAAO,WAAiC,CAAA;IAC1C,CAAC,CAAC,CAAA;AACJ,CAAC;AA1BD,kCA0BC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { StepExecutionContext, WorkflowData } from "./type";
|
|
2
|
+
type Func1<T extends object | WorkflowData, U> = (input: T extends WorkflowData<infer U> ? U : T extends object ? {
|
|
3
|
+
[K in keyof T]: T[K] extends WorkflowData<infer U> ? U : T[K];
|
|
4
|
+
} : {}, context: StepExecutionContext) => U | Promise<U>;
|
|
5
|
+
type Func<T, U> = (input: T, context: StepExecutionContext) => U | Promise<U>;
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* This function transforms the output of other utility functions.
|
|
9
|
+
*
|
|
10
|
+
* For example, if you're using the value(s) of some step(s) as an input to a later step. As you can't directly manipulate data in the workflow constructor function passed to {@link createWorkflow},
|
|
11
|
+
* the `transform` function provides access to the runtime value of the step(s) output so that you can manipulate them.
|
|
12
|
+
*
|
|
13
|
+
* Another example is if you're using the runtime value of some step(s) as the output of a workflow.
|
|
14
|
+
*
|
|
15
|
+
* If you're also retrieving the output of a hook and want to check if its value is set, you must use a workflow to get the runtime value of that hook.
|
|
16
|
+
*
|
|
17
|
+
* @returns There's no expected value to be returned by the `transform` function.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* import {
|
|
21
|
+
* createWorkflow,
|
|
22
|
+
* transform
|
|
23
|
+
* } from "@medusajs/workflows-sdk"
|
|
24
|
+
* import { step1, step2 } from "./steps"
|
|
25
|
+
*
|
|
26
|
+
* type WorkflowInput = {
|
|
27
|
+
* name: string
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* type WorkflowOutput = {
|
|
31
|
+
* message: string
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* const myWorkflow = createWorkflow<
|
|
35
|
+
* WorkflowInput,
|
|
36
|
+
* WorkflowOutput
|
|
37
|
+
* >
|
|
38
|
+
* ("hello-world", (input) => {
|
|
39
|
+
* const str1 = step1(input)
|
|
40
|
+
* const str2 = step2(input)
|
|
41
|
+
*
|
|
42
|
+
* return transform({
|
|
43
|
+
* str1,
|
|
44
|
+
* str2
|
|
45
|
+
* }, (input) => ({
|
|
46
|
+
* message: `${input.str1}${input.str2}`
|
|
47
|
+
* }))
|
|
48
|
+
* })
|
|
49
|
+
*/
|
|
50
|
+
export declare function transform<T extends object | WorkflowData, RFinal>(
|
|
51
|
+
/**
|
|
52
|
+
* The output(s) of other step functions.
|
|
53
|
+
*/
|
|
54
|
+
values: T,
|
|
55
|
+
/**
|
|
56
|
+
* The transform function used to perform action on the runtime values of the provided `values`.
|
|
57
|
+
*/
|
|
58
|
+
...func: [Func1<T, RFinal>]): WorkflowData<RFinal>;
|
|
59
|
+
/**
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
export declare function transform<T extends object | WorkflowData, RA, RFinal>(values: T, ...func: [Func1<T, RFinal>] | [Func1<T, RA>, Func<RA, RFinal>]): WorkflowData<RFinal>;
|
|
63
|
+
/**
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
export declare function transform<T extends object | WorkflowData, RA, RB, RFinal>(values: T, ...func: [Func1<T, RFinal>] | [Func1<T, RA>, Func<RA, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RFinal>]): WorkflowData<RFinal>;
|
|
67
|
+
/**
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
export declare function transform<T extends object | WorkflowData, RA, RB, RC, RFinal>(values: T, ...func: [Func1<T, RFinal>] | [Func1<T, RA>, Func<RA, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RFinal>]): WorkflowData<RFinal>;
|
|
71
|
+
/**
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
export declare function transform<T extends object | WorkflowData, RA, RB, RC, RD, RFinal>(values: T, ...func: [Func1<T, RFinal>] | [Func1<T, RA>, Func<RA, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RD>, Func<RD, RFinal>]): WorkflowData<RFinal>;
|
|
75
|
+
/**
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
78
|
+
export declare function transform<T extends object | WorkflowData, RA, RB, RC, RD, RE, RFinal>(values: T, ...func: [Func1<T, RFinal>] | [Func1<T, RA>, Func<RA, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RD>, Func<RD, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RD>, Func<RD, RE>, Func<RE, RFinal>]): WorkflowData<RFinal>;
|
|
79
|
+
/**
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
export declare function transform<T extends object | WorkflowData, RA, RB, RC, RD, RE, RF, RFinal>(values: T, ...func: [Func1<T, RFinal>] | [Func1<T, RA>, Func<RA, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RD>, Func<RD, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RD>, Func<RD, RE>, Func<RE, RFinal>] | [Func1<T, RA>, Func<RA, RB>, Func<RB, RC>, Func<RC, RD>, Func<RD, RE>, Func<RE, RF>, Func<RF, RFinal>]): WorkflowData<RFinal>;
|
|
83
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transform = void 0;
|
|
4
|
+
const helpers_1 = require("./helpers");
|
|
5
|
+
const proxy_1 = require("./helpers/proxy");
|
|
6
|
+
function transform(values, ...functions) {
|
|
7
|
+
const ret = {
|
|
8
|
+
__type: helpers_1.SymbolWorkflowStepTransformer,
|
|
9
|
+
__resolver: undefined,
|
|
10
|
+
};
|
|
11
|
+
const returnFn = async function (transactionContext) {
|
|
12
|
+
const allValues = await (0, helpers_1.resolveValue)(values, transactionContext);
|
|
13
|
+
const stepValue = allValues
|
|
14
|
+
? JSON.parse(JSON.stringify(allValues))
|
|
15
|
+
: allValues;
|
|
16
|
+
let finalResult;
|
|
17
|
+
for (let i = 0; i < functions.length; i++) {
|
|
18
|
+
const fn = functions[i];
|
|
19
|
+
const arg = i === 0 ? stepValue : finalResult;
|
|
20
|
+
finalResult = await fn.apply(fn, [arg, transactionContext]);
|
|
21
|
+
}
|
|
22
|
+
return finalResult;
|
|
23
|
+
};
|
|
24
|
+
const proxyfiedRet = (0, proxy_1.proxify)(ret);
|
|
25
|
+
proxyfiedRet.__resolver = returnFn;
|
|
26
|
+
return proxyfiedRet;
|
|
27
|
+
}
|
|
28
|
+
exports.transform = transform;
|
|
29
|
+
//# sourceMappingURL=transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.js","sourceRoot":"","sources":["../../../src/utils/composer/transform.ts"],"names":[],"mappings":";;;AAAA,uCAAuE;AAEvE,2CAAyC;AA8JzC,SAAgB,SAAS,CACvB,MAAmB,EACnB,GAAG,SAAqB;IAExB,MAAM,GAAG,GAAG;QACV,MAAM,EAAE,uCAA6B;QACrC,UAAU,EAAE,SAAS;KACtB,CAAA;IAED,MAAM,QAAQ,GAAG,KAAK,WAAW,kBAAkB;QACjD,MAAM,SAAS,GAAG,MAAM,IAAA,sBAAY,EAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;QAChE,MAAM,SAAS,GAAG,SAAS;YACzB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;YACvC,CAAC,CAAC,SAAS,CAAA;QAEb,IAAI,WAAW,CAAA;QACf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACzC,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,CAAA;YACvB,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAA;YAE7C,WAAW,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,kBAAkB,CAAC,CAAC,CAAA;SAC5D;QAED,OAAO,WAAW,CAAA;IACpB,CAAC,CAAA;IAED,MAAM,YAAY,GAAG,IAAA,eAAO,EAC1B,GAA8B,CAC/B,CAAA;IACD,YAAY,CAAC,UAAU,GAAG,QAAe,CAAA;IAEzC,OAAO,YAAY,CAAA;AACrB,CAAC;AAhCD,8BAgCC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { OrchestratorBuilder, TransactionContext as OriginalWorkflowTransactionContext, TransactionPayload, WorkflowHandler } from "@medusajs/orchestration";
|
|
2
|
+
import { Context, MedusaContainer } from "@medusajs/types";
|
|
3
|
+
export type StepFunctionResult<TOutput extends unknown | unknown[] = unknown> = (this: CreateWorkflowComposerContext) => TOutput extends [] ? [
|
|
4
|
+
...WorkflowData<{
|
|
5
|
+
[K in keyof TOutput]: TOutput[number][K];
|
|
6
|
+
}>[]
|
|
7
|
+
] : WorkflowData<{
|
|
8
|
+
[K in keyof TOutput]: TOutput[K];
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* A step function to be used in a workflow.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam TInput - The type of the input of the step.
|
|
14
|
+
* @typeParam TOutput - The type of the output of the step.
|
|
15
|
+
*/
|
|
16
|
+
export type StepFunction<TInput extends object = object, TOutput = unknown> = {
|
|
17
|
+
(input: {
|
|
18
|
+
[K in keyof TInput]: WorkflowData<TInput[K]>;
|
|
19
|
+
}): WorkflowData<{
|
|
20
|
+
[K in keyof TOutput]: TOutput[K];
|
|
21
|
+
}>;
|
|
22
|
+
} & WorkflowDataProperties<{
|
|
23
|
+
[K in keyof TOutput]: TOutput[K];
|
|
24
|
+
}>;
|
|
25
|
+
export type WorkflowDataProperties<T = unknown> = {
|
|
26
|
+
__type: Symbol;
|
|
27
|
+
__step__: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* This type is used to encapsulate the input or output type of all utils.
|
|
31
|
+
*
|
|
32
|
+
* @typeParam T - The type of a step's input or result.
|
|
33
|
+
*/
|
|
34
|
+
export type WorkflowData<T = unknown> = (T extends object ? {
|
|
35
|
+
[Key in keyof T]: WorkflowData<T[Key]>;
|
|
36
|
+
} : WorkflowDataProperties<T>) & WorkflowDataProperties<T>;
|
|
37
|
+
export type CreateWorkflowComposerContext = {
|
|
38
|
+
hooks_: string[];
|
|
39
|
+
hooksCallback_: Record<string, Function[]>;
|
|
40
|
+
workflowId: string;
|
|
41
|
+
flow: OrchestratorBuilder;
|
|
42
|
+
handlers: WorkflowHandler;
|
|
43
|
+
stepBinder: <TOutput = unknown>(fn: StepFunctionResult) => WorkflowData<TOutput>;
|
|
44
|
+
hookBinder: <TOutput = unknown>(name: string, fn: Function) => WorkflowData<TOutput>;
|
|
45
|
+
parallelizeBinder: <TOutput extends WorkflowData[] = WorkflowData[]>(fn: (this: CreateWorkflowComposerContext) => TOutput) => TOutput;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* The step's context.
|
|
49
|
+
*/
|
|
50
|
+
export interface StepExecutionContext {
|
|
51
|
+
/**
|
|
52
|
+
* The container used to access resources, such as services, in the step.
|
|
53
|
+
*/
|
|
54
|
+
container: MedusaContainer;
|
|
55
|
+
/**
|
|
56
|
+
* Metadata passed in the input.
|
|
57
|
+
*/
|
|
58
|
+
metadata: TransactionPayload["metadata"];
|
|
59
|
+
/**
|
|
60
|
+
* {@inheritDoc Context}
|
|
61
|
+
*/
|
|
62
|
+
context: Context;
|
|
63
|
+
}
|
|
64
|
+
export type WorkflowTransactionContext = StepExecutionContext & OriginalWorkflowTransactionContext & {
|
|
65
|
+
invoke: {
|
|
66
|
+
[key: string]: {
|
|
67
|
+
output: any;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../../src/utils/composer/type.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@medusajs/workflows-sdk",
|
|
3
|
+
"version": "0.2.0-next-20231124140416",
|
|
4
|
+
"description": "Set of workflows tooling for Medusa",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/medusajs/medusa",
|
|
10
|
+
"directory": "packages/workflows"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"author": "Medusa",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@medusajs/types": "1.11.8-next-20231124140416",
|
|
22
|
+
"cross-env": "^5.2.1",
|
|
23
|
+
"jest": "^29.6.3",
|
|
24
|
+
"rimraf": "^5.0.1",
|
|
25
|
+
"ts-jest": "^29.1.1",
|
|
26
|
+
"typescript": "^5.1.6"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@medusajs/modules-sdk": "1.12.4-next-20231124140416",
|
|
30
|
+
"@medusajs/orchestration": "0.5.0-next-20231124140416",
|
|
31
|
+
"@medusajs/utils": "1.11.1-next-20231124140416",
|
|
32
|
+
"awilix": "^8.0.1",
|
|
33
|
+
"ulid": "^2.3.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
|
|
37
|
+
"build": "rimraf dist && tsc --build",
|
|
38
|
+
"watch": "tsc --build --watch",
|
|
39
|
+
"test": "jest --runInBand --bail --forceExit"
|
|
40
|
+
}
|
|
41
|
+
}
|