跳到导航

同步你的 AsyncAPI 规范

自动同步你的 AsyncAPI 规范变更,保持 SDK 和文档的最新状态
以 Markdown 格式查看

保持你的 AsyncAPI 规范与代码库同步对于维护准确的 SDK 和文档至关重要。Fern 提供了几种自动化选项来简化这个过程。

GitHub Actions

使用 Fern 的 GitHub Action 在你的 AsyncAPI 规范发生变更时自动更新 SDK 和文档:

.github/workflows/fern.yml
name: Fern
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
fern-check:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Check AsyncAPI spec
uses: fern-api/action@v0
with:
command: check
env:
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
fern-generate:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Generate SDKs and docs
uses: fern-api/action@v0
with:
command: generate
env:
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}

Webhook 集成

设置 webhook 以在 AsyncAPI 规范更新时触发 SDK 生成:

generators.yml
api:
specs:
- spec: asyncapi.yml
github:
repository: your-org/your-repo
webhooks:
- url: https://your-api.com/webhooks/fern
events: [generate]
generators:
- name: fern-typescript-sdk
version: 0.8.8
output:
location: npm
package-name: "@your-org/sdk"

从源自动同步

配置 Fern 自动从各种源拉取你的 AsyncAPI 规范:

从 URL

generators.yml
api:
specs:
- spec: https://api.yourcompany.com/asyncapi.yml
auto-sync: true
generators:
- name: fern-typescript-sdk
version: 0.8.8

从 git 仓库

generators.yml
api:
specs:
- spec:
git:
repository: https://github.com/your-org/api-specs
path: asyncapi/api.yml
branch: main
generators:
- name: fern-typescript-sdk
version: 0.8.8

CI/CD 集成

CircleCI

.circleci/config.yml
version: 2.1
orbs:
fern: fernapi/fern@1.0
workflows:
version: 2
build-and-test:
jobs:
- build
- test:
requires:
- build
- fern/generate:
requires:
- test
filters:
branches:
only: main
context:
- fern-context

GitLab CI

.gitlab-ci.yml
stages:
- build
- test
- generate
variables:
FERN_TOKEN: $FERN_TOKEN
build:
stage: build
script:
- echo "Building application..."
generate-sdks:
stage: generate
image: fernapi/fern:latest
script:
- fern generate
only:
- main

定时更新

设置定时更新以确保你的 SDK 保持最新:

.github/workflows/scheduled-update.yml
name: Scheduled AsyncAPI Update
on:
schedule:
- cron: '0 2 * * 1' # Every Monday at 2 AM UTC
workflow_dispatch:
jobs:
update-specs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Update AsyncAPI specs
run: |
curl -o fern/asyncapi/asyncapi.yml https://api.yourcompany.com/asyncapi.yml
- name: Generate with latest spec
uses: fern-api/action@v0
with:
command: generate
env:
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}

监控变更

跟踪你的 AsyncAPI 规范的变更:

generators.yml
api:
specs:
- spec: asyncapi.yml
change-detection:
enabled: true
breaking-changes: error
notifications:
slack: ${{ secrets.SLACK_WEBHOOK }}
email: team@yourcompany.com
generators:
- name: fern-typescript-sdk
version: 0.8.8

这确保了对你的 AsyncAPI 规范的任何破坏性变更都会被检测到,并且在变更传播到你的 SDK 和文档之前通知相应的团队成员。