|
1
|
--- |
|
2
|
sidebar_label: "prefer-allow-fields-middleware" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `prefer-allow-fields-middleware - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule suggests using the `allowFields` middleware instead of writing to `req.allowed` directly. |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This rule is available since [Medusa v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.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 the `src/api/middlewares.ts` file. It reports inline middleware functions inside `defineMiddlewares` that mutate `req.allowed`, whether by assignment or by calling `.push()`. |
|
26
|
|
|
27
|
The following code is reported by the rule: |
|
28
|
|
|
29
|
```ts title="src/api/middlewares.ts" |
|
30
|
export default defineMiddlewares({ |
|
31
|
routes: [ |
|
32
|
{ |
|
33
|
matcher: "/store/customers/me", |
|
34
|
middlewares: [ |
|
35
|
(req, res, next) => { |
|
36
|
req.allowed.push("b2b_company") |
|
37
|
next() |
|
38
|
}, |
|
39
|
], |
|
40
|
}, |
|
41
|
], |
|
42
|
}) |
|
43
|
``` |
|
44
|
|
|
45
|
Instead, use the `allowFields` middleware from `@medusajs/framework/http`: |
|
46
|
|
|
47
|
```ts title="src/api/middlewares.ts" |
|
48
|
import { allowFields, defineMiddlewares } from "@medusajs/framework/http" |
|
49
|
|
|
50
|
export default defineMiddlewares({ |
|
51
|
routes: [ |
|
52
|
{ |
|
53
|
matcher: "/store/customers/me", |
|
54
|
middlewares: [allowFields("b2b_company")], |
|
55
|
}, |
|
56
|
], |
|
57
|
}) |
|
58
|
``` |
|
59
|
|
|
60
|
## Why it's Important |
|
61
|
|
|
62
|
The `allowFields` middleware is the idiomatic way to expand the allowed fields on a route. It is more concise, self-documenting, and composable: several plugins or middlewares can each call `allowFields` on the same route without overwriting one another. |
|
63
|
|
|
64
|
<Note> |
|
65
|
|
|
66
|
`allowFields` is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). If the project runs an earlier version, keep the `req.allowed.push(...)` pattern and disable this rule. |
|
67
|
|
|
68
|
</Note> |
|
69
|
|
|
70
|
Learn more in the [Allowed Fields in API Routes documentation](!docs!/learn/fundamentals/api-routes/allowed-fields). |
|
71
|
|
|
72
|
## Fixable |
|
73
|
|
|
74
|
This rule isn't auto-fixable. |
|
75
|
|
|
76
|
## Turn it Off |
|
77
|
|
|
78
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
79
|
|
|
80
|
```ts title="eslint.config.ts" |
|
81
|
import { defineConfig } from "eslint/config" |
|
82
|
import medusa from "@medusajs/eslint-plugin" |
|
83
|
|
|
84
|
export default defineConfig([ |
|
85
|
...medusa.configs.recommended, |
|
86
|
{ |
|
87
|
rules: { |
|
88
|
"@medusajs/prefer-allow-fields-middleware": "off", |
|
89
|
}, |
|
90
|
}, |
|
91
|
]) |
|
92
|
``` |
|
93
|
|
|
94
|
Or disable it for a single line using an inline comment: |
|
95
|
|
|
96
|
```ts |
|
97
|
// eslint-disable-next-line @medusajs/prefer-allow-fields-middleware |
|
98
|
req.allowed.push("b2b_company") |
|
99
|
``` |