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
2024-12-12 18:10:29 +02:00
committed by GitHub
parent ec5c7cf63f
commit 78d8698a24
2 changed files with 129 additions and 3 deletions
@@ -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
+1 -1
View File
@@ -36,7 +36,7 @@ export const generatedEditDates = {
"app/learn/fundamentals/modules/module-link-directions/page.mdx": "2024-07-24T09:16:01+02:00",
"app/learn/fundamentals/admin/page.mdx": "2024-10-23T07:08:55.898Z",
"app/learn/fundamentals/workflows/long-running-workflow/page.mdx": "2024-12-04T07:37:59.822Z",
"app/learn/fundamentals/workflows/constructor-constraints/page.mdx": "2024-12-11T08:36:08.282Z",
"app/learn/fundamentals/workflows/constructor-constraints/page.mdx": "2024-12-12T15:09:41.179Z",
"app/learn/fundamentals/data-models/write-migration/page.mdx": "2024-11-11T15:27:59.794Z",
"app/learn/fundamentals/data-models/manage-relationships/page.mdx": "2024-12-12T14:23:26.254Z",
"app/learn/fundamentals/modules/remote-query/page.mdx": "2024-07-21T21:20:24+02:00",