swagger-express-ts
Advanced tools
Comparing version 1.0.0-rc.3 to 1.0.0-rc.4
@@ -0,1 +1,27 @@ | ||
<a name="1.0.0-rc.4"></a> | ||
# [1.0.0-rc.4](2018-05-18) | ||
## Features | ||
### Add global responses | ||
Example: | ||
```ts | ||
swagger.express({ | ||
definition: { | ||
... | ||
responses: { | ||
500: {} | ||
}, | ||
... | ||
} | ||
}) | ||
``` | ||
## Fixes | ||
### Fix ApiModel when arg "name" and class name are equal. | ||
<a name="1.0.0-rc.3"></a> | ||
@@ -2,0 +28,0 @@ |
import { ISwaggerInfo, ISwaggerExternalDocs } from "./i-swagger"; | ||
import { IApiOperationArgsBaseResponse } from "./i-api-operation-args.base"; | ||
export interface ISwaggerBuildDefinitionModelProperty { | ||
@@ -115,3 +116,10 @@ /** | ||
}; | ||
/** | ||
* Define global responses. | ||
* Optional. | ||
*/ | ||
responses?: { | ||
[key: string]: IApiOperationArgsBaseResponse; | ||
}; | ||
} | ||
export declare function build(buildDefinition: ISwaggerBuildDefinition): void; |
@@ -38,4 +38,7 @@ "use strict"; | ||
} | ||
if (buildDefinition.responses) { | ||
swagger_service_1.SwaggerService.getInstance().setGlobalResponses(buildDefinition.responses); | ||
} | ||
swagger_service_1.SwaggerService.getInstance().buildSwagger(); | ||
} | ||
exports.build = build; |
import { ISwagger, ISwaggerInfo, ISwaggerExternalDocs } from "./i-swagger"; | ||
import { IApiPathArgs } from "./api-path.decorator"; | ||
import { IApiOperationPostArgs } from "./api-operation-post.decorator"; | ||
import { IApiOperationArgsBaseResponse } from "./i-api-operation-args.base"; | ||
import { IApiOperationGetArgs } from "./api-operation-get.decorator"; | ||
@@ -14,2 +15,3 @@ import { IApiModelPropertyArgs } from "./api-model-property.decorator"; | ||
private modelsMap; | ||
private globalResponses; | ||
private constructeur(); | ||
@@ -31,2 +33,5 @@ static getInstance(): SwaggerService; | ||
setExternalDocs(externalDocs: ISwaggerExternalDocs): void; | ||
setGlobalResponses(globalResponses: { | ||
[key: string]: IApiOperationArgsBaseResponse; | ||
}): void; | ||
addPath(args: IApiPathArgs, target: any): void; | ||
@@ -43,2 +48,3 @@ addOperationGet(args: IApiOperationGetArgs, target: any, propertyKey: string | symbol): void; | ||
private buildOperation(args, target, propertyKey); | ||
private buildOperationResponses(responses); | ||
private buildOperationSecurity(argsSecurity); | ||
@@ -45,0 +51,0 @@ private buildParameters(type, parameters); |
@@ -112,2 +112,5 @@ "use strict"; | ||
}; | ||
SwaggerService.prototype.setGlobalResponses = function (globalResponses) { | ||
this.globalResponses = this.buildOperationResponses(globalResponses); | ||
}; | ||
SwaggerService.prototype.addPath = function (args, target) { | ||
@@ -257,74 +260,3 @@ var currentController = { | ||
if (args.responses) { | ||
operation.responses = {}; | ||
for (var responseIndex in args.responses) { | ||
var response = args.responses[responseIndex]; | ||
var newSwaggerOperationResponse = {}; | ||
if (response.description) { | ||
newSwaggerOperationResponse.description = response.description; | ||
} | ||
else { | ||
switch (responseIndex) { | ||
case "200": | ||
newSwaggerOperationResponse.description = "Success"; | ||
break; | ||
case "201": | ||
newSwaggerOperationResponse.description = "Created"; | ||
break; | ||
case "202": | ||
newSwaggerOperationResponse.description = "Accepted"; | ||
break; | ||
case "203": | ||
newSwaggerOperationResponse.description = "Non-Authoritative Information"; | ||
break; | ||
case "204": | ||
newSwaggerOperationResponse.description = "No Content"; | ||
break; | ||
case "205": | ||
newSwaggerOperationResponse.description = "Reset Content"; | ||
break; | ||
case "206": | ||
newSwaggerOperationResponse.description = "Partial Content"; | ||
break; | ||
case "400": | ||
newSwaggerOperationResponse.description = "Client error and Bad Request"; | ||
break; | ||
case "401": | ||
newSwaggerOperationResponse.description = "Client error and Unauthorized"; | ||
break; | ||
case "404": | ||
newSwaggerOperationResponse.description = "Client error and Not Found"; | ||
break; | ||
case "406": | ||
newSwaggerOperationResponse.description = "Client error and Not Acceptable"; | ||
break; | ||
case "500": | ||
newSwaggerOperationResponse.description = "Internal Server Error"; | ||
break; | ||
case "501": | ||
newSwaggerOperationResponse.description = "Not Implemented"; | ||
break; | ||
case "503": | ||
newSwaggerOperationResponse.description = "Service Unavailable"; | ||
break; | ||
default: | ||
newSwaggerOperationResponse.description = null; | ||
} | ||
} | ||
if (response.model) { | ||
var ref = this.buildRef(response.model); | ||
var newSwaggerOperationResponseSchema = { | ||
$ref: ref | ||
}; | ||
if (_.isEqual(response.type, swagger_definition_constant_1.SwaggerDefinitionConstant.Response.Type.ARRAY)) { | ||
newSwaggerOperationResponseSchema = { | ||
items: { | ||
$ref: ref | ||
}, | ||
type: swagger_definition_constant_1.SwaggerDefinitionConstant.Response.Type.ARRAY | ||
}; | ||
} | ||
newSwaggerOperationResponse.schema = newSwaggerOperationResponseSchema; | ||
} | ||
operation.responses[responseIndex] = newSwaggerOperationResponse; | ||
} | ||
operation.responses = this.buildOperationResponses(args.responses); | ||
} | ||
@@ -336,2 +268,77 @@ if (args.security) { | ||
}; | ||
SwaggerService.prototype.buildOperationResponses = function (responses) { | ||
var swaggerOperationResponses = {}; | ||
for (var responseIndex in responses) { | ||
var response = responses[responseIndex]; | ||
var newSwaggerOperationResponse = {}; | ||
if (response.description) { | ||
newSwaggerOperationResponse.description = response.description; | ||
} | ||
else { | ||
switch (responseIndex) { | ||
case "200": | ||
newSwaggerOperationResponse.description = "Success"; | ||
break; | ||
case "201": | ||
newSwaggerOperationResponse.description = "Created"; | ||
break; | ||
case "202": | ||
newSwaggerOperationResponse.description = "Accepted"; | ||
break; | ||
case "203": | ||
newSwaggerOperationResponse.description = "Non-Authoritative Information"; | ||
break; | ||
case "204": | ||
newSwaggerOperationResponse.description = "No Content"; | ||
break; | ||
case "205": | ||
newSwaggerOperationResponse.description = "Reset Content"; | ||
break; | ||
case "206": | ||
newSwaggerOperationResponse.description = "Partial Content"; | ||
break; | ||
case "400": | ||
newSwaggerOperationResponse.description = "Client error and Bad Request"; | ||
break; | ||
case "401": | ||
newSwaggerOperationResponse.description = "Client error and Unauthorized"; | ||
break; | ||
case "404": | ||
newSwaggerOperationResponse.description = "Client error and Not Found"; | ||
break; | ||
case "406": | ||
newSwaggerOperationResponse.description = "Client error and Not Acceptable"; | ||
break; | ||
case "500": | ||
newSwaggerOperationResponse.description = "Internal Server Error"; | ||
break; | ||
case "501": | ||
newSwaggerOperationResponse.description = "Not Implemented"; | ||
break; | ||
case "503": | ||
newSwaggerOperationResponse.description = "Service Unavailable"; | ||
break; | ||
default: | ||
newSwaggerOperationResponse.description = null; | ||
} | ||
} | ||
if (response.model) { | ||
var ref = this.buildRef(response.model); | ||
var newSwaggerOperationResponseSchema = { | ||
$ref: ref | ||
}; | ||
if (_.isEqual(response.type, swagger_definition_constant_1.SwaggerDefinitionConstant.Response.Type.ARRAY)) { | ||
newSwaggerOperationResponseSchema = { | ||
items: { | ||
$ref: ref | ||
}, | ||
type: swagger_definition_constant_1.SwaggerDefinitionConstant.Response.Type.ARRAY | ||
}; | ||
} | ||
newSwaggerOperationResponse.schema = newSwaggerOperationResponseSchema; | ||
} | ||
swaggerOperationResponses[responseIndex] = newSwaggerOperationResponse; | ||
} | ||
return swaggerOperationResponses; | ||
}; | ||
SwaggerService.prototype.buildOperationSecurity = function (argsSecurity) { | ||
@@ -411,3 +418,3 @@ var securityToReturn = []; | ||
data.tags.push({ | ||
name: _.capitalize(controller.name), | ||
name: _.upperFirst(controller.name), | ||
description: controller.description | ||
@@ -431,7 +438,10 @@ }); | ||
} | ||
operation.tags = [_.capitalize(controller.name)]; | ||
if (this.globalResponses) { | ||
operation.responses = _.mergeWith(_.cloneDeep(this.globalResponses), operation.responses); | ||
} | ||
operation.tags = [_.upperFirst(controller.name)]; | ||
return operation; | ||
}; | ||
SwaggerService.prototype.buildRef = function (definition) { | ||
return "#/definitions/".concat(_.capitalize(definition)); | ||
return "#/definitions/".concat(_.upperFirst(definition)); | ||
}; | ||
@@ -471,5 +481,8 @@ SwaggerService.prototype.addApiModelProperty = function (args, target, propertyKey, propertyType) { | ||
if (args.name) { | ||
this.modelsMap[_.capitalize(args.name)] = _.clone(this.modelsMap[definitionKey]); | ||
delete this.modelsMap[definitionKey]; | ||
delete this.data.definitions[definitionKey]; | ||
var name_1 = _.upperFirst(args.name); | ||
this.modelsMap[name_1] = _.cloneDeep(this.modelsMap[definitionKey]); | ||
if (!_.isEqual(name_1, definitionKey)) { | ||
delete this.modelsMap[definitionKey]; | ||
delete this.data.definitions[definitionKey]; | ||
} | ||
} | ||
@@ -476,0 +489,0 @@ } |
{ | ||
"name": "swagger-express-ts", | ||
"version": "1.0.0-rc.3", | ||
"version": "1.0.0-rc.4", | ||
"description": "Generate and serve swagger.json", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
# IApiOperationArgsBaseResponse | ||
## description: string | ||
Define description of response. | ||
- Optional | ||
## isArray: boolean | ||
Define if response is array. | ||
- Optional | ||
* Optional | ||
## type: string | ||
Define type of response. | ||
* Optional | ||
## model: string | ||
Define model reference | ||
- Optional | ||
- If you want specify Array of model, you must set type with [SwaggerDefinitionConstant](./swagger-definition-constant.md).Definition.Property.Type.ARRAY | ||
* Optional | ||
* If you want specify Array of model, you must set type with [SwaggerDefinitionConstant](./swagger-definition-constant.md).Definition.Property.Type.ARRAY |
@@ -1,45 +0,69 @@ | ||
# ISwaggerBuildDefinition | ||
## setBasePath: string | ||
Define base URL for all API. | ||
- Optional. | ||
- Default is "/" | ||
* Optional. | ||
* Default is "/" | ||
## setOpenapi: string | ||
Define version of OpenAPI. | ||
- Optional. | ||
* Optional. | ||
## setInfo : [SwaggerInfo](./i-swagger-setInfo.md) | ||
Define setInfo. | ||
## setConsumes: string[] | ||
Define the MIME types supported by the API for setConsumes. The root-level definition can be overridden in individual operations. | ||
- Optional | ||
- Default is [SwaggerDefinition](./swagger-definition-constant.md).Consume.JSON or "application/json". | ||
* Optional | ||
* Default is [SwaggerDefinition](./swagger-definition-constant.md).Consume.JSON or "application/json". | ||
## setProduces: string[] | ||
Define the MIME types supported by the API for setProduces. The root-level definition can be overridden in individual operations. | ||
- Optional | ||
- Default is [SwaggerDefinition](./swagger-definition-constant.md).Consume.JSON or "application/json". | ||
* Optional | ||
* Default is [SwaggerDefinition](./swagger-definition-constant.md).Consume.JSON or "application/json". | ||
## setSchemes: string[] | ||
Define Schemes. | ||
- Optional | ||
- Default is [SwaggerDefinition](./swagger-definition-constant.md).Scheme.HTTP = "http" | ||
* Optional | ||
* Default is [SwaggerDefinition](./swagger-definition-constant.md).Scheme.HTTP = "http" | ||
## setHost: string | ||
Define setHost. | ||
- Optional | ||
* Optional | ||
## models: {[key: string]: [ISwaggerBuildDefinitionModel](./i-swagger-build-definition-model.md)} | ||
Define all model. | ||
- Required | ||
* Required | ||
## externalDocs: [ISwaggerExternalDocs](./i-swagger-external-docs.md) | ||
Define external docs | ||
- Optional | ||
* Optional | ||
## securityDefinitions: {[key: string]: [ISwaggerSecurityDefinition](./i-swagger-security-definition.md)} | ||
Define security definitions | ||
- Optional | ||
* Optional | ||
## responses: {[key: string]: [IApiOperationArgsBaseResponse](./i-api-operation-args-base-response.md)} | ||
Define global responses | ||
* Optional |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
515188
10898