medusajs / roadmap

3 stars 2 forks source link

Subscribers #23

Closed olivermrbl closed 5 months ago

olivermrbl commented 5 months ago

Services in Medusa emit events whenever actions like creating and updating records occur. Subscribers in Medusa provide a way for developers to run asynchronous logic in response to these events. This could include syncing a product to an external service when it is updated in Medusa or applying a special discount when a set of specific products is added to a cart.

The new Subscribers API will allow you to create subscribers using a pattern similar to how you build API Routes.

Here's a basic example of the API:

// src/subscribers/order-placed.ts
export default async function ({ data, container }) {
    const { id } = data

    const slackService = container.resolve("slackService")
    const orderService = container.resolve("orderService")

    const order = await orderService.retrieve(id)

    await slackService.sendNotis({ data: order })
}

export const config: SubscriberConfig = {
    event: OrderService.Events.PLACED,
}
olivermrbl commented 5 months ago

Released in v1.18.0.

Read the announcement post and documentation.