此软件包是在 AsyncAPI CPI 单列波。 。 。 .
概览
一个官方库,可以让你捆绑/调试或合并到您的 AsyncAPI 文档中.
AsyncAPI 软件 Bundler可以帮助你,如果:
您的规格文件被分成不同的较小文件, 并且正在使用 JSON `$ref' 属性来引用组件
# asyncapi.yaml
asyncapi: '2.4.0'
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signup:
subscribe:
message:
$ref: './messages.yaml#/messages/UserSignedUp'
# messages.yaml
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
# After combining
asyncapi: 2.4.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signedup:
subscribe:
message:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
您有不同的独立规格文件来定义更大的系统,请参见这里的例子
# signup.yaml
asyncapi: '2.4.0'
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user Signup
channels:
user/signedup:
subscribe:
message:
payload:
type: object
properties:
displayName:
type: string
email:
type: string
format: email
# login.yaml
asyncapi: '2.4.0'
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signup
channels:
user/loggenin:
subscribe:
message:
payload:
type: object
properties:
displayName:
type: string
# After combining
# asyncapi.yaml
asyncapi: '2.4.0'
info:
title: Account Service
version: 1.0.0
description: This service is in charge for processing user authentication
channles:
user/signedup:
subscribe:
message:
payload:
type: object
properties:
displayName:
type: string
email:
type: string
format: email
user/loggedin:
subscribe:
message:
payload:
type: object
properties:
displayName:
type: string
安装
npm install @asyncapi/bundler
使用量
AsyncAPI 软件 Bundler 可以很容易地在您的 JavaScript 项目中用作节点.js 模块 :
'use strict';
const { writeFileSync } = require('fs');
const bundle = require('@asyncapi/bundler');
async function main() {
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
if (document.yml()) {
console.log(document.yml()); // the complete bundled AsyncAPI document
writeFileSync('asyncapi.yaml', document.yml()); // the complete bundled AsyncAPI document
}
}
main().catch(e => console.error(e));
外部参考资料的参考
Bundler 尽可能地调试所提供的 AsyncAPI 文档, 仅保留那些 MUSST 的内部引用 Reference Objects 根据 AsyncAPI 规格(因此,绝不应被忽略):
- AsyncAPI 规格 v 2. 6.0
没有内部参考文献显示 Reference Object编号.
- AsyncAPI 规格 v3. 0.0
内部参考文献 Reference Object编号:
/#\/channels\/.*\/servers/
/#\/operations\/.*\/channel/
/#\/operations\/.*\/messages/
/#\/operations\/.*\/reply\/channel/
/#\/operations\/.*\/reply\/messages/
/#\/components\/channels\/.*\/servers/
/#\/components\/operations\/.*\/channel/
/#\/components\/operations\/.*\/messages/
/#\/components\/operations\/.*\/reply\/channel/
/#\/components\/operations\/.*\/reply\/messages/
选项 baseDir
选项 baseDir 代表程序的主要工作目录“ root 目录” , 相对而言, 将解决 AsyncAPI 文档传递到 Bundler。 。 。 .
从 Bundler v0.5.0, 选项 baseDir 以改变逻辑的方式重新执行,以及 Bundler 仅接受 路径 asyncAPI 文档,将随 readFileSync() 内部.
简言之,这个过程是这样的:
-
AsyncAPI 文档路径作为
'main.yaml'|'./main.yaml'|'../main.yaml'|['./main.yaml']|['main.yaml', 'audio.yaml']等 (中文(简体) ). -
路径/路径保证
Array类型为Array.from()让他们可以活下来. -
程序的工作目录改为
baseDir与process.chdir()。 。 。 . -
只有这样 是 AsyncAPI 文档的路径,从当前数组开始逐一读取,解析路径和
$refs 相对baseDir。 。 。 .
看看这个 ./example/bundle-cjs.cjs,表明与 baseDir 财务报告和审定财务报表 $ref不同水平的巢穴.
财产 x-origin
财产 x-origin 用于源头追踪 Bundler 和组件命名 Optimizer。 。 。 .
来源于 本评论 在长达一年的讨论中:
$ref通常还带有一个表意来理解它是什么(例如"$ref: financial-system.yaml#/组件/schemas/ Bank AccountIdentifer"). 如果捆绑只是解决了这个 ref inline, $ref指针的示意图意义会丢失, 无法在后期步骤中恢复 。 优化器在移动到组件部分时需要为"银行会计识别器"发明一个人工组件名称.
因此,财产 x-origin 包含被忽略的历史值 $refs,也用于 Optimizer 为它通过AsyncAPI文档移动的组件提供有意义的名称.
然而,如果用户不需要/不需要 x-origin asyncAPI 文档结构中的属性(值) x-origin 属性可能泄露AsyncAPI文档描述的系统结构的内部细节,) { xOrigin: false } (或省略通过) xOrigin )改为: Bundler 在选项对象中.
部件移动到 components
所有 AsyncAPI 规格验证组件移动到 components asyncAPI 文档从 Bundler v0.5.0 由 Optimizer v1.0.0+ (中文(简体) ).
要在 CI/ 编码中获取 AsyncAPI 文档,该文档将被删除 尽最大可能 它的所有组件都移到 components 区域,原始 AsyncAPI 文档必须运行于链条 Bundler -> Optimizer。 。 。 .
若为 Optimizer 无法找到 x-origin 属性在优化提供的 AsyncAPI 文档时,将组件的现有名称用作倒置机制,但记住组件名称在此情况下可能缺乏语义意义.
代码示例
类型脚本
import { writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';
async function main() {
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
if (document.yml()) {
writeFileSync('asyncapi.yaml', document.yml());
}
main().catch(e => console.error(e));
JavaScript CJS 模块系统
'use strict';
const { writeFileSync } = require('fs');
const bundle = require('@asyncapi/bundler');
async function main() {
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
if (document.yml()) {
writeFileSync('asyncapi.yaml', document.yml());
}
main().catch(e => console.error(e));
JavaScript 无害环境管理模块系统
'use strict';
import { writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';
async function main() {
const document = await bundle(['social-media/comments-service/main.yaml'], {
baseDir: 'example-data',
xOrigin: true,
});
if (document.yml()) {
writeFileSync('asyncapi.yaml', document.yml());
}
main().catch(e => console.error(e));
捆绑( 文件, [选项])
仁爱:全球职能
| 参数 | 类型 | 说明 |
|---|---|---|
| 文件 | string | Array.<string> | 应捆绑的 AsyncAPI 文档的一个或多个相对/绝对路径 . |
| [可选 | Object | |
| [选项.base] | string | 一个相对/绝对的去向对象的路径,其属性将被保留. |
| [选项.baseDir] | string | 一个相对/绝对的目录路径,相对于应该被捆绑的 AsyncAPI 文档路径,将会被解决. |
| [选项.xOrigin] | boolean | 过 |
贡献者
感谢这些伟大的人emoji 密钥:
苏维克语 💻 🤔 🎨 👀 🚧 📖 |
马西杰·乌尔班奇克 🤔 👀 |
穆罕默德·图基尔·汗 🚇 |
杨先生 📖 |
阿芒普雷特·辛格·贝迪 📖 |
阿列克谢·瓦西里耶维奇 📖 |
维亚切斯拉夫·图罗夫斯基伊 💻 🚇 📖 🚧 👀 🤔 |
卢卡斯·戈尔尼茨基 🚇 👀 |
阿克萨特·内马 💻 |
萨姆巴夫古普塔0705 💻 |
本项目遵循 全部贡献者 规格。 任何捐款都欢迎!
