docs: document InferTypeOf (#9321)

- Add documentation on how to use InferTypeOf
- Use InferTypeOf in recipes and examples
This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-09-26 13:42:29 +00:00
1 parent c5bf22f3f4
commit b3a204e974
12 files changed
+170 -112

No files matched your search

@@ -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) {
@@ -80,13 +80,17 @@ If the service integrating the third-party system was a main service, it receive
Next, add the following methods to simulate sending requests to the third-party system:
export const methodsHighlights = [
["6", "sendRequest", "Since the third-party system isn't real, this method only logs a message."],
["15", "createBrand", "A method that creates a brand in the third-party system."],
["19", "deleteBrand", "A method that deletes a brand in the third-party system."],
["23", "retrieveBrands", "A method that retrieves a brand from a third-party system."]
["10", "sendRequest", "Since the third-party system isn't real, this method only logs a message."],
["19", "createBrand", "A method that creates a brand in the third-party system."],
["23", "deleteBrand", "A method that deletes a brand in the third-party system."],
["27", "retrieveBrands", "A method that retrieves a brand from a third-party system."]
]
```ts title="src/modules/brand/services/client.ts" highlights={methodsHighlights}
// other imports...
import { InferTypeOf } from "@medusajs/types"
import { Brand } from "../models/brand"
export class BrandClient {
// ...
@@ -101,7 +105,7 @@ export class BrandClient {
}`)
}
async createBrand(brand: Record<string, any>) {
async createBrand(brand: InferTypeOf<typeof Brand>) {
await this.sendRequest("/brands", "POST", brand)
}
@@ -121,7 +125,7 @@ The `sendRequest` method is a dummy method to simulate sending a request to a th
You also add three methods that use the `sendRequest` method:
- `createBrand` that creates a brand in the third-party system.
- `createBrand` that creates a brand in the third-party system. To reference a brand's type, you use the `InferTypeOf` utility imported from `@medusajs/types`. This transforms a data model, which is a variable, to its equivalent type.
- `deleteBrand` that deletes the brand in the third-party system.
- `retrieveBrands` to retrieve a brand from the third-party system.