docs: document alternatives for conditional operators (#10588)

* docs: document alternatives for conditional operators

* more operators

* fix lint errors
This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-12-12 18:10:29 +02:00
1 parent ec5c7cf63f
commit 78d8698a24
2 files changed
+129 -3

No files matched your search

@@ -165,13 +165,13 @@ You can't use conditional operators in a workflow, such as `??` or `||`.
<Note>
Learn more about why you can't use if-conditions [in this chapter](../conditions/page.mdx#why-if-conditions-arent-allowed-in-workflows)
Learn more about why you can't use conditional operators [in this chapter](../conditions/page.mdx#why-if-conditions-arent-allowed-in-workflows)
</Note>
Instead, use `transform` to store the desired value in a variable.
For example:
### Logical Or (||) Alternative
```ts
// Don't
@@ -197,6 +197,132 @@ const myWorkflow = createWorkflow(
})
```
### Nullish Coalescing (??) Alternative
```ts
// Don't
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const message = input.message ?? "Hello"
})
// Do
// other imports...
import { transform } from "@medusajs/framework/workflows-sdk"
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const message = transform(
{
input,
},
(data) => data.input.message ?? "hello"
)
})
```
### Double Not (!!) Alternative
```ts
// Don't
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
step1({
isActive: !!input.is_active,
})
})
// Do
// other imports...
import { transform } from "@medusajs/framework/workflows-sdk"
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const isActive = transform(
{
input,
},
(data) => !!data.input.is_active
)
step1({
isActive,
})
})
```
### Ternary Alternative
```ts
// Don't
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
step1({
message: input.is_active ? "active" : "inactive",
})
})
// Do
// other imports...
import { transform } from "@medusajs/framework/workflows-sdk"
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const message = transform(
{
input,
},
(data) => {
return data.input.is_active ? "active" : "inactive"
}
)
step1({
message,
})
})
```
### Optional Chaining (?.) Alternative
```ts
// Don't
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
step1({
name: input.customer?.name,
})
})
// Do
// other imports...
import { transform } from "@medusajs/framework/workflows-sdk"
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const name = transform(
{
input,
},
(data) => data.input.customer?.name
)
step1({
name,
})
})
```
---
## Step Constraints