|
1
|
--- |
|
2
|
sidebar_label: "medusa-context-on-context-param" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `medusa-context-on-context-param - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires that service method parameters typed `Context` are decorated with `@MedusaContext()`. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets service files in your project's `src/modules` directory. It reports a method parameter typed `Context` on a service class when the parameter isn't decorated with `@MedusaContext()`. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```ts title="src/modules/blog/service.ts" |
|
24
|
import { MedusaService } from "@medusajs/framework/utils" |
|
25
|
import { Context } from "@medusajs/framework/types" |
|
26
|
|
|
27
|
class BlogModuleService extends MedusaService({}) { |
|
28
|
async list(sharedContext: Context = {}) {} |
|
29
|
} |
|
30
|
``` |
|
31
|
|
|
32
|
Instead, decorate the parameter with `@MedusaContext()`: |
|
33
|
|
|
34
|
```ts title="src/modules/blog/service.ts" |
|
35
|
import { |
|
36
|
MedusaService, |
|
37
|
MedusaContext, |
|
38
|
} from "@medusajs/framework/utils" |
|
39
|
import { Context } from "@medusajs/framework/types" |
|
40
|
|
|
41
|
class BlogModuleService extends MedusaService({}) { |
|
42
|
async list( |
|
43
|
@MedusaContext() sharedContext: Context = {} |
|
44
|
) {} |
|
45
|
} |
|
46
|
``` |
|
47
|
|
|
48
|
## Why it's Important |
|
49
|
|
|
50
|
The `@MedusaContext()` decorator injects the shared context, which carries the transaction and entity manager. Without it, the context isn't propagated and operations may run outside the intended transaction. |
|
51
|
|
|
52
|
Learn more in the [Database Operations in Service documentation](!docs!/learn/fundamentals/modules/db-operations). |
|
53
|
|
|
54
|
## Fixable |
|
55
|
|
|
56
|
This rule is auto-fixable. Run the linter with the `--fix` option to apply the fix automatically. |
|
57
|
|
|
58
|
## Turn it Off |
|
59
|
|
|
60
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
61
|
|
|
62
|
```ts title="eslint.config.ts" |
|
63
|
import { defineConfig } from "eslint/config" |
|
64
|
import medusa from "@medusajs/eslint-plugin" |
|
65
|
|
|
66
|
export default defineConfig([ |
|
67
|
...medusa.configs.recommended, |
|
68
|
{ |
|
69
|
rules: { |
|
70
|
"@medusajs/medusa-context-on-context-param": "off", |
|
71
|
}, |
|
72
|
}, |
|
73
|
]) |
|
74
|
``` |
|
75
|
|
|
76
|
Or disable it for a single line using an inline comment: |
|
77
|
|
|
78
|
```ts |
|
79
|
// eslint-disable-next-line @medusajs/medusa-context-on-context-param |
|
80
|
async list(sharedContext: Context = {}) {} |
|
81
|
``` |