generator icon indicating copy to clipboard operation
generator copied to clipboard

[FEATURE] Migrate packages/helpers to typescript for Parser v3 and type safety

Open devxdh opened this issue 1 month ago • 1 comments

Why do we need this improvement?

@asyncapi/generator-helpers is a core utility package used across templates and generator components to extract structured data from parsed AsyncAPI documents. Currently, the package is authored in pure CommonJS JavaScript with manual JSDoc annotations and no build step.

While reviewing the package, I noticed a few concrete areas where manual JSDocs have drifted from reality, which could lead to confusion or subtle bugs for template authors:

  1. JSDoc Drift vs. Real Parser APIs:

    • In packages/helpers/src/utils.js: getClientName documents @param {object} info, but the implementation actually accepts asyncapi (the root document) and calls getTitle(asyncapi) on line 57.
    • In packages/helpers/src/bindings.js: getDefaultQueryParamsForChannels documents @param {Map<string,string>} channels, but lines 44 and 48 call .isEmpty() and .all() (which are methods on Parser v3's ChannelsInterface, not native JavaScript Maps).
  2. Parser v3 Angle:

    • @asyncapi/parser v3 already exports rich TypeScript interfaces (AsyncAPIDocumentInterface, ChannelsInterface, OperationInterface, ServerInterface, etc.).
    • Authoring helpers in TypeScript allows us to bind helper parameters directly to these official parser interfaces, guaranteeing that our utilities stay in sync with parser updates.
  3. Synergy with Components (#1724):

    • packages/components imports helpers extensively (for example, Readme.js imports getClientName, getServer, getInfo).
    • As packages/components migrates to TypeScript under #1724, having emitted .d.ts declaration files from packages/helpers provides end-to-end type safety instead of having components fall back to any.

How will this change help?

  • Type Safety for Templates & Components: Emitting clean .d.ts type declarations enables strong autocomplete and compile-time type checking for template authors and React components.
  • Self-Documenting Code: Eliminates manual JSDoc drift by letting the TypeScript compiler enforce accurate parameter and return types against Parser v3 interfaces.
  • No Breaking Changes: The build would transpile src/*.ts $\rightarrow$ CommonJS in lib/ (or preserve main compatibility) with declaration files, ensuring existing generator templates continue to work seamlessly.

Screenshots

No response

How could it be implemented/designed?

  1. Add a minimal tsconfig.json to packages/helpers extending the root configuration (similar to apps/react-sdk).
  2. Add a build script in package.json (tsc -p tsconfig.json) integrated with Turborepo (turbo.json).
  3. Migrate src/*.js to src/*.ts, importing interfaces from @asyncapi/parser (AsyncAPIDocumentInterface, ChannelsInterface, etc.).
  4. Emit CommonJS output with declaration files (.d.ts) to maintain backward compatibility.
  5. Update packages/helpers/package.json entry points (main and types).

Tradeoff to Consider: Currently, packages/helpers has no build step (src/ is published directly). Adding TypeScript introduces a compilation step before testing and publishing, similar to apps/react-sdk.

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

  • [x] I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

Yes I am willing to submit a PR!

devxdh avatar Oct 22 '26 08:10 devxdh

@Adi-204,

While this issue is still awaiting approval, I ran a spike on my fork to test the migration path and see what the architecture would look like in practice: https://github.com/devxdh/asyncapi-generator/pull/4.

On the technical side, I set up a minimal tsconfig.json that compiles src/*.ts to CommonJS in lib/ and emits .d.ts declaration files. Keeping the output as CommonJS preserves backward compatibility with @asyncapi/generator and existing templates without causing ESM friction. The build is integrated with Turborepo (tsc on build/pretest), so local testing and downstream consumer builds pick it up automatically. In package.json, only lib/** is scoped for published files, keeping the package minimal without adding unrequested files.

For the types, rather than using manual annotations or falling back to any, the helpers now bind directly to the official @asyncapi/parser interfaces (AsyncAPIDocumentInterface, ChannelsInterface, etc.) and @asyncapi/modelina. For internal helper shapes like discriminators and test doubles, I defined dedicated types so the package stays strictly typed. I also replaced loose optional chaining with explicit assertions (getServers, getServer, getTitle, getOperations), so if an AsyncAPI document is missing a required section or server, it fails fast with a descriptive error instead of returning undefined. All downstream tests in @asyncapi/generator-components and the generator core passed without issues.

Please let me know your thoughts on this.

devxdh avatar Nov 03 '26 15:11 devxdh