|
1
|
--- |
|
2
|
sidebar_label: "no-if-in-workflow-constructor" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `no-if-in-workflow-constructor - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule disallows `if` and `else` statements directly inside a `createWorkflow` constructor or a `when().then()` callback. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets files in your project's `src/workflows` directory. It reports `if` statements placed directly in a workflow's constructor or in a `when().then()` callback. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```ts title="src/workflows/example.ts" |
|
24
|
import { |
|
25
|
createWorkflow, |
|
26
|
WorkflowResponse, |
|
27
|
} from "@medusajs/framework/workflows-sdk" |
|
28
|
|
|
29
|
createWorkflow("my-workflow", (input) => { |
|
30
|
if (input) { |
|
31
|
return new WorkflowResponse(1) |
|
32
|
} |
|
33
|
return new WorkflowResponse(2) |
|
34
|
}) |
|
35
|
``` |
|
36
|
|
|
37
|
Instead, use `when(...).then(...)` to conditionally execute steps: |
|
38
|
|
|
39
|
```ts title="src/workflows/example.ts" |
|
40
|
import { |
|
41
|
createWorkflow, |
|
42
|
when, |
|
43
|
WorkflowResponse, |
|
44
|
} from "@medusajs/framework/workflows-sdk" |
|
45
|
|
|
46
|
createWorkflow("my-workflow", (input) => { |
|
47
|
const result = when( |
|
48
|
{ input }, |
|
49
|
(data) => !!data.input |
|
50
|
).then(() => { |
|
51
|
return doSomethingStep() |
|
52
|
}) |
|
53
|
|
|
54
|
return new WorkflowResponse(result) |
|
55
|
}) |
|
56
|
``` |
|
57
|
|
|
58
|
## Why it's Important |
|
59
|
|
|
60
|
A workflow's constructor and `when().then()` callbacks run at definition time, not execution time. So, an `if` statement evaluates against placeholder values and doesn't behave as you might expect. Use `when(...).then(...)` to conditionally execute steps. |
|
61
|
|
|
62
|
Learn more in the [Conditions in Workflows documentation](!docs!/learn/fundamentals/workflows/conditions). |
|
63
|
|
|
64
|
## Fixable |
|
65
|
|
|
66
|
This rule isn't auto-fixable. |