template-monorepo/generate/doc-generate/example/example_respdoc.api

96 lines
4.0 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

syntax = "v1"
info (
title: "演示 API" // 對應 swagger 的 title
description: "演示 api 生成 swagger 文件的 api 完整寫法" // 對應 swagger 的 description
version: "v1" // 對應 swagger 的 version
termsOfService: "https://github.com/zeromicro/go-zero" // 對應 swagger 的 termsOfService
contactName: "keson.an" // 對應 swagger 的 contactName
contactURL: "https://github.com/zeromicro/go-zero" // 對應 swagger 的 contactURL
contactEmail: "example@gmail.com" // 對應 swagger 的 contactEmail
licenseName: "MIT" // 對應 swagger 的 licenseName
licenseURL: "https://github.com/zeromicro/go-zero" // 對應 swagger 的 licenseURL
consumes: "application/json" // 對應 swagger 的 consumes不填預設為 application/json
produces: "application/json" // 對應 swagger 的 produces不填預設為 application/json
schemes: "http,https" // 對應 swagger 的 schemes不填預設為 https
host: "example.com" // 對應 swagger 的 host不填預設為 127.0.0.1
basePath: "/v1" // 對應 swagger 的 basePath不填預設為 /
wrapCodeMsg: true // 是否用 code-msg 通用回應體,如果開啟,則以格式 {"code":0,"msg":"OK","data":$data} 包括回應體
bizCodeEnumDescription: "1001-未登入<br>1002-無權限操作" // 全域業務錯誤碼列舉描述json 格式key 為業務錯誤碼value 為該錯誤碼的描述,僅當 wrapCodeMsg 為 true 時生效
// securityDefinitionsFromJson 為自訂鑑權配置json 內容將直接放入 swagger 的 securityDefinitions 中,
// 格式參考 https://swagger.io/specification/v2/#security-definitions-object
// 在 api 的 @server 中可宣告 authType 來指定其路由使用的鑑權類型
securityDefinitionsFromJson: `{"apiKey":{"description":"apiKey 類型鑑權自訂","type":"apiKey","name":"x-api-key","in":"header"}}`
useDefinitions: true// 開啟宣告將生成 models 進行關聯definitions 僅對回應體和 json 請求體生效
)
type (
QueryReq {
Id int `form:"id,range=[1:10000],example=10"`
Name string `form:"name,example=keson.an"`
Avatar string `form:"avatar,optional,example=https://example.com/avatar.png"`
}
QueryResp {
Id int `json:"id,example=10"`
Name string `json:"name,example=keson.an"`
}
// 錯誤響應
ErrorResponse {
Code string `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
// 具體錯誤類型
ValidationError {
Code string `json:"code"`
Message string `json:"message"`
Fields []string `json:"fields"`
}
InsufficientStockError {
Code string `json:"code"`
Message string `json:"message"`
ProductID string `json:"product_id"`
Required int `json:"required"`
Available int `json:"available"`
}
InvalidPaymentError {
Code string `json:"code"`
Message string `json:"message"`
Method string `json:"method"`
}
UnauthorizedError {
Code string `json:"code"`
Message string `json:"message"`
Reason string `json:"reason"`
}
)
@server (
tags: "query 演示" // 對應 swagger 的 tags可以對 swagger 中的 api 進行分組
summary: "query 類型介面集合" // 對應 swagger 的 summary
prefix: v1
authType: apiKey // 指定該路由使用的鑑權類型,值為 securityDefinitionsFromJson 中定義的名稱
group:"demo"
)
service Swagger {
@doc (
description: "query 接口"
)
/*
@respdoc-201 (QueryResp) // 創建成功
@respdoc-400 (
300101: (ValidationError) 參數驗證失敗
300102: (InsufficientStockError) 庫存不足
300103: (InvalidPaymentError) 支付方式無效
) // 客戶端錯誤
@respdoc-401 (UnauthorizedError) // 未授權
@respdoc-500 (ErrorResponse) // 服務器錯誤
*/
@handler query
get /query (QueryReq) returns (QueryResp)
}