1 import * as path from "path"
2 import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"
3 import { createRule } from "../../create-rule"
4 import { isUnderApiDir } from "../../util/api-route"
5
6 type MessageIds = "wrongFileName" | "noHttpExports"
7
8 const HTTP_METHODS = new Set([
9 "GET",
10 "POST",
11 "PUT",
12 "PATCH",
13 "DELETE",
14 "HEAD",
15 "OPTIONS",
16 ])
17
18 const ROUTE_FILE_BASENAMES = new Set(["route.ts", "route.js"])
19
20 function collectExportedHttpMethodNodes(
21 node: TSESTree.ExportNamedDeclaration
22 ): TSESTree.Node[] {
23 const hits: TSESTree.Node[] = []
24
25 if (node.declaration) {
26 const decl = node.declaration
27 if (decl.type === AST_NODE_TYPES.FunctionDeclaration && decl.id) {
28 if (HTTP_METHODS.has(decl.id.name)) {
29 hits.push(decl.id)
30 }
31 } else if (decl.type === AST_NODE_TYPES.VariableDeclaration) {
32 for (const d of decl.declarations) {
33 if (
34 d.id.type === AST_NODE_TYPES.Identifier &&
35 HTTP_METHODS.has(d.id.name)
36 ) {
37 hits.push(d.id)
38 }
39 }
40 }
41 }
42
43 for (const spec of node.specifiers) {
44 if (spec.type !== AST_NODE_TYPES.ExportSpecifier) {
45 continue
46 }
47 const exported = spec.exported
48 const exportedName =
49 exported.type === AST_NODE_TYPES.Identifier
50 ? exported.name
51 : typeof exported.value === "string"
52 ? exported.value
53 : null
54 if (exportedName && HTTP_METHODS.has(exportedName)) {
55 hits.push(exported)
56 }
57 }
58
59 return hits
60 }
61
62 export const rule = createRule<[], MessageIds>({
63 name: "route-file-naming",
64 meta: {
65 type: "problem",
66 docs: {
67 description:
68 "API route handler files must be named `route.ts` / `route.js` and live under `src/api/`.",
69 },
70 messages: {
71 wrongFileName:
72 "API route handlers must be exported from `route.ts` or `route.js`. Move this `{{name}}` handler into a `route.{ts,js}` file.",
73 noHttpExports:
74 "`route.{ts,js}` files must export at least one HTTP method handler (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS).",
75 },
76 schema: [],
77 },
78 defaultOptions: [],
79 create(context) {
80 const filename = context.filename
81 if (!filename || filename.startsWith("<")) {
82 return {}
83 }
84 if (!isUnderApiDir(filename)) {
85 return {}
86 }
87
88 const basename = path.basename(filename)
89 const isRouteFile = ROUTE_FILE_BASENAMES.has(basename)
90
91 const httpExports: TSESTree.Node[] = []
92
93 return {
94 ExportNamedDeclaration(node) {
95 for (const hit of collectExportedHttpMethodNodes(node)) {
96 httpExports.push(hit)
97 }
98 },
99
100 "Program:exit"(node) {
101 if (!isRouteFile) {
102 for (const hit of httpExports) {
103 const name =
104 hit.type === AST_NODE_TYPES.Identifier
105 ? hit.name
106 : hit.type === AST_NODE_TYPES.Literal &&
107 typeof hit.value === "string"
108 ? hit.value
109 : "handler"
110 context.report({
111 node: hit,
112 messageId: "wrongFileName",
113 data: { name },
114 })
115 }
116 return
117 }
118
119 if (httpExports.length === 0) {
120 context.report({ node, messageId: "noHttpExports" })
121 }
122 },
123 }
124 },
125 })
126
127 export default rule