docs: document InferTypeOf (#9321)

- Add documentation on how to use InferTypeOf
- Use InferTypeOf in recipes and examples
This commit is contained in:
Shahed Nasser
2024-09-26 13:42:29 +00:00
committed by GitHub
parent c5bf22f3f4
commit b3a204e974
12 changed files with 170 additions and 112 deletions
@@ -66,20 +66,22 @@ The step returns the retrieved brands.
Next, create the step that creates new brands in Medusa in the file `src/workflows/sync-brands-from-system/steps/create-brands.ts`:
export const createBrandsHighlights = [
["19", "createBrands", "Create the brands in Medusa"],
["28", "deleteBrands", "Delete the brands from Medusa"]
["21", "createBrands", "Create the brands in Medusa"],
["30", "deleteBrands", "Delete the brands from Medusa"]
]
```ts title="src/workflows/sync-brands-from-system/steps/create-brands.ts" highlights={createBrandsHighlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
```ts title="src/workflows/sync-brands-from-system/steps/create-brands.ts" highlights={createBrandsHighlights} collapsibleLines="1-9" expandButtonLabel="Show Imports"
import {
createStep,
StepResponse,
} from "@medusajs/workflows-sdk"
import { InferTypeOf } from "@medusajs/types"
import BrandModuleService from "../../../modules/brand/service"
import { BRAND_MODULE } from "../../../modules/brand"
import { Brand } from "../../../modules/brand/models/brand"
type CreateBrandsInput = {
brands: Record<string, string>[]
brands: InferTypeOf<typeof Brand>[]
}
export const createBrandsStep = createStep(
@@ -105,6 +107,12 @@ export const createBrandsStep = createStep(
This step receives the brands to create as input.
<Note title="Tip">
Since a data model is a variable, use the `InferTypeOf` utility imported from `@medusajs/types` to infer its type.
</Note>
In the step, you resolve the Brand Module's main service and uses its `createBrands` method to create the brands.
You return the created brands and pass their IDs to the compensation function, which deletes the brands if an error occurs.
@@ -114,21 +122,23 @@ You return the created brands and pass their IDs to the compensation function, w
To create the step that updates existing brands in Medusa, create the file `src/workflows/sync-brands-from-system/steps/update-brands.ts` with the following content:
export const updateBrandsHighlights = [
["19", "prevUpdatedBrands", "Retrieve the data of the brands before the update."],
["23", "updateBrands", "Update the brands in Medusa."],
["32", "updateBrands", "Revert the update by reverting the brands' to before the update."]
["21", "prevUpdatedBrands", "Retrieve the data of the brands before the update."],
["25", "updateBrands", "Update the brands in Medusa."],
["34", "updateBrands", "Revert the update by reverting the brands' to before the update."]
]
```ts title="src/workflows/sync-brands-from-system/steps/update-brands.ts" highlights={updateBrandsHighlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
```ts title="src/workflows/sync-brands-from-system/steps/update-brands.ts" highlights={updateBrandsHighlights} collapsibleLines="1-9" expandButtonLabel="Show Imports"
import {
createStep,
StepResponse,
} from "@medusajs/workflows-sdk"
import { InferTypeOf } from "@medusajs/types"
import BrandModuleService from "../../../modules/brand/service"
import { BRAND_MODULE } from "../../../modules/brand"
import { Brand } from "../../../modules/brand/models/brand"
type UpdateBrandsInput = {
brands: Record<string, string>[]
brands: InferTypeOf<typeof Brand>[]
}
export const updateBrandsStep = createStep(
@@ -172,9 +182,11 @@ import {
WorkflowResponse,
transform,
} from "@medusajs/workflows-sdk"
import { InferTypeOf } from "@medusajs/types"
import { retrieveBrandsFromSystemStep } from "./steps/retrieve-brands-from-system"
import { createBrandsStep } from "./steps/create-brands"
import { updateBrandsStep } from "./steps/update-brands"
import { Brand } from "../../modules/brand/models/brand"
export const syncBrandsFromSystemWorkflow = createWorkflow(
"sync-brands-from-system",
@@ -204,8 +216,8 @@ const { toCreate, toUpdate } = transform(
brands,
},
(data) => {
const toCreate: Record<string, string>[] = []
const toUpdate: Record<string, string>[] = []
const toCreate: InferTypeOf<typeof Brand>[] = []
const toUpdate: InferTypeOf<typeof Brand>[] = []
data.brands.forEach((brand) => {
if (brand.external_id) {