@asyncapi/optimizer

中文文档
2.0.1发布于 2026/11/02Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 29.3万

This library will optimize the AsyncAPI specification file.

原始使用文档
文档目录6 节
  1. Testing
  2. Usage
  3. Node.js
  4. Generating report
  5. Applying the suggested changes
  6. API documentation

AsyncAPI Optimizer

AsyncAPI offers many ways to reuse certain parts of the document like messages or schemas definitions or references to external files, not to even mention the traits. Purpose of AsyncAPI Optimizer is to enable different ways to optimize AsyncAPI files. It is a library that can be used in UIs and CLIs.

This package now lives in the asyncapi/cli monorepo under packages/optimizer/ and is released from there (same npm name @asyncapi/optimizer).

v2 is a breaking release for direct library consumers. Errors are now typed classes carrying a stable .code (OptimizerError / OptimizerErrorCode) instead of plain Error + console.error; getReport() now returns { type, elements }[]; and @asyncapi/parser is now a peerDependency (install it alongside). The optimization algorithm and getOptimizedDocument() behaviour are unchanged. See the migration guide: docs/optimizer/optimizer-migration.md.

npm npm

Testing

  1. Clone the CLI monorepo git clone https://github.com/asyncapi/cli.git
  2. Install the dependencies from the repo root npm i
  3. Build this package with npm run optimizer:build, or from packages/optimizer run npm run example. You can open examples/index.js and modify it or add your own AsyncAPI document.

Usage

Node.js

import { Optimizer } from '@asyncapi/optimizer'
import type { Report } from '@asyncapi/optimizer'

const yaml = `
asyncapi: 3.0.0
info:
  title: Example Service
  version: 1.0.0
  description: Example Service.
servers:
  production:
    host: 'test.mosquitto.org:{port}'
    protocol: mqtt
    description: Test broker
    variables:
      port:
        description: Secure connection (TLS) is available through port 8883.
        default: '1883'
        enum:
          - '1883'
          - '8883'
operations:
  user/deleteAccount.subscribe:
    action: send
    channel:
      $ref: '#/channels/commentLikedChannel'
channels:
  commentLikedChannel:
    address: comment/liked
    messages:
      commentLikedMessage:
        description: Message that is being sent when a comment has been liked by someone.
        payload:
          type: object
          title: commentLikedPayload
          properties:
            commentId:
              type: string
              description: an id object
              x-origin: ./schemas.yaml#/schemas/idSchema
          x-origin: ./schemas.yaml#/schemas/commentLikedSchema
        x-origin: ./messages.yaml#/messages/commentLikedMessage
    x-origin: ./channels.yaml#/channels/commentLikedChannel`

const optimizer = new Optimizer(yaml)

Generating report

const report = await optimizer.getReport()
/*
v2: getReport() returns { type, elements }[] (not a keyed object).

[
  { type: 'removeComponents', elements: [] },
  { type: 'reuseComponents', elements: [] },
  {
    type: 'moveAllToComponents',
    elements: [
      {
        path: 'channels.commentLikedChannel.messages.commentLikedMessage.payload.properties.commentId',
        action: 'move',
        target: 'components.schemas.idSchema'
      },
      {
        path: 'channels.commentLikedChannel.messages.commentLikedMessage.payload',
        action: 'move',
        target: 'components.schemas.commentLikedSchema'
      },
      {
        path: 'channels.commentLikedChannel.messages.commentLikedMessage',
        action: 'move',
        target: 'components.messages.commentLikedMessage'
      },
      {
        path: 'operations.user/deleteAccount.subscribe',
        action: 'move',
        target: 'components.operations.subscribe'
      },
      {
        path: 'channels.commentLikedChannel',
        action: 'move',
        target: 'components.channels.commentLikedChannel'
      },
      {
        path: 'servers.production',
        action: 'move',
        target: 'components.servers.production'
      }
    ]
  },
  { type: 'moveDuplicatesToComponents', elements: [] }
]
 */

Applying the suggested changes

const optimizedDocument = optimizer.getOptimizedDocument({
  output: 'YAML',
  rules: {
    reuseComponents: true,
    removeComponents: true,
    moveAllToComponents: true,
    moveDuplicatesToComponents: false,
  },
  disableOptimizationFor: {
    schema: false,
  },
})
/*
the optimizedDocument value will be:

asyncapi: 3.0.0
info:
  title: Example Service
  version: 1.0.0
  description: Example Service.
servers:
  production:
    $ref: '#/components/servers/production'
operations:
  user/deleteAccount.subscribe:
    action: send
    channel:
      $ref: '#/channels/commentLikedChannel'
  user/deleteAccount:
    subscribe:
      $ref: '#/components/operations/subscribe'
channels:
  commentLikedChannel:
    $ref: '#/components/channels/commentLikedChannel'
components:
  schemas:
    idSchema:
      type: string
      description: an id object
      x-origin: ./schemas.yaml#/schemas/idSchema
    commentLikedSchema:
      type: object
      title: commentLikedPayload
      properties:
        commentId:
          $ref: '#/components/schemas/idSchema'
      x-origin: ./schemas.yaml#/schemas/commentLikedSchema
  messages:
    commentLikedMessage:
      description: Message that is being sent when a comment has been liked by someone.
      payload:
        $ref: '#/components/schemas/commentLikedSchema'
      x-origin: ./messages.yaml#/messages/commentLikedMessage
  operations: {}
  channels:
    commentLikedChannel:
      address: comment/liked
      messages:
        commentLikedMessage:
          $ref: '#/components/messages/commentLikedMessage'
      x-origin: ./channels.yaml#/channels/commentLikedChannel
  servers:
    production:
      host: test.mosquitto.org:{port}
      protocol: mqtt
      description: Test broker
      variables:
        port:
          description: Secure connection (TLS) is available through port 8883.
          default: '1883'
          enum:
            - '1883'
            - '8883'
 */

API documentation

For using the optimizer to optimize file you just need to import the Optimizer class. Use its two methods to get the report (getReport()) and get the optimized document (getOptimizedDocument()).

See API documentation for more example and full API reference information.

版本与发布记录

@asyncapi/optimizer 共发布 56 个版本,首次发布于 2021/08/10;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.1latest2026/11/025 天前
2.0.02026/09/231 个月前
1.0.62025/11/2411 个月前
1.0.52025/11/2411 个月前
1.0.42024/10/222 年前
1.0.32024/08/242 年前

关于 @asyncapi/optimizer 的常见问题

@asyncapi/optimizer 是什么?

This library will optimize the AsyncAPI specification file.

如何安装 @asyncapi/optimizer?

在项目目录运行 npm install @asyncapi/optimizer,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@asyncapi/parser(^3.6.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@asyncapi/optimizer 支持 TypeScript 吗?

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

@asyncapi/optimizer 支持 ESM 还是 CommonJS?

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

@asyncapi/optimizer 可以商用吗?

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

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

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

@asyncapi/optimizer 还在维护吗?

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