使用受众过滤您的 API
使用受众过滤您的 API
使用 x-fern-audiences 过滤相关方法、参数和模式
受众是一个有用的工具,用于为不同的消费者分割您的 JSON-RPC API。常见的受众示例包括 public
和 beta。
记住在指定受众后过滤您的 SDK 和文档。如果未指定受众, 将不会过滤任何内容。
SDK
以下示例配置 SDK 过滤到 public 受众:
generators.yml
1 groups: 2 sdks: 3 audiences: 4 - public 5 generators: 6 - name: fernapi/fern-typescript-sdk 7 version: 0.8.8
文档
以下示例配置文档过滤到 public 受众:
docs.yml
1 navigation: 2 - api: API Reference 3 audiences: 4 - public
过滤方法
向方法添加 x-fern-audiences 来控制特定受众包含哪些方法:
openrpc.yml
1 methods: 2 - name: public.getInfo 3 summary: Get public API information 4 description: Publicly available information about the API 5 x-fern-audiences: 6 - public 7 params: [] 8 result: 9 name: info 10 schema: 11 $ref: '#/components/schemas/ApiInfo' 12 - name: admin.getUsers 13 summary: Get all users (admin only) 14 x-fern-audiences: 15 - admin 16 params: 17 - name: limit 18 schema: 19 type: integer 20 default: 100 21 result: 22 name: users 23 schema: 24 type: array 25 items: 26 $ref: '#/components/schemas/User'
过滤方法参数
您可以过滤方法内的特定参数:
openrpc.yml
1 methods: 2 - name: user.create 3 summary: Create a new user 4 params: 5 - name: userData 6 schema: 7 type: object 8 properties: 9 email: 10 type: string 11 format: email 12 name: 13 type: string 14 adminNotes: 15 type: string 16 x-fern-audiences: 17 - admin 18 internalId: 19 type: string 20 x-fern-audiences: 21 - internal 22 required: 23 24 - name 25 required: true 26 result: 27 name: user 28 schema: 29 $ref: '#/components/schemas/User'
过滤模式
将整个模式过滤到不同的受众:
openrpc.yml
1 components: 2 schemas: 3 PublicUser: 4 type: object 5 x-fern-audiences: 6 - public 7 properties: 8 id: 9 type: string 10 name: 11 type: string 12 email: 13 type: string 14 required: 15 - id 16 - name 17 18 AdminUser: 19 allOf: 20 - $ref: '#/components/schemas/PublicUser' 21 - type: object 22 x-fern-audiences: 23 - admin 24 properties: 25 role: 26 type: string 27 permissions: 28 type: array 29 items: 30 type: string 31 createdAt: 32 type: string 33 format: date-time 34 lastLoginAt: 35 type: string 36 format: date-time
过滤模式属性
您可以过滤模式内的各个属性:
openrpc.yml
1 components: 2 schemas: 3 Order: 4 type: object 5 properties: 6 id: 7 type: string 8 amount: 9 type: number 10 x-fern-audiences: 11 - public 12 internalCost: 13 type: number 14 x-fern-audiences: 15 - internal 16 debugInfo: 17 type: object 18 x-fern-audiences: 19 - debug 20 customerInfo: 21 type: object 22 properties: 23 id: 24 type: string 25 email: 26 type: string 27 x-fern-audiences: 28 - admin
过滤错误响应
根据受众过滤错误信息:
openrpc.yml
1 methods: 2 - name: payment.process 3 summary: Process a payment 4 params: 5 - name: paymentData 6 schema: 7 $ref: '#/components/schemas/PaymentRequest' 8 errors: 9 - code: -32001 10 message: Payment failed 11 x-fern-audiences: 12 - public 13 data: 14 type: object 15 properties: 16 error: 17 type: string 18 const: "Payment could not be processed" 19 - code: -32001 20 message: Payment failed 21 x-fern-audiences: 22 - admin 23 data: 24 type: object 25 properties: 26 error: 27 type: string 28 errorCode: 29 type: string 30 gatewayResponse: 31 type: object 32 debugTrace: 33 type: string
服务器级别过滤
在服务器级别应用受众过滤:
openrpc.yml
1 servers: 2 - name: public-api 3 url: https://api.example.com/rpc 4 x-fern-audiences: 5 - public 6 description: Public API server 7 - name: admin-api 8 url: https://admin-api.example.com/rpc 9 x-fern-audiences: 10 - admin 11 description: Admin API server with additional privileges
条件方法可用性
使用受众使方法仅在特定上下文中可用:
openrpc.yml
1 methods: 2 - name: debug.getSystemInfo 3 summary: Get system debug information 4 description: Internal system information for debugging 5 x-fern-audiences: 6 - debug 7 params: [] 8 result: 9 name: systemInfo 10 schema: 11 type: object 12 additionalProperties: true 13 - name: beta.advancedSearch 14 summary: Advanced search functionality 15 x-fern-audiences: 16 - beta 17 params: 18 - name: query 19 schema: 20 type: object 21 properties: 22 text: 23 type: string 24 filters: 25 type: object 26 additionalProperties: true 27 result: 28 name: results 29 schema: 30 $ref: '#/components/schemas/SearchResults' 31 - name: internal.resetCache 32 summary: Reset internal caches 33 x-fern-audiences: 34 - internal 35 params: 36 - name: cacheType 37 schema: 38 type: string 39 enum: [user, product, session, all] 40 # Notification - no result expected
这允许您为不同类型的消费者创建同一个 JSON-RPC API 的不同视图,确保每个受众只看到与其用例相关的方法和数据。