@nestjs/swagger
Advanced tools
Comparing version 2.2.0 to 2.3.0
export declare const ApiImplicitBody: (metadata: { | ||
name: string; | ||
type: any; | ||
description?: string; | ||
required?: boolean; | ||
type?: any; | ||
isArray?: boolean; | ||
}) => MethodDecorator; |
@@ -7,3 +7,4 @@ "use strict"; | ||
name: '', | ||
required: true | ||
required: true, | ||
type: String | ||
}; | ||
@@ -10,0 +11,0 @@ exports.ApiImplicitBody = (metadata) => { |
@@ -0,1 +1,2 @@ | ||
import { SwaggerEnumType } from '../types/swagger-enum.type'; | ||
export declare const ApiImplicitQuery: (metadata: { | ||
@@ -7,3 +8,4 @@ name: string; | ||
isArray?: boolean; | ||
enum?: SwaggerEnumType; | ||
collectionFormat?: "csv" | "ssv" | "tsv" | "pipes" | "multi"; | ||
}) => MethodDecorator; |
@@ -16,15 +16,30 @@ "use strict"; | ||
type: metadata.type, | ||
enum: undefined, | ||
items: undefined, | ||
collectionFormat: undefined | ||
}; | ||
if (metadata.enum) { | ||
param.type = String; | ||
param.enum = metadata.enum; | ||
} | ||
if (metadata.isArray) { | ||
param.type = Array; | ||
param.items = { | ||
type: metadata.type | ||
}; | ||
param.collectionFormat = lodash_1.isNil(metadata.collectionFormat) | ||
? 'csv' | ||
: metadata.collectionFormat; | ||
if (metadata.enum) { | ||
param.items = { | ||
type: 'String', | ||
enum: metadata.enum | ||
}; | ||
param.collectionFormat = 'multi'; | ||
param.enum = undefined; | ||
} | ||
else { | ||
param.items = { | ||
type: metadata.type | ||
}; | ||
param.collectionFormat = lodash_1.isNil(metadata.collectionFormat) | ||
? 'csv' | ||
: metadata.collectionFormat; | ||
} | ||
} | ||
return helpers_1.createParamDecorator(param, initialMetadata); | ||
}; |
@@ -11,2 +11,3 @@ import { SwaggerEnumType } from '../types/swagger-enum.type'; | ||
format?: string; | ||
in?: string; | ||
multipleOf?: number; | ||
@@ -37,2 +38,3 @@ maximum?: number; | ||
format?: string; | ||
in?: string; | ||
multipleOf?: number; | ||
@@ -39,0 +41,0 @@ maximum?: number; |
@@ -0,6 +1,29 @@ | ||
export interface responseMetadata { | ||
description?: string; | ||
type?: any; | ||
isArray?: boolean; | ||
} | ||
export declare const ApiResponse: (metadata: { | ||
status: number; | ||
description?: string; | ||
headers?: any; | ||
type?: any; | ||
isArray?: boolean; | ||
}) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiOkResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiCreatedResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiBadRequestResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiInternalServerErrorResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiBadGatewayResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiConflictResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiForbiddenResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiGatewayTimeoutResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiGoneResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiMethodNotAllowedResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiNotAcceptableResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiNotImplementedResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiPayloadTooLargeResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiRequestTimeoutResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiServiceUnavailableResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiUnprocessableEntityResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; | ||
export declare const ApiUnsupportedMediaTypeResponse: (metadata: responseMetadata) => (target: any, key?: any, descriptor?: PropertyDescriptor) => any; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const constants_1 = require("../constants"); | ||
const http_status_enum_1 = require("@nestjs/common/enums/http-status.enum"); | ||
const initialMetadata = { | ||
@@ -25,1 +26,18 @@ status: 0, | ||
}; | ||
exports.ApiOkResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.OK })); | ||
exports.ApiCreatedResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.CREATED })); | ||
exports.ApiBadRequestResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.BAD_REQUEST })); | ||
exports.ApiInternalServerErrorResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.INTERNAL_SERVER_ERROR })); | ||
exports.ApiBadGatewayResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.BAD_GATEWAY })); | ||
exports.ApiConflictResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.CONFLICT })); | ||
exports.ApiForbiddenResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.FORBIDDEN })); | ||
exports.ApiGatewayTimeoutResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.GATEWAY_TIMEOUT })); | ||
exports.ApiGoneResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.GONE })); | ||
exports.ApiMethodNotAllowedResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.METHOD_NOT_ALLOWED })); | ||
exports.ApiNotAcceptableResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.NOT_ACCEPTABLE })); | ||
exports.ApiNotImplementedResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.NOT_IMPLEMENTED })); | ||
exports.ApiPayloadTooLargeResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.PAYLOAD_TOO_LARGE })); | ||
exports.ApiRequestTimeoutResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.REQUEST_TIMEOUT })); | ||
exports.ApiServiceUnavailableResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.SERVICE_UNAVAILABLE })); | ||
exports.ApiUnprocessableEntityResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.UNPROCESSABLE_ENTITY })); | ||
exports.ApiUnsupportedMediaTypeResponse = (metadata) => exports.ApiResponse(Object.assign({}, metadata, { status: http_status_enum_1.HttpStatus.UNSUPPORTED_MEDIA_TYPE })); |
@@ -15,5 +15,5 @@ import { SwaggerBaseConfig } from './interfaces/swagger-base-config.interface'; | ||
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; | ||
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; | ||
} |
@@ -37,3 +37,5 @@ "use strict"; | ||
setBasePath(basePath) { | ||
this.document.basePath = basePath.startsWith('/') ? basePath : '/' + basePath; | ||
this.document.basePath = basePath.startsWith('/') | ||
? basePath | ||
: '/' + basePath; | ||
return this; | ||
@@ -54,11 +56,17 @@ } | ||
addBearerAuth(name = 'Authorization', location = 'header', type = 'apiKey') { | ||
this.document.securityDefinitions.bearer = { | ||
type, name, in: location, | ||
}; | ||
this.document.securityDefinitions = Object.assign({}, (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, | ||
}; | ||
this.document.securityDefinitions = Object.assign({}, (this.document.securityDefinitions || {}), { oauth2: { | ||
type: 'oauth2', | ||
flow, | ||
authorizationUrl, | ||
tokenUrl, | ||
scopes | ||
} }); | ||
return this; | ||
@@ -65,0 +73,0 @@ } |
@@ -94,3 +94,17 @@ "use strict"; | ||
} | ||
return { name: key, $ref, required: metadata.required }; | ||
const strippedMetadata = lodash_1.omit(metadata, [ | ||
'type', | ||
'isArray', | ||
'collectionFormat', | ||
'required' | ||
]); | ||
if (Object.keys(strippedMetadata).length === 0) { | ||
return { name: key, required: metadata.required, $ref }; | ||
} | ||
return { | ||
name: key, | ||
required: metadata.required, | ||
title: nestedModelName, | ||
allOf: [{ $ref }, strippedMetadata] | ||
}; | ||
} | ||
@@ -97,0 +111,0 @@ const metatype = metadata.type && shared_utils_1.isFunction(metadata.type) |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const common_1 = require("@nestjs/common"); | ||
const constants_1 = require("@nestjs/common/constants"); | ||
const shared_utils_1 = require("@nestjs/common/utils/shared.utils"); | ||
const lodash_1 = require("lodash"); | ||
const constants_1 = require("../constants"); | ||
const constants_2 = require("../constants"); | ||
const api_parameters_explorer_1 = require("./api-parameters.explorer"); | ||
exports.exploreGlobalApiResponseMetadata = (definitions, metatype) => { | ||
const responses = Reflect.getMetadata(constants_1.DECORATORS.API_RESPONSE, metatype); | ||
const responses = Reflect.getMetadata(constants_2.DECORATORS.API_RESPONSE, metatype); | ||
return responses | ||
@@ -16,8 +18,25 @@ ? { | ||
exports.exploreApiResponseMetadata = (definitions, instance, prototype, method) => { | ||
const responses = Reflect.getMetadata(constants_1.DECORATORS.API_RESPONSE, method); | ||
if (!responses) { | ||
return undefined; | ||
const responses = Reflect.getMetadata(constants_2.DECORATORS.API_RESPONSE, method); | ||
if (responses) { | ||
return mapResponsesToSwaggerResponses(responses, definitions); | ||
} | ||
return mapResponsesToSwaggerResponses(responses, definitions); | ||
const status = getStatusCode(method); | ||
if (status) { | ||
return { [status]: { description: '' } }; | ||
} | ||
return undefined; | ||
}; | ||
const getStatusCode = method => { | ||
const status = Reflect.getMetadata(constants_1.HTTP_CODE_METADATA, method); | ||
if (status) { | ||
return status; | ||
} | ||
const requestMethod = Reflect.getMetadata(constants_1.METHOD_METADATA, method); | ||
switch (requestMethod) { | ||
case common_1.RequestMethod.POST: | ||
return common_1.HttpStatus.CREATED; | ||
default: | ||
return common_1.HttpStatus.OK; | ||
} | ||
}; | ||
const omitParamType = param => lodash_1.omit(param, 'type'); | ||
@@ -32,3 +51,3 @@ const mapResponsesToSwaggerResponses = (responses, definitions) => lodash_1.mapValues(lodash_1.mapValues(responses, response => { | ||
if (!(shared_utils_1.isFunction(type) && | ||
!defaultTypes.find(defaultType => defaultType === type))) { | ||
!defaultTypes.some(defaultType => defaultType === type))) { | ||
const metatype = type && shared_utils_1.isFunction(type) ? type.name : type; | ||
@@ -35,0 +54,0 @@ const swaggerType = api_parameters_explorer_1.mapTypesToSwaggerTypes(metatype); |
@@ -12,3 +12,2 @@ import { SwaggerScheme } from '../interfaces/swagger-base-config.interface'; | ||
schemes: SwaggerScheme[]; | ||
securityDefinitions: {}; | ||
}; |
@@ -12,4 +12,3 @@ "use strict"; | ||
tags: [], | ||
schemes: ['http'], | ||
securityDefinitions: {} | ||
schemes: ['http'] | ||
}; |
export * from './decorators'; | ||
export * from './document-builder'; | ||
export * from './interfaces'; | ||
export * from './swagger-module'; | ||
export * from './document-builder'; |
@@ -7,3 +7,3 @@ "use strict"; | ||
__export(require("./decorators")); | ||
__export(require("./document-builder")); | ||
__export(require("./swagger-module")); | ||
__export(require("./document-builder")); |
@@ -6,5 +6,5 @@ import { Controller } from '@nestjs/common/interfaces'; | ||
private readonly modelsDefinitions; | ||
exploreController({instance, metatype}: InstanceWrapper<Controller>): any[]; | ||
exploreController({instance, metatype}: InstanceWrapper<Controller>, modulePath: string): any[]; | ||
getModelsDefinitons(): any[]; | ||
private generateDenormalizedDocument(metatype, prototype, instance, explorersSchema); | ||
private generateDenormalizedDocument(metatype, prototype, instance, explorersSchema, modulePath); | ||
private exploreGlobalMetadata(metatype); | ||
@@ -11,0 +11,0 @@ private exploreRoutePathAndMethod(instance, prototype, method, globalPath); |
@@ -22,3 +22,3 @@ "use strict"; | ||
} | ||
exploreController({ instance, metatype }) { | ||
exploreController({ instance, metatype }, modulePath) { | ||
const prototype = Object.getPrototypeOf(instance); | ||
@@ -37,3 +37,3 @@ const explorersSchema = { | ||
}; | ||
return this.generateDenormalizedDocument(metatype, prototype, instance, explorersSchema); | ||
return this.generateDenormalizedDocument(metatype, prototype, instance, explorersSchema, modulePath); | ||
} | ||
@@ -43,4 +43,7 @@ getModelsDefinitons() { | ||
} | ||
generateDenormalizedDocument(metatype, prototype, instance, explorersSchema) { | ||
const path = this.validateRoutePath(this.reflectControllerPath(metatype)); | ||
generateDenormalizedDocument(metatype, prototype, instance, explorersSchema, modulePath) { | ||
let path = this.validateRoutePath(this.reflectControllerPath(metatype)); | ||
if (modulePath) { | ||
path = modulePath + path; | ||
} | ||
const self = this; | ||
@@ -47,0 +50,0 @@ const globalMetadata = this.exploreGlobalMetadata(metatype); |
@@ -6,3 +6,3 @@ import { SwaggerDocument } from './interfaces'; | ||
scanApplication(app: any): SwaggerDocument; | ||
scanModuleRoutes(routes: any): SwaggerDocument; | ||
scanModuleRoutes(routes: any, modulePath: any): SwaggerDocument; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
const swagger_explorer_1 = require("./swagger-explorer"); | ||
const lodash_1 = require("lodash"); | ||
const swagger_transformer_1 = require("./swagger-transformer"); | ||
const constants_1 = require("@nestjs/common/constants"); | ||
class SwaggerScanner { | ||
@@ -14,7 +15,12 @@ constructor() { | ||
const modules = container.getModules(); | ||
const denormalizedPaths = [...modules.values()].map(({ routes }) => this.scanModuleRoutes(routes)); | ||
const denormalizedPaths = [...modules.values()].map(({ routes, metatype }) => { | ||
const path = metatype | ||
? Reflect.getMetadata(constants_1.MODULE_PATH, metatype) | ||
: undefined; | ||
return this.scanModuleRoutes(routes, path); | ||
}); | ||
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)); | ||
scanModuleRoutes(routes, modulePath) { | ||
const denormalizedArray = [...routes.values()].map(ctrl => this.explorer.exploreController(ctrl, modulePath)); | ||
return lodash_1.flatten(denormalizedArray); | ||
@@ -21,0 +27,0 @@ } |
{ | ||
"name": "@nestjs/swagger", | ||
"version": "2.2.0", | ||
"description": "Nest - modern, fast, powerful node.js web framework (@swagger)", | ||
"version": "2.3.0", | ||
"description": | ||
"Nest - modern, fast, powerful node.js web framework (@swagger)", | ||
"author": "Kamil Mysliwiec", | ||
@@ -37,7 +38,4 @@ "license": "MIT", | ||
"lint-staged": { | ||
"*.ts": [ | ||
"prettier --write", | ||
"git add -f" | ||
] | ||
"*.ts": ["prettier --write", "git add -f"] | ||
} | ||
} |
@@ -29,3 +29,3 @@ <p align="center"> | ||
This is an [OpenAPI (Swagger)](https://www.openapis.org/) module for [Nest](https://github.com/nestjs/nest). | ||
The [OpenAPI (Swagger)](https://www.openapis.org/) module for [Nest](https://github.com/nestjs/nest). | ||
@@ -32,0 +32,0 @@ ## Installation |
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
75984
1313