@asyncapi/converter

中文文档
2.0.2发布于 2026/08/06Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 80.1万

Convert AsyncAPI documents from older to newer versions.

原始使用文档
文档目录11 节
  1. Installation
  2. Usage
  3. From CLI
  4. In JS
  5. In TS
  6. Conversion 2.x.x to 3.x.x
  7. Known missing features
  8. OpenAPI 3.0 to AsyncAPI 3.0 Conversion
  9. Development
  10. Contribution
  11. Contributors ✨

AsyncAPI Converter

Convert AsyncAPI documents older to newer versions and you can also convert OpenAPI documents to AsyncAPI documents.

All Contributors

Installation

npm i @asyncapi/converter

Usage

From CLI

To convert an AsyncAPI document in the console needs the official AsyncAPI CLI.

If you don't have CLI installed, run this command to install the CLI globally on your system:

npm install -g @asyncapi/cli

Minimal usage example with output given:

asyncapi convert streetlights.yml -o streetlights2.yml

# Result:
asyncapi: '2.0.0'
channels:
...

Convert to a specific version:

asyncapi convert streetlights.yml -o streetlights2.yml -t 2.3.0

# Result:
asyncapi: '2.3.0'
channels:
...

In JS

const fs = require('fs');
const { convert } = require('@asyncapi/converter')

try {
  const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
  console.log(convert(asyncapi, '2.6.0'));
} catch (e) {
  console.error(e);
}

In TS

import { convert } from '@asyncapi/converter';
import type { ConvertVersion, ConvertOptions } from '@asyncapi/converter';

try {
  const toVersion: ConvertVersion = '2.6.0';
  const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
  console.log(convert(asyncapi, toVersion));
} catch (e) {
  console.error(e)
}

Conversion 2.x.x to 3.x.x

NOTE: This feature is still WIP, and is until the final release of 3.0.0.

Conversion to version 3.x.x from 2.x.x has several assumptions that should be known before converting:

  • The input must be valid AsyncAPI document.

  • External references are not resolved and converted, they remain untouched, even if they are incorrect.

  • In version 3.0.0, the channel identifier is no longer its address, but due to the difficulty of defining a unique identifier, we still treat the address as an identifier. If there is a need to assign an identifier other than an address, an x-channelId extension should be defined at the level of the given channel.

    # 2.x.x
    channels:
      users/signup:
        x-channelId: 'userSignUp'
        ...
      users/logout:
        ...
    
    # 3.0.0
    channels:
      userSignUp:
        ...
      users/logout:
        ...
    
  • The publish operation is treated as a receive action, and subscribe is treated as a send action. Conversion by default is embraced from the application perspective. If you want to change this logic, you need to specify v2tov3.pointOfView configuration as client.

  • If the operation does not have an operationId field defined, the unique identifier of the operation will be defined as a combination of the identifier of the channel on which the operation was defined + the type of operation, publish or subscribe. Identical situation is with messages. However, here the priority is the messageId field and then the concatenation {publish|subscribe}.messages.{optional index of oneOf messages}.

    # 2.x.x
    channels:
      users/signup:
        publish:
          message:
            ...
        subscribe:
          operationId: 'userSignUpEvent'
          message:
            oneOf:
              - messageId: 'userSignUpEventMessage'
                ...
              - ...
          
    
    # 3.0.0
    channels:
      users/signup:
        messages:
          publish.message:
            ...
          userSignUpEventMessage:
            ...
          userSignUpEvent.message.1:
            ...
    operations:
      users/signup.publish:
        action: receive
        ...
      userSignUpEvent:
        action: send
        ...
    
  • Security requirements that use scopes are defined in the appropriate places inline, the rest as a reference to the components.securitySchemes objects.

  • If servers are defined at the channel level, they are converted as references to the corresponding objects defined in the servers field.

  • Channels and servers defined in components are also converted (unless configured otherwise).

