什么是 OpenRPC 规范?
OpenRPC 规范是开发人员用来文档化 JSON-RPC API 的框架。该规范使用 JSON 或 YAML 编写,包含您的所有方法、参数、模式和服务器配置。Fern 兼容 OpenRPC 规范 v1.3.2 和 v1.2.6。
以下是 OpenRPC 文件的示例:
openrpc.yml
openrpc: 1.3.2info:title: Calculator APIversion: 1.0.0description: |A simple calculator API that performs basic arithmetic operationsusing JSON-RPC 2.0 protocol.servers:- name: productionurl: https://api.calculator.com/rpcdescription: Production JSON-RPC server- name: developmenturl: http://localhost:8080/rpcdescription: Development servermethods:- name: addsummary: Add two numbersdescription: Performs addition of two numeric valuesparams:- name: aschema:type: numberrequired: truedescription: First number to add- name: bschema:type: numberrequired: truedescription: Second number to addresult:name: sumschema:type: numberdescription: The sum of the two numbersexamples:- name: AddExampledescription: Example of adding two numbersparams:a: 5b: 3result: 8- name: dividesummary: Divide two numbersdescription: Performs division of two numeric valuesparams:- name: dividendschema:type: numberrequired: truedescription: Number to be divided- name: divisorschema:type: numberrequired: truedescription: Number to divide byresult:name: quotientschema:type: numberdescription: The result of the divisionerrors:- code: -32602message: Division by zerodata:type: objectproperties:error:type: stringconst: "Cannot divide by zero"examples:- name: DivideExampledescription: Example of dividing two numbersparams:dividend: 10divisor: 2result: 5- name: notify_calculationsummary: Notify about calculationdescription: Send a notification about a completed calculation (no response expected)params:- name: operationschema:type: stringenum: [add, subtract, multiply, divide]required: true- name: resultschema:type: numberrequired: true- name: timestampschema:type: stringformat: date-timerequired: truecomponents:schemas:CalculationRequest:type: objectproperties:operation:type: stringenum: [add, subtract, multiply, divide]operands:type: arrayitems:type: numberminItems: 2maxItems: 2precision:type: integerminimum: 0maximum: 10default: 2required:- operation- operandsCalculationResult:type: objectproperties:result:type: numberoperation:type: stringtimestamp:type: stringformat: date-timerequired:- result- operation- timestamp
设置您的 fern 文件夹
2
添加您的 OpenRPC 规范
将您的 OpenRPC 规范添加到 fern 目录中。您可以将其放在名为 openrpc 的子文件夹中,或直接放在 fern 目录中。
fern/└─ openrpc/└─ openrpc.yml
3
创建一个 fern.config.json 文件
在您的 fern 目录中添加一个 fern.config.json 文件,列出您的组织和当前版本的 Fern CLI:
fern.config.json
{"organization": "your-organization","version": "null"}
fern/├─ fern.config.json└─ openrpc/└─ openrpc.yml
4
创建一个 generators.yml 文件
在您的 fern 目录中创建一个 generators.yml 文件,并添加对您的 OpenRPC 规范的引用:
generators.yml
# Your API definitionapi:specs:- openrpc: ./openrpc/openrpc.ymlgroups:external:generators:# Your generator configurations here
您的最终目录结构:
fern/├─ fern.config.json├─ generators.yml└─ openrpc/└─ openrpc.yml