swagger-express-ts
Advanced tools
Comparing version 1.0.0-alpha.4 to 1.0.0-alpha.5
@@ -0,1 +1,46 @@ | ||
<a name="1.0.0-alpha.5"></a> | ||
# [1.0.0-alpha.5] (2018-02-18) | ||
## Features | ||
### externalDocs | ||
Example: | ||
```ts | ||
app.use( swagger.express( | ||
{ | ||
definition : { | ||
info : { | ||
title : "My api" , | ||
version : "1.0" | ||
} , | ||
models : { | ||
Version : { | ||
properties : { | ||
id : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING , | ||
required : true | ||
} , | ||
name : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING , | ||
required : true | ||
} , | ||
description : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING | ||
} , | ||
version : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING | ||
} | ||
} | ||
} | ||
} , | ||
externalDocs : { | ||
url : "My url" | ||
} | ||
} | ||
} | ||
) ); | ||
``` | ||
<a name="1.0.0-alpha.4"></a> | ||
@@ -34,3 +79,3 @@ # [1.0.0-alpha.4] (2018-01-19) | ||
definition : { | ||
info : { | ||
setInfo : { | ||
title : "My api", | ||
@@ -37,0 +82,0 @@ version : "1.0" |
@@ -6,5 +6,5 @@ "use strict"; | ||
return function (target, propertyKey, descriptor) { | ||
swagger_service_1.SwaggerService.addOperationDelete(args, target, propertyKey); | ||
swagger_service_1.SwaggerService.getInstance().addOperationDelete(args, target, propertyKey); | ||
}; | ||
} | ||
exports.ApiOperationDelete = ApiOperationDelete; |
@@ -6,5 +6,5 @@ "use strict"; | ||
return function (target, propertyKey, descriptor) { | ||
swagger_service_1.SwaggerService.addOperationGet(args, target, propertyKey); | ||
swagger_service_1.SwaggerService.getInstance().addOperationGet(args, target, propertyKey); | ||
}; | ||
} | ||
exports.ApiOperationGet = ApiOperationGet; |
@@ -6,5 +6,5 @@ "use strict"; | ||
return function (target, propertyKey, descriptor) { | ||
swagger_service_1.SwaggerService.addOperationPatch(args, target, propertyKey); | ||
swagger_service_1.SwaggerService.getInstance().addOperationPatch(args, target, propertyKey); | ||
}; | ||
} | ||
exports.ApiOperationPatch = ApiOperationPatch; |
@@ -6,5 +6,5 @@ "use strict"; | ||
return function (target, propertyKey, descriptor) { | ||
swagger_service_1.SwaggerService.addOperationPost(args, target, propertyKey); | ||
swagger_service_1.SwaggerService.getInstance().addOperationPost(args, target, propertyKey); | ||
}; | ||
} | ||
exports.ApiOperationPost = ApiOperationPost; |
@@ -6,5 +6,5 @@ "use strict"; | ||
return function (target, propertyKey, descriptor) { | ||
swagger_service_1.SwaggerService.addOperationPut(args, target, propertyKey); | ||
swagger_service_1.SwaggerService.getInstance().addOperationPut(args, target, propertyKey); | ||
}; | ||
} | ||
exports.ApiOperationPut = ApiOperationPut; |
@@ -6,5 +6,5 @@ "use strict"; | ||
return function (target) { | ||
swagger_service_1.SwaggerService.addPath(args, target); | ||
swagger_service_1.SwaggerService.getInstance().addPath(args, target); | ||
}; | ||
} | ||
exports.ApiPath = ApiPath; |
@@ -25,5 +25,6 @@ "use strict"; | ||
router.get(path, function (request, response, next) { | ||
response.json(swagger_service_1.SwaggerService.getData()); | ||
var data = swagger_service_1.SwaggerService.getInstance().getData(); | ||
response.json(data); | ||
}); | ||
return router; | ||
} |
@@ -94,3 +94,3 @@ export interface ISwaggerLicense { | ||
basePath?: string; | ||
openapi: string; | ||
openapi?: string; | ||
info: ISwaggerInfo; | ||
@@ -110,2 +110,3 @@ servers?: [ISwaggerServer]; | ||
}; | ||
externalDocs?: ISwaggerExternalDocs; | ||
} |
export { IApiPathArgs, ApiPath } from "./api-path.decorator"; | ||
export { IApiOperationGetArgs, ApiOperationGet } from "./api-operation-get.decorator"; | ||
export { IApiOperationPostArgs, ApiOperationPost } from "./api-operation-post.decorator"; | ||
export { IApiOperationPutArgs, ApiOperationPut } from "./api-operation-put.decorator"; | ||
export { IApiOperationPatchArgs, ApiOperationPatch } from "./api-operation-patch.decorator"; | ||
export { IApiOperationDeleteArgs, ApiOperationDelete } from "./api-operation-delete.decorator"; | ||
export { SwaggerDefinitionConstant } from "./swagger-definition.constant"; | ||
export { express, ISwaggerExpressOptions } from "./express.configurator"; | ||
export { build } from "./swagger.builder"; |
@@ -9,2 +9,8 @@ "use strict"; | ||
exports.ApiOperationPost = api_operation_post_decorator_1.ApiOperationPost; | ||
var api_operation_put_decorator_1 = require("./api-operation-put.decorator"); | ||
exports.ApiOperationPut = api_operation_put_decorator_1.ApiOperationPut; | ||
var api_operation_patch_decorator_1 = require("./api-operation-patch.decorator"); | ||
exports.ApiOperationPatch = api_operation_patch_decorator_1.ApiOperationPatch; | ||
var api_operation_delete_decorator_1 = require("./api-operation-delete.decorator"); | ||
exports.ApiOperationDelete = api_operation_delete_decorator_1.ApiOperationDelete; | ||
var swagger_definition_constant_1 = require("./swagger-definition.constant"); | ||
@@ -11,0 +17,0 @@ exports.SwaggerDefinitionConstant = swagger_definition_constant_1.SwaggerDefinitionConstant; |
export declare class SwaggerDefinitionConstant { | ||
private static JSON; | ||
private static XML; | ||
private static OBJECT; | ||
private static INTEGER; | ||
private static STRING; | ||
private static ARRAY; | ||
static Produce: { | ||
@@ -16,3 +20,3 @@ XML: string; | ||
}; | ||
static Definition: { | ||
static Model: { | ||
Type: { | ||
@@ -19,0 +23,0 @@ OBJECT: string; |
@@ -8,2 +8,6 @@ "use strict"; | ||
SwaggerDefinitionConstant.XML = "application/xml"; | ||
SwaggerDefinitionConstant.OBJECT = "object"; | ||
SwaggerDefinitionConstant.INTEGER = "integer"; | ||
SwaggerDefinitionConstant.STRING = "string"; | ||
SwaggerDefinitionConstant.ARRAY = "array"; | ||
SwaggerDefinitionConstant.Produce = { | ||
@@ -21,10 +25,10 @@ XML: SwaggerDefinitionConstant.XML, | ||
}; | ||
SwaggerDefinitionConstant.Definition = { | ||
SwaggerDefinitionConstant.Model = { | ||
Type: { | ||
OBJECT: "object" | ||
OBJECT: SwaggerDefinitionConstant.OBJECT | ||
}, | ||
Property: { | ||
Type: { | ||
INTEGER: "integer", | ||
STRING: "string" | ||
INTEGER: SwaggerDefinitionConstant.INTEGER, | ||
STRING: SwaggerDefinitionConstant.STRING | ||
}, | ||
@@ -38,5 +42,5 @@ Format: { | ||
Type: { | ||
INTEGER: "integer", | ||
STRING: "string", | ||
ARRAY: "array" | ||
INTEGER: SwaggerDefinitionConstant.INTEGER, | ||
STRING: SwaggerDefinitionConstant.STRING, | ||
ARRAY: SwaggerDefinitionConstant.ARRAY | ||
}, | ||
@@ -52,5 +56,5 @@ In: { | ||
Type: { | ||
INTEGER: "integer", | ||
STRING: "string", | ||
ARRAY: "array" | ||
INTEGER: SwaggerDefinitionConstant.INTEGER, | ||
STRING: SwaggerDefinitionConstant.STRING, | ||
ARRAY: SwaggerDefinitionConstant.ARRAY | ||
} | ||
@@ -57,0 +61,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { ISwaggerInfo } from "./i-swagger"; | ||
import { ISwaggerInfo, ISwaggerExternalDocs } from "./i-swagger"; | ||
export interface ISwaggerBuildDefinitionModelProperty { | ||
@@ -68,3 +68,8 @@ /** | ||
}; | ||
/** | ||
* Define external doc | ||
* Optional. | ||
*/ | ||
externalDocs?: ISwaggerExternalDocs; | ||
} | ||
export declare function build(buildDefinition: ISwaggerBuildDefinition): void; |
@@ -10,22 +10,25 @@ "use strict"; | ||
if (buildDefinition.basePath) { | ||
swagger_service_1.SwaggerService.setBasePath(buildDefinition.basePath); | ||
swagger_service_1.SwaggerService.getInstance().setBasePath(buildDefinition.basePath); | ||
} | ||
if (buildDefinition.openapi) { | ||
swagger_service_1.SwaggerService.setOpenapi(buildDefinition.openapi); | ||
swagger_service_1.SwaggerService.getInstance().setOpenapi(buildDefinition.openapi); | ||
} | ||
if (buildDefinition.info) { | ||
swagger_service_1.SwaggerService.setInfo(buildDefinition.info); | ||
swagger_service_1.SwaggerService.getInstance().setInfo(buildDefinition.info); | ||
} | ||
if (buildDefinition.schemes) { | ||
swagger_service_1.SwaggerService.setSchemes(buildDefinition.schemes); | ||
swagger_service_1.SwaggerService.getInstance().setSchemes(buildDefinition.schemes); | ||
} | ||
if (buildDefinition.produces) { | ||
swagger_service_1.SwaggerService.setProduces(buildDefinition.produces); | ||
swagger_service_1.SwaggerService.getInstance().setProduces(buildDefinition.produces); | ||
} | ||
if (buildDefinition.consumes) { | ||
swagger_service_1.SwaggerService.setConsumes(buildDefinition.consumes); | ||
swagger_service_1.SwaggerService.getInstance().setConsumes(buildDefinition.consumes); | ||
} | ||
if (buildDefinition.host) { | ||
swagger_service_1.SwaggerService.setHost(buildDefinition.host); | ||
swagger_service_1.SwaggerService.getInstance().setHost(buildDefinition.host); | ||
} | ||
if (buildDefinition.externalDocs) { | ||
swagger_service_1.SwaggerService.getInstance().setExternalDocs(buildDefinition.externalDocs); | ||
} | ||
if (buildDefinition.models) { | ||
@@ -36,3 +39,3 @@ var definitions = {}; | ||
var newDefinition = { | ||
type: swagger_definition_constant_1.SwaggerDefinitionConstant.Definition.Type.OBJECT, | ||
type: swagger_definition_constant_1.SwaggerDefinitionConstant.Model.Type.OBJECT, | ||
properties: {}, | ||
@@ -56,6 +59,6 @@ required: [] | ||
} | ||
swagger_service_1.SwaggerService.setDefinitions(definitions); | ||
swagger_service_1.SwaggerService.getInstance().setDefinitions(definitions); | ||
} | ||
swagger_service_1.SwaggerService.buildSwagger(); | ||
swagger_service_1.SwaggerService.getInstance().buildSwagger(); | ||
} | ||
exports.build = build; |
@@ -1,2 +0,2 @@ | ||
import { ISwagger, ISwaggerInfo, ISwaggerDefinition } from "./i-swagger"; | ||
import { ISwagger, ISwaggerInfo, ISwaggerDefinition, ISwaggerExternalDocs } from "./i-swagger"; | ||
import { IApiPathArgs } from "./api-path.decorator"; | ||
@@ -6,27 +6,33 @@ import { IApiOperationPostArgs } from "./api-operation-post.decorator"; | ||
export declare class SwaggerService { | ||
private static controllerMap; | ||
private static definitionsMap; | ||
private static data; | ||
static getData(): ISwagger; | ||
static setBasePath(basePath: string): void; | ||
static setOpenapi(openapi: string): void; | ||
static setInfo(info: ISwaggerInfo): void; | ||
static setSchemes(schemes: string[]): void; | ||
static setProduces(produces: string[]): void; | ||
static setConsumes(consumes: string[]): void; | ||
static setHost(host: string): void; | ||
static setDefinitions(definitions: { | ||
private static instance; | ||
private controllerMap; | ||
private definitionsMap; | ||
private data; | ||
private constructeur(); | ||
static getInstance(): SwaggerService; | ||
resetData(): void; | ||
private initData(); | ||
getData(): ISwagger; | ||
setBasePath(basePath: string): void; | ||
setOpenapi(openapi: string): void; | ||
setInfo(info: ISwaggerInfo): void; | ||
setSchemes(schemes: string[]): void; | ||
setProduces(produces: string[]): void; | ||
setConsumes(consumes: string[]): void; | ||
setHost(host: string): void; | ||
setDefinitions(definitions: { | ||
[key: string]: ISwaggerDefinition; | ||
}): void; | ||
static addPath(args: IApiPathArgs, target: any): void; | ||
static addOperationGet(args: IApiOperationGetArgs, target: any, propertyKey: string | symbol): void; | ||
static addOperationPost(args: IApiOperationPostArgs, target: any, propertyKey: string | symbol): void; | ||
static addOperationPut(args: IApiOperationPostArgs, target: any, propertyKey: string | symbol): void; | ||
static addOperationPatch(args: IApiOperationPostArgs, target: any, propertyKey: string | symbol): void; | ||
static addOperationDelete(args: IApiOperationPostArgs, target: any, propertyKey: string | symbol): void; | ||
private static addOperation(operation, args, target, propertyKey); | ||
private static buildOperation(args, target, propertyKey); | ||
private static buildParameters(type, parameters); | ||
static buildSwagger(): void; | ||
private static buildRef(definition); | ||
setExternalDocs(externalDocs: ISwaggerExternalDocs): void; | ||
addPath(args: IApiPathArgs, target: any): void; | ||
addOperationGet(args: IApiOperationGetArgs, target: any, propertyKey: string | symbol): void; | ||
addOperationPost(args: IApiOperationPostArgs, target: any, propertyKey: string | symbol): void; | ||
addOperationPut(args: IApiOperationPostArgs, target: any, propertyKey: string | symbol): void; | ||
addOperationPatch(args: IApiOperationPostArgs, target: any, propertyKey: string | symbol): void; | ||
addOperationDelete(args: IApiOperationPostArgs, target: any, propertyKey: string | symbol): void; | ||
private addOperation(operation, args, target, propertyKey); | ||
private buildOperation(args, target, propertyKey); | ||
private buildParameters(type, parameters); | ||
buildSwagger(): void; | ||
private buildRef(definition); | ||
} |
@@ -8,31 +8,65 @@ "use strict"; | ||
function SwaggerService() { | ||
this.controllerMap = []; | ||
this.definitionsMap = {}; | ||
} | ||
SwaggerService.getData = function () { | ||
return _.cloneDeep(SwaggerService.data); | ||
SwaggerService.prototype.constructeur = function () { | ||
}; | ||
SwaggerService.setBasePath = function (basePath) { | ||
SwaggerService.data.basePath = basePath; | ||
SwaggerService.getInstance = function () { | ||
if (!SwaggerService.instance) { | ||
var newSwaggerService = new SwaggerService(); | ||
newSwaggerService.initData(); | ||
SwaggerService.instance = newSwaggerService; | ||
} | ||
return SwaggerService.instance; | ||
}; | ||
SwaggerService.setOpenapi = function (openapi) { | ||
SwaggerService.data.openapi = openapi; | ||
SwaggerService.prototype.resetData = function () { | ||
this.initData(); | ||
}; | ||
SwaggerService.setInfo = function (info) { | ||
SwaggerService.data.info = info; | ||
SwaggerService.prototype.initData = function () { | ||
this.data = { | ||
basePath: "/", | ||
info: { | ||
title: "", | ||
version: "" | ||
}, | ||
paths: {}, | ||
tags: [], | ||
schemes: [swagger_definition_constant_1.SwaggerDefinitionConstant.Scheme.HTTP], | ||
produces: [swagger_definition_constant_1.SwaggerDefinitionConstant.Produce.JSON], | ||
consumes: [swagger_definition_constant_1.SwaggerDefinitionConstant.Consume.JSON], | ||
definitions: {}, | ||
swagger: "2.0" | ||
}; | ||
}; | ||
SwaggerService.setSchemes = function (schemes) { | ||
SwaggerService.data.schemes = schemes; | ||
SwaggerService.prototype.getData = function () { | ||
return _.cloneDeep(this.data); | ||
}; | ||
SwaggerService.setProduces = function (produces) { | ||
SwaggerService.data.produces = produces; | ||
SwaggerService.prototype.setBasePath = function (basePath) { | ||
this.data.basePath = basePath; | ||
}; | ||
SwaggerService.setConsumes = function (consumes) { | ||
SwaggerService.data.consumes = consumes; | ||
SwaggerService.prototype.setOpenapi = function (openapi) { | ||
this.data.openapi = openapi; | ||
}; | ||
SwaggerService.setHost = function (host) { | ||
SwaggerService.data.host = host; | ||
SwaggerService.prototype.setInfo = function (info) { | ||
this.data.info = info; | ||
}; | ||
SwaggerService.setDefinitions = function (definitions) { | ||
SwaggerService.data.definitions = definitions; | ||
SwaggerService.prototype.setSchemes = function (schemes) { | ||
this.data.schemes = schemes; | ||
}; | ||
SwaggerService.addPath = function (args, target) { | ||
SwaggerService.prototype.setProduces = function (produces) { | ||
this.data.produces = produces; | ||
}; | ||
SwaggerService.prototype.setConsumes = function (consumes) { | ||
this.data.consumes = consumes; | ||
}; | ||
SwaggerService.prototype.setHost = function (host) { | ||
this.data.host = host; | ||
}; | ||
SwaggerService.prototype.setDefinitions = function (definitions) { | ||
this.data.definitions = definitions; | ||
}; | ||
SwaggerService.prototype.setExternalDocs = function (externalDocs) { | ||
this.data.externalDocs = externalDocs; | ||
}; | ||
SwaggerService.prototype.addPath = function (args, target) { | ||
var currentController = { | ||
@@ -46,4 +80,4 @@ path: args.path, | ||
} | ||
for (var controllerIndex in SwaggerService.controllerMap) { | ||
var controller = SwaggerService.controllerMap[controllerIndex]; | ||
for (var controllerIndex in this.controllerMap) { | ||
var controller = this.controllerMap[controllerIndex]; | ||
if (controllerIndex === target.name) { | ||
@@ -56,5 +90,5 @@ currentController = controller; | ||
} | ||
SwaggerService.controllerMap[target.name] = currentController; | ||
this.controllerMap[target.name] = currentController; | ||
}; | ||
SwaggerService.addOperationGet = function (args, target, propertyKey) { | ||
SwaggerService.prototype.addOperationGet = function (args, target, propertyKey) { | ||
assert.ok(args, "Args are required."); | ||
@@ -65,23 +99,23 @@ assert.ok(args.responses, "Responses are required."); | ||
} | ||
SwaggerService.addOperation("get", args, target, propertyKey); | ||
this.addOperation("get", args, target, propertyKey); | ||
}; | ||
SwaggerService.addOperationPost = function (args, target, propertyKey) { | ||
SwaggerService.prototype.addOperationPost = function (args, target, propertyKey) { | ||
assert.ok(args, "Args are required."); | ||
assert.ok(args.parameters, "Parameters are required."); | ||
assert.ok(args.responses, "Responses are required."); | ||
SwaggerService.addOperation("post", args, target, propertyKey); | ||
this.addOperation("post", args, target, propertyKey); | ||
}; | ||
SwaggerService.addOperationPut = function (args, target, propertyKey) { | ||
SwaggerService.prototype.addOperationPut = function (args, target, propertyKey) { | ||
assert.ok(args, "Args are required."); | ||
assert.ok(args.parameters, "Parameters are required."); | ||
assert.ok(args.responses, "Responses are required."); | ||
SwaggerService.addOperation("put", args, target, propertyKey); | ||
this.addOperation("put", args, target, propertyKey); | ||
}; | ||
SwaggerService.addOperationPatch = function (args, target, propertyKey) { | ||
SwaggerService.prototype.addOperationPatch = function (args, target, propertyKey) { | ||
assert.ok(args, "Args are required."); | ||
assert.ok(args.parameters, "Parameters are required."); | ||
assert.ok(args.responses, "Responses are required."); | ||
SwaggerService.addOperation("patch", args, target, propertyKey); | ||
this.addOperation("patch", args, target, propertyKey); | ||
}; | ||
SwaggerService.addOperationDelete = function (args, target, propertyKey) { | ||
SwaggerService.prototype.addOperationDelete = function (args, target, propertyKey) { | ||
assert.ok(args, "Args are required."); | ||
@@ -91,10 +125,10 @@ assert.ok(args.parameters, "Parameters are required."); | ||
assert.ok(args.responses, "Responses are required."); | ||
SwaggerService.addOperation("delete", args, target, propertyKey); | ||
this.addOperation("delete", args, target, propertyKey); | ||
}; | ||
SwaggerService.addOperation = function (operation, args, target, propertyKey) { | ||
SwaggerService.prototype.addOperation = function (operation, args, target, propertyKey) { | ||
var currentController = { | ||
paths: {} | ||
}; | ||
for (var index in SwaggerService.controllerMap) { | ||
var controller = SwaggerService.controllerMap[index]; | ||
for (var index in this.controllerMap) { | ||
var controller = this.controllerMap[index]; | ||
if (index === target.constructor.name) { | ||
@@ -119,19 +153,19 @@ currentController = controller; | ||
if ("get" === operation) { | ||
currentPath.get = SwaggerService.buildOperation(args, target, propertyKey); | ||
currentPath.get = this.buildOperation(args, target, propertyKey); | ||
} | ||
if ("post" === operation) { | ||
currentPath.post = SwaggerService.buildOperation(args, target, propertyKey); | ||
currentPath.post = this.buildOperation(args, target, propertyKey); | ||
} | ||
if ("put" === operation) { | ||
currentPath.put = SwaggerService.buildOperation(args, target, propertyKey); | ||
currentPath.put = this.buildOperation(args, target, propertyKey); | ||
} | ||
if ("patch" === operation) { | ||
currentPath.patch = SwaggerService.buildOperation(args, target, propertyKey); | ||
currentPath.patch = this.buildOperation(args, target, propertyKey); | ||
} | ||
if ("delete" === operation) { | ||
currentPath.delete = SwaggerService.buildOperation(args, target, propertyKey); | ||
currentPath.delete = this.buildOperation(args, target, propertyKey); | ||
} | ||
SwaggerService.controllerMap[target.constructor.name] = currentController; | ||
this.controllerMap[target.constructor.name] = currentController; | ||
}; | ||
SwaggerService.buildOperation = function (args, target, propertyKey) { | ||
SwaggerService.prototype.buildOperation = function (args, target, propertyKey) { | ||
var operation = { | ||
@@ -156,6 +190,6 @@ operationId: propertyKey, | ||
if (args.parameters.path) { | ||
operation.parameters = _.concat(operation.parameters, SwaggerService.buildParameters(swagger_definition_constant_1.SwaggerDefinitionConstant.Parameter.In.PATH, args.parameters.path)); | ||
operation.parameters = _.concat(operation.parameters, this.buildParameters(swagger_definition_constant_1.SwaggerDefinitionConstant.Parameter.In.PATH, args.parameters.path)); | ||
} | ||
if (args.parameters.query) { | ||
operation.parameters = _.concat(operation.parameters, SwaggerService.buildParameters(swagger_definition_constant_1.SwaggerDefinitionConstant.Parameter.In.QUERY, args.parameters.query)); | ||
operation.parameters = _.concat(operation.parameters, this.buildParameters(swagger_definition_constant_1.SwaggerDefinitionConstant.Parameter.In.QUERY, args.parameters.query)); | ||
} | ||
@@ -172,3 +206,3 @@ if (args.parameters.body) { | ||
var swaggerOperationSchema = { | ||
$ref: SwaggerService.buildRef(args.parameters.body.model) | ||
$ref: this.buildRef(args.parameters.body.model) | ||
}; | ||
@@ -179,3 +213,3 @@ newParameterBody.schema = swaggerOperationSchema; | ||
if (args.parameters.formData) { | ||
operation.parameters = _.concat(operation.parameters, SwaggerService.buildParameters(swagger_definition_constant_1.SwaggerDefinitionConstant.Parameter.In.FORM_DATA, args.parameters.formData)); | ||
operation.parameters = _.concat(operation.parameters, this.buildParameters(swagger_definition_constant_1.SwaggerDefinitionConstant.Parameter.In.FORM_DATA, args.parameters.formData)); | ||
} | ||
@@ -219,3 +253,3 @@ } | ||
if (response.model) { | ||
var ref = SwaggerService.buildRef(response.model); | ||
var ref = this.buildRef(response.model); | ||
var newSwaggerOperationResponseSchema = { | ||
@@ -239,3 +273,3 @@ $ref: ref | ||
}; | ||
SwaggerService.buildParameters = function (type, parameters) { | ||
SwaggerService.prototype.buildParameters = function (type, parameters) { | ||
var swaggerOperationParameter = []; | ||
@@ -268,98 +302,84 @@ for (var parameterIndex in parameters) { | ||
}; | ||
SwaggerService.buildSwagger = function () { | ||
var data = _.cloneDeep(SwaggerService.data); | ||
for (var controllerIndex in SwaggerService.controllerMap) { | ||
var controller = SwaggerService.controllerMap[controllerIndex]; | ||
for (var pathIndex in controller.paths) { | ||
var path = controller.paths[pathIndex]; | ||
var swaggerPath = {}; | ||
if (path.get) { | ||
swaggerPath.get = path.get; | ||
if (!swaggerPath.get.produces) { | ||
swaggerPath.get.produces = SwaggerService.data.produces; | ||
SwaggerService.prototype.buildSwagger = function () { | ||
var data = _.cloneDeep(this.data); | ||
for (var controllerIndex in this.controllerMap) { | ||
var controller = this.controllerMap[controllerIndex]; | ||
if (_.toArray(controller.paths).length > 0) { | ||
for (var pathIndex in controller.paths) { | ||
var path = controller.paths[pathIndex]; | ||
var swaggerPath = {}; | ||
if (path.get) { | ||
swaggerPath.get = path.get; | ||
if (!swaggerPath.get.produces) { | ||
swaggerPath.get.produces = this.data.produces; | ||
} | ||
if (!swaggerPath.get.consumes) { | ||
swaggerPath.get.consumes = this.data.consumes; | ||
} | ||
swaggerPath.get.tags = [_.capitalize(controller.name)]; | ||
} | ||
if (!swaggerPath.get.consumes) { | ||
swaggerPath.get.consumes = SwaggerService.data.consumes; | ||
if (path.post) { | ||
swaggerPath.post = path.post; | ||
if (!swaggerPath.post.produces) { | ||
swaggerPath.post.produces = this.data.produces; | ||
} | ||
if (!swaggerPath.post.consumes) { | ||
swaggerPath.post.consumes = this.data.consumes; | ||
} | ||
swaggerPath.post.tags = [_.capitalize(controller.name)]; | ||
} | ||
swaggerPath.get.tags = [_.capitalize(controller.name)]; | ||
} | ||
if (path.post) { | ||
swaggerPath.post = path.post; | ||
if (!swaggerPath.post.produces) { | ||
swaggerPath.post.produces = SwaggerService.data.produces; | ||
if (path.put) { | ||
swaggerPath.put = path.put; | ||
if (!swaggerPath.put.produces) { | ||
swaggerPath.put.produces = this.data.produces; | ||
} | ||
if (!swaggerPath.put.consumes) { | ||
swaggerPath.put.consumes = this.data.consumes; | ||
} | ||
swaggerPath.put.tags = [_.capitalize(controller.name)]; | ||
} | ||
if (!swaggerPath.post.consumes) { | ||
swaggerPath.post.consumes = SwaggerService.data.consumes; | ||
if (path.patch) { | ||
swaggerPath.patch = path.patch; | ||
if (!swaggerPath.patch.produces) { | ||
swaggerPath.patch.produces = this.data.produces; | ||
} | ||
if (!swaggerPath.patch.consumes) { | ||
swaggerPath.patch.consumes = this.data.consumes; | ||
} | ||
swaggerPath.patch.tags = [_.capitalize(controller.name)]; | ||
} | ||
swaggerPath.post.tags = [_.capitalize(controller.name)]; | ||
} | ||
if (path.put) { | ||
swaggerPath.put = path.put; | ||
if (!swaggerPath.put.produces) { | ||
swaggerPath.put.produces = SwaggerService.data.produces; | ||
if (path.delete) { | ||
swaggerPath.delete = path.delete; | ||
if (!swaggerPath.delete.produces) { | ||
swaggerPath.delete.produces = this.data.produces; | ||
} | ||
if (!swaggerPath.delete.consumes) { | ||
swaggerPath.delete.consumes = this.data.consumes; | ||
} | ||
swaggerPath.delete.tags = [_.capitalize(controller.name)]; | ||
} | ||
if (!swaggerPath.put.consumes) { | ||
swaggerPath.put.consumes = SwaggerService.data.consumes; | ||
if (path.path && path.path.length > 0) { | ||
data.paths[controller.path.concat(path.path)] = swaggerPath; | ||
} | ||
swaggerPath.put.tags = [_.capitalize(controller.name)]; | ||
} | ||
if (path.patch) { | ||
swaggerPath.patch = path.patch; | ||
if (!swaggerPath.patch.produces) { | ||
swaggerPath.patch.produces = SwaggerService.data.produces; | ||
else { | ||
data.paths[controller.path] = swaggerPath; | ||
} | ||
if (!swaggerPath.patch.consumes) { | ||
swaggerPath.patch.consumes = SwaggerService.data.consumes; | ||
} | ||
swaggerPath.patch.tags = [_.capitalize(controller.name)]; | ||
} | ||
if (path.delete) { | ||
swaggerPath.delete = path.delete; | ||
if (!swaggerPath.delete.produces) { | ||
swaggerPath.delete.produces = SwaggerService.data.produces; | ||
} | ||
if (!swaggerPath.delete.consumes) { | ||
swaggerPath.delete.consumes = SwaggerService.data.consumes; | ||
} | ||
swaggerPath.delete.tags = [_.capitalize(controller.name)]; | ||
} | ||
if (path.path && path.path.length > 0) { | ||
data.paths[controller.path.concat(path.path)] = swaggerPath; | ||
} | ||
else { | ||
data.paths[controller.path] = swaggerPath; | ||
} | ||
} | ||
else { | ||
var swaggerPath = {}; | ||
data.paths[controller.path] = swaggerPath; | ||
} | ||
data.tags.push({ | ||
name: _.capitalize(controller.name), description: controller.description | ||
name: _.capitalize(controller.name), | ||
description: controller.description | ||
}); | ||
} | ||
SwaggerService.data = data; | ||
this.data = data; | ||
}; | ||
SwaggerService.buildRef = function (definition) { | ||
SwaggerService.prototype.buildRef = function (definition) { | ||
return "#/definitions/".concat(_.capitalize(definition)); | ||
}; | ||
SwaggerService.controllerMap = []; | ||
SwaggerService.definitionsMap = {}; | ||
SwaggerService.data = { | ||
basePath: "/", | ||
openapi: "", | ||
info: { | ||
title: "", | ||
contact: {}, | ||
license: { | ||
name: "" | ||
}, | ||
version: "" | ||
}, | ||
paths: {}, | ||
tags: [], | ||
schemes: [swagger_definition_constant_1.SwaggerDefinitionConstant.Scheme.HTTP], | ||
produces: [swagger_definition_constant_1.SwaggerDefinitionConstant.Produce.JSON], | ||
consumes: [swagger_definition_constant_1.SwaggerDefinitionConstant.Consume.JSON], | ||
definitions: {}, | ||
swagger: "2.0" | ||
}; | ||
return SwaggerService; | ||
}()); | ||
exports.SwaggerService = SwaggerService; |
{ | ||
"name": "swagger-express-ts", | ||
"version": "1.0.0-alpha.4", | ||
"version": "1.0.0-alpha.5", | ||
"description": "Generate and serve swagger.json", | ||
@@ -8,6 +8,7 @@ "main": "./dist/index.js", | ||
"scripts": { | ||
"package": "echo \"Packaging...\" && node node_modules/rimraf/bin.js built dist && node node_modules/typescript/bin/tsc -p tsconfig.package.json", | ||
"package": "echo \"Packaging...\" && npm run clean && node node_modules/typescript/bin/tsc -p tsconfig.package.json", | ||
"build": "echo \"Building...\" && node node_modules/gulp/bin/gulp.js build", | ||
"test": "echo \"Testing...\" && node node_modules/gulp/bin/gulp.js test", | ||
"test:coverage": "echo \"Testing coverage...\" && node node_modules/gulp/bin/gulp.js test:coverage", | ||
"clean": "echo \"Cleaning...\" && node node_modules/gulp/bin/gulp.js clean", | ||
"tslint": "echo \"TsLint...\" && node node_modules/gulp/bin/gulp.js tslint", | ||
@@ -14,0 +15,0 @@ "dev": "echo \"Developing...\" && node node_modules/gulp/bin/gulp.js dev", |
171
README.md
@@ -22,2 +22,3 @@ # swagger-express-ts | ||
```ts | ||
import * as bodyParser from "body-parser"; | ||
import * as express from "express"; | ||
@@ -30,37 +31,55 @@ import "reflect-metadata"; | ||
import { SwaggerDefinitionConstant } from "swagger-express-ts"; | ||
const config = require ( "../config.json" ); | ||
// set up container | ||
const container = new Container(); | ||
const container = new Container (); | ||
// bind your controllers to Controller | ||
container.bind<interfaces.Controller>( TYPE.Controller ) | ||
.to( VersionController ).whenTargetNamed( VersionController.TARGET_NAME ); | ||
// note that you *must* bind your controllers to Controller | ||
container.bind<interfaces.Controller> ( TYPE.Controller ) | ||
.to( VersionController ).inSingletonScope().whenTargetNamed( VersionController.TARGET_NAME ); | ||
// create server | ||
const server = new InversifyExpressServer( container ); | ||
const server = new InversifyExpressServer ( container ); | ||
// configure server | ||
server.setConfig( ( app: any ) => { | ||
app.use( swagger.express( { | ||
definition : { | ||
info : { | ||
title : "My api", | ||
version : "1.0" | ||
}, | ||
models : { | ||
Version : { | ||
properties : { | ||
name : { type : SwaggerDefinitionConstant.Definition.Property.Type.STRING, required : true }, | ||
description : { type : SwaggerDefinitionConstant.Definition.Property.Type.STRING }, | ||
version : { type : SwaggerDefinitionConstant.Definition.Property.Type.STRING, required : true } | ||
server.setConfig( ( app : any ) => { | ||
app.use( '/api-docs/swagger' , express.static( 'swagger' ) ); | ||
app.use( '/api-docs/swagger/assets' , express.static( 'node_modules/swagger-ui-dist' ) ); | ||
app.use( bodyParser.json() ); | ||
app.use( swagger.express( | ||
{ | ||
definition : { | ||
info : { | ||
title : "My api" , | ||
version : "1.0" | ||
} , | ||
models : { | ||
Version : { | ||
properties : { | ||
id : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING , | ||
required : true | ||
} , | ||
name : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING , | ||
required : true | ||
} , | ||
description : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING | ||
} , | ||
version : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING | ||
} | ||
} | ||
} | ||
} , | ||
externalDocs : { | ||
url : "My url" | ||
} | ||
} | ||
} | ||
} ) ); | ||
) ); | ||
} ); | ||
// configure error | ||
server.setErrorConfig( ( app: any ) => { | ||
app.use( ( err: Error, request: express.Request, response: express.Response, next: express.NextFunction ) => { | ||
server.setErrorConfig( ( app : any ) => { | ||
app.use( ( err : Error , request : express.Request , response : express.Response , next : express.NextFunction ) => { | ||
console.error( err.stack ); | ||
@@ -71,6 +90,7 @@ response.status( 500 ).send( "Something broke!" ); | ||
// start server | ||
const app = server.build(); | ||
app.listen( 3000 ); | ||
app.listen( config.port ); | ||
console.info( "Server is listening on port : " + config.port ); | ||
``` | ||
@@ -84,7 +104,8 @@ | ||
import { controller, httpGet, interfaces, httpPost, requestParam, httpPut } from "inversify-express-utils"; | ||
import { ApiPath, ApiOperationGet, ApiOperationPost, ApiOperationPut, SwaggerDefinitionConstant } from "swagger-express-ts"; | ||
import { SwaggerDefinitionConstant, ApiPath, ApiOperationGet, ApiOperationPost, ApiOperationPut } from "swagger-express-ts"; | ||
import "reflect-metadata"; | ||
const pkg = require ( "../../package.json" ); | ||
@ApiPath( { | ||
path : "/versions", | ||
path : "/versions" , | ||
name : "Version" | ||
@@ -95,14 +116,14 @@ } ) | ||
export class VersionController implements interfaces.Controller { | ||
public static TARGET_NAME: string = "VersionController"; | ||
private data: [any] = [ | ||
public static TARGET_NAME : string = "VersionController"; | ||
private data : [any] = [ | ||
{ | ||
id : "1", | ||
name : "Version 1", | ||
description : "Description Version 1", | ||
id : "1" , | ||
name : "Version 1" , | ||
description : "Description Version 1" , | ||
version : "1.0.0" | ||
}, | ||
} , | ||
{ | ||
id : "2", | ||
name : "Version 2", | ||
description : "Description Version 2", | ||
id : "2" , | ||
name : "Version 2" , | ||
description : "Description Version 2" , | ||
version : "2.0.0" | ||
@@ -113,8 +134,10 @@ } | ||
@ApiOperationGet( { | ||
description : "Get version object" , | ||
summary : "Get version" , | ||
responses : { | ||
200 : { description : "Success", isArray : true, model : "Version" } | ||
200 : { description : "Success" , isArray : true , model : "Version" } | ||
} | ||
} ) | ||
@httpGet( "/" ) | ||
public getVersions( request: express.Request, response: express.Response, next: express.NextFunction ): void { | ||
public getVersions ( request : express.Request , response : express.Response , next : express.NextFunction ) : void { | ||
response.json( this.data ); | ||
@@ -124,7 +147,9 @@ } | ||
@ApiOperationPost( { | ||
description : "Post version object" , | ||
summary : "Post new version" , | ||
parameters : { | ||
body : { description : "New version", required : true, model : "Version" } | ||
}, | ||
body : { description : "New version" , required : true , model : "Version" } | ||
} , | ||
responses : { | ||
200 : { description : "Success", model : "Version" }, | ||
200 : { description : "Success" } , | ||
400 : { description : "Parameters fail" } | ||
@@ -134,3 +159,3 @@ } | ||
@httpPost( "/" ) | ||
public postVersion( request: express.Request, response: express.Response, next: express.NextFunction ): void { | ||
public postVersion ( request : express.Request , response : express.Response , next : express.NextFunction ) : void { | ||
if ( ! request.body ) { | ||
@@ -144,16 +169,23 @@ return response.status( 400 ).end(); | ||
@ApiOperationGet( { | ||
path : "/{id}", | ||
path : "/{id}" , | ||
description : "Get version by id" , | ||
summary : "Get version detail" , | ||
parameters : { | ||
path : { | ||
id : { description : "Id of version", type : SwaggerDefinitionConstant.Parameter.Type.STRING, required : true } | ||
id : { | ||
description : "Id of version" , | ||
type : SwaggerDefinitionConstant.Parameter.Type.STRING , | ||
required : true | ||
} | ||
} | ||
}, | ||
} , | ||
responses : { | ||
200 : { description : "Success", model : "Version" }, | ||
200 : { description : "Success" , model : "Version" } , | ||
404 : { description : "Version not exist" } | ||
}, | ||
} , | ||
produces : [ SwaggerDefinitionConstant.Produce.JSON ] | ||
} ) | ||
@httpGet( "/:id" ) | ||
public getVersion( @requestParam( "id" ) id: string, request: express.Request, response: express.Response, next: express.NextFunction ): void { | ||
this.data.forEach( ( version: any )=> { | ||
public getVersion ( @requestParam( "id" ) id : string , request : express.Request , response : express.Response , next : express.NextFunction ) : void { | ||
this.data.forEach( ( version : any )=> { | ||
if ( version.id === id ) { | ||
@@ -165,2 +197,43 @@ return response.json( version ); | ||
} | ||
@ApiOperationPut( { | ||
path : "/{id}" , | ||
description: "Put version by id", | ||
summary: "Put version", | ||
parameters : { | ||
path : { | ||
id : { | ||
description : "Id of version" , | ||
type : SwaggerDefinitionConstant.Parameter.Type.STRING , | ||
required : true | ||
} | ||
} , | ||
body : { | ||
description : "Updated version" , | ||
model : "Version" , | ||
required : true | ||
} | ||
} , | ||
responses : { | ||
200 : { model : "Version" } | ||
} | ||
} ) | ||
@httpPut( "/:id" ) | ||
public putVersion ( @requestParam( "id" ) id : string , request : express.Request , response : express.Response , next : express.NextFunction ) : void { | ||
if ( ! request.body ) { | ||
return response.status( 400 ).end(); | ||
} | ||
this.data.forEach( ( version : any , index : number )=> { | ||
if ( version.id === id ) { | ||
let newVersion = request.body; | ||
version.id = newVersion.id; | ||
version.name = newVersion.name; | ||
version.description = newVersion.description; | ||
version.version = newVersion.version; | ||
this.data[ index ] = version; | ||
return response.json( version ); | ||
} | ||
} ); | ||
response.status( 404 ).end(); | ||
} | ||
} | ||
@@ -167,0 +240,0 @@ |
@@ -87,5 +87,5 @@ # @ApiOperationDelete(args: IApiOperationDeleteArgs) | ||
## produces: string[] | ||
## setProduces: string[] | ||
Define type list that resource produce. | ||
- Optional | ||
- Default is global type list defined in ISwaggerBuildDefinition when execute [.express(options: ISwaggerExpressOptions)](./configuration.md) |
@@ -73,5 +73,5 @@ # @ApiOperationGet(args: IApiOperationGetArgs) | ||
## produces: string[] | ||
## setProduces: string[] | ||
Define type list that resource produce. | ||
- Optional | ||
- Default is global type list defined in ISwaggerBuildDefinition when execute [.express(options: ISwaggerExpressOptions)](./configuration.md) |
@@ -95,5 +95,5 @@ # @ApiOperationPatch(args: IApiOperationPatchArgs) | ||
## produces: string[] | ||
## setProduces: string[] | ||
Define type list that resource produce. | ||
- Optional | ||
- Default is global type list defined in ISwaggerBuildDefinition when execute [.express(options: ISwaggerExpressOptions)](./configuration.md) |
@@ -81,5 +81,5 @@ # @ApiOperationPost(args: IApiOperationPostArgs) | ||
## produces: string[] | ||
## setProduces: string[] | ||
Define type list that resource produce. | ||
- Optional | ||
- Default is global type list defined in ISwaggerBuildDefinition when execute [.express(options: ISwaggerExpressOptions)](./configuration.md) |
@@ -100,5 +100,5 @@ # @ApiOperationPut(args: IApiOperationPutArgs) | ||
## produces: string[] | ||
## setProduces: string[] | ||
Define type list that resource produce. | ||
- Optional | ||
- Default is global type list defined in ISwaggerBuildDefinition when execute [.express(options: ISwaggerExpressOptions)](./configuration.md) |
@@ -7,32 +7,33 @@ # Configuration | ||
```ts | ||
app.use( swagger.express( | ||
{ | ||
definition : { | ||
info : { | ||
title : "My api", | ||
version : "1.0" | ||
}, | ||
models : { | ||
Version : { | ||
properties : { | ||
id : { | ||
type : SwaggerDefinitionConstant.Definition.Property.Type.STRING, | ||
required : true | ||
}, | ||
name : { | ||
type : SwaggerDefinitionConstant.Definition.Property.Type.STRING, | ||
required : true | ||
}, | ||
description : { | ||
type : SwaggerDefinitionConstant.Definition.Property.Type.STRING | ||
}, | ||
version : { | ||
type : SwaggerDefinitionConstant.Definition.Property.Type.STRING | ||
} | ||
app.use( swagger.express({ | ||
definition : { | ||
info : { | ||
title : "My api" , | ||
version : "1.0" | ||
} , | ||
models : { | ||
Version : { | ||
properties : { | ||
id : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING , | ||
required : true | ||
} , | ||
name : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING , | ||
required : true | ||
} , | ||
description : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING | ||
} , | ||
version : { | ||
type : SwaggerDefinitionConstant.Model.Property.Type.STRING | ||
} | ||
} | ||
} | ||
} , | ||
externalDocs : { | ||
url : "My url" | ||
} | ||
} | ||
) ); | ||
}) ); | ||
``` | ||
@@ -39,0 +40,0 @@ |
# ISwaggerBuildDefinition | ||
## basePath: string | ||
## setBasePath: string | ||
Define base URL for all API. | ||
@@ -9,20 +9,20 @@ - Optional. | ||
## openapi: string | ||
## setOpenapi: string | ||
Define version of OpenAPI. | ||
- Optional. | ||
## info : [SwaggerInfo](./i-swagger-info.md) | ||
Define info. | ||
## setInfo : [SwaggerInfo](./i-swagger-setInfo.md) | ||
Define setInfo. | ||
## consumes: string[] | ||
Define the MIME types supported by the API for consumes. The root-level definition can be overridden in individual operations. | ||
## 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". | ||
## produces: string[] | ||
Define the MIME types supported by the API for produces. The root-level definition can be overridden in individual operations. | ||
## 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". | ||
## schemes: string[] | ||
## setSchemes: string[] | ||
Define Schemes. | ||
@@ -32,4 +32,4 @@ - Optional | ||
## host: string | ||
Define host. | ||
## setHost: string | ||
Define setHost. | ||
- Optional | ||
@@ -40,1 +40,5 @@ | ||
- Required | ||
## externalDocs: [ISwaggerExternalDocs](./i-swagger-external-docs.md) | ||
Define external docs | ||
- Optional |
# Installation | ||
You can get the latest release and the type definitions using npm: | ||
You can get the latest release and the type setDefinitions using npm: | ||
@@ -9,3 +9,3 @@ ```sh | ||
The InversifyJS type definitions are included in the inversify npm package. | ||
The InversifyJS type setDefinitions are included in the inversify npm package. | ||
InversifyJS requires the experimentalDecorators, emitDecoratorMetadataand lib compilation options in your tsconfig.json file. | ||
@@ -12,0 +12,0 @@ |
@@ -21,3 +21,3 @@ # SwaggerDefinitionConstant | ||
## Definition | ||
## Model | ||
@@ -24,0 +24,0 @@ ### Type |
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
88554
51
962
388