| 78 | * @returns { name: string, arg: string | null } |
| 79 | */ |
| 80 | export function parseDecorator(ts: any, sf: any, decorator: any): { name: string; arg: string | null } { |
| 81 | const SK = ts.SyntaxKind; |
| 82 | const expr = decorator.expression; |
| 83 | if (!expr) return { name: "", arg: null }; |
| 84 | |
| 85 | // @Get() or @Get('path') — CallExpression |
| 86 | if (expr.kind === SK.CallExpression) { |
| 87 | const callee = expr.expression; |
| 88 | const name = callee.kind === SK.Identifier ? callee.getText(sf) : ""; |
| 89 | let arg: string | null = null; |
| 90 | if (expr.arguments?.length > 0) { |
| 91 | const first = expr.arguments[0]; |
| 92 | if (first.kind === SK.StringLiteral || first.kind === SK.NoSubstitutionTemplateLiteral) { |
| 93 | arg = first.text; |
| 94 | } |
| 95 | } |
| 96 | return { name, arg }; |
| 97 | } |
| 98 | |
| 99 | // @Controller (without parens) — Identifier |
| 100 | if (expr.kind === SK.Identifier) { |
| 101 | return { name: expr.getText(sf), arg: null }; |
| 102 | } |
| 103 | |
| 104 | return { name: "", arg: null }; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get text from a node safely. |