记录日期: 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
import { AxiosResponse } from "axios";
export class AbpServiceBase {
protected transformResult(url: string, response: AxiosResponse, processor: (response: AxiosResponse) => Promise): Promise {
// if(response.data.result){
// response.data=response.data.result;
// }
console.log(response)
return processor(response);
}
}
修改 File.liquid 文件,在48行增加 :
NSwag 中大量使用 启动新进程的方式调用代码,调试起来比较麻烦,在此记录
断点位置:NSwag.Commands.dll!NSwag.Commands.NSwagDocument.ExecuteCommandLineAsync.AnonymousMethod__0() 行 128
在 C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs(128)
> NSwag.Commands.dll!NSwag.Commands.NSwagDocument.StartCommandLineProcessAsync(string command) 行 258 C#
Console.Write("NSwag command line tool for .NET 4.6.1+ " + RuntimeUtilities.CurrentRuntime + ", ");
return new NSwagCommandProcessor(new ConsoleHost()).Process(args);
回到源代码,搜索:NSwag command line tool for .NET 4.6.1+
打开 NSwag.Console 项目,跟踪到 NSwagCommandProcessor 类的 Process(string[] args) 方法
跟踪到:OpenApiToCSharpControllerCommand.RunAsync() 加上断点
进入 C:\Program Files (x86)\Rico Suter\NSwagStudio\Win
Shift 右键打开powershell 输入 .\NSwag.exe run "C:\Users\hyzx8\source\repos\hyzx86\vue-typescript-admin-template\nswag\service.config.nswag"
先不要执行,此时使用vs附加到该 Powershell 的进程