ABP 集成 nswag 到 VUE 项目, 自动生成操作类代码
2024-10-06 17:12:19
记录日期: 2019-9-22 23:12:39
原文链接:https://www.cnblogs.com/Qbit/p/11569906.html
集成记录:
npm install nswag --save-dev
复制 NSwag\src\NSwag.CodeGeneration.TypeScript\Templates 目录下的所有文件到 ts 项目的 nswag 目录下,nswag 源码下载困难,人肉拷贝吧 https://github.com/RicoSuter/NSwag/tree/master/src/NSwag.CodeGeneration.TypeScript/Templates
修改 service.config.nswag 中的 templateDirectory 为: "templateDirectory": "./Templates",
至此,可以随意调整复制过来的 liquid 模板,比如增加clientBaseClass 的引用 我的 完整配置如下:
{
"runtime": "Default",
"defaultVariables": null,
"documentGenerator": {
"fromDocument": {
"json": "",
"url": "http://localhost:21021/swagger/v1/swagger.json",
"output": null
}
},
"codeGenerators": {
"openApiToTypeScriptClient": {
"className": "{controller}ServiceProxy",
"moduleName": "",
"namespace": "",
"typeScriptVersion": 2.7,
"template": "Axios",
"promiseType": "Promise",
"httpClass": "HttpClient",
"useSingletonProvider": false,
"injectionTokenType": "InjectionToken",
"rxJsVersion": 6.0,
"dateTimeType": "Date",
"nullValue": "Null",
"generateClientClasses": true,
"generateClientInterfaces": false,
"generateOptionalParameters": false,
"exportTypes": true,
"wrapDtoExceptions": false,
"exceptionClass": "ApiException",
"clientBaseClass": "AbpServiceBase",
"wrapResponses": false,
"wrapResponseMethods": [],
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"protectedMethods": [],
"configurationClass": null,
"useTransformOptionsMethod": false,
"useTransformResultMethod": true,
"generateDtoTypes": true,
"operationGenerationMode": "MultipleClientsFromPathSegments",
"markOptionalProperties": false,
"generateCloneMethod": false,
"typeStyle": "Class",
"classTypes": [],
"extendedClasses": [],
"extensionCode": "",
"generateDefaultValues": true,
"excludedTypeNames": [],
"excludedParameterNames": [],
"handleReferences": true,
"generateConstructorInterface": false,
"convertConstructorInterfaceData": true,
"importRequiredTypes": true,
"useGetBaseUrlMethod": false,
"baseUrlTokenName": "API_BASE_URL",
"queryNullValue": "",
"inlineNamedDictionaries": false,
"inlineNamedAny": false,
"templateDirectory": "./Templates",
"typeNameGeneratorType": null,
"propertyNameGeneratorType": null,
"enumNameGeneratorType": null,
"serviceHost": null,
"serviceSchemes": null,
"output": "../src/api/abp-service-proxies.ts"
}
}
}
ABP 的swagger 必须使用这种方式获取data.result
修改 Client.ProcessResponse.ReadBodyStart.liquid
{% elseif Framework.IsAxios -%}
const _responseText = response.data.result;
或者你不去修改模板,使用 我的配置,然后继承这个类,去除注释代码
import { AxiosResponse } from "axios"; export class AbpServiceBase { protected transformResult(url: string, response: AxiosResponse, processor: (response: AxiosResponse) => Promise<any>): Promise<any> {
// if(response.data.result){
// response.data=response.data.result;
// }
console.log(response)
return processor(response);
}
}
修改 File.liquid 文件,在48行增加 :
import { AbpServiceBase } from './abp-service-base';
在项目的 packge.json scripts 中增加:
"genApi":"cd nswag &.\\refresh.bat"
这样可以不用每次输入代码执行生成
最新文章
- javascript 类型转换。
- spring4mvc返回json(bean,list,map)
- js实现页面跳转的几种方式
- junit高级篇(参数化、打包测试)-实例代码
- 网友对twisted deferr的理解
- Google Analytics SEO 实时 网站 访问量 统计
- 内核与内核模块:depmod,lsmod,modinfo,insmod,rmmod,mdprobe
- sql -以零作除数
- 写入和读取LOB类型的对象
- [置顶] SSO单点登录系列6:cas单点登录防止登出退出后刷新后退ticket失效报500错
- 4.java.lang.NumberFormatException(数据类型转换异常)
- ZenCoding Syntax
- CSS属性合写
- Java文件操作(IO流)
- css中单位 px、em 的区别【转载】
- 在Hadoop2.2基础上安装Spark(伪分布式)
- thinkphp3.2-更改控制器名后找不到相应的表?报1146的错
- angular2 学习笔记 ( Rxjs, Promise, Async/Await 的区别 )
- window.location各属性含义
- asp.net针对SQLSERVER数据库备份和恢复的一揽子问题解决