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

[FEATURE] Generate Exported Constants for Properties with const Values #1582

Open
2 tasks done
frankyhun opened this issue Nov 25, 2024 · 1 comment
Open
2 tasks done
Labels
enhancement New feature or request

Comments

@frankyhun
Copy link

Why do we need this improvement?

When defining schemas in AsyncAPI documents, the const keyword is used to enforce that a property must have a specific, fixed value. However, in the current TypeScript model generation, these fixed values are hardcoded in the interfaces, limiting reusability and clarity.

We propose that properties defined with the const keyword in the AsyncAPI schema should also generate exported constants in the TypeScript models. These constants can then be referenced in the generated interfaces, improving code maintainability and consistency.

Example Input Schema:

asyncapi: '3.0.0'
info:
  title: Example API
  version: '1.0.0'
channels:
  example/channel:
    publish:
      message:
        contentType: application/json
        payload:
          type: object
          properties:
            eventType:
              type: string
              const: "EXAMPLE_EVENT"

Expected Output in TypeScript:

// Exported constant
export const EVENT_TYPE = "EXAMPLE_EVENT";

// Interface using the constant
export interface ExampleMessage {
  eventType: typeof EVENT_TYPE; // Enforces the fixed value
}

How will this change help?

  • Improved Reusability: The constant can be reused across the codebase instead of hardcoding the value multiple times.
  • Enhanced Readability: Exported constants make the intent of fixed values more explicit.
  • Better Maintainability: If the constant value changes, it only needs to be updated in one place.
  • Alignment with TypeScript Best Practices: Encourages using constants for fixed values instead of inline literals.

Screenshots

No response

How could it be implemented/designed?

  • Modify the TypeScript template in the AsyncAPI Generator to:
  • Identify properties with the const keyword.
  • Generate an exported constant for each property with a const value.
  • Reference the exported constant in the corresponding interface.
  • This feature could optionally be configurable (e.g., via a generator flag) to provide flexibility for developers who may not require exported constants.

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

None

@frankyhun frankyhun added the enhancement New feature or request label Nov 25, 2024
@github-project-automation github-project-automation bot moved this to To Triage in CLI - Kanban Nov 25, 2024
Copy link
Contributor

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: To Triage
Development

No branches or pull requests

1 participant