Integrated automatic ABP nswag class code generating operation according Swagger API

Record Date: 2019-9-22 23:12:39

Original link: https://www.cnblogs.com/Qbit/p/11569906.html

 

Integrated records:

 

npm install nswag --save-dev

 

Copy   NSwag \ src \ NSwag.CodeGeneration.TypeScript \ Templates all the files in the directory to the directory under nswag ts project, nswag difficult to download the source code, copy it human flesh  https://github.com/RicoSuter/NSwag/tree/master/src /NSwag.CodeGeneration.TypeScript/Templates

Modify service.config.nswag in templateDirectory as: "templateDirectory": "./Templates",

At this point, you can freely adjust the liquid copied template, such as adding a reference clientBaseClass of my complete configuration is as follows:

{
  "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's swagger in this way must be used to obtain data.result

Modify Client.ProcessResponse.ReadBodyStart.liquid 

{% elseif Framework.IsAxios -%}
const _responseText = response.data.result;    
 
Or you do not modify the template, use my configuration, and then inherit this class
 
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);
    }
}

  

Modify File.liquid file, add the line in 48:

import { AbpServiceBase } from './abp-service-base';
 
Increase in packge.json scripts of the project:
  "genApi":"cd nswag &.\\refresh.bat"
 
This code is executed without generating each entry
 

 

 

 
 
Debug log:  special Mody. . Not up and running dependent version to install all or do not try to change, a direct follow-up human flesh tracking it, skip this section

NSwag extensive use of the new process started way calling code, debugging up more trouble in this record

Breakpoint location:! NSwag.Commands.dll NSwag.Commands.NSwagDocument.ExecuteCommandLineAsync.AnonymousMethod__0 () line 128
in the C: \ projects \ nswag \ src \ NSwag.Commands \ NSwagDocument.cs (128)

The machine has been installed nswagStuido 
Breakpoint location information to start the process:
Path: "C: \\ Program Files (x86) \\ Rico Suter \\ NSwagStudio \\ Win / nswag.exe"
GetArgumentsPrefix () outputs ""
 
Breakpoint information:

> 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"
After performing output
 
 
Use LSpy open: "C: \\ Program Files (x86) \\ Rico Suter \\ NSwagStudio \\ Win / nswag.exe
Find the main function
	Console.Write("NSwag command line tool for .NET 4.6.1+ " + RuntimeUtilities.CurrentRuntime + ", ");
	return new NSwagCommandProcessor(new ConsoleHost()).Process(args);

 Back to the source code search: NSwag command line tool for .NET 4.6.1+

Open NSwag.Console project tracked Process NSwagCommandProcessor class (string [] args) Method

Tracking to: OpenApiToCSharpControllerCommand.RunAsync () plus break

 

 

 

Enter C: \ Program Files (x86) \ Rico Suter \ NSwagStudio \ Win  

. Shift Right Open powershell input \ NSwag.exe run "C: \ Users \ hyzx8 \ source \ repos \ hyzx86 \ vue-typescript-admin-template \ nswag \ service.config.nswag"

 

Do not execute, this time using vs attach to the process of the Powershell

 

 

 

 

 
 

 

Guess you like

Origin www.cnblogs.com/Qbit/p/11569906.html