使用受众过滤您的 API
使用 x-fern-audiences 过滤相关方法、参数和模式
受众是一个有用的工具,用于为不同的消费者分割您的 JSON-RPC API。常见的受众示例包括 public
和 beta。
记住在指定受众后过滤您的 SDK 和文档。如果未指定受众, 将不会过滤任何内容。
SDK
以下示例配置 SDK 过滤到 public 受众:
generators.yml
groups:sdks:audiences:- publicgenerators:- name: fern-typescript-sdkversion: 0.8.8
文档
以下示例配置文档过滤到 public 受众:
docs.yml
navigation:- api: API Referenceaudiences:- public
过滤方法
向方法添加 x-fern-audiences 来控制特定受众包含哪些方法:
openrpc.yml
methods:- name: public.getInfosummary: Get public API informationdescription: Publicly available information about the APIx-fern-audiences:- publicparams: []result:name: infoschema:$ref: '#/components/schemas/ApiInfo'- name: admin.getUserssummary: Get all users (admin only)x-fern-audiences:- adminparams:- name: limitschema:type: integerdefault: 100result:name: usersschema:type: arrayitems:$ref: '#/components/schemas/User'
过滤方法参数
您可以过滤方法内的特定参数:
openrpc.yml
methods:- name: user.createsummary: Create a new userparams:- name: userDataschema:type: objectproperties:email:type: stringformat: emailname:type: stringadminNotes:type: stringx-fern-audiences:- admininternalId:type: stringx-fern-audiences:- internalrequired:- namerequired: trueresult:name: userschema:$ref: '#/components/schemas/User'
过滤模式
将整个模式过滤到不同的受众:
openrpc.yml
components:schemas:PublicUser:type: objectx-fern-audiences:- publicproperties:id:type: stringname:type: stringemail:type: stringrequired:- id- nameAdminUser:allOf:- $ref: '#/components/schemas/PublicUser'- type: objectx-fern-audiences:- adminproperties:role:type: stringpermissions:type: arrayitems:type: stringcreatedAt:type: stringformat: date-timelastLoginAt:type: stringformat: date-time
过滤模式属性
您可以过滤模式内的各个属性:
openrpc.yml
components:schemas:Order:type: objectproperties:id:type: stringamount:type: numberx-fern-audiences:- publicinternalCost:type: numberx-fern-audiences:- internaldebugInfo:type: objectx-fern-audiences:- debugcustomerInfo:type: objectproperties:id:type: stringemail:type: stringx-fern-audiences:- admin
过滤错误响应
根据受众过滤错误信息:
openrpc.yml
methods:- name: payment.processsummary: Process a paymentparams:- name: paymentDataschema:$ref: '#/components/schemas/PaymentRequest'errors:- code: -32001message: Payment failedx-fern-audiences:- publicdata:type: objectproperties:error:type: stringconst: "Payment could not be processed"- code: -32001message: Payment failedx-fern-audiences:- admindata:type: objectproperties:error:type: stringerrorCode:type: stringgatewayResponse:type: objectdebugTrace:type: string
服务器级别过滤
在服务器级别应用受众过滤:
openrpc.yml
servers:- name: public-apiurl: https://api.example.com/rpcx-fern-audiences:- publicdescription: Public API server- name: admin-apiurl: https://admin-api.example.com/rpcx-fern-audiences:- admindescription: Admin API server with additional privileges
条件方法可用性
使用受众使方法仅在特定上下文中可用:
openrpc.yml
methods:- name: debug.getSystemInfosummary: Get system debug informationdescription: Internal system information for debuggingx-fern-audiences:- debugparams: []result:name: systemInfoschema:type: objectadditionalProperties: true- name: beta.advancedSearchsummary: Advanced search functionalityx-fern-audiences:- betaparams:- name: queryschema:type: objectproperties:text:type: stringfilters:type: objectadditionalProperties: trueresult:name: resultsschema:$ref: '#/components/schemas/SearchResults'- name: internal.resetCachesummary: Reset internal cachesx-fern-audiences:- internalparams:- name: cacheTypeschema:type: stringenum: [user, product, session, all]# Notification - no result expected
这允许您为不同类型的消费者创建同一个 JSON-RPC API 的不同视图,确保每个受众只看到与其用例相关的方法和数据。