什么是 AsyncAPI 规范?

以 Markdown 格式查看

Fern 支持为 TypeScript、Python、Java 和 C# 生成 AsyncAPI SDK。提交 issue 来请求支持其他语言。

AsyncAPI 规范是开发人员用来文档化事件驱动 API 的框架。该规范使用 JSON 或 YAML 编写,包含您的所有通道、消息、模式和身份验证方案。Fern 与 AsyncAPI 规范 v2.6.0v3.0.0 兼容。

以下是一个 AsyncAPI 文件的示例:

asyncapi.yml
1asyncapi: 3.0.0
2info:
3 title: User Notification Service
4 version: 1.0.0
5 description: |
6 Service that handles user notifications through various channels
7 including email, SMS, and push notifications.
8channels:
9 user/signup:
10 address: user/signup
11 messages:
12 UserSignedUp:
13 $ref: '#/components/messages/UserSignedUp'
14 notification/send:
15 address: notification/send
16 messages:
17 SendNotification:
18 $ref: '#/components/messages/SendNotification'
19operations:
20 onUserSignup:
21 action: receive
22 channel:
23 $ref: '#/channels/user~1signup'
24 summary: User signup event
25 description: Triggered when a user signs up for the service
26 sendNotification:
27 action: send
28 channel:
29 $ref: '#/channels/notification~1send'
30 summary: Send notification
31 description: Send a notification to a user
32components:
33 messages:
34 UserSignedUp:
35 name: UserSignedUp
36 title: User Signed Up
37 summary: User has signed up for the service
38 contentType: application/json
39 payload:
40 $ref: '#/components/schemas/User'
41 SendNotification:
42 name: SendNotification
43 title: Send Notification
44 summary: Send a notification to a user
45 contentType: application/json
46 payload:
47 $ref: '#/components/schemas/Notification'
48 schemas:
49 User:
50 type: object
51 properties:
52 id:
53 type: string
54 format: uuid
55 description: Unique identifier for the user
56 email:
57 type: string
58 format: email
59 description: User's email address
60 name:
61 type: string
62 description: User's full name
63 createdAt:
64 type: string
65 format: date-time
66 description: When the user was created
67 required:
68 - id
69 - email
70 - name
71 Notification:
72 type: object
73 properties:
74 userId:
75 type: string
76 format: uuid
77 description: ID of the user to notify
78 type:
79 type: string
80 enum: [email, sms, push]
81 description: Type of notification to send
82 message:
83 type: string
84 description: Message content
85 priority:
86 type: string
87 enum: [low, medium, high, urgent]
88 default: medium
89 description: Priority level of the notification
90 required:
91 - userId
92 - type
93 - message

设置您的 fern 文件夹

正在考虑生成 AsyncAPI 规范的选项?在这里获得实时支持

1

创建您的 fern 目录

在您的项目根目录中创建一个 fern/ 文件夹。

fern
2

添加您的 AsyncAPI 规范

将您的 AsyncAPI 规范添加到 fern 目录中。您可以将其放在名为 asyncapi 的子文件夹中,或直接放在 fern 目录中。

fern
asyncapi
asyncapi.yml
3

创建 fern.config.json 文件

在您的 fern 目录中添加一个 fern.config.json 文件,其中列出您的组织和当前版本的 Fern CLI:

fern.config.json
1{
2 "organization": "your-organization",
3 "version": "5.7.5"
4}
fern
fern.config.json
asyncapi
asyncapi.yml
4

创建 generators.yml 文件

在您的 fern 目录中创建一个 generators.yml 文件,并添加对您的 AsyncAPI 规范的引用:

generators.yml
1# Your API definition
2api:
3 specs:
4 - asyncapi: ./asyncapi/asyncapi.yml
5
6groups:
7 external:
8 generators:
9 # Your generator configurations here

您的最终目录结构:

fern
fern.config.json
generators.yml
asyncapi
asyncapi.yml