@nestjs/swagger
Advanced tools
Comparing version 1.1.4 to 1.2.0
@@ -1,12 +0,13 @@ | ||
export declare const DECORATORS_PREFIX = "swagger"; | ||
export declare const DECORATORS_PREFIX = 'swagger'; | ||
export declare const DECORATORS: { | ||
API_OPERATION: string; | ||
API_RESPONSE: string; | ||
API_PRODUCES: string; | ||
API_CONSUMES: string; | ||
API_USE_TAGS: string; | ||
API_PARAMETERS: string; | ||
API_MODEL_PROPERTIES: string; | ||
API_MODEL_PROPERTIES_ARRAY: string; | ||
API_BEARER: string; | ||
API_OPERATION: string; | ||
API_RESPONSE: string; | ||
API_PRODUCES: string; | ||
API_CONSUMES: string; | ||
API_USE_TAGS: string; | ||
API_PARAMETERS: string; | ||
API_MODEL_PROPERTIES: string; | ||
API_MODEL_PROPERTIES_ARRAY: string; | ||
API_BEARER: string; | ||
API_OAUTH2: string; | ||
}; |
@@ -1,14 +0,17 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.DECORATORS_PREFIX = 'swagger'; | ||
exports.DECORATORS = { | ||
API_OPERATION: `${exports.DECORATORS_PREFIX}/apiOperation`, | ||
API_RESPONSE: `${exports.DECORATORS_PREFIX}/apiResponse`, | ||
API_PRODUCES: `${exports.DECORATORS_PREFIX}/apiProduces`, | ||
API_CONSUMES: `${exports.DECORATORS_PREFIX}/apiConsumes`, | ||
API_USE_TAGS: `${exports.DECORATORS_PREFIX}/apiUseTags`, | ||
API_PARAMETERS: `${exports.DECORATORS_PREFIX}/apiParameters`, | ||
API_MODEL_PROPERTIES: `${exports.DECORATORS_PREFIX}/apiModelProperties`, | ||
API_MODEL_PROPERTIES_ARRAY: `${exports.DECORATORS_PREFIX}/apiModelPropertiesArray`, | ||
API_BEARER: `${exports.DECORATORS_PREFIX}/apiBearer`, | ||
API_OPERATION: `${exports.DECORATORS_PREFIX}/apiOperation`, | ||
API_RESPONSE: `${exports.DECORATORS_PREFIX}/apiResponse`, | ||
API_PRODUCES: `${exports.DECORATORS_PREFIX}/apiProduces`, | ||
API_CONSUMES: `${exports.DECORATORS_PREFIX}/apiConsumes`, | ||
API_USE_TAGS: `${exports.DECORATORS_PREFIX}/apiUseTags`, | ||
API_PARAMETERS: `${exports.DECORATORS_PREFIX}/apiParameters`, | ||
API_MODEL_PROPERTIES: `${exports.DECORATORS_PREFIX}/apiModelProperties`, | ||
API_MODEL_PROPERTIES_ARRAY: `${ | ||
exports.DECORATORS_PREFIX | ||
}/apiModelPropertiesArray`, | ||
API_BEARER: `${exports.DECORATORS_PREFIX}/apiBearer`, | ||
API_OAUTH2: `${exports.DECORATORS_PREFIX}/apiOauth2`, | ||
}; |
@@ -1,1 +0,5 @@ | ||
export declare const ApiBearerAuth: () => (target: object, key?: any, descriptor?: any) => any; | ||
export declare const ApiBearerAuth: () => ( | ||
target: object, | ||
key?: any, | ||
descriptor?: any, | ||
) => any; |
@@ -1,7 +0,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
const helpers_1 = require("./helpers"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const helpers_1 = require('./helpers'); | ||
exports.ApiBearerAuth = () => { | ||
return helpers_1.createMixedDecorator(constants_1.DECORATORS.API_BEARER, []); | ||
return helpers_1.createMixedDecorator(constants_1.DECORATORS.API_BEARER, []); | ||
}; |
@@ -1,1 +0,3 @@ | ||
export declare const ApiConsumes: (...mimeTypes: string[]) => (target: object, key?: any, descriptor?: any) => any; | ||
export declare const ApiConsumes: ( | ||
...mimeTypes: string[] | ||
) => (target: object, key?: any, descriptor?: any) => any; |
@@ -1,7 +0,10 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
const helpers_1 = require("./helpers"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const helpers_1 = require('./helpers'); | ||
exports.ApiConsumes = (...mimeTypes) => { | ||
return helpers_1.createMixedDecorator(constants_1.DECORATORS.API_CONSUMES, mimeTypes); | ||
return helpers_1.createMixedDecorator( | ||
constants_1.DECORATORS.API_CONSUMES, | ||
mimeTypes, | ||
); | ||
}; |
@@ -1,2 +0,3 @@ | ||
export declare const ApiImplicitBody: (metadata: { | ||
export declare const ApiImplicitBody: ( | ||
metadata: { | ||
name: string; | ||
@@ -6,2 +7,4 @@ description?: string; | ||
type?: any; | ||
}) => MethodDecorator; | ||
isArray?: boolean; | ||
}, | ||
) => MethodDecorator; |
@@ -1,18 +0,19 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const helpers_1 = require("./helpers"); | ||
const lodash_1 = require("lodash"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const helpers_1 = require('./helpers'); | ||
const lodash_1 = require('lodash'); | ||
const initialMetadata = { | ||
name: '', | ||
required: true, | ||
name: '', | ||
required: true, | ||
}; | ||
exports.ApiImplicitBody = (metadata) => { | ||
const param = { | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'body', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: metadata.type, | ||
}; | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
exports.ApiImplicitBody = metadata => { | ||
const param = { | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'body', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: metadata.type, | ||
isArray: metadata.isArray, | ||
}; | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
}; |
@@ -1,5 +0,14 @@ | ||
export declare const ApiImplicitHeader: (metadata: { | ||
export declare const ApiImplicitHeader: ( | ||
metadata: { | ||
name: string; | ||
description?: string; | ||
required?: boolean; | ||
}) => MethodDecorator; | ||
}, | ||
) => MethodDecorator; | ||
export declare const ApiImplicitHeaders: ( | ||
headers: { | ||
name: string; | ||
description?: string; | ||
required?: boolean; | ||
}[], | ||
) => MethodDecorator; |
@@ -1,18 +0,28 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const helpers_1 = require("./helpers"); | ||
const lodash_1 = require("lodash"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const helpers_1 = require('./helpers'); | ||
const lodash_1 = require('lodash'); | ||
const initialMetadata = { | ||
name: '', | ||
required: true, | ||
name: '', | ||
required: true, | ||
}; | ||
exports.ApiImplicitHeader = (metadata) => { | ||
const param = { | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'header', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: String, | ||
}; | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
exports.ApiImplicitHeader = metadata => { | ||
const param = { | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'header', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: String, | ||
}; | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
}; | ||
exports.ApiImplicitHeaders = headers => { | ||
const multiMetadata = headers.map(metadata => ({ | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'header', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: String, | ||
})); | ||
return helpers_1.createMultipleParamDecorator(multiMetadata, initialMetadata); | ||
}; |
@@ -1,2 +0,3 @@ | ||
export declare const ApiImplicitParam: (metadata: { | ||
export declare const ApiImplicitParam: ( | ||
metadata: { | ||
name: string; | ||
@@ -6,2 +7,3 @@ description?: string; | ||
type?: any; | ||
}) => MethodDecorator; | ||
}, | ||
) => MethodDecorator; |
@@ -1,18 +0,18 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const helpers_1 = require("./helpers"); | ||
const lodash_1 = require("lodash"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const helpers_1 = require('./helpers'); | ||
const lodash_1 = require('lodash'); | ||
const initialMetadata = { | ||
name: '', | ||
required: true, | ||
name: '', | ||
required: true, | ||
}; | ||
exports.ApiImplicitParam = (metadata) => { | ||
const param = { | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'path', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: metadata.type, | ||
}; | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
exports.ApiImplicitParam = metadata => { | ||
const param = { | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'path', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: metadata.type, | ||
}; | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
}; |
@@ -1,2 +0,3 @@ | ||
export declare const ApiImplicitQuery: (metadata: { | ||
export declare const ApiImplicitQuery: ( | ||
metadata: { | ||
name: string; | ||
@@ -6,2 +7,3 @@ description?: string; | ||
type?: any; | ||
}) => MethodDecorator; | ||
}, | ||
) => MethodDecorator; |
@@ -1,18 +0,18 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const helpers_1 = require("./helpers"); | ||
const lodash_1 = require("lodash"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const helpers_1 = require('./helpers'); | ||
const lodash_1 = require('lodash'); | ||
const initialMetadata = { | ||
name: '', | ||
required: true, | ||
name: '', | ||
required: true, | ||
}; | ||
exports.ApiImplicitQuery = (metadata) => { | ||
const param = { | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'query', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: metadata.type, | ||
}; | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
exports.ApiImplicitQuery = metadata => { | ||
const param = { | ||
name: lodash_1.isNil(metadata.name) ? initialMetadata.name : metadata.name, | ||
in: 'query', | ||
description: metadata.description, | ||
required: metadata.required, | ||
type: metadata.type, | ||
}; | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
}; |
@@ -1,2 +0,3 @@ | ||
export declare const ApiModelProperty: (metadata?: { | ||
export declare const ApiModelProperty: ( | ||
metadata?: { | ||
description?: string; | ||
@@ -7,4 +8,24 @@ required?: boolean; | ||
default?: any; | ||
}) => PropertyDecorator; | ||
export declare const ApiModelPropertyOptional: (metadata?: { | ||
enum?: string[] | number[] | (string | number)[]; | ||
format?: string; | ||
multipleOf?: number; | ||
maximum?: number; | ||
exclusiveMaximum?: number; | ||
minimum?: number; | ||
exclusiveMinimum?: number; | ||
maxLength?: number; | ||
minLength?: number; | ||
pattern?: string; | ||
maxItems?: number; | ||
minItems?: number; | ||
uniqueItems?: boolean; | ||
maxProperties?: number; | ||
minProperties?: number; | ||
readOnly?: boolean; | ||
xml?: any; | ||
example?: any; | ||
}, | ||
) => PropertyDecorator; | ||
export declare const ApiModelPropertyOptional: ( | ||
metadata?: { | ||
description?: string; | ||
@@ -14,2 +35,21 @@ type?: any; | ||
default?: any; | ||
}) => PropertyDecorator; | ||
enum?: string[] | number[] | (string | number)[]; | ||
format?: string; | ||
multipleOf?: number; | ||
maximum?: number; | ||
exclusiveMaximum?: number; | ||
minimum?: number; | ||
exclusiveMinimum?: number; | ||
maxLength?: number; | ||
minLength?: number; | ||
pattern?: string; | ||
maxItems?: number; | ||
minItems?: number; | ||
uniqueItems?: boolean; | ||
maxProperties?: number; | ||
minProperties?: number; | ||
readOnly?: boolean; | ||
xml?: any; | ||
example?: any; | ||
}, | ||
) => PropertyDecorator; |
@@ -1,8 +0,12 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
const helpers_1 = require("./helpers"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const helpers_1 = require('./helpers'); | ||
exports.ApiModelProperty = (metadata = {}) => { | ||
return helpers_1.createPropertyDecorator(constants_1.DECORATORS.API_MODEL_PROPERTIES, metadata); | ||
return helpers_1.createPropertyDecorator( | ||
constants_1.DECORATORS.API_MODEL_PROPERTIES, | ||
metadata, | ||
); | ||
}; | ||
exports.ApiModelPropertyOptional = (metadata = {}) => exports.ApiModelProperty(Object.assign({}, metadata, { required: false })); | ||
exports.ApiModelPropertyOptional = (metadata = {}) => | ||
exports.ApiModelProperty(Object.assign({}, metadata, { required: false })); |
@@ -1,5 +0,7 @@ | ||
export declare const ApiOperation: (metadata: { | ||
export declare const ApiOperation: ( | ||
metadata: { | ||
title: string; | ||
description?: string; | ||
operationId?: string; | ||
}) => MethodDecorator; | ||
}, | ||
) => MethodDecorator; |
@@ -1,11 +0,23 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
const helpers_1 = require("./helpers"); | ||
const lodash_1 = require("lodash"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const helpers_1 = require('./helpers'); | ||
const lodash_1 = require('lodash'); | ||
const initialMetadata = { | ||
summary: '', | ||
summary: '', | ||
}; | ||
exports.ApiOperation = (metadata) => { | ||
return helpers_1.createMethodDecorator(constants_1.DECORATORS.API_OPERATION, lodash_1.pickBy(Object.assign({}, initialMetadata, { summary: lodash_1.isNil(metadata.title) ? initialMetadata.summary : metadata.title, description: metadata.description, operationId: metadata.operationId }), lodash_1.negate(lodash_1.isUndefined))); | ||
exports.ApiOperation = metadata => { | ||
return helpers_1.createMethodDecorator( | ||
constants_1.DECORATORS.API_OPERATION, | ||
lodash_1.pickBy( | ||
Object.assign({}, initialMetadata, { | ||
summary: lodash_1.isNil(metadata.title) | ||
? initialMetadata.summary | ||
: metadata.title, | ||
description: metadata.description, | ||
operationId: metadata.operationId, | ||
}), | ||
lodash_1.negate(lodash_1.isUndefined), | ||
), | ||
); | ||
}; |
@@ -1,1 +0,3 @@ | ||
export declare const ApiProduces: (...mimeTypes: string[]) => (target: object, key?: any, descriptor?: any) => any; | ||
export declare const ApiProduces: ( | ||
...mimeTypes: string[] | ||
) => (target: object, key?: any, descriptor?: any) => any; |
@@ -1,7 +0,10 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
const helpers_1 = require("./helpers"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const helpers_1 = require('./helpers'); | ||
exports.ApiProduces = (...mimeTypes) => { | ||
return helpers_1.createMixedDecorator(constants_1.DECORATORS.API_PRODUCES, mimeTypes); | ||
return helpers_1.createMixedDecorator( | ||
constants_1.DECORATORS.API_PRODUCES, | ||
mimeTypes, | ||
); | ||
}; |
@@ -1,2 +0,3 @@ | ||
export declare const ApiResponse: (metadata: { | ||
export declare const ApiResponse: ( | ||
metadata: { | ||
status: number; | ||
@@ -6,2 +7,3 @@ description?: string; | ||
isArray?: boolean; | ||
}) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
}, | ||
) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; |
@@ -1,23 +0,38 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
const lodash_1 = require("lodash"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const lodash_1 = require('lodash'); | ||
const initialMetadata = { | ||
status: 0, | ||
type: String, | ||
isArray: false, | ||
status: 0, | ||
type: String, | ||
isArray: false, | ||
}; | ||
exports.ApiResponse = (metadata) => { | ||
metadata.description = metadata.description ? metadata.description : ''; | ||
const groupedMetadata = { [metadata.status]: lodash_1.omit(metadata, 'status') }; | ||
return (target, key, descriptor) => { | ||
if (descriptor) { | ||
const responses = Reflect.getMetadata(constants_1.DECORATORS.API_RESPONSE, descriptor.value) || {}; | ||
Reflect.defineMetadata(constants_1.DECORATORS.API_RESPONSE, Object.assign({}, responses, groupedMetadata), descriptor.value); | ||
return descriptor; | ||
} | ||
const responses = Reflect.getMetadata(constants_1.DECORATORS.API_RESPONSE, target) || {}; | ||
Reflect.defineMetadata(constants_1.DECORATORS.API_RESPONSE, Object.assign({}, responses, groupedMetadata), target); | ||
return target; | ||
}; | ||
exports.ApiResponse = metadata => { | ||
metadata.description = metadata.description ? metadata.description : ''; | ||
const groupedMetadata = { | ||
[metadata.status]: lodash_1.omit(metadata, 'status'), | ||
}; | ||
return (target, key, descriptor) => { | ||
if (descriptor) { | ||
const responses = | ||
Reflect.getMetadata( | ||
constants_1.DECORATORS.API_RESPONSE, | ||
descriptor.value, | ||
) || {}; | ||
Reflect.defineMetadata( | ||
constants_1.DECORATORS.API_RESPONSE, | ||
Object.assign({}, responses, groupedMetadata), | ||
descriptor.value, | ||
); | ||
return descriptor; | ||
} | ||
const responses = | ||
Reflect.getMetadata(constants_1.DECORATORS.API_RESPONSE, target) || {}; | ||
Reflect.defineMetadata( | ||
constants_1.DECORATORS.API_RESPONSE, | ||
Object.assign({}, responses, groupedMetadata), | ||
target, | ||
); | ||
return target; | ||
}; | ||
}; |
@@ -1,1 +0,3 @@ | ||
export declare const ApiUseTags: (...tags: string[]) => (target: object, key?: any, descriptor?: any) => any; | ||
export declare const ApiUseTags: ( | ||
...tags: string[] | ||
) => (target: object, key?: any, descriptor?: any) => any; |
@@ -1,7 +0,10 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
const helpers_1 = require("./helpers"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const helpers_1 = require('./helpers'); | ||
exports.ApiUseTags = (...tags) => { | ||
return helpers_1.createMixedDecorator(constants_1.DECORATORS.API_USE_TAGS, tags); | ||
return helpers_1.createMixedDecorator( | ||
constants_1.DECORATORS.API_USE_TAGS, | ||
tags, | ||
); | ||
}; |
@@ -1,5 +0,32 @@ | ||
export declare const createMethodDecorator: (metakey: any, metadata: any) => MethodDecorator; | ||
export declare const createClassDecorator: (metakey: any, metadata: any) => ClassDecorator; | ||
export declare const createPropertyDecorator: (metakey: any, metadata: any) => PropertyDecorator; | ||
export declare const createMixedDecorator: (metakey: any, metadata: any) => (target: object, key?: any, descriptor?: any) => any; | ||
export declare const createParamDecorator: (metadata: any, initial: any) => (target: any, key: any, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
export declare const createMethodDecorator: ( | ||
metakey: any, | ||
metadata: any, | ||
) => MethodDecorator; | ||
export declare const createClassDecorator: ( | ||
metakey: any, | ||
metadata: any, | ||
) => ClassDecorator; | ||
export declare const createPropertyDecorator: ( | ||
metakey: any, | ||
metadata: any, | ||
) => PropertyDecorator; | ||
export declare const createMixedDecorator: ( | ||
metakey: any, | ||
metadata: any, | ||
) => (target: object, key?: any, descriptor?: any) => any; | ||
export declare const createParamDecorator: ( | ||
metadata: any, | ||
initial: any, | ||
) => ( | ||
target: any, | ||
key: any, | ||
descriptor: PropertyDescriptor, | ||
) => PropertyDescriptor; | ||
export declare const createMultipleParamDecorator: ( | ||
multiMetadata: any[], | ||
initial: any, | ||
) => ( | ||
target: any, | ||
key: any, | ||
descriptor: PropertyDescriptor, | ||
) => PropertyDescriptor; |
@@ -1,43 +0,95 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
const constants_1 = require("../constants"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const lodash_1 = require('lodash'); | ||
const constants_1 = require('../constants'); | ||
exports.createMethodDecorator = (metakey, metadata) => { | ||
return (target, key, descriptor) => { | ||
Reflect.defineMetadata(metakey, metadata, descriptor.value); | ||
return descriptor; | ||
}; | ||
return (target, key, descriptor) => { | ||
Reflect.defineMetadata(metakey, metadata, descriptor.value); | ||
return descriptor; | ||
}; | ||
}; | ||
exports.createClassDecorator = (metakey, metadata) => { | ||
return (target) => { | ||
Reflect.defineMetadata(metakey, metadata, target); | ||
return target; | ||
}; | ||
return target => { | ||
Reflect.defineMetadata(metakey, metadata, target); | ||
return target; | ||
}; | ||
}; | ||
exports.createPropertyDecorator = (metakey, metadata) => { | ||
return (target, propertyKey) => { | ||
const properties = Reflect.getMetadata(constants_1.DECORATORS.API_MODEL_PROPERTIES_ARRAY, target) || []; | ||
Reflect.defineMetadata(constants_1.DECORATORS.API_MODEL_PROPERTIES_ARRAY, [...properties, `:${propertyKey}`], target); | ||
Reflect.defineMetadata(metakey, Object.assign({ type: Reflect.getMetadata('design:type', target, propertyKey) }, metadata), target, propertyKey); | ||
}; | ||
return (target, propertyKey) => { | ||
const properties = | ||
Reflect.getMetadata( | ||
constants_1.DECORATORS.API_MODEL_PROPERTIES_ARRAY, | ||
target, | ||
) || []; | ||
Reflect.defineMetadata( | ||
constants_1.DECORATORS.API_MODEL_PROPERTIES_ARRAY, | ||
[...properties, `:${propertyKey}`], | ||
target, | ||
); | ||
Reflect.defineMetadata( | ||
metakey, | ||
Object.assign( | ||
{ type: Reflect.getMetadata('design:type', target, propertyKey) }, | ||
metadata, | ||
), | ||
target, | ||
propertyKey, | ||
); | ||
}; | ||
}; | ||
exports.createMixedDecorator = (metakey, metadata) => { | ||
return (target, key, descriptor) => { | ||
if (descriptor) { | ||
Reflect.defineMetadata(metakey, metadata, descriptor.value); | ||
return descriptor; | ||
} | ||
Reflect.defineMetadata(metakey, metadata, target); | ||
return target; | ||
}; | ||
return (target, key, descriptor) => { | ||
if (descriptor) { | ||
Reflect.defineMetadata(metakey, metadata, descriptor.value); | ||
return descriptor; | ||
} | ||
Reflect.defineMetadata(metakey, metadata, target); | ||
return target; | ||
}; | ||
}; | ||
exports.createParamDecorator = (metadata, initial) => { | ||
return (target, key, descriptor) => { | ||
const parameters = Reflect.getMetadata(constants_1.DECORATORS.API_PARAMETERS, descriptor.value) || []; | ||
Reflect.defineMetadata(constants_1.DECORATORS.API_PARAMETERS, [ | ||
...parameters, | ||
Object.assign({}, initial, lodash_1.pickBy(metadata, lodash_1.negate(lodash_1.isUndefined))), | ||
], descriptor.value); | ||
return descriptor; | ||
}; | ||
return (target, key, descriptor) => { | ||
const parameters = | ||
Reflect.getMetadata( | ||
constants_1.DECORATORS.API_PARAMETERS, | ||
descriptor.value, | ||
) || []; | ||
Reflect.defineMetadata( | ||
constants_1.DECORATORS.API_PARAMETERS, | ||
[ | ||
...parameters, | ||
Object.assign( | ||
{}, | ||
initial, | ||
lodash_1.pickBy(metadata, lodash_1.negate(lodash_1.isUndefined)), | ||
), | ||
], | ||
descriptor.value, | ||
); | ||
return descriptor; | ||
}; | ||
}; | ||
exports.createMultipleParamDecorator = (multiMetadata, initial) => { | ||
return (target, key, descriptor) => { | ||
const parameters = | ||
Reflect.getMetadata( | ||
constants_1.DECORATORS.API_PARAMETERS, | ||
descriptor.value, | ||
) || []; | ||
Reflect.defineMetadata( | ||
constants_1.DECORATORS.API_PARAMETERS, | ||
[ | ||
...parameters, | ||
...multiMetadata.map(metadata => | ||
Object.assign( | ||
{}, | ||
initial, | ||
lodash_1.pickBy(metadata, lodash_1.negate(lodash_1.isUndefined)), | ||
), | ||
), | ||
], | ||
descriptor.value, | ||
); | ||
return descriptor; | ||
}; | ||
}; |
@@ -12,1 +12,2 @@ export * from './api-operation.decorator'; | ||
export * from './api-use-tags.decorator'; | ||
export * from './api-oauth2.decorator'; |
@@ -1,16 +0,17 @@ | ||
"use strict"; | ||
'use strict'; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./api-operation.decorator")); | ||
__export(require("./api-response.decorator")); | ||
__export(require("./api-produces.decorator")); | ||
__export(require("./api-consumes.decorator")); | ||
__export(require("./api-bearer.decorator")); | ||
__export(require("./api-implicit-body.decorator")); | ||
__export(require("./api-implicit-header.decorator")); | ||
__export(require("./api-implicit-param.decorator")); | ||
__export(require("./api-implicit-query.decorator")); | ||
__export(require("./api-model-property.decorator")); | ||
__export(require("./api-use-tags.decorator")); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
__export(require('./api-operation.decorator')); | ||
__export(require('./api-response.decorator')); | ||
__export(require('./api-produces.decorator')); | ||
__export(require('./api-consumes.decorator')); | ||
__export(require('./api-bearer.decorator')); | ||
__export(require('./api-implicit-body.decorator')); | ||
__export(require('./api-implicit-header.decorator')); | ||
__export(require('./api-implicit-param.decorator')); | ||
__export(require('./api-implicit-query.decorator')); | ||
__export(require('./api-model-property.decorator')); | ||
__export(require('./api-use-tags.decorator')); | ||
__export(require('./api-oauth2.decorator')); |
import { SwaggerBaseConfig } from './interfaces/swagger-base-config.interface'; | ||
export declare class DocumentBuilder { | ||
private readonly document; | ||
setTitle(title: string): this; | ||
setDescription(description: string): this; | ||
setVersion(version: string): this; | ||
setTermsOfService(termsOfService: string): this; | ||
setContactEmail(email: string): this; | ||
setLicense(name: string, url: string): this; | ||
setHost(host: string): this; | ||
setBasePath(basePath: string): this; | ||
setExternalDoc(description: string, url: string): this; | ||
setSchemes(...schemes: ('http' | 'https')[]): this; | ||
addTag(name: string, description?: string): this; | ||
addBearerAuth(name?: string, location?: ('header' | 'body' | 'query')): this; | ||
build(): SwaggerBaseConfig; | ||
private readonly document; | ||
setTitle(title: string): this; | ||
setDescription(description: string): this; | ||
setVersion(version: string): this; | ||
setTermsOfService(termsOfService: string): this; | ||
setContactEmail(email: string): this; | ||
setLicense(name: string, url: string): this; | ||
setHost(host: string): this; | ||
setBasePath(basePath: string): this; | ||
setExternalDoc(description: string, url: string): this; | ||
setSchemes(...schemes: ('http' | 'https')[]): this; | ||
addTag(name: string, description?: string): this; | ||
addBearerAuth( | ||
name?: string, | ||
location?: 'header' | 'body' | 'query', | ||
type?: string, | ||
): this; | ||
addOAuth2( | ||
flow?: 'implicit' | 'password' | 'application' | 'accessCode', | ||
authorizationUrl?: string, | ||
tokenUrl?: string, | ||
scopes?: object, | ||
): this; | ||
build(): SwaggerBaseConfig; | ||
} |
@@ -1,62 +0,76 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const document_base_1 = require("./fixtures/document.base"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const document_base_1 = require('./fixtures/document.base'); | ||
class DocumentBuilder { | ||
constructor() { | ||
this.document = document_base_1.documentBase; | ||
} | ||
setTitle(title) { | ||
this.document.info.title = title; | ||
return this; | ||
} | ||
setDescription(description) { | ||
this.document.info.description = description; | ||
return this; | ||
} | ||
setVersion(version) { | ||
this.document.info.version = version; | ||
return this; | ||
} | ||
setTermsOfService(termsOfService) { | ||
this.document.info.termsOfService = termsOfService; | ||
return this; | ||
} | ||
setContactEmail(email) { | ||
this.document.info.contact = { email }; | ||
return this; | ||
} | ||
setLicense(name, url) { | ||
this.document.info.license = { name, url }; | ||
return this; | ||
} | ||
setHost(host) { | ||
this.document.host = host; | ||
return this; | ||
} | ||
setBasePath(basePath) { | ||
this.document.basePath = basePath.startsWith('/') ? basePath : '/' + basePath; | ||
return this; | ||
} | ||
setExternalDoc(description, url) { | ||
this.document.externalDocs = { description, url }; | ||
return this; | ||
} | ||
setSchemes(...schemes) { | ||
this.document.schemes = schemes; | ||
return this; | ||
} | ||
addTag(name, description = '') { | ||
this.document.tags = this.document.tags.concat({ name, description }); | ||
return this; | ||
} | ||
addBearerAuth(name = 'Authorization', location = 'header') { | ||
this.document.securityDefinitions = { | ||
bearer: { type: 'apiKey', name, in: location }, | ||
}; | ||
return this; | ||
} | ||
build() { | ||
return this.document; | ||
} | ||
constructor() { | ||
this.document = document_base_1.documentBase; | ||
} | ||
setTitle(title) { | ||
this.document.info.title = title; | ||
return this; | ||
} | ||
setDescription(description) { | ||
this.document.info.description = description; | ||
return this; | ||
} | ||
setVersion(version) { | ||
this.document.info.version = version; | ||
return this; | ||
} | ||
setTermsOfService(termsOfService) { | ||
this.document.info.termsOfService = termsOfService; | ||
return this; | ||
} | ||
setContactEmail(email) { | ||
this.document.info.contact = { email }; | ||
return this; | ||
} | ||
setLicense(name, url) { | ||
this.document.info.license = { name, url }; | ||
return this; | ||
} | ||
setHost(host) { | ||
this.document.host = host; | ||
return this; | ||
} | ||
setBasePath(basePath) { | ||
this.document.basePath = basePath.startsWith('/') | ||
? basePath | ||
: '/' + basePath; | ||
return this; | ||
} | ||
setExternalDoc(description, url) { | ||
this.document.externalDocs = { description, url }; | ||
return this; | ||
} | ||
setSchemes(...schemes) { | ||
this.document.schemes = schemes; | ||
return this; | ||
} | ||
addTag(name, description = '') { | ||
this.document.tags = this.document.tags.concat({ name, description }); | ||
return this; | ||
} | ||
addBearerAuth(name = 'Authorization', location = 'header', type = 'apiKey') { | ||
this.document.securityDefinitions.bearer = { | ||
type, | ||
name, | ||
in: location, | ||
}; | ||
return this; | ||
} | ||
addOAuth2(flow = 'password', authorizationUrl, tokenUrl, scopes) { | ||
this.document.securityDefinitions.oauth2 = { | ||
type: 'oauth2', | ||
flow, | ||
authorizationUrl, | ||
tokenUrl, | ||
scopes, | ||
}; | ||
return this; | ||
} | ||
build() { | ||
return this.document; | ||
} | ||
} | ||
exports.DocumentBuilder = DocumentBuilder; |
@@ -1,4 +0,10 @@ | ||
export declare const exploreGlobalApiConsumesMetadata: (metatype: any) => { | ||
consumes: any; | ||
export declare const exploreGlobalApiConsumesMetadata: ( | ||
metatype: any, | ||
) => { | ||
consumes: any; | ||
}; | ||
export declare const exploreApiConsumesMetadata: (instance: any, prototype: any, method: any) => any; | ||
export declare const exploreApiConsumesMetadata: ( | ||
instance: any, | ||
prototype: any, | ||
method: any, | ||
) => any; |
@@ -1,13 +0,22 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const defaultConsumes = 'application/json'; | ||
exports.exploreGlobalApiConsumesMetadata = (metatype) => { | ||
const consumes = Reflect.getMetadata(constants_1.DECORATORS.API_CONSUMES, metatype); | ||
return consumes ? { consumes } : { | ||
exports.exploreGlobalApiConsumesMetadata = metatype => { | ||
const consumes = Reflect.getMetadata( | ||
constants_1.DECORATORS.API_CONSUMES, | ||
metatype, | ||
); | ||
return consumes | ||
? { consumes } | ||
: { | ||
consumes: [defaultConsumes], | ||
}; | ||
}; | ||
}; | ||
exports.exploreApiConsumesMetadata = (instance, prototype, method) => { | ||
return Reflect.getMetadata(constants_1.DECORATORS.API_CONSUMES, method) || [defaultConsumes]; | ||
return ( | ||
Reflect.getMetadata(constants_1.DECORATORS.API_CONSUMES, method) || [ | ||
defaultConsumes, | ||
] | ||
); | ||
}; |
@@ -1,1 +0,5 @@ | ||
export declare const exploreApiOperationMetadata: (instance: any, prototype: any, method: any) => any; | ||
export declare const exploreApiOperationMetadata: ( | ||
instance: any, | ||
prototype: any, | ||
method: any, | ||
) => any; |
@@ -1,6 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
exports.exploreApiOperationMetadata = (instance, prototype, method) => { | ||
return Reflect.getMetadata(constants_1.DECORATORS.API_OPERATION, method); | ||
return Reflect.getMetadata(constants_1.DECORATORS.API_OPERATION, method); | ||
}; |
@@ -1,5 +0,13 @@ | ||
export declare const exploreApiParametersMetadata: (definitions: any, instance: any, prototype: any, method: any) => { | ||
parameters: any; | ||
export declare const exploreApiParametersMetadata: ( | ||
definitions: any, | ||
instance: any, | ||
prototype: any, | ||
method: any, | ||
) => { | ||
parameters: any; | ||
}; | ||
export declare const exploreModelDefinition: (type: any, definitions: any) => any; | ||
export declare const exploreModelDefinition: ( | ||
type: any, | ||
definitions: any, | ||
) => any; | ||
export declare const mapTypesToSwaggerTypes: (type: string) => string; |
@@ -1,141 +0,260 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
const constants_2 = require("@nestjs/common/constants"); | ||
const route_paramtypes_enum_1 = require("@nestjs/common/enums/route-paramtypes.enum"); | ||
const lodash_1 = require("lodash"); | ||
const shared_utils_1 = require("@nestjs/common/utils/shared.utils"); | ||
exports.exploreApiParametersMetadata = (definitions, instance, prototype, method) => { | ||
const implicitParameters = Reflect.getMetadata(constants_1.DECORATORS.API_PARAMETERS, method); | ||
const reflectedParameters = exploreApiReflectedParametersMetadata(instance, prototype, method); | ||
const noAnyImplicit = lodash_1.isNil(implicitParameters); | ||
if (noAnyImplicit && lodash_1.isNil(reflectedParameters)) { | ||
return undefined; | ||
} | ||
const allReflectedParameters = transformModelToProperties(reflectedParameters || []); | ||
const mergedParameters = noAnyImplicit ? | ||
allReflectedParameters : | ||
lodash_1.map(allReflectedParameters, (item) => lodash_1.assign(item, lodash_1.find(implicitParameters, ['name', item.name]))); | ||
const unionParameters = noAnyImplicit ? | ||
mergedParameters : | ||
lodash_1.unionWith(mergedParameters, implicitParameters, (arrVal, othVal) => { | ||
return arrVal.name === othVal.name && arrVal.in === othVal.in; | ||
}); | ||
const paramsWithDefinitions = mapModelsToDefinitons(unionParameters, definitions); | ||
const parameters = mapParametersTypes(paramsWithDefinitions); | ||
return parameters ? { parameters } : undefined; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const constants_2 = require('@nestjs/common/constants'); | ||
const route_paramtypes_enum_1 = require('@nestjs/common/enums/route-paramtypes.enum'); | ||
const lodash_1 = require('lodash'); | ||
const shared_utils_1 = require('@nestjs/common/utils/shared.utils'); | ||
exports.exploreApiParametersMetadata = ( | ||
definitions, | ||
instance, | ||
prototype, | ||
method, | ||
) => { | ||
const implicitParameters = Reflect.getMetadata( | ||
constants_1.DECORATORS.API_PARAMETERS, | ||
method, | ||
); | ||
const reflectedParameters = exploreApiReflectedParametersMetadata( | ||
instance, | ||
prototype, | ||
method, | ||
); | ||
const noAnyImplicit = lodash_1.isNil(implicitParameters); | ||
if (noAnyImplicit && lodash_1.isNil(reflectedParameters)) { | ||
return undefined; | ||
} | ||
const allReflectedParameters = transformModelToProperties( | ||
reflectedParameters || [], | ||
); | ||
const mergedParameters = noAnyImplicit | ||
? allReflectedParameters | ||
: lodash_1.map(allReflectedParameters, item => | ||
lodash_1.assign( | ||
item, | ||
lodash_1.find(implicitParameters, ['name', item.name]), | ||
), | ||
); | ||
const unionParameters = noAnyImplicit | ||
? mergedParameters | ||
: lodash_1.unionWith( | ||
mergedParameters, | ||
implicitParameters, | ||
(arrVal, othVal) => { | ||
return arrVal.name === othVal.name && arrVal.in === othVal.in; | ||
}, | ||
); | ||
const paramsWithDefinitions = mapModelsToDefinitons( | ||
unionParameters, | ||
definitions, | ||
); | ||
const parameters = mapParametersTypes(paramsWithDefinitions); | ||
return parameters ? { parameters } : undefined; | ||
}; | ||
const DEFAULT_PARAM_TOKEN = '_'; | ||
const exploreApiReflectedParametersMetadata = (instance, prototype, method) => { | ||
const types = Reflect.getMetadata(constants_2.PARAMTYPES_METADATA, instance, method.name); | ||
const parametersMetadata = Reflect.getMetadata(constants_2.ROUTE_ARGS_METADATA, instance, method.name) || {}; | ||
const parametersWithType = lodash_1.mapValues(parametersMetadata, (param) => ({ | ||
type: types[param.index], | ||
name: param.data, | ||
required: true, | ||
})); | ||
const parameters = lodash_1.omitBy(lodash_1.mapValues(parametersWithType, (val, key) => (Object.assign({}, val, { in: mapParamType(key) }))), (val) => val.in === DEFAULT_PARAM_TOKEN || val.name && val.in === 'body'); | ||
return !lodash_1.isEmpty(parameters) ? parameters : undefined; | ||
const types = Reflect.getMetadata( | ||
constants_2.PARAMTYPES_METADATA, | ||
instance, | ||
method.name, | ||
); | ||
const parametersMetadata = | ||
Reflect.getMetadata( | ||
constants_2.ROUTE_ARGS_METADATA, | ||
instance, | ||
method.name, | ||
) || {}; | ||
const parametersWithType = lodash_1.mapValues(parametersMetadata, param => ({ | ||
type: types[param.index], | ||
name: param.data, | ||
required: true, | ||
})); | ||
const parameters = lodash_1.omitBy( | ||
lodash_1.mapValues(parametersWithType, (val, key) => | ||
Object.assign({}, val, { in: mapParamType(key) }), | ||
), | ||
val => val.in === DEFAULT_PARAM_TOKEN || (val.name && val.in === 'body'), | ||
); | ||
return !lodash_1.isEmpty(parameters) ? parameters : undefined; | ||
}; | ||
const exploreModelProperties = (prototype) => { | ||
const props = Reflect.getMetadata(constants_1.DECORATORS.API_MODEL_PROPERTIES_ARRAY, prototype) || []; | ||
return props | ||
.filter(lodash_1.isString) | ||
.filter((prop) => prop.charAt(0) === ':' && !shared_utils_1.isFunction(prototype[prop])) | ||
.map((prop) => prop.slice(1)); | ||
const exploreModelProperties = prototype => { | ||
const props = | ||
Reflect.getMetadata( | ||
constants_1.DECORATORS.API_MODEL_PROPERTIES_ARRAY, | ||
prototype, | ||
) || []; | ||
return props | ||
.filter(lodash_1.isString) | ||
.filter( | ||
prop => | ||
prop.charAt(0) === ':' && !shared_utils_1.isFunction(prototype[prop]), | ||
) | ||
.map(prop => prop.slice(1)); | ||
}; | ||
const isBodyParameter = (param) => param.in === 'body'; | ||
const transformModelToProperties = (reflectedParameters) => { | ||
return lodash_1.flatMap(reflectedParameters, (param) => { | ||
const { prototype } = param.type; | ||
if (param.name) | ||
return param; | ||
if (isBodyParameter(param)) { | ||
const name = param.type && shared_utils_1.isFunction(param.type) ? param.type.name : param.type; | ||
return Object.assign({}, param, { name }); | ||
} | ||
const modelProperties = exploreModelProperties(prototype); | ||
return modelProperties.map((key) => { | ||
const reflectedParam = Reflect.getMetadata(constants_1.DECORATORS.API_MODEL_PROPERTIES, prototype, key) || {}; | ||
return Object.assign({}, param, reflectedParam, { name: key }); | ||
}); | ||
const isBodyParameter = param => param.in === 'body'; | ||
const transformModelToProperties = reflectedParameters => { | ||
return lodash_1.flatMap(reflectedParameters, param => { | ||
if (!param) { | ||
return null; | ||
} | ||
const { prototype } = param.type; | ||
if (param.name) return param; | ||
if (isBodyParameter(param)) { | ||
const name = | ||
param.type && shared_utils_1.isFunction(param.type) | ||
? param.type.name | ||
: param.type; | ||
return Object.assign({}, param, { name }); | ||
} | ||
const modelProperties = exploreModelProperties(prototype); | ||
return modelProperties.map(key => { | ||
const reflectedParam = | ||
Reflect.getMetadata( | ||
constants_1.DECORATORS.API_MODEL_PROPERTIES, | ||
prototype, | ||
key, | ||
) || {}; | ||
return Object.assign({}, param, reflectedParam, { name: key }); | ||
}); | ||
}); | ||
}; | ||
const transformToArrayModelProperty = (metadata, key, type) => (Object.assign({}, metadata, { name: key, type: 'array', items: Object.assign({}, type) })); | ||
const transformToArrayModelProperty = (metadata, key, type) => | ||
Object.assign({}, metadata, { | ||
name: key, | ||
type: 'array', | ||
items: Object.assign({}, type), | ||
}); | ||
exports.exploreModelDefinition = (type, definitions) => { | ||
const { prototype } = type; | ||
const modelProperties = exploreModelProperties(prototype); | ||
const propertiesWithType = modelProperties.map((key) => { | ||
const metadata = Reflect.getMetadata(constants_1.DECORATORS.API_MODEL_PROPERTIES, prototype, key) || {}; | ||
const defaultTypes = [String, Boolean, Number, Object, Array]; | ||
if (shared_utils_1.isFunction(metadata.type) && !defaultTypes.find((defaultType) => defaultType === metadata.type)) { | ||
const nestedModelName = exports.exploreModelDefinition(metadata.type, definitions); | ||
const $ref = getDefinitionPath(nestedModelName); | ||
if (metadata.isArray) { | ||
return transformToArrayModelProperty(metadata, key, { $ref }); | ||
} | ||
return { name: key, $ref }; | ||
} | ||
const metatype = metadata.type && shared_utils_1.isFunction(metadata.type) ? metadata.type.name : metadata.type; | ||
const swaggerType = exports.mapTypesToSwaggerTypes(metatype); | ||
if (metadata.isArray) { | ||
return transformToArrayModelProperty(metadata, key, { type: swaggerType }); | ||
} | ||
return Object.assign({}, metadata, { name: key, type: swaggerType }); | ||
}); | ||
const typeDefinition = { | ||
type: 'object', | ||
properties: lodash_1.mapValues(lodash_1.keyBy(propertiesWithType, 'name'), (property) => lodash_1.omit(property, ['name', 'isArray', 'required'])) | ||
}; | ||
const typeDefinitionRequiredFields = propertiesWithType | ||
.filter((property) => property.required != false) | ||
.map((property) => property.name); | ||
if (typeDefinitionRequiredFields.length > 0) { | ||
typeDefinition['required'] = typeDefinitionRequiredFields; | ||
const { prototype } = type; | ||
const modelProperties = exploreModelProperties(prototype); | ||
const propertiesWithType = modelProperties.map(key => { | ||
const metadata = | ||
Reflect.getMetadata( | ||
constants_1.DECORATORS.API_MODEL_PROPERTIES, | ||
prototype, | ||
key, | ||
) || {}; | ||
const defaultTypes = [String, Boolean, Number, Object, Array]; | ||
if ( | ||
shared_utils_1.isFunction(metadata.type) && | ||
!defaultTypes.find(defaultType => defaultType === metadata.type) | ||
) { | ||
const nestedModelName = exports.exploreModelDefinition( | ||
metadata.type, | ||
definitions, | ||
); | ||
const $ref = getDefinitionPath(nestedModelName); | ||
if (metadata.isArray) { | ||
return transformToArrayModelProperty(metadata, key, { $ref }); | ||
} | ||
return Object.assign({}, metadata, { name: key, $ref }); | ||
} | ||
definitions.push({ | ||
[type.name]: typeDefinition | ||
const metatype = | ||
metadata.type && shared_utils_1.isFunction(metadata.type) | ||
? metadata.type.name | ||
: metadata.type; | ||
const swaggerType = exports.mapTypesToSwaggerTypes(metatype); | ||
if (metadata.isArray) { | ||
return transformToArrayModelProperty(metadata, key, { | ||
type: swaggerType, | ||
}); | ||
} | ||
return Object.assign({}, metadata, { | ||
name: key, | ||
type: metadata.enum ? getEnumType(metadata.enum) : swaggerType, | ||
}); | ||
return type.name; | ||
}); | ||
const typeDefinition = { | ||
type: 'object', | ||
properties: lodash_1.mapValues( | ||
lodash_1.keyBy(propertiesWithType, 'name'), | ||
property => lodash_1.omit(property, ['name', 'isArray', 'required']), | ||
), | ||
}; | ||
const typeDefinitionRequiredFields = propertiesWithType | ||
.filter(property => property.required != false) | ||
.map(property => property.name); | ||
if (typeDefinitionRequiredFields.length > 0) { | ||
typeDefinition['required'] = typeDefinitionRequiredFields; | ||
} | ||
definitions.push({ | ||
[type.name]: typeDefinition, | ||
}); | ||
return type.name; | ||
}; | ||
const mapParamType = (key) => { | ||
const keyPair = key.split(':'); | ||
switch (Number(keyPair[0])) { | ||
case route_paramtypes_enum_1.RouteParamtypes.BODY: return 'body'; | ||
case route_paramtypes_enum_1.RouteParamtypes.PARAM: return 'path'; | ||
case route_paramtypes_enum_1.RouteParamtypes.QUERY: return 'query'; | ||
default: return DEFAULT_PARAM_TOKEN; | ||
} | ||
const getEnumType = values => { | ||
const hasString = values.filter(lodash_1.isString).length > 0; | ||
return hasString ? 'string' : 'number'; | ||
}; | ||
const mapParametersTypes = (parameters) => parameters.map((param) => { | ||
const mapParamType = key => { | ||
const keyPair = key.split(':'); | ||
switch (Number(keyPair[0])) { | ||
case route_paramtypes_enum_1.RouteParamtypes.BODY: | ||
return 'body'; | ||
case route_paramtypes_enum_1.RouteParamtypes.PARAM: | ||
return 'path'; | ||
case route_paramtypes_enum_1.RouteParamtypes.QUERY: | ||
return 'query'; | ||
case route_paramtypes_enum_1.RouteParamtypes.HEADERS: | ||
return 'header'; | ||
default: | ||
return DEFAULT_PARAM_TOKEN; | ||
} | ||
}; | ||
const mapParametersTypes = parameters => | ||
parameters.map(param => { | ||
if (isBodyParameter(param)) { | ||
return param; | ||
return param; | ||
} | ||
const { type } = param; | ||
const paramWithStringifiedType = lodash_1.pickBy(Object.assign({}, param, { type: type && shared_utils_1.isFunction(type) ? exports.mapTypesToSwaggerTypes(type.name) : exports.mapTypesToSwaggerTypes(type) }), lodash_1.negate(shared_utils_1.isUndefined)); | ||
const paramWithStringifiedType = lodash_1.pickBy( | ||
Object.assign({}, param, { | ||
type: | ||
type && shared_utils_1.isFunction(type) | ||
? exports.mapTypesToSwaggerTypes(type.name) | ||
: exports.mapTypesToSwaggerTypes(type), | ||
}), | ||
lodash_1.negate(shared_utils_1.isUndefined), | ||
); | ||
if (paramWithStringifiedType.isArray) { | ||
return Object.assign({}, paramWithStringifiedType, { type: 'array', items: { | ||
type: exports.mapTypesToSwaggerTypes(param.type), | ||
} }); | ||
return Object.assign({}, paramWithStringifiedType, { | ||
type: 'array', | ||
items: { | ||
type: exports.mapTypesToSwaggerTypes(param.type), | ||
}, | ||
}); | ||
} | ||
return paramWithStringifiedType; | ||
}); | ||
exports.mapTypesToSwaggerTypes = (type) => { | ||
if (!(type && type.charAt)) { | ||
return ''; | ||
} | ||
return type.charAt(0).toLowerCase() + type.slice(1); | ||
}); | ||
exports.mapTypesToSwaggerTypes = type => { | ||
if (!(type && type.charAt)) { | ||
return ''; | ||
} | ||
return type.charAt(0).toLowerCase() + type.slice(1); | ||
}; | ||
const getDefinitionPath = (modelName) => `#/definitions/${modelName}`; | ||
const getDefinitionPath = modelName => `#/definitions/${modelName}`; | ||
const mapModelsToDefinitons = (parameters, definitions) => { | ||
return parameters.map((param) => { | ||
if (!isBodyParameter(param)) { | ||
return param; | ||
} | ||
const modelName = exports.exploreModelDefinition(param.type, definitions); | ||
const name = param.name ? param.name : modelName; | ||
return Object.assign({}, param, { name, schema: { | ||
$ref: getDefinitionPath(modelName), | ||
} }); | ||
return parameters.map(param => { | ||
if (!isBodyParameter(param)) { | ||
return param; | ||
} | ||
const modelName = exports.exploreModelDefinition(param.type, definitions); | ||
const name = param.name ? param.name : modelName; | ||
const schema = { | ||
$ref: getDefinitionPath(modelName), | ||
}; | ||
if (param.isArray) { | ||
return Object.assign({}, param, { | ||
name, | ||
schema: { | ||
type: 'array', | ||
items: schema, | ||
}, | ||
}); | ||
} | ||
return Object.assign({}, param, { | ||
name, | ||
schema, | ||
}); | ||
}); | ||
}; |
@@ -1,4 +0,10 @@ | ||
export declare const exploreGlobalApiProducesMetadata: (metatype: any) => { | ||
produces: any; | ||
export declare const exploreGlobalApiProducesMetadata: ( | ||
metatype: any, | ||
) => { | ||
produces: any; | ||
}; | ||
export declare const exploreApiProducesMetadata: (instance: any, prototype: any, method: any) => any; | ||
export declare const exploreApiProducesMetadata: ( | ||
instance: any, | ||
prototype: any, | ||
method: any, | ||
) => any; |
@@ -1,13 +0,22 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
const defaultProduces = 'application/json'; | ||
exports.exploreGlobalApiProducesMetadata = (metatype) => { | ||
const produces = Reflect.getMetadata(constants_1.DECORATORS.API_PRODUCES, metatype); | ||
return produces ? { produces } : { | ||
exports.exploreGlobalApiProducesMetadata = metatype => { | ||
const produces = Reflect.getMetadata( | ||
constants_1.DECORATORS.API_PRODUCES, | ||
metatype, | ||
); | ||
return produces | ||
? { produces } | ||
: { | ||
produces: [defaultProduces], | ||
}; | ||
}; | ||
}; | ||
exports.exploreApiProducesMetadata = (instance, prototype, method) => { | ||
return Reflect.getMetadata(constants_1.DECORATORS.API_PRODUCES, method) || [defaultProduces]; | ||
return ( | ||
Reflect.getMetadata(constants_1.DECORATORS.API_PRODUCES, method) || [ | ||
defaultProduces, | ||
] | ||
); | ||
}; |
@@ -1,6 +0,20 @@ | ||
export declare const exploreGlobalApiResponseMetadata: (definitions: any, metatype: any) => { | ||
responses: any; | ||
export declare const exploreGlobalApiResponseMetadata: ( | ||
definitions: any, | ||
metatype: any, | ||
) => { | ||
responses: any; | ||
}; | ||
export declare const exploreApiResponseMetadata: (definitions: any, instance: any, prototype: any, method: any) => any; | ||
export declare const toArrayResponseWithDefinition: (response: any, name: any) => any; | ||
export declare const toResponseWithDefinition: (response: any, name: any) => any; | ||
export declare const exploreApiResponseMetadata: ( | ||
definitions: any, | ||
instance: any, | ||
prototype: any, | ||
method: any, | ||
) => any; | ||
export declare const toArrayResponseWithDefinition: ( | ||
response: any, | ||
name: any, | ||
) => any; | ||
export declare const toResponseWithDefinition: ( | ||
response: any, | ||
name: any, | ||
) => any; |
@@ -1,47 +0,79 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const api_parameters_explorer_1 = require("./api-parameters.explorer"); | ||
const lodash_1 = require("lodash"); | ||
const constants_1 = require("../constants"); | ||
const shared_utils_1 = require("@nestjs/common/utils/shared.utils"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const api_parameters_explorer_1 = require('./api-parameters.explorer'); | ||
const lodash_1 = require('lodash'); | ||
const constants_1 = require('../constants'); | ||
const shared_utils_1 = require('@nestjs/common/utils/shared.utils'); | ||
exports.exploreGlobalApiResponseMetadata = (definitions, metatype) => { | ||
const responses = Reflect.getMetadata(constants_1.DECORATORS.API_RESPONSE, metatype); | ||
return responses ? { | ||
const responses = Reflect.getMetadata( | ||
constants_1.DECORATORS.API_RESPONSE, | ||
metatype, | ||
); | ||
return responses | ||
? { | ||
responses: mapResponsesToSwaggerResponses(responses, definitions), | ||
} : undefined; | ||
} | ||
: undefined; | ||
}; | ||
exports.exploreApiResponseMetadata = (definitions, instance, prototype, method) => { | ||
const responses = Reflect.getMetadata(constants_1.DECORATORS.API_RESPONSE, method); | ||
if (!responses) { | ||
return undefined; | ||
} | ||
return mapResponsesToSwaggerResponses(responses, definitions); | ||
exports.exploreApiResponseMetadata = ( | ||
definitions, | ||
instance, | ||
prototype, | ||
method, | ||
) => { | ||
const responses = Reflect.getMetadata( | ||
constants_1.DECORATORS.API_RESPONSE, | ||
method, | ||
); | ||
if (!responses) { | ||
return undefined; | ||
} | ||
return mapResponsesToSwaggerResponses(responses, definitions); | ||
}; | ||
const mapResponsesToSwaggerResponses = (responses, definitions) => lodash_1.mapValues(responses, (response) => { | ||
const mapResponsesToSwaggerResponses = (responses, definitions) => | ||
lodash_1.mapValues(responses, response => { | ||
const { type, isArray } = response; | ||
response = lodash_1.omit(response, ['isArray']); | ||
if (!type) | ||
return response; | ||
if (!type) return response; | ||
const defaultTypes = [String, Boolean, Number, Object, Array]; | ||
if (!(shared_utils_1.isFunction(type) && !defaultTypes.find((defaultType) => defaultType === type))) { | ||
const metatype = type && shared_utils_1.isFunction(type) ? type.name : type; | ||
const swaggerType = api_parameters_explorer_1.mapTypesToSwaggerTypes(metatype); | ||
return Object.assign({}, response, { schema: { | ||
type: swaggerType, | ||
} }); | ||
if ( | ||
!( | ||
shared_utils_1.isFunction(type) && | ||
!defaultTypes.find(defaultType => defaultType === type) | ||
) | ||
) { | ||
const metatype = | ||
type && shared_utils_1.isFunction(type) ? type.name : type; | ||
const swaggerType = api_parameters_explorer_1.mapTypesToSwaggerTypes( | ||
metatype, | ||
); | ||
return Object.assign({}, response, { | ||
schema: { | ||
type: swaggerType, | ||
}, | ||
}); | ||
} | ||
const name = api_parameters_explorer_1.exploreModelDefinition(type, definitions); | ||
const name = api_parameters_explorer_1.exploreModelDefinition( | ||
type, | ||
definitions, | ||
); | ||
if (isArray) { | ||
return exports.toArrayResponseWithDefinition(response, name); | ||
return exports.toArrayResponseWithDefinition(response, name); | ||
} | ||
return exports.toResponseWithDefinition(response, name); | ||
}); | ||
exports.toArrayResponseWithDefinition = (response, name) => (Object.assign({}, response, { schema: { | ||
type: 'array', | ||
items: { | ||
$ref: `#/definitions/${name}`, | ||
}, | ||
} })); | ||
exports.toResponseWithDefinition = (response, name) => (Object.assign({}, response, { schema: { | ||
}); | ||
exports.toArrayResponseWithDefinition = (response, name) => | ||
Object.assign({}, response, { | ||
schema: { | ||
type: 'array', | ||
items: { | ||
$ref: `#/definitions/${name}`, | ||
} })); | ||
}, | ||
}, | ||
}); | ||
exports.toResponseWithDefinition = (response, name) => | ||
Object.assign({}, response, { | ||
schema: { | ||
$ref: `#/definitions/${name}`, | ||
}, | ||
}); |
@@ -1,4 +0,6 @@ | ||
export declare const exploreGlobalApiUseTagsMetadata: (metatype: any) => { | ||
tags: any; | ||
export declare const exploreGlobalApiUseTagsMetadata: ( | ||
metatype: any, | ||
) => { | ||
tags: any; | ||
}; | ||
export declare const exploreApiUseTagsMetadata: (metatype: any) => any; |
@@ -1,10 +0,13 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("../constants"); | ||
exports.exploreGlobalApiUseTagsMetadata = (metatype) => { | ||
const tags = Reflect.getMetadata(constants_1.DECORATORS.API_USE_TAGS, metatype); | ||
return tags ? { tags } : undefined; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('../constants'); | ||
exports.exploreGlobalApiUseTagsMetadata = metatype => { | ||
const tags = Reflect.getMetadata( | ||
constants_1.DECORATORS.API_USE_TAGS, | ||
metatype, | ||
); | ||
return tags ? { tags } : undefined; | ||
}; | ||
exports.exploreApiUseTagsMetadata = (metatype) => { | ||
return Reflect.getMetadata(constants_1.DECORATORS.API_USE_TAGS, metatype); | ||
exports.exploreApiUseTagsMetadata = metatype => { | ||
return Reflect.getMetadata(constants_1.DECORATORS.API_USE_TAGS, metatype); | ||
}; |
import { SwaggerScheme } from '../interfaces/swagger-base-config.interface'; | ||
export declare const documentBase: { | ||
swagger: string; | ||
info: { | ||
description: string; | ||
version: string; | ||
title: string; | ||
}; | ||
basePath: string; | ||
tags: any[]; | ||
schemes: SwaggerScheme[]; | ||
swagger: string; | ||
info: { | ||
description: string; | ||
version: string; | ||
title: string; | ||
}; | ||
basePath: string; | ||
tags: any[]; | ||
schemes: SwaggerScheme[]; | ||
securityDefinitions: {}; | ||
}; |
@@ -1,13 +0,14 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.documentBase = { | ||
swagger: '2.0', | ||
info: { | ||
description: '', | ||
version: '1.0.0', | ||
title: '', | ||
}, | ||
basePath: '/', | ||
tags: [], | ||
schemes: ['http'], | ||
swagger: '2.0', | ||
info: { | ||
description: '', | ||
version: '1.0.0', | ||
title: '', | ||
}, | ||
basePath: '/', | ||
tags: [], | ||
schemes: ['http'], | ||
securityDefinitions: {}, | ||
}; |
export * from './decorators'; | ||
export * from './swagger-module'; | ||
export * from './document-builder'; |
12
index.js
@@ -1,8 +0,8 @@ | ||
"use strict"; | ||
'use strict'; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./decorators")); | ||
__export(require("./swagger-module")); | ||
__export(require("./document-builder")); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
__export(require('./decorators')); | ||
__export(require('./swagger-module')); | ||
__export(require('./document-builder')); |
export * from './swagger-base-config.interface'; | ||
export * from './swagger-document.interface'; | ||
export * from './swagger-custom-options.interface'; |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); |
export interface SwaggerBaseConfig { | ||
swagger?: string; | ||
info?: { | ||
description?: string; | ||
version?: string; | ||
title?: string; | ||
termsOfService?: string; | ||
contact?: { | ||
email: string; | ||
}; | ||
license?: { | ||
name: string; | ||
url: string; | ||
}; | ||
swagger?: string; | ||
info?: { | ||
description?: string; | ||
version?: string; | ||
title?: string; | ||
termsOfService?: string; | ||
contact?: { | ||
email: string; | ||
}; | ||
tags?: { | ||
name: string; | ||
description: string; | ||
}[]; | ||
host?: string; | ||
basePath?: string; | ||
externalDocs?: { | ||
description: string; | ||
url: string; | ||
license?: { | ||
name: string; | ||
url: string; | ||
}; | ||
schemes?: SwaggerScheme[]; | ||
securityDefinitions?: { | ||
bearer: { | ||
type: 'apiKey'; | ||
name: string; | ||
in: 'body' | 'query' | 'header'; | ||
}; | ||
}; | ||
tags?: { | ||
name: string; | ||
description: string; | ||
}[]; | ||
host?: string; | ||
basePath?: string; | ||
externalDocs?: { | ||
description: string; | ||
url: string; | ||
}; | ||
schemes?: SwaggerScheme[]; | ||
securityDefinitions?: { | ||
bearer?: { | ||
type: string; | ||
name: string; | ||
in: 'body' | 'query' | 'header'; | ||
}; | ||
oauth2?: { | ||
type: 'oauth2'; | ||
flow: 'implicit' | 'password' | 'application' | 'accessCode'; | ||
authorizationUrl?: string; | ||
tokenUrl?: string; | ||
scopes?: object; | ||
}; | ||
}; | ||
} | ||
export declare type SwaggerScheme = 'http' | 'https'; |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); |
import { SwaggerBaseConfig } from './swagger-base-config.interface'; | ||
export interface SwaggerDocument extends SwaggerBaseConfig { | ||
definitions: any; | ||
paths: any; | ||
definitions: any; | ||
paths: any; | ||
} |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "@nestjs/swagger", | ||
"version": "1.1.4", | ||
"description": "Nest - modern, fast, powerful node.js web framework (@swagger)", | ||
"version": "1.2.0", | ||
"description": | ||
"Nest - modern, fast, powerful node.js web framework (@swagger)", | ||
"author": "Kamil Mysliwiec", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.json" | ||
}, | ||
"dependencies": { | ||
"lodash": "4.17.4", | ||
"swagger-ui-express": "2.0.11" | ||
"lodash": "^4.17.4", | ||
"swagger-ui-express": "^2.0.11" | ||
}, | ||
"peerDependencies": { | ||
"@nestjs/common": "^4.*", | ||
"@nestjs/core": "^4.*", | ||
"reflect-metadata": "0.1.10" | ||
"@nestjs/common": "^4.6.6", | ||
"@nestjs/core": "^4.6.6" | ||
} | ||
} |
<p align="center"> | ||
<a href="http://nestjs.com/" target="blank"><img src="http://kamilmysliwiec.com/public/nest-logo.png#1" alt="Nest Logo" /></a> | ||
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a> | ||
</p> | ||
@@ -9,14 +9,17 @@ | ||
[linux-url]: https://travis-ci.org/nestjs/nest | ||
<p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable web applications.</p> | ||
<p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable server-side applications, heavily inspired by <a href="https://angular.io" target="blank">Angular</a>.</p> | ||
<p align="center"> | ||
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/swagger.svg" alt="NPM Version" /></a> | ||
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a> | ||
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a> | ||
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/swagger.svg" alt="NPM Downloads" /></a> | ||
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a> | ||
<a href="https://travis-ci.org/nestjs/nest"><img src="https://api.travis-ci.org/nestjs/nest.svg?branch=master" alt="Travis" /></a> | ||
<a href="https://travis-ci.org/nestjs/nest"><img src="https://img.shields.io/travis/nestjs/nest/master.svg?label=linux" alt="Linux" /></a> | ||
<a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#2" alt="Coverage" /></a> | ||
<a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#5" alt="Coverage" /></a> | ||
<a href="https://gitter.im/nestjs/nestjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge"><img src="https://badges.gitter.im/nestjs/nestjs.svg" alt="Gitter" /></a> | ||
<a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a> | ||
<a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a> | ||
<a href="https://paypal.me/kamilmysliwiec"><img src="https://img.shields.io/badge/Donate-PayPal-dc3d53.svg"/></a> | ||
<img src="https://img.shields.io/badge/đ-Production Ready-78c7ff.svg"/> | ||
<a href="https://twitter.com/nestframework"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a> | ||
</p> | ||
@@ -28,3 +31,3 @@ <!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer) | ||
This's an [OpenAPI (Swagger)](https://www.openapis.org/) module for [Nest](https://github.com/nestjs/nest). | ||
This is an [OpenAPI (Swagger)](https://www.openapis.org/) module for [Nest](https://github.com/nestjs/nest). | ||
@@ -34,3 +37,3 @@ ## Installation | ||
```bash | ||
$ npm i --save @nestjs/swagger reflect-metadata | ||
$ npm i --save @nestjs/swagger | ||
``` | ||
@@ -42,5 +45,14 @@ | ||
## People | ||
## Support | ||
- Author - [Kamil MyĆliwiec](https://kamilmysliwiec.com) | ||
- Website - [https://nestjs.com](https://nestjs.com/) | ||
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://opencollective.com/nest). | ||
## Stay in touch | ||
* Author - [Kamil MyĆliwiec](https://kamilmysliwiec.com) | ||
* Website - [https://nestjs.com](https://nestjs.com/) | ||
* Twitter - [@nestframework](https://twitter.com/nestframework) | ||
## License | ||
Nest is [MIT licensed](LICENSE). |
import { Controller } from '@nestjs/common/interfaces'; | ||
import { InstanceWrapper } from '@nestjs/core/injector/container'; | ||
export declare class SwaggerExplorer { | ||
private readonly metadataScanner; | ||
private readonly modelsDefinitions; | ||
exploreController({instance, metatype}: InstanceWrapper<Controller>): any[]; | ||
getModelsDefinitons(): any[]; | ||
private generateDenormalizedDocument(metatype, prototype, instance, explorersSchema); | ||
private exploreGlobalMetadata(metatype); | ||
private exploreRoutePathAndMethod(instance, prototype, method, globalPath); | ||
private reflectControllerPath(metatype); | ||
private validateRoutePath(path); | ||
private readonly metadataScanner; | ||
private readonly modelsDefinitions; | ||
exploreController({ instance, metatype }: InstanceWrapper<Controller>): any[]; | ||
getModelsDefinitons(): any[]; | ||
private generateDenormalizedDocument( | ||
metatype, | ||
prototype, | ||
instance, | ||
explorersSchema, | ||
); | ||
private exploreGlobalMetadata(metatype); | ||
private exploreRoutePathAndMethod(instance, prototype, method, globalPath); | ||
private reflectControllerPath(metatype); | ||
private validateRoutePath(path); | ||
private mergeMetadata(globalMetadata, methodMetadata); | ||
} |
@@ -1,95 +0,154 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const constants_1 = require("@nestjs/common/constants"); | ||
const api_bearer_explorer_1 = require("./explorers/api-bearer.explorer"); | ||
const api_consumes_explorer_1 = require("./explorers/api-consumes.explorer"); | ||
const api_produces_explorer_1 = require("./explorers/api-produces.explorer"); | ||
const api_response_explorer_1 = require("./explorers/api-response.explorer"); | ||
const api_use_tags_explorer_1 = require("./explorers/api-use-tags.explorer"); | ||
const lodash_1 = require("lodash"); | ||
const shared_utils_1 = require("@nestjs/common/utils/shared.utils"); | ||
const metadata_scanner_1 = require("@nestjs/core/metadata-scanner"); | ||
const common_1 = require("@nestjs/common"); | ||
const api_operation_explorer_1 = require("./explorers/api-operation.explorer"); | ||
const api_parameters_explorer_1 = require("./explorers/api-parameters.explorer"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const constants_1 = require('@nestjs/common/constants'); | ||
const api_consumes_explorer_1 = require('./explorers/api-consumes.explorer'); | ||
const api_produces_explorer_1 = require('./explorers/api-produces.explorer'); | ||
const api_response_explorer_1 = require('./explorers/api-response.explorer'); | ||
const api_security_explorer_1 = require('./explorers/api-security.explorer'); | ||
const api_use_tags_explorer_1 = require('./explorers/api-use-tags.explorer'); | ||
const lodash_1 = require('lodash'); | ||
const shared_utils_1 = require('@nestjs/common/utils/shared.utils'); | ||
const metadata_scanner_1 = require('@nestjs/core/metadata-scanner'); | ||
const common_1 = require('@nestjs/common'); | ||
const api_operation_explorer_1 = require('./explorers/api-operation.explorer'); | ||
const api_parameters_explorer_1 = require('./explorers/api-parameters.explorer'); | ||
class SwaggerExplorer { | ||
constructor() { | ||
this.metadataScanner = new metadata_scanner_1.MetadataScanner(); | ||
this.modelsDefinitions = []; | ||
constructor() { | ||
this.metadataScanner = new metadata_scanner_1.MetadataScanner(); | ||
this.modelsDefinitions = []; | ||
} | ||
exploreController({ instance, metatype }) { | ||
const prototype = Object.getPrototypeOf(instance); | ||
const explorersSchema = { | ||
root: [ | ||
this.exploreRoutePathAndMethod, | ||
api_operation_explorer_1.exploreApiOperationMetadata, | ||
api_parameters_explorer_1.exploreApiParametersMetadata.bind( | ||
null, | ||
this.modelsDefinitions, | ||
), | ||
], | ||
produces: [api_produces_explorer_1.exploreApiProducesMetadata], | ||
consumes: [api_consumes_explorer_1.exploreApiConsumesMetadata], | ||
security: [api_security_explorer_1.exploreApiSecurityMetadata], | ||
tags: [api_use_tags_explorer_1.exploreApiUseTagsMetadata], | ||
responses: [ | ||
api_response_explorer_1.exploreApiResponseMetadata.bind( | ||
null, | ||
this.modelsDefinitions, | ||
), | ||
], | ||
}; | ||
return this.generateDenormalizedDocument( | ||
metatype, | ||
prototype, | ||
instance, | ||
explorersSchema, | ||
); | ||
} | ||
getModelsDefinitons() { | ||
return this.modelsDefinitions; | ||
} | ||
generateDenormalizedDocument(metatype, prototype, instance, explorersSchema) { | ||
const path = this.validateRoutePath(this.reflectControllerPath(metatype)); | ||
const self = this; | ||
const globalMetadata = this.exploreGlobalMetadata(metatype); | ||
const denormalizedPaths = this.metadataScanner.scanFromPrototype( | ||
instance, | ||
prototype, | ||
name => { | ||
const targetCallback = prototype[name]; | ||
const methodMetadata = lodash_1.mapValues(explorersSchema, explorers => | ||
explorers.reduce((metadata, fn) => { | ||
const exploredMetadata = fn.call( | ||
self, | ||
instance, | ||
prototype, | ||
targetCallback, | ||
path, | ||
); | ||
if (!exploredMetadata) { | ||
return metadata; | ||
} | ||
if (!lodash_1.isArray(exploredMetadata)) { | ||
return Object.assign({}, metadata, exploredMetadata); | ||
} | ||
return lodash_1.isArray(metadata) | ||
? [...metadata, ...exploredMetadata] | ||
: exploredMetadata; | ||
}, {}), | ||
); | ||
const mergedMethodMetadata = this.mergeMetadata( | ||
globalMetadata, | ||
lodash_1.omitBy(methodMetadata, lodash_1.isEmpty), | ||
); | ||
return Object.assign( | ||
{ responses: {} }, | ||
globalMetadata, | ||
mergedMethodMetadata, | ||
); | ||
}, | ||
); | ||
return denormalizedPaths; | ||
} | ||
exploreGlobalMetadata(metatype) { | ||
const globalExplorers = [ | ||
api_produces_explorer_1.exploreGlobalApiProducesMetadata, | ||
api_use_tags_explorer_1.exploreGlobalApiUseTagsMetadata, | ||
api_consumes_explorer_1.exploreGlobalApiConsumesMetadata, | ||
api_security_explorer_1.exploreGlobalApiSecurityMetadata, | ||
api_response_explorer_1.exploreGlobalApiResponseMetadata.bind( | ||
null, | ||
this.modelsDefinitions, | ||
), | ||
]; | ||
const globalMetadata = globalExplorers | ||
.map(explorer => explorer.call(explorer, metatype)) | ||
.filter(val => !shared_utils_1.isUndefined(val)) | ||
.reduce((curr, next) => Object.assign({}, curr, next), {}); | ||
return globalMetadata; | ||
} | ||
exploreRoutePathAndMethod(instance, prototype, method, globalPath) { | ||
const routePath = Reflect.getMetadata(constants_1.PATH_METADATA, method); | ||
if (shared_utils_1.isUndefined(routePath)) { | ||
return undefined; | ||
} | ||
exploreController({ instance, metatype }) { | ||
const prototype = Object.getPrototypeOf(instance); | ||
const explorersSchema = { | ||
root: [ | ||
this.exploreRoutePathAndMethod, | ||
api_operation_explorer_1.exploreApiOperationMetadata, | ||
api_parameters_explorer_1.exploreApiParametersMetadata.bind(null, this.modelsDefinitions), | ||
], | ||
produces: [api_produces_explorer_1.exploreApiProducesMetadata], | ||
consumes: [api_consumes_explorer_1.exploreApiConsumesMetadata], | ||
security: [api_bearer_explorer_1.exploreApiBearerMetadata], | ||
tags: [api_use_tags_explorer_1.exploreApiUseTagsMetadata], | ||
responses: [api_response_explorer_1.exploreApiResponseMetadata.bind(null, this.modelsDefinitions)], | ||
}; | ||
return this.generateDenormalizedDocument(metatype, prototype, instance, explorersSchema); | ||
const requestMethod = Reflect.getMetadata( | ||
constants_1.METHOD_METADATA, | ||
method, | ||
); | ||
const fullPath = globalPath + this.validateRoutePath(routePath); | ||
return { | ||
method: common_1.RequestMethod[requestMethod].toLowerCase(), | ||
path: fullPath === '' ? '/' : fullPath, | ||
}; | ||
} | ||
reflectControllerPath(metatype) { | ||
return Reflect.getMetadata(constants_1.PATH_METADATA, metatype); | ||
} | ||
validateRoutePath(path) { | ||
if (shared_utils_1.isUndefined(path)) { | ||
return ''; | ||
} | ||
getModelsDefinitons() { | ||
return this.modelsDefinitions; | ||
} | ||
generateDenormalizedDocument(metatype, prototype, instance, explorersSchema) { | ||
const path = this.validateRoutePath(this.reflectControllerPath(metatype)); | ||
const self = this; | ||
const globalMetadata = this.exploreGlobalMetadata(metatype); | ||
const denormalizedPaths = this.metadataScanner.scanFromPrototype(instance, prototype, (name) => { | ||
const targetCallback = prototype[name]; | ||
const methodMetadata = lodash_1.mapValues(explorersSchema, (explorers) => explorers.reduce((metadata, fn) => { | ||
const exploredMetadata = fn.call(self, instance, prototype, targetCallback, path); | ||
if (!exploredMetadata) { | ||
return metadata; | ||
} | ||
if (!lodash_1.isArray(exploredMetadata)) { | ||
return Object.assign({}, metadata, exploredMetadata); | ||
} | ||
return lodash_1.isArray(metadata) ? [...metadata, ...exploredMetadata] : exploredMetadata; | ||
}, {})); | ||
return Object.assign({ responses: {} }, globalMetadata, lodash_1.omitBy(methodMetadata, lodash_1.isEmpty)); | ||
}); | ||
return denormalizedPaths; | ||
} | ||
exploreGlobalMetadata(metatype) { | ||
const globalExplorers = [ | ||
api_produces_explorer_1.exploreGlobalApiProducesMetadata, | ||
api_use_tags_explorer_1.exploreGlobalApiUseTagsMetadata, | ||
api_consumes_explorer_1.exploreGlobalApiConsumesMetadata, | ||
api_bearer_explorer_1.exploreGlobalApiBearerMetadata, | ||
api_response_explorer_1.exploreGlobalApiResponseMetadata.bind(null, this.modelsDefinitions), | ||
]; | ||
const globalMetadata = (globalExplorers.map((explorer) => explorer.call(explorer, metatype)).filter((val) => !shared_utils_1.isUndefined(val))).reduce((curr, next) => (Object.assign({}, curr, next)), {}); | ||
return globalMetadata; | ||
} | ||
exploreRoutePathAndMethod(instance, prototype, method, globalPath) { | ||
const routePath = Reflect.getMetadata(constants_1.PATH_METADATA, method); | ||
if (shared_utils_1.isUndefined(routePath)) { | ||
return undefined; | ||
} | ||
const requestMethod = Reflect.getMetadata(constants_1.METHOD_METADATA, method); | ||
const fullPath = globalPath + this.validateRoutePath(routePath); | ||
return { | ||
method: common_1.RequestMethod[requestMethod].toLowerCase(), | ||
path: fullPath === '' ? '/' : fullPath, | ||
}; | ||
} | ||
reflectControllerPath(metatype) { | ||
return Reflect.getMetadata(constants_1.PATH_METADATA, metatype); | ||
} | ||
validateRoutePath(path) { | ||
if (shared_utils_1.isUndefined(path)) { | ||
return ''; | ||
} | ||
const pathWithParams = path.replace(/([:].*?[^\/]*)/g, (str) => { | ||
return `{${str.slice(1, str.length)}}`; | ||
}); | ||
return pathWithParams === '/' ? '' : shared_utils_1.validatePath(pathWithParams); | ||
} | ||
const pathWithParams = path.replace(/([:].*?[^\/]*)/g, str => { | ||
str = str.replace(/\(.*\)$/, ''); | ||
return `{${str.slice(1, str.length)}}`; | ||
}); | ||
return pathWithParams === '/' | ||
? '' | ||
: shared_utils_1.validatePath(pathWithParams); | ||
} | ||
mergeMetadata(globalMetadata, methodMetadata) { | ||
return lodash_1.mapValues(methodMetadata, (value, key) => { | ||
if (!globalMetadata[key]) { | ||
return value; | ||
} | ||
const globalValue = globalMetadata[key]; | ||
if (!lodash_1.isArray(globalValue)) { | ||
return Object.assign({}, globalValue, value); | ||
} | ||
return [...globalValue, ...value]; | ||
}); | ||
} | ||
} | ||
exports.SwaggerExplorer = SwaggerExplorer; |
import { INestApplication } from '@nestjs/common'; | ||
import { SwaggerBaseConfig, SwaggerDocument } from './interfaces'; | ||
import { | ||
SwaggerBaseConfig, | ||
SwaggerDocument, | ||
SwaggerCustomOptions, | ||
} from './interfaces'; | ||
export declare class SwaggerModule { | ||
private static readonly swaggerScanner; | ||
static createDocument(app: INestApplication, config: SwaggerBaseConfig): SwaggerDocument; | ||
static setup(path: string, app: INestApplication, document: SwaggerDocument): void; | ||
private static readonly swaggerScanner; | ||
static createDocument( | ||
app: INestApplication, | ||
config: SwaggerBaseConfig, | ||
): SwaggerDocument; | ||
static setup( | ||
path: string, | ||
app: INestApplication, | ||
document: SwaggerDocument, | ||
options?: SwaggerCustomOptions, | ||
): void; | ||
} |
@@ -1,15 +0,15 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const swaggerUi = require("swagger-ui-express"); | ||
const swagger_scanner_1 = require("./swagger-scanner"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const swaggerUi = require('swagger-ui-express'); | ||
const swagger_scanner_1 = require('./swagger-scanner'); | ||
class SwaggerModule { | ||
static createDocument(app, config) { | ||
const document = this.swaggerScanner.scanApplication(app); | ||
return Object.assign({}, config, document, { swagger: '2.0' }); | ||
} | ||
static setup(path, app, document) { | ||
app.use(path, swaggerUi.serve, swaggerUi.setup(document)); | ||
} | ||
static createDocument(app, config) { | ||
const document = this.swaggerScanner.scanApplication(app); | ||
return Object.assign({}, config, document, { swagger: '2.0' }); | ||
} | ||
static setup(path, app, document, options) { | ||
app.use(path, swaggerUi.serve, swaggerUi.setup(document, options)); | ||
} | ||
} | ||
SwaggerModule.swaggerScanner = new swagger_scanner_1.SwaggerScanner(); | ||
exports.SwaggerModule = SwaggerModule; |
import { SwaggerDocument } from './interfaces'; | ||
export declare class SwaggerScanner { | ||
private readonly explorer; | ||
private readonly transfomer; | ||
scanApplication(app: any): SwaggerDocument; | ||
scanModuleRoutes(routes: any): SwaggerDocument; | ||
private readonly explorer; | ||
private readonly transfomer; | ||
scanApplication(app: any): SwaggerDocument; | ||
scanModuleRoutes(routes: any): SwaggerDocument; | ||
} |
@@ -1,22 +0,35 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const swagger_explorer_1 = require("./swagger-explorer"); | ||
const lodash_1 = require("lodash"); | ||
const swagger_transformer_1 = require("./swagger-transformer"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const swagger_explorer_1 = require('./swagger-explorer'); | ||
const lodash_1 = require('lodash'); | ||
const swagger_transformer_1 = require('./swagger-transformer'); | ||
class SwaggerScanner { | ||
constructor() { | ||
this.explorer = new swagger_explorer_1.SwaggerExplorer(); | ||
this.transfomer = new swagger_transformer_1.SwaggerTransformer(); | ||
} | ||
scanApplication(app) { | ||
const { container } = app; | ||
const modules = container.getModules(); | ||
const denormalizedPaths = [...modules.values()].map(({ routes }) => this.scanModuleRoutes(routes)); | ||
return Object.assign({}, this.transfomer.normalizePaths(lodash_1.flatten(denormalizedPaths)), { definitions: lodash_1.reduce(this.explorer.getModelsDefinitons(), lodash_1.extend) }); | ||
} | ||
scanModuleRoutes(routes) { | ||
const denormalizedArray = [...routes.values()].map((ctrl) => this.explorer.exploreController(ctrl)); | ||
return lodash_1.flatten(denormalizedArray); | ||
} | ||
constructor() { | ||
this.explorer = new swagger_explorer_1.SwaggerExplorer(); | ||
this.transfomer = new swagger_transformer_1.SwaggerTransformer(); | ||
} | ||
scanApplication(app) { | ||
const { container } = app; | ||
const modules = container.getModules(); | ||
const denormalizedPaths = [...modules.values()].map(({ routes }) => | ||
this.scanModuleRoutes(routes), | ||
); | ||
return Object.assign( | ||
{}, | ||
this.transfomer.normalizePaths(lodash_1.flatten(denormalizedPaths)), | ||
{ | ||
definitions: lodash_1.reduce( | ||
this.explorer.getModelsDefinitons(), | ||
lodash_1.extend, | ||
), | ||
}, | ||
); | ||
} | ||
scanModuleRoutes(routes) { | ||
const denormalizedArray = [...routes.values()].map(ctrl => | ||
this.explorer.exploreController(ctrl), | ||
); | ||
return lodash_1.flatten(denormalizedArray); | ||
} | ||
} | ||
exports.SwaggerScanner = SwaggerScanner; |
export declare class SwaggerTransformer { | ||
normalizePaths(denormalizedDoc: any): { | ||
paths: any; | ||
}; | ||
normalizePaths( | ||
denormalizedDoc: any, | ||
): { | ||
paths: any; | ||
}; | ||
} |
@@ -1,19 +0,23 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const lodash_1 = require('lodash'); | ||
class SwaggerTransformer { | ||
normalizePaths(denormalizedDoc) { | ||
const doc = lodash_1.filter(denormalizedDoc, (r) => r.root); | ||
const groupedByPath = lodash_1.groupBy(doc, ({ root }) => root.path); | ||
const paths = lodash_1.mapValues(groupedByPath, (routes) => { | ||
const keyByMethod = lodash_1.keyBy(routes, ({ root }) => root.method); | ||
return lodash_1.mapValues(keyByMethod, (route) => { | ||
return Object.assign({}, lodash_1.omit(route.root, ['method', 'path']), lodash_1.omit(route, 'root')); | ||
}); | ||
}); | ||
return { | ||
paths, | ||
}; | ||
} | ||
normalizePaths(denormalizedDoc) { | ||
const doc = lodash_1.filter(denormalizedDoc, r => r.root); | ||
const groupedByPath = lodash_1.groupBy(doc, ({ root }) => root.path); | ||
const paths = lodash_1.mapValues(groupedByPath, routes => { | ||
const keyByMethod = lodash_1.keyBy(routes, ({ root }) => root.method); | ||
return lodash_1.mapValues(keyByMethod, route => { | ||
return Object.assign( | ||
{}, | ||
lodash_1.omit(route.root, ['method', 'path']), | ||
lodash_1.omit(route, 'root'), | ||
); | ||
}); | ||
}); | ||
return { | ||
paths, | ||
}; | ||
} | ||
} | ||
exports.SwaggerTransformer = SwaggerTransformer; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
48921
4
68
1501
55
1
+ Addedlodash@4.17.21(transitive)
+ Addedswagger-ui-express@2.0.15(transitive)
- Removedlodash@4.17.4(transitive)
- Removedswagger-ui-express@2.0.11(transitive)
Updatedlodash@^4.17.4
Updatedswagger-ui-express@^2.0.11