|
| 1 | +# Namecheap TSOA Fork |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This is a Namecheap fork of [tsoa](https://github.com/lukeautry/tsoa) — a framework that generates OpenAPI specs and Express/Koa/Hapi route boilerplate from TypeScript controller classes and decorators. Packages are published under `@namecheap/` scope instead of the upstream `tsoa` scope. |
| 6 | + |
| 7 | +## Monorepo Structure |
| 8 | + |
| 9 | +``` |
| 10 | +packages/ |
| 11 | + runtime/ @namecheap/tsoa-runtime — decorators, interfaces, runtime validation helpers |
| 12 | + cli/ @namecheap/tsoa-cli — metadata extraction, spec + route generation |
| 13 | + tsoa/ @namecheap/tsoa — re-exports both; the package end-users install |
| 14 | +tests/ |
| 15 | + fixtures/ — controllers, services, models used by tests |
| 16 | + unit/ — unit tests (spec generation, metadata, templating) |
| 17 | + integration/ — full server integration tests (express, koa, hapi, inversify) |
| 18 | +``` |
| 19 | + |
| 20 | +`tests` is a Yarn workspace sibling. Its `tsconfig.json` maps `@namecheap/tsoa-cli/*` and `@namecheap/tsoa-runtime/*` to the package source directories so tests run against unbuilt TypeScript. |
| 21 | + |
| 22 | +## Architecture |
| 23 | + |
| 24 | +### Two-Phase Code Generation |
| 25 | + |
| 26 | +**Phase 1 — Metadata extraction** (`packages/cli/src/metadataGeneration/`): |
| 27 | +`MetadataGenerator` creates a TypeScript `Program`, walks source files for classes decorated with `@Route`, and delegates to: |
| 28 | +- `ControllerGenerator` → per-class metadata |
| 29 | +- `MethodGenerator` → per-method metadata (params, responses, security, extensions) |
| 30 | +- `ParameterGenerator` → per-parameter metadata |
| 31 | +- `TypeResolver` → resolves TypeScript types to `Tsoa.Type` (handles generics, imports, aliases, enums, intersections) |
| 32 | + |
| 33 | +The output is `Tsoa.Metadata` — a plain serializable object describing all controllers and a reference type map. |
| 34 | + |
| 35 | +**Phase 2a — Spec generation** (`packages/cli/src/swagger/`): |
| 36 | +`SpecGenerator2` / `SpecGenerator3` consume `Tsoa.Metadata` and emit a Swagger 2.0 or OpenAPI 3.0 JSON/YAML document. |
| 37 | + |
| 38 | +**Phase 2b — Route generation** (`packages/cli/src/routeGeneration/`): |
| 39 | +`RouteGenerator` consumes `Tsoa.Metadata` and renders one of three Handlebars templates (`express.hbs`, `koa.hbs`, `hapi.hbs`) to produce a routes file with runtime validation. |
| 40 | + |
| 41 | +Both phases are triggered by `generateSpec` / `generateRoutes` / `generateSpecAndRoutes` in `packages/cli/src/module/`. |
| 42 | + |
| 43 | +### Runtime Package |
| 44 | + |
| 45 | +`packages/runtime/` contains only what ships to end-users at runtime: |
| 46 | +- TypeScript decorators (`@Route`, `@Get`, `@Security`, `@Body`, etc.) |
| 47 | +- Validation logic used in generated routes |
| 48 | +- Type definitions (`Tsoa.*`, `Swagger.*`, `Config`) |
| 49 | + |
| 50 | +It has no dependency on the CLI or TypeScript compiler API. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Namecheap-Specific Source Code Changes |
| 55 | + |
| 56 | +### 1. Pluggable Security Hook |
| 57 | + |
| 58 | +**Files:** `packages/cli/src/metadataGeneration/metadataGenerator.ts`, `controllerGenerator.ts`, `methodGenerator.ts`, `securityGenerator.ts` |
| 59 | + |
| 60 | +Added `MetadataGeneratorOptions` with an optional `securityGenerator` callback: |
| 61 | + |
| 62 | +```ts |
| 63 | +type SecurityGenerator = (node: ClassDeclaration | MethodDeclaration, typeChecker: TypeChecker, inheritedSecurities?: Tsoa.Security[]) => Tsoa.Security[] |
| 64 | +``` |
| 65 | +
|
| 66 | +When provided, both `ControllerGenerator.getSecurity()` and `MethodGenerator.getSecurity()` short-circuit to call it instead of reading `@Security` / `@NoSecurity` decorators. Lets callers implement custom security extraction logic entirely from outside tsoa. |
| 67 | +
|
| 68 | +--- |
| 69 | +
|
| 70 | +### 2. Custom Decorator Extension System (NodeDecoratorProcessor) |
| 71 | +
|
| 72 | +**Files:** `packages/cli/src/metadataGeneration/methodGenerator.ts`, `packages/cli/src/metadataGeneration/types/nodeDecoratorProcessor.ts` |
| 73 | +
|
| 74 | +Added `customDecoratorProcessors: Record<string, NodeDecoratorProcessor>` to `MetadataGeneratorOptions`. After building method metadata, `MethodGenerator` loops over registered processors, finds matching decorators on the AST node, and calls: |
| 75 | +
|
| 76 | +```ts |
| 77 | +interface DecoratorProcessorContext { methodObject: Tsoa.Method; decoratorArguments: any[] } |
| 78 | +type NodeDecoratorProcessor = (context: DecoratorProcessorContext) => void |
| 79 | +``` |
| 80 | +
|
| 81 | +The processor mutates `methodObject` in-place (e.g. pushing to `extensions` for OpenAPI `x-*` fields). Errors include the AST node in `GenerateMetadataError` for better diagnostics. Decorator arguments are resolved via `getInitializerValue` in `initializer-value.ts`. |
| 82 | +
|
| 83 | +--- |
| 84 | +
|
| 85 | +### 3. Decorator Argument Resolution in `initializer-value.ts` |
| 86 | +
|
| 87 | +**File:** `packages/cli/src/metadataGeneration/initializer-value.ts` |
| 88 | +
|
| 89 | +Extended `getInitializerValue` to handle more expression forms when reading decorator arguments at compile time: |
| 90 | +
|
| 91 | +- **Named import aliases** (`import { cfg } from './x'; @Dec(cfg)`) — follows the import alias chain via `getAliasedSymbol` |
| 92 | +- **`as const` / type assertions** — handles `AsExpression` (`x as const`), unwrapping it to evaluate the inner expression |
| 93 | +- **Arithmetic expressions** — handles `BinaryExpression` for `*` and `/` operators (e.g. `15 * 60` evaluates to `900`) |
| 94 | +- **`ImportSpecifier`** — refactored into a shared `getImportSpecifierValue` helper to avoid duplication |
| 95 | +
|
| 96 | +--- |
| 97 | +
|
| 98 | +### 4. TypeScript v6 Compatibility |
| 99 | +
|
| 100 | +**Files:** `packages/cli/src/metadataGeneration/typeResolver.ts`, `packages/cli/src/swagger/specGenerator2.ts`, `packages/cli/src/metadataGeneration/metadataGenerator.ts` |
| 101 | +
|
| 102 | +#### `typeResolver.ts` |
| 103 | +
|
| 104 | +- **`getTypeOfSymbol` instead of `getTypeOfSymbolAtLocation`** — TS v6 removed `getTypeOfSymbolAtLocation`; replaced with `getTypeOfSymbol` when resolving object property types. |
| 105 | +- **Optional property `undefined` stripping** — TS v6's `getTypeOfSymbol` includes `undefined` in the returned union for optional properties. The code now strips it before calling `typeToTypeNode`, so optional enum properties don't fall through the union path. |
| 106 | +- **Synthetic union member matching** — When a union `TypeNode` has `pos === -1` (synthetic, produced by `typeToTypeNode`), member order may differ from the semantic union's `.types`. Members are now matched by `TypeFlags` (Undefined / Null / other) instead of position. |
| 107 | +- **`SymbolFlags.TypeParameter` fix** — `symbol.getFlags()` returns `SymbolFlags`, not `TypeFlags`; corrected the flag constant used for generic `keyof T` detection. |
| 108 | +- **`isMappedTypeNode` guard** — When resolving indexed access types, skip following `symbol.valueDeclaration.type` if the declaration is a mapped type node; TS v6 can produce mapped type declarations where the `.type` doesn't resolve correctly. |
| 109 | +- **Built-in declaration filter** — The filter that drops declarations inside `node_modules/typescript` (to exclude lib types) now only applies when it leaves at least one declaration. TS v6 ships built-ins like `Error` with all declarations inside its own lib files, so the old filter would drop everything. |
| 110 | +- **`this.referencer` fallback for synthetic type aliases** — When resolving a type alias on a synthetic `TypeNode` (`pos === -1`), the referencer is now `this.referencer` instead of `undefined`, preserving context through the synthetic node boundary. |
| 111 | +
|
| 112 | +#### `specGenerator2.ts` |
| 113 | +
|
| 114 | +- **`T | undefined` collapsing** — When a union reduces to a single non-undefined type (i.e. `T | undefined`), the underlying type is returned directly instead of generating a Swagger union. Optionality is expressed via the `required` array, not the type. |
| 115 | +
|
| 116 | +#### `metadataGenerator.ts` |
| 117 | +
|
| 118 | +- **Raw `compilerOptions` normalisation** — Added `resolveCompilerOptions()` which runs options through `convertCompilerOptionsFromJson` before every `createProgram` call. Callers (e.g. host apps) often pass string values (`target: 'ES2020'`) from JSON config; TypeScript's `createProgram` requires numeric enum values. |
0 commit comments