Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use @Response decorator with variables #1565

Closed
2 of 4 tasks
sheppard30 opened this issue Feb 17, 2024 · 4 comments
Closed
2 of 4 tasks

Cannot use @Response decorator with variables #1565

sheppard30 opened this issue Feb 17, 2024 · 4 comments
Labels

Comments

@sheppard30
Copy link

sheppard30 commented Feb 17, 2024

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

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

  • Confirm you were using yarn not npm: [ ]
Copy link

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
Copy link
Collaborator

WoH commented Feb 17, 2024

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

@sheppard30
Copy link
Author

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"
    }
}

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants