|
1
|
--- |
|
2
|
sidebar_label: "prefer-workflow-event-over-module-event" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `prefer-workflow-event-over-module-event - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule prefers workflow event enums (`*WorkflowEvents`) over internal module-level event enums in a subscriber's `config.event`. |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This rule is available since [Medusa v2.19.0](https://github.com/medusajs/medusa/releases/tag/v2.19.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
## Severity |
|
20
|
|
|
21
|
`warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
22
|
|
|
23
|
## What it Targets |
|
24
|
|
|
25
|
This rule targets subscriber files in your project. It reports usages of internal module events (such as `ProductEvents.PRODUCT_CATEGORY_DELETED`) in the `event` property of the exported `config` object, and suggests the equivalent workflow event enum instead. |
|
26
|
|
|
27
|
The following code is reported by the rule: |
|
28
|
|
|
29
|
```ts title="src/subscribers/product-category-deleted.ts" |
|
30
|
import { ProductEvents } from "@medusajs/framework/utils" |
|
31
|
|
|
32
|
export const config = { |
|
33
|
event: ProductEvents.PRODUCT_CATEGORY_DELETED, |
|
34
|
} |
|
35
|
``` |
|
36
|
|
|
37
|
Instead, use the workflow event enum: |
|
38
|
|
|
39
|
```ts title="src/subscribers/product-category-deleted.ts" |
|
40
|
import { ProductCategoryWorkflowEvents } from "@medusajs/framework/utils" |
|
41
|
|
|
42
|
export const config = { |
|
43
|
event: ProductCategoryWorkflowEvents.DELETED, |
|
44
|
} |
|
45
|
``` |
|
46
|
|
|
47
|
The rule also flags internal event string literals: |
|
48
|
|
|
49
|
```ts title="src/subscribers/product-category-deleted.ts" |
|
50
|
// non-compliant |
|
51
|
export const config = { |
|
52
|
event: "product.product-category.deleted", |
|
53
|
} |
|
54
|
``` |
|
55
|
|
|
56
|
For events with no workflow-event equivalent, the rule warns that the event is an internal implementation detail and may change in future releases. |
|
57
|
|
|
58
|
## Why it's Important |
|
59
|
|
|
60
|
Internal module events (enums ending in `Events`, with values in the form `{module}.{data-model}.{action}`) are implementation details of Medusa's service layer. They can change between releases without a deprecation notice. |
|
61
|
|
|
62
|
Workflow events (`*WorkflowEvents` enums) are the stable, public API for subscribing to commerce actions. They're also only triggered if the action is successful. Using them ensures your subscribers react to the correct commerce events. |
|
63
|
|
|
64
|
## Fixable |
|
65
|
|
|
66
|
This rule is auto-fixable when a workflow-event equivalent exists. Run the linter with the `--fix` option to apply the fix automatically. The fix rewrites the event reference and adds the required import. |
|
67
|
|
|
68
|
When a workflow-event equivalent does not exist, the rule warns but provides no automatic fix. Review whether subscribing to an internal event is intentional. |
|
69
|
|
|
70
|
## Turn it Off |
|
71
|
|
|
72
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
73
|
|
|
74
|
```ts title="eslint.config.ts" |
|
75
|
import { defineConfig } from "eslint/config" |
|
76
|
import medusa from "@medusajs/eslint-plugin" |
|
77
|
|
|
78
|
export default defineConfig([ |
|
79
|
...medusa.configs.recommended, |
|
80
|
{ |
|
81
|
rules: { |
|
82
|
"@medusajs/prefer-workflow-event-over-module-event": "off", |
|
83
|
}, |
|
84
|
}, |
|
85
|
]) |
|
86
|
``` |
|
87
|
|
|
88
|
Or disable it for a single line using an inline comment: |
|
89
|
|
|
90
|
```ts |
|
91
|
// eslint-disable-next-line @medusajs/prefer-workflow-event-over-module-event |
|
92
|
export const config = { event: ProductEvents.PRODUCT_CATEGORY_DELETED } |
|
93
|
``` |