Known missing features

  • When converting from 1.x to 2.x, Streaming APIs (those using stream instead of topics or events) are converted correctly but information about framing type and delimiter is missing until a protocolInfo for that purpose is created.
  • When converting from 2.x to 3.x, and parameter.schema is defined with a reference, it will NOT look into the schema reference and include any relevant keywords for the v3 parameter. It will just create an empty parameter but leave the schema in the components section as is.
    # 2.x.x
    channels:
      "{myParameter}":
        parameters: 
          myParameter: 
            schema: 
              $ref: "#/components/schemas/mySchema"
    components: 
      schemas:
        mySchema:
          enum: ["test"]
          default: "test"
          examples: ["test"]
    
    # 3.0.0
    channels:
      "{myParameter}":
        parameters: 
          myParameter: {}
    
    components: 
      schemas:
        mySchema:
          enum: ["test"]
          default: "test"
          examples: ["test"]
    

OpenAPI 3.0 to AsyncAPI 3.0 Conversion

The converter now supports transformation from OpenAPI 3.0 to AsyncAPI 3.0. This feature enables easy transition of existing OpenAPI 3.0 documents to AsyncAPI 3.0.

To use this new conversion feature:

const fs = require('fs');
const { convertOpenAPI } = require('@asyncapi/converter')

try {
  const openapi = fs.readFileSync('openapi.yml', 'utf-8')
  const asyncapi = convertOpenAPI(openapi, '3.0.0', { from: 'openapi' });
  console.log(asyncapi);
} catch (e) {
  console.error(e);
}

When converting from OpenAPI to AsyncAPI you can now specify the perspective of the conversion using the perspective option. This allows you to choose whether the conversion should be from an application or client point of view

const { convertOpenAPI } = require('@asyncapi/converter')

try {
  const asyncapi2 = fs.readFileSync('asyncapi2.yml', 'utf-8')
  const asyncapi3 = convertOpenAPI(asyncapi2, '3.0.0', { openAPIToAsyncAPI: { perspective: 'client' } });
  console.log(asyncapi3);
} catch (e) {
  console.error(e);
}

The perspective option can be set to either 'server' (default) or 'client'.

  • With server perspective: action becomes receive

  • With client perspective: action becomes send

Limitations

  • External to internal references: The converter does not support scenarios where an external schema file references internal components of the OpenAPI document. In such cases, manual adjustment of the converted document may be necessary.

Development

  1. Setup project by installing dependencies npm install
  2. Write code and tests.
  3. Make sure all tests pass npm test

Contribution

Read CONTRIBUTING guide.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Maciej Urbańczyk
Maciej Urbańczyk

🚧 💻 🐛 👀 ⚠️ 📖
Fran Méndez
Fran Méndez

🚧 💻 🐛 👀 ⚠️ 📖
Lukasz Gornicki
Lukasz Gornicki

🚧 💻 🐛 👀 ⚠️ 📖 🚇
Germán Schnyder
Germán Schnyder

💻 ⚠️
Barbara Czyż
Barbara Czyż

🚇
depimomo
depimomo

💻
Orville Daley
Orville Daley

💻
Ryan R Sundberg
Ryan R Sundberg

💻
Mohan Kumar
Mohan Kumar

💻 ⚠️ 📖 💡
Mintu Gogoi
Mintu Gogoi

💻 📖 💡 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

版本与发布记录

@asyncapi/converter 共发布 53 个版本,首次发布于 2020/12/14;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.2latest2026/08/063 个月前
2.0.12026/02/019 个月前
2.0.02026/02/019 个月前
1.7.02026/01/319 个月前
1.6.42025/11/2411 个月前
1.6.32025/11/2411 个月前

关于 @asyncapi/converter 的常见问题

@asyncapi/converter 是什么?

Convert AsyncAPI documents from older to newer versions.

如何安装 @asyncapi/converter?

在项目目录运行 npm install @asyncapi/converter,或使用 pnpm add / yarn add / bun add。

@asyncapi/converter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@asyncapi/converter 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@asyncapi/converter 可以商用吗?

@asyncapi/converter 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@asyncapi/converter 的下载量有多少?

根据 npm 官方下载统计,@asyncapi/converter 在 2026/10/07 – 2026/11/05 这 30 天内被下载 800,886 次,日均约 26,696 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@asyncapi/converter 还在维护吗?

最新版本 2.0.2 发布于 2026/08/06(3 个月前);至今共发布 53 个版本,其中过去 12 个月发布了 6 个;首次发布于 2020/12/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。