nswag如何自定义输出文件的模板
业务背景
一直以来,写接口都是从接口文档(apizz, yapi, postman等等)复制链接,参数,自己写ts模型,封装公共请求函数...这些操作完全是重复的...每次新增项目,新增接口都要手工操作一下...简直是浪费生命
这几天就在捣鼓swagger open api自动生成客户端请求文件及ts模型文件...看了好多插件,阿里的pont,swagger官方的生成工具..最后还是觉得nswag这个比较好用
现在就是通过nswag可视化工具,自动把swagger生成接口文档.
我的问题
自动生成的代码,在http响应里面,发生错误时是throw的一个异常
而我的编码习惯是...统一返回[err | null, res | null]这种响应体
要在哪以及如何修改模板呢???
英语水平有限,在官方文档上木有看到相关说明
下面截图这部分是自动生成的代码,我想自定义这一部分..
示例
swagger open api yaml文件
openapi: 3.0.0
# 服务器相关信息 array 可选
servers:
# Added by API Auto Mocking Plugin
- description: SwaggerHub API Auto Mocking
url: https://virtserver.swaggerhub.com/ranwawa/gift-admin/1.0.0
- description: 测试环境链接
url: https://e2e873a7-3cfe-4a2d-8c8c-a985b2e5c69c.bspapp.com/http/controller-admin
# 项目描述信息
info:
title: 接口文档-管理后台
description: 管理后台相关接口,用于供应商,分站,商品及全局状态的管理
termsOfService: https://terms-of-service.ranwawa.cn
contact:
name: 冉娃娃
url: https://www.ranwawa.com
email: 274544338@qq.com
license:
name: Apache 2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
version: "1.0.0"
# 分类信息
tags:
- name: supplier
description: 供应商相关接口
- name: agent
description: 分站代理相关接口
# 接口
paths:
/supplier-infos/getList:
get:
tags:
- supplier
summary: 查询所有供应商
operationId: getSupplierInfoList
description: 传入页码和页数即可
parameters:
- in: query
name: pageSize
description: 每页多少条数据
required: true
schema:
type: integer
format: int32
minimum: 5
maximum: 100
default: 10
- in: query
name: currentPage
description: 当前第几页
required: true
schema:
type: integer
format: int32
minimum: 1
default: 1
responses:
'200':
description: 查询成功
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/SupplierInfo'
/supplier-goods/getList:
get:
tags:
- supplier
summary: 查询供应商的所有商品
operationId: getSupplierGoodsList
parameters:
- in: query
name: supplierId
description: 供应商ID
schema:
type: string
responses:
'200':
description: 查询成功
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/SupplierGoodsInfo'
/agent-infos/getList:
get:
tags:
- agent
summary: 查询所有分站代理
operationId: getAgentInfoList
parameters:
- in: query
name: pageSize
description: 每页多少条数据
required: true
schema:
type: integer
format: int32
minimum: 5
maximum: 100
default: 10
- in: query
name: currentPage
description: 当前第几页
required: true
schema:
type: integer
format: int32
minimum: 1
default: 1
responses:
'200':
description: 查询成功
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/SupplierInfo'
# 模型
components:
schemas:
SupplierInfo:
description: 供应商信息
type: object
required: [_id, name, wechat, phone]
properties:
_id:
type: string
description: 供应商id
name:
type: string
description: 名称
wechat:
type: string
description: 微信号
phone:
type: string
description: 手机号
SupplierGoodsInfo:
description: 供应商商品信息
type: object
properties:
goodsId:
type: string
description: 供应商商品id
goodsName:
type: string
description: 商品名称
spId:
type: string
description: 供应商id
spName:
type: string
description: 供应商名称
storeId:
type: string
description: 仓库Id
storeName:
type: string
description: 仓库名称
goodsCostPrice:
type: number
description: 成本价
agentPriceNormal:
type: number
description: 普通分站进货价
agentPriceVip:
type: number
description: 高级分站进货价
sales:
type: integer
description: 销量
inventory:
type: integer
description: 库存
content:
type: string
description: 商品描述
自动生成的代码
/* tslint:disable */
/* eslint-disable */
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.8.2.0 (NJsonSchema v10.2.1.0 (Newtonsoft.Json v11.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming
import axios, {
AxiosError,
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
CancelToken
} from 'axios';
export class SupplierClient {
private instance: AxiosInstance;
private baseUrl: string;
protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;
constructor(baseUrl?: string, instance?: AxiosInstance) {
this.instance = instance ? instance : axios.create();
this.baseUrl = baseUrl !== undefined && baseUrl !== null ? baseUrl : "https://virtserver.swaggerhub.com/ranwawa/gift-admin/1.0.0";
}
/**
* 查询所有供应商
* @param pageSize 每页多少条数据
* @param currentPage 当前第几页
* @return 查询成功
*/
getSupplierInfoList(pageSize: number, currentPage: number, cancelToken?: CancelToken | undefined): Promise<SupplierInfo[]> {
let url_ = this.baseUrl + "/supplier-infos/getList?";
if (pageSize === undefined || pageSize === null)
throw new Error("The parameter 'pageSize' must be defined and cannot be null.");
else
url_ += "pageSize=" + encodeURIComponent("" + pageSize) + "&";
if (currentPage === undefined || currentPage === null)
throw new Error("The parameter 'currentPage' must be defined and cannot be null.");
else
url_ += "currentPage=" + encodeURIComponent("" + currentPage) + "&";
url_ = url_.replace(/[?&]$/, "");
let options_ = <AxiosRequestConfig>{
method: "GET",
url: url_,
headers: {
"Accept": "application/json"
},
cancelToken
};
return this.instance.request(options_).catch((_error: any) => {
if (isAxiosError(_error) && _error.response) {
return _error.response;
} else {
throw _error;
} }).then((_response: AxiosResponse) => {
return this.processGetSupplierInfoList(_response);
}); }
protected processGetSupplierInfoList(response: AxiosResponse): Promise<SupplierInfo[]> {
const status = response.status;
let _headers: any = {};
if (response.headers && typeof response.headers === "object") {
for (let k in response.headers) {
if (response.headers.hasOwnProperty(k)) {
_headers[k] = response.headers[k];
} } } if (status === 200) {
const _responseText = response.data;
let result200: any = null;
let resultData200 = _responseText;
console.log(_responseText);
if (Array.isArray(resultData200)) {
result200 = [] as any;
for (let item of resultData200)
result200!.push(SupplierInfo.fromJS(item));
} return result200;
} else if (status !== 200 && status !== 204) {
const _responseText = response.data;
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
} return Promise.resolve<SupplierInfo[]>(<any>null);
}
/**
* 查询供应商的所有商品
* @param supplierId (optional) 供应商ID
* @return 查询成功
*/
getSupplierGoodsList(supplierId: string | undefined, cancelToken?: CancelToken | undefined): Promise<SupplierGoodsInfo[]> {
let url_ = this.baseUrl + "/supplier-goods/getList?";
if (supplierId === null)
throw new Error("The parameter 'supplierId' cannot be null.");
else if (supplierId !== undefined)
url_ += "supplierId=" + encodeURIComponent("" + supplierId) + "&";
url_ = url_.replace(/[?&]$/, "");
let options_ = <AxiosRequestConfig>{
method: "GET",
url: url_,
headers: {
"Accept": "application/json"
},
cancelToken
};
return this.instance.request(options_).catch((_error: any) => {
if (isAxiosError(_error) && _error.response) {
return _error.response;
} else {
throw _error;
} }).then((_response: AxiosResponse) => {
return this.processGetSupplierGoodsList(_response);
}); }
protected processGetSupplierGoodsList(response: AxiosResponse): Promise<SupplierGoodsInfo[]> {
const status = response.status;
let _headers: any = {};
if (response.headers && typeof response.headers === "object") {
for (let k in response.headers) {
if (response.headers.hasOwnProperty(k)) {
_headers[k] = response.headers[k];
} } } if (status === 200) {
const _responseText = response.data;
let result200: any = null;
let resultData200 = _responseText;
if (Array.isArray(resultData200)) {
result200 = [] as any;
for (let item of resultData200)
result200!.push(SupplierGoodsInfo.fromJS(item));
} return result200;
} else if (status !== 200 && status !== 204) {
const _responseText = response.data;
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
} return Promise.resolve<SupplierGoodsInfo[]>(<any>null);
}}
/** 供应商信息 */
export class SupplierInfo implements ISupplierInfo {
/** 供应商id */
_id!: string;
/** 名称 */
name!: string;
/** 微信号 */
wechat!: string;
/** 手机号 */
phone!: string;
constructor(data?: ISupplierInfo) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
} } }
init(_data?: any) {
if (_data) {
this._id = _data["_id"];
this.name = _data["name"];
this.wechat = _data["wechat"];
this.phone = _data["phone"];
} }
static fromJS(data: any): SupplierInfo {
data = typeof data === 'object' ? data : {};
let result = new SupplierInfo();
result.init(data);
return result;
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
data["_id"] = this._id;
data["name"] = this.name;
data["wechat"] = this.wechat;
data["phone"] = this.phone;
return data;
}}
/** 供应商信息 */
export interface ISupplierInfo {
/** 供应商id */
_id: string;
/** 名称 */
name: string;
/** 微信号 */
wechat: string;
/** 手机号 */
phone: string;
}
/** 供应商商品信息 */
export class SupplierGoodsInfo implements ISupplierGoodsInfo {
/** 供应商商品id */
goodsId?: string;
/** 商品名称 */
goodsName?: string;
/** 供应商id */
spId?: string;
/** 供应商名称 */
spName?: string;
/** 仓库Id */
storeId?: string;
/** 仓库名称 */
storeName?: string;
/** 成本价 */
goodsCostPrice?: number;
/** 普通分站进货价 */
agentPriceNormal?: number;
/** 高级分站进货价 */
agentPriceVip?: number;
/** 销量 */
sales?: number;
/** 库存 */
inventory?: number;
/** 商品描述 */
content?: string;
constructor(data?: ISupplierGoodsInfo) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
} } }
init(_data?: any) {
if (_data) {
this.goodsId = _data["goodsId"];
this.goodsName = _data["goodsName"];
this.spId = _data["spId"];
this.spName = _data["spName"];
this.storeId = _data["storeId"];
this.storeName = _data["storeName"];
this.goodsCostPrice = _data["goodsCostPrice"];
this.agentPriceNormal = _data["agentPriceNormal"];
this.agentPriceVip = _data["agentPriceVip"];
this.sales = _data["sales"];
this.inventory = _data["inventory"];
this.content = _data["content"];
} }
static fromJS(data: any): SupplierGoodsInfo {
data = typeof data === 'object' ? data : {};
let result = new SupplierGoodsInfo();
result.init(data);
return result;
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
data["goodsId"] = this.goodsId;
data["goodsName"] = this.goodsName;
data["spId"] = this.spId;
data["spName"] = this.spName;
data["storeId"] = this.storeId;
data["storeName"] = this.storeName;
data["goodsCostPrice"] = this.goodsCostPrice;
data["agentPriceNormal"] = this.agentPriceNormal;
data["agentPriceVip"] = this.agentPriceVip;
data["sales"] = this.sales;
data["inventory"] = this.inventory;
data["content"] = this.content;
return data;
}}
/** 供应商商品信息 */
export interface ISupplierGoodsInfo {
/** 供应商商品id */
goodsId?: string;
/** 商品名称 */
goodsName?: string;
/** 供应商id */
spId?: string;
/** 供应商名称 */
spName?: string;
/** 仓库Id */
storeId?: string;
/** 仓库名称 */
storeName?: string;
/** 成本价 */
goodsCostPrice?: number;
/** 普通分站进货价 */
agentPriceNormal?: number;
/** 高级分站进货价 */
agentPriceVip?: number;
/** 销量 */
sales?: number;
/** 库存 */
inventory?: number;
/** 商品描述 */
content?: string;
}
export class ApiException extends Error {
message: string;
status: number;
response: string;
headers: { [key: string]: any; };
result: any;
constructor(message: string, status: number, response: string, headers: { [key: string]: any; }, result: any) {
super();
this.message = message;
this.status = status;
this.response = response;
this.headers = headers;
this.result = result;
}
protected isApiException = true;
static isApiException(obj: any): obj is ApiException {
return obj.isApiException === true;
}}
function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
if (result !== null && result !== undefined)
throw result;
else
throw new ApiException(message, status, response, headers, null);
}
function isAxiosError(obj: any | undefined): obj is AxiosError {
return obj && obj.isAxiosError === true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论