asyncapi / modelina

A library for generating typed models based on inputs such as AsyncAPI, OpenAPI, and JSON Schema documents with high customization
https://modelina.org
Apache License 2.0
295 stars 170 forks source link

Generate TypeScript event name -> message type map #1437

Open trevordixon opened 1 year ago

trevordixon commented 1 year ago

Reason/Context

Please try answering few of those questions

I'd like TypeScript to be able to verify that I'm associating the correct message type with a channel. Modelina could generate a type map, e.g.:

type ChannelToMessageType = {
  'foo.bar': FooBarMessage,
  'bar.baz': BarBazMessage,
}

This type could then be used to write safer code where TypeScript would ensure channels and message types match.

function pub<C extends keyof ChannelToMessageType>(channel: C, payload: ChannelToMessageType[C]) {
}

Description

Please try answering few of those questions

Not sure about this. I'd take a crack at implementing it if I could get a few pointers.

github-actions[bot] commented 1 year 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.

jonaslagoni commented 1 year ago

Hey @trevordixon, thanks for the issue!

By using the https://modelina.org/examples?selectedExample=asyncapi-from-parser and https://modelina.org/examples?selectedExample=meta-model you can pretty easily assemble this yourself 🙂

Here is some pseudo code to get you started:

const customModel = new ObjectModel('ChannelToMessageType', undefined, {});
for (channel in asyncapi.channels()) {
  const messageType = FormatHelpers.toPascalCase(channel.publish().message().payload().uid());
  const refModel = new ReferenceModel(messageType, undefined, {}, new ObjectModel(messageType, undefined, {}))
  const propertyModel = new ObjectPropertyModel(
    refModel.name,
    false,
    refModel
  );
  customModel.properties[propertyModel.propertyName] = propertyModel;
}
const inputModel = new InputMetaModel();
inputModel.models[customModel.name] = customModel;

... generator.generate(inputModel);

This means 15 lines of code and you have the model in all languages (just use different generators) 🙂

Whether this is something we want part of the native AsyncAPI input processor, not sure 🤔 Similar issues are https://github.com/asyncapi/modelina/issues/181 and https://github.com/asyncapi/modelina/issues/982

github-actions[bot] commented 9 months ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart: