|
1
|
import * as path from "path" |
|
2
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { toPosix } from "../../util/filename" |
|
5
|
import { isUnderApiDir } from "../../util/api-route" |
|
6
|
|
|
7
|
type MessageIds = |
|
8
|
| "singularFilename" |
|
9
|
| "wrongDirectory" |
|
10
|
| "missingDefineMiddlewares" |
|
11
|
|
|
12
|
const SINGULAR_BASENAMES = new Set(["middleware.ts", "middleware.js"]) |
|
13
|
const PLURAL_BASENAMES = new Set(["middlewares.ts", "middlewares.js"]) |
|
14
|
const DEFINE_MIDDLEWARES = "defineMiddlewares" |
|
15
|
|
|
16
|
function isCanonicalLocation(filename: string): boolean { |
|
17
|
const posix = toPosix(filename) |
|
18
|
return ( |
|
19
|
/(^|\/)src\/api\/middlewares\.(ts|js)$/.test(posix) || |
|
20
|
/(^|\/)api\/middlewares\.(ts|js)$/.test(posix) |
|
21
|
) |
|
22
|
} |
|
23
|
|
|
24
|
function isDefineMiddlewaresCall(node: TSESTree.Node): boolean { |
|
25
|
return ( |
|
26
|
node.type === AST_NODE_TYPES.CallExpression && |
|
27
|
node.callee.type === AST_NODE_TYPES.Identifier && |
|
28
|
node.callee.name === DEFINE_MIDDLEWARES |
|
29
|
) |
|
30
|
} |
|
31
|
|
|
32
|
export const rule = createRule<[], MessageIds>({ |
|
33
|
name: "middlewares-file-location-and-name", |
|
34
|
meta: { |
|
35
|
type: "problem", |
|
36
|
docs: { |
|
37
|
description: |
|
38
|
"Medusa middlewares must be defined in `src/api/middlewares.{ts,js}` with a default export of `defineMiddlewares(...)`. Other filenames or locations are ignored by the framework.", |
|
39
|
}, |
|
40
|
messages: { |
|
41
|
singularFilename: |
|
42
|
"Middleware file must be named `middlewares.{ts,js}` (plural). The framework only loads `src/api/middlewares.{ts,js}`; `middleware.{ts,js}` is ignored.", |
|
43
|
wrongDirectory: |
|
44
|
"Middleware file must live at `src/api/middlewares.{ts,js}`. The framework does not load `defineMiddlewares(...)` from other locations.", |
|
45
|
missingDefineMiddlewares: |
|
46
|
"`src/api/middlewares.{ts,js}` must have a default export of `defineMiddlewares(...)`.", |
|
47
|
}, |
|
48
|
schema: [], |
|
49
|
}, |
|
50
|
defaultOptions: [], |
|
51
|
create(context) { |
|
52
|
const filename = context.filename |
|
53
|
if (!filename || filename.startsWith("<")) { |
|
54
|
return {} |
|
55
|
} |
|
56
|
|
|
57
|
const basename = path.basename(filename) |
|
58
|
const isSingular = SINGULAR_BASENAMES.has(basename) |
|
59
|
const isPlural = PLURAL_BASENAMES.has(basename) |
|
60
|
|
|
61
|
if (!isSingular && !isPlural) { |
|
62
|
return {} |
|
63
|
} |
|
64
|
|
|
65
|
const canonical = isPlural && isCanonicalLocation(filename) |
|
66
|
|
|
67
|
let defaultExport: TSESTree.ExportDefaultDeclaration | null = null |
|
68
|
let definesMiddlewares = false |
|
69
|
|
|
70
|
return { |
|
71
|
ExportDefaultDeclaration(node) { |
|
72
|
defaultExport = node |
|
73
|
if (isDefineMiddlewaresCall(node.declaration as TSESTree.Node)) { |
|
74
|
definesMiddlewares = true |
|
75
|
} |
|
76
|
}, |
|
77
|
|
|
78
|
CallExpression(node) { |
|
79
|
if (isDefineMiddlewaresCall(node)) { |
|
80
|
definesMiddlewares = true |
|
81
|
} |
|
82
|
}, |
|
83
|
|
|
84
|
"Program:exit"(program) { |
|
85
|
// Singular `middleware.{ts,js}` under api/ — always wrong name. |
|
86
|
if (isSingular && isUnderApiDir(filename)) { |
|
87
|
if (definesMiddlewares) { |
|
88
|
context.report({ node: program, messageId: "singularFilename" }) |
|
89
|
} |
|
90
|
return |
|
91
|
} |
|
92
|
|
|
93
|
// Plural `middlewares.{ts,js}` in the wrong directory. |
|
94
|
if (isPlural && !canonical) { |
|
95
|
if (definesMiddlewares) { |
|
96
|
context.report({ node: program, messageId: "wrongDirectory" }) |
|
97
|
} |
|
98
|
return |
|
99
|
} |
|
100
|
|
|
101
|
// Canonical location — must default-export defineMiddlewares(...). |
|
102
|
if (canonical) { |
|
103
|
if ( |
|
104
|
!defaultExport || |
|
105
|
!isDefineMiddlewaresCall( |
|
106
|
(defaultExport as TSESTree.ExportDefaultDeclaration) |
|
107
|
.declaration as TSESTree.Node |
|
108
|
) |
|
109
|
) { |
|
110
|
context.report({ |
|
111
|
node: defaultExport ?? program, |
|
112
|
messageId: "missingDefineMiddlewares", |
|
113
|
}) |
|
114
|
} |
|
115
|
} |
|
116
|
}, |
|
117
|
} |
|
118
|
}, |
|
119
|
}) |
|
120
|
|
|
121
|
export default rule |