Back to Medusa

DevServerUtils

www/apps/resources/references/utils/utils.DevServerUtils/page.mdx

2.14.26.1 KB
Original Source

import { TypeList } from "docs-ui"

DevServerUtils

Functions

Interfaces

Type Aliases

Variables

Functions

registerDevServerResource

**registerDevServerResource**(data): void``

Register a resource in the dev server for hot module reloading

This function uses a strategy pattern where each resource type has its own handler. The handler is responsible for:

  • Validating the registration data
  • Resolving the source path
  • Creating the registry entry
  • Generating the inverse registry key

Example

typescript
// Register a workflow
registerDevServerResource({
  type: "workflow",
  id: "create-product",
  sourcePath: "/src/workflows/create-product.ts"
})

// Register a step
registerDevServerResource({
  type: "step",
  id: "validate-product",
  workflowId: "create-product"
})

Parameters

<TypeList types={[{"name":"data","type":"ResourceRegistrationData","description":"Resource registration data","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"string","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"type","type":"\"workflow\"","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"sourcePath","type":"string","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"subscriberId","type":"string","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"events","type":"string[]","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"workflowId","type":"string","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="registerDevServerResource"/>

Throws

Error if validation fails or handler is not found

**registerDevServerResource**<TypeParameter T>(data): void``

Register a resource in the dev server for hot module reloading

This function uses a strategy pattern where each resource type has its own handler. The handler is responsible for:

  • Validating the registration data
  • Resolving the source path
  • Creating the registry entry
  • Generating the inverse registry key

Example

typescript
// Register a workflow
registerDevServerResource({
  type: "workflow",
  id: "create-product",
  sourcePath: "/src/workflows/create-product.ts"
})

// Register a step
registerDevServerResource({
  type: "step",
  id: "validate-product",
  workflowId: "create-product"
})

Type Parameters

<TypeList types={[{"name":"T","type":"BaseResourceData","description":"","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"type","type":"string","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"id","type":"string","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"sourcePath","type":"string","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="registerDevServerResource"/>

Parameters

<TypeList types={[{"name":"data","type":"T","description":"Resource registration data","optional":false,"defaultValue":"","expandable":false,"children":[]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="registerDevServerResource"/>

Throws

Error if validation fails or handler is not found

registerResourceTypeHandler

Register a resource type handler

Example

typescript
class RouteHandler implements ResourceTypeHandler<RouteData> {
  readonly type = "route"
  validate(data: RouteData): void { ... }
  resolveSourcePath(data: RouteData): string { ... }
  createEntry(data: RouteData): ResourceEntry { ... }
  getInverseKey(data: RouteData): string { ... }
}

registerResourceTypeHandler(new RouteHandler())

Parameters

<TypeList types={[{"name":"handler","type":"ResourceTypeHandler","description":"","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"type","type":"string","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="registerResourceTypeHandler"/>


Variables

globalDevServerRegistry

Const globalDevServerRegistry: Map<string, ResourceMap>

Maps source file paths to their registered resources Structure: sourcePath -> Map<resourceType, ResourceEntry[]>

inverseDevServerRegistry

Const inverseDevServerRegistry: Map<string, string[]>

Inverse registry for looking up source paths by resource Structure: "type:id" -> sourcePath[] Used to find which files contain a specific resource