Swagger API generator

Foreword

If the back-end API documentation is described by Swagger, a complete set of API codes can be generated through the following commands:

ng g ng-alain:sta --url=https://petstore3.swagger.io/api/v3/openapi.json 

Command format

ng g ng-alain:sta --name=<Swagger name> --url= --filePath= --output=
NameDefaultDescription
namestaName for swagger project name
url-URL to swagger schema, Choose one of filePath and url.
filePath-Path to swagger schema, Choose one of filePath and url.
outputsrc/app/${name}Path to folder where will been located the created api module
responseDataField-The real data field of Response
modelTypePrefix-Model name prefix
httpClientTypedelonHttpClient request method, 1. delon use _HttpClient of @delon/theme, 2. angular use HttpClient
generateApiOptions-swagger-typescript-api options

FAQ

Path and Service Association

By default, the first tags of each path will be merged into one Service. Please use [a-zA-Z][-_a-zA-Z]+ to describe tag as much as possible.

Unexpected name

By default, it will be processed according to the operationId item, otherwise it will be automatically combined according to the path and method. A few ways to turn on the languages:

.NET CORE

// Swashbuckle
services.AddSwaggerGen(c =>
{
  c.CustomOperationIds(e =>
  {
    var name = e.ActionDescriptor.RouteValues["action"] ?? "";
    return name[0].ToString().ToLower() + name.Substring(1);
  });
}

JAVA

Please refer to Configuring the output of operationId in a Swagger 2.0 spec.

Global Response

When all paths have a fixed output format, such as success and exceptions have a unified format, they all return:

{
   "status": 200,
   "error": "Error Message",
   "result": {}
}

If an interceptor is used to handle exception messages, when subscribing only needs to always get the data of the result field, it can be solved by specifying --responseDataField="result"