|
1
|
--- |
|
2
|
sidebar_label: "middleware-must-call-next" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `middleware-must-call-next - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires middleware functions registered via `defineMiddlewares` to reference their `next` parameter. |
|
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 the `src/api/middlewares.ts` file. It reports a middleware function that declares a `next` parameter but never references it, which stops the middleware chain. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```ts title="src/api/middlewares.ts" |
|
24
|
export default defineMiddlewares({ |
|
25
|
routes: [ |
|
26
|
{ |
|
27
|
matcher: "/store/custom", |
|
28
|
middlewares: [(req, res, next) => { res.json({}) }], |
|
29
|
}, |
|
30
|
], |
|
31
|
}) |
|
32
|
``` |
|
33
|
|
|
34
|
Instead, call `next` so the chain continues: |
|
35
|
|
|
36
|
```ts title="src/api/middlewares.ts" |
|
37
|
export default defineMiddlewares({ |
|
38
|
routes: [ |
|
39
|
{ |
|
40
|
matcher: "/store/custom", |
|
41
|
middlewares: [(req, res, next) => { next() }], |
|
42
|
}, |
|
43
|
], |
|
44
|
}) |
|
45
|
``` |
|
46
|
|
|
47
|
## Why it's Important |
|
48
|
|
|
49
|
A middleware that doesn't call `next` (or end the response) leaves the request hanging, so the route handler is never reached. Referencing `next` keeps the middleware chain moving. |
|
50
|
|
|
51
|
Learn more in the [Middlewares documentation](!docs!/learn/fundamentals/api-routes/middlewares). |
|
52
|
|
|
53
|
## Fixable |
|
54
|
|
|
55
|
This rule isn't auto-fixable. |
|
56
|
|
|
57
|
## Turn it Off |
|
58
|
|
|
59
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
60
|
|
|
61
|
```ts title="eslint.config.ts" |
|
62
|
import { defineConfig } from "eslint/config" |
|
63
|
import medusa from "@medusajs/eslint-plugin" |
|
64
|
|
|
65
|
export default defineConfig([ |
|
66
|
...medusa.configs.recommended, |
|
67
|
{ |
|
68
|
rules: { |
|
69
|
"@medusajs/middleware-must-call-next": "off", |
|
70
|
}, |
|
71
|
}, |
|
72
|
]) |
|
73
|
``` |
|
74
|
|
|
75
|
Or disable it for a single line using an inline comment: |
|
76
|
|
|
77
|
```ts |
|
78
|
// eslint-disable-next-line @medusajs/middleware-must-call-next |
|
79
|
middlewares: [(req, res, next) => { res.json({}) }], |
|
80
|
``` |