Method calls and decorators, which is how JavaScript, TypeScript and Python register.
(source: string)
| 122 | |
| 123 | /** Method calls and decorators, which is how JavaScript, TypeScript and Python register. */ |
| 124 | function readScriptRoutes(source: string): SourceRoute[] { |
| 125 | const routes: SourceRoute[] = []; |
| 126 | for (const match of source.matchAll(VERB_CALL_PATTERN)) { |
| 127 | routes.push({ |
| 128 | method: match[1].toUpperCase(), |
| 129 | path: match[3], |
| 130 | line: lineAt(source, match.index ?? 0) |
| 131 | }); |
| 132 | } |
| 133 | for (const match of source.matchAll(DECORATOR_ROUTE_PATTERN)) { |
| 134 | const line = lineAt(source, match.index ?? 0); |
| 135 | const methods = DECORATOR_METHODS_PATTERN.exec(match[3] ?? ''); |
| 136 | if (!methods) { |
| 137 | // A decorator with no `methods=` accepts GET in every framework that |
| 138 | // spells it this way, but this reader is not the place to encode |
| 139 | // that: the honest answer is that the registration names no verb. |
| 140 | routes.push({ path: match[2], line }); |
| 141 | continue; |
| 142 | } |
| 143 | for (const verb of methods[1].split(',')) { |
| 144 | const cleaned = verb.replace(/['"\s]/g, '').toUpperCase(); |
| 145 | if (cleaned.length > 0) { |
| 146 | routes.push({ method: cleaned, path: match[2], line }); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | return routes; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Annotations, which is how Spring and its relatives register. |
no test coverage detected