AsyncAPI提供了许多方式来再利用文档的某些部分,如消息或计划定义或外部文件的引用,更不用提特征了. 宗旨 AsyncAPI 优化器 使 AsyncAPI 文件实现优化。 它是一个可以用于UI和CLI的库.
这个包裹现在生活在
asyncapi/cli单相 下级packages/optimizer/从那里释放(同一个 npm 名称)@asyncapi/optimizer) (中文(简体) ).v2是直通图书馆消费者的破解发行. 错误现已输入包含一个马厩的类
.code(单位:千美元)OptimizerError编号 :OptimizerErrorCode) 而不是平庸的Error+ 键console.error· ;getReport()现在返回{ type, elements }[]; 和 :@asyncapi/parser现在是一个 依赖性 (安装在旁边). 优化算法和getOptimizedDocument()行为不变。 见移徙指南:docs/optimizer/optimizer-migration.md。 。 。 .
测试
- 克隆 CLI 单列
git clone https://github.com/asyncapi/cli.git - 从 repo 根安装依赖性
npm i - 构建此软件包
npm run optimizer:build,或从packages/optimizer运行npm run example打开examples/index.js并修改或添加自己的 AsyncAPI 文档.
使用量
节点.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)
生成报告
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: [] }
]
*/
应用建议的修改
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 文档
要使用优化器优化文件, 您只需要导入 Optimizer 课。 使用其两种方法获取报告getReport())并获得优化文档(getOptimizedDocument()) (中文(简体) ).
见 API 文档 以更多实例和完整的 API 参考信息.
