记录日期: 2019-9-22 23:12:39
集成记录:
NPM install nswag --save-dev
复制 NSwag\src\NSwag.CodeGeneration.TypeScript\Templates 目录下的所有文件到 ts 项目的 nswag 目录下, nswag 源码下载困难, 人肉拷贝吧
修改 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": "SingleClientFromPathSegments",
- "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 {
- // transformResult(url_: string, _response: AxiosResponse<any>, arg2: (_response: AxiosResponse<any>) => Promise<any>): any {
- // }
- 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"
这样可以不用每次输入代码执行生成
调试记录: 特么的.. 跑不起来依赖版本要全部安装或者改别尝试了, 后续直接人肉跟踪吧, 跳过此节
NSwag 中大量使用 启动新进程的方式调用代码, 调试起来比较麻烦, 在此记录
断点位置: NSwag.Commands.dll!NSwag.Commands.NSwagDocument.ExecuteCommandLineAsync.AnonymousMethod__0() 行 128
在 C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs(128)
本机已安装 nswagStuido
断点位置启动进程信息:
路径:"C:\\Program Files (x86)\\Rico Suter\\NSwagStudio\\Win/nswag.exe"
GetArgumentsPrefix() 输出 ""
断点信息:
> NSwag.Commands.dll!NSwag.Commands.NSwagDocument.StartCommandLineProcessAsync(string command) 行 258 C#
参数: run "C:\Users\hyzx8\AppData\Local\Temp\nswag_document_1a466f99-7edb-4012-9450-30e5bc3d7133_config.json"
执行后输出 结果
使用 LSpy 打开:"C:\\Program Files (x86)\\Rico Suter\\NSwagStudio\\Win/nswag.exe
找到 main 函数
- 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 的进程
来源: http://www.bubuko.com/infodetail-3209224.html