asyncapi / cli

CLI to work with your AsyncAPI files. You can validate them and in the future use a generator and even bootstrap a new file. Contributions are welcomed!
https://www.asyncapi.com/tools/cli
Apache License 2.0
188 stars 164 forks source link

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

Open frankyhun opened 1 day ago

frankyhun commented 1 day ago

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?

Screenshots

No response

How could it be implemented/designed?

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

None

github-actions[bot] commented 1 day ago

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.