跳到导航

使用受众筛选您的 API

使用 x-fern-audiences 筛选相关的操作、通道和消息模式

以 Markdown 格式查看
团队版和企业版功能

此功能仅适用于团队版和企业版计划。要开始使用,请联系 support@buildwithfern.com

受众是对事件驱动 API 进行分段以服务不同消费者的有用工具。常见的受众示例包括 publicbeta

记住在指定受众后要筛选您的 SDK 和文档。如果未指定受众,将不会进行任何筛选。

以下示例配置 SDK 筛选到 public 受众:

generators.yml
groups:
sdks:
audiences:
- public
generators:
- name: fern-typescript-sdk
version: 0.8.8

以下示例配置文档筛选到 public 受众:

docs.yml
navigation:
- api: API Reference
audiences:
- public

筛选操作

在操作中添加 x-fern-audiences 来控制特定受众包含哪些操作:

asyncapi.yml
operations:
sendPublicNotification:
action: send
channel:
$ref: '#/channels/public~1notifications'
x-fern-audiences:
- public
sendBetaAlert:
action: send
channel:
$ref: '#/channels/beta~1alerts'
x-fern-audiences:
- beta

筛选通道

您还可以按受众筛选整个通道:

asyncapi.yml
channels:
public/events:
address: public/events
messages:
PublicEvent:
$ref: '#/components/messages/PublicEvent'
x-fern-audiences:
- public
internal/events:
address: internal/events
messages:
InternalEvent:
$ref: '#/components/messages/InternalEvent'
x-fern-audiences:
- internal

筛选消息模式

将特定消息模式筛选到不同受众:

asyncapi.yml
components:
messages:
PublicUserEvent:
contentType: application/json
payload:
$ref: '#/components/schemas/PublicUser'
x-fern-audiences:
- public
AdminUserEvent:
contentType: application/json
payload:
$ref: '#/components/schemas/AdminUser'
x-fern-audiences:
- admin
schemas:
PublicUser:
type: object
properties:
id:
type: string
name:
type: string
email:
type: string
x-fern-audiences:
- public
AdminUser:
allOf:
- $ref: '#/components/schemas/PublicUser'
- type: object
properties:
role:
type: string
permissions:
type: array
items:
type: string
x-fern-audiences:
- admin

筛选模式属性

您可以筛选模式内的单个属性:

asyncapi.yml
components:
schemas:
UserEvent:
type: object
properties:
id:
type: string
email:
type: string
x-fern-audiences:
- internal
publicName:
type: string
x-fern-audiences:
- public
internalNotes:
type: string
x-fern-audiences:
- internal

这使您能够为不同受众提供相同事件模式的不同视图,向每个消费者只显示相关信息。