|
1
|
import { createRule } from "../../create-rule" |
|
2
|
import { ALLOW_FIELDS_MIN_MEDUSA_VERSION } from "../../constants" |
|
3
|
import { |
|
4
|
collectMiddlewareRoutes, |
|
5
|
findAllowedMutation, |
|
6
|
getDefineMiddlewaresArg, |
|
7
|
} from "../../util/middlewares" |
|
8
|
|
|
9
|
type MessageIds = "preferAllowFields" |
|
10
|
|
|
11
|
export const rule = createRule<[], MessageIds>({ |
|
12
|
name: "prefer-allow-fields-middleware", |
|
13
|
meta: { |
|
14
|
type: "suggestion", |
|
15
|
docs: { |
|
16
|
description: |
|
17
|
"Middlewares that add allowed fields should use the `allowFields` middleware from `@medusajs/framework/http` instead of writing to `req.allowed`.", |
|
18
|
}, |
|
19
|
messages: { |
|
20
|
preferAllowFields: `Use the \`allowFields\` middleware from \`@medusajs/framework/http\` instead of writing to \`req.allowed\`, for example \`middlewares: [allowFields("brand")]\`. \`allowFields\` requires Medusa v${ALLOW_FIELDS_MIN_MEDUSA_VERSION} or later, so keep this middleware if the project runs an earlier version.`, |
|
21
|
}, |
|
22
|
schema: [], |
|
23
|
}, |
|
24
|
defaultOptions: [], |
|
25
|
create(context) { |
|
26
|
return { |
|
27
|
CallExpression(node) { |
|
28
|
const arg = getDefineMiddlewaresArg(node) |
|
29
|
if (!arg) { |
|
30
|
return |
|
31
|
} |
|
32
|
|
|
33
|
for (const route of collectMiddlewareRoutes(arg)) { |
|
34
|
for (const middleware of route.middlewares) { |
|
35
|
const mutation = findAllowedMutation(middleware) |
|
36
|
if (mutation) { |
|
37
|
context.report({ node: mutation, messageId: "preferAllowFields" }) |
|
38
|
} |
|
39
|
} |
|
40
|
} |
|
41
|
}, |
|
42
|
} |
|
43
|
}, |
|
44
|
}) |
|
45
|
|
|
46
|
export default rule |