lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.33k stars 481 forks source link

Cannot use @Response decorator with variables #1565

Closed sheppard30 closed 3 months ago

sheppard30 commented 4 months ago

Expected Behavior

I shoud be able to import my type and dictionary of errors, use them in the @Response decorator to avoid duplication of code. I think it might be an issue with aliases. Relative path works better but still not ideal (eg I do not have my error messages from errors in swagger).

// controller.ts

import { ErrorResponse } from "@types"
import { errors } from "@utils"
...
    @Response<ErrorResponse>(
        errors.Unauthorized.code,
        errors.Unauthorized.message,
        errors.Unauthorized
    )
...
// types.ts
export type ErrorResponse = {
    code:
        | HttpStatusCodeLiteral
        | HttpStatusCodeStringLiteral
        | OtherValidOpenApiHttpStatusCode;
    slug: ErrorSlug;
    message?: string;
};

export type ResponseErrors = Record<ErrorSlug, ErrorResponse>;
// errors.ts
export const errors: ResponseErrors = {
    Unauthorized: {
        code: '401',
        slug: 'Unauthorized',
        message: 'User unauthorized.',
    },

Current Behavior

When I am trying to build routes I get an error

> npx tsoa routes  
Generate routes error.
 GenerateMetadataError: No declarations found for referenced type ErrorResponse.
    at TypeResolver.getModelTypeDeclarations (C:\Users\mscir\Projekty\wykop-gpt-backend\node_modules\@tsoa\cli\dist\metadataGeneration\typeResolver.js:1127:19)

Possible Solution

No idea

Steps to Reproduce

  1. Create aliases in TS
  2. Import type and some dictionary from another file via alias
  3. Use this type and dictionary in @Response decorator

Context (Environment)

Version of the library: 6.0.1 Version of NodeJS: 20.10

github-actions[bot] commented 4 months ago

Hello there sheppard30 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

WoH commented 4 months ago

Seems like an issue with your tsc config. Make sure the types are included, not ignored.

sheppard30 commented 4 months ago

Seems like an issue with your tsc config. Make sure the types are included, not ignored.

Hello. Thank you for your reply. This is my tsconfig.json and tsoa.json.

There are two issues:

  1. Error I presented above when I use aliases
  2. Cannot use variables inside decorator if they are in another file
{
    "compilerOptions": {
        "target": "es2022",
        "module": "CommonJS",
        "moduleResolution": "node",
        "outDir": "./dist",
        "rootDir": "./src",
        "baseUrl": "./",
        "resolveJsonModule": true,
        "paths": {
            "@config": ["src/config"],
            "@controllers": ["src/controllers"],
            "@env": ["src/env"],
            "@helpers": ["src/helpers"],
            "@middleware": ["src/middleware"],
            "@models": ["src/models"],
            "@services": ["src/services"],
            "@types": ["src/types"],
            "@utils": ["src/utils"]
        },
        "strict": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    },
    "include": ["src/**/*.ts", "src/test-setup.ts"],
    "exclude": ["node_modules", "src/**/*.spec.ts"]
}
{
    "entryFile": "src/main.ts",
    "noImplicitAdditionalProperties": "throw-on-extras",
    "controllerPathGlobs": ["src/**/*.controller.ts"],
    "spec": {
        "outputDirectory": "src/schema",
        "specVersion": 3,
        "securityDefinitions": {
            "jwt": {
                "type": "apiKey",
                "in": "header",
                "name": "Authentication"
            }
        }
    },
    "routes": {
        "authenticationModule": "src/middleware/authentication.middleware.ts",
        "routesDir": "src/routes"
    }
}
github-actions[bot] commented 3 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days