Socket
Socket
Sign inDemoInstall

@nestjs/swagger

Package Overview
Dependencies
Maintainers
4
Versions
206
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/swagger - npm Package Compare versions

Comparing version 4.2.12 to 4.3.0

dist/decorators/api-extension.decorator.d.ts

1

dist/constants.d.ts

@@ -15,2 +15,3 @@ export declare const DECORATORS_PREFIX = "swagger";

API_EXTRA_MODELS: string;
API_EXTENSION: string;
};

3

dist/constants.js

@@ -16,3 +16,4 @@ "use strict";

API_EXCLUDE_ENDPOINT: `${exports.DECORATORS_PREFIX}/apiExcludeEndpoint`,
API_EXTRA_MODELS: `${exports.DECORATORS_PREFIX}/apiExtraModels`
API_EXTRA_MODELS: `${exports.DECORATORS_PREFIX}/apiExtraModels`,
API_EXTENSION: `${exports.DECORATORS_PREFIX}/apiExtension`
};

@@ -8,2 +8,3 @@ import { Type } from '@nestjs/common';

enum?: SwaggerEnumType;
enumName?: string;
}

@@ -10,0 +11,0 @@ interface ApiParamSchemaHost extends ParameterOptions {

@@ -12,7 +12,12 @@ "use strict";

const param = Object.assign({ name: lodash_1.isNil(options.name) ? defaultParamOptions.name : options.name, in: 'path' }, lodash_1.omit(options, 'enum'));
if (options.enum) {
const apiParamMetadata = options;
if (apiParamMetadata.enum) {
param.schema = param.schema || {};
const enumValues = enum_utils_1.getEnumValues(options.enum);
param.schema.type = enum_utils_1.getEnumType(enumValues);
param.schema.enum = enumValues;
const paramSchema = param.schema;
const enumValues = enum_utils_1.getEnumValues(apiParamMetadata.enum);
paramSchema.type = enum_utils_1.getEnumType(enumValues);
paramSchema.enum = enumValues;
if (apiParamMetadata.enumName) {
param.enumName = apiParamMetadata.enumName;
}
}

@@ -19,0 +24,0 @@ return helpers_1.createParamDecorator(param, defaultParamOptions);

@@ -5,6 +5,7 @@ import { SchemaObjectMetadata } from '../interfaces/schema-object-metadata.interface';

enum?: any[] | Record<string, any>;
enumName?: string;
}
export declare function ApiProperty(options?: ApiPropertyOptions): PropertyDecorator;
export declare function createApiPropertyDecorator(options?: ApiPropertyOptions, overrideExisting?: boolean): PropertyDecorator;
export declare function ApiPropertyOptional(options?: ApiPropertyOptions): PropertyDecorator;
export declare function ApiResponseProperty(options?: Pick<ApiPropertyOptions, 'type' | 'example' | 'format' | 'enum' | 'deprecated'>): PropertyDecorator;
export declare function createApiPropertyDecorator(options?: ApiPropertyOptions, overrideExisting?: boolean): PropertyDecorator;

@@ -11,10 +11,2 @@ "use strict";

exports.ApiProperty = ApiProperty;
function ApiPropertyOptional(options = {}) {
return ApiProperty(Object.assign(Object.assign({}, options), { required: false }));
}
exports.ApiPropertyOptional = ApiPropertyOptional;
function ApiResponseProperty(options = {}) {
return ApiProperty(Object.assign({ readOnly: true }, options));
}
exports.ApiResponseProperty = ApiResponseProperty;
function createApiPropertyDecorator(options = {}, overrideExisting = true) {

@@ -41,1 +33,9 @@ const [type, isArray] = helpers_1.getTypeIsArrayTuple(options.type, options.isArray);

exports.createApiPropertyDecorator = createApiPropertyDecorator;
function ApiPropertyOptional(options = {}) {
return ApiProperty(Object.assign(Object.assign({}, options), { required: false }));
}
exports.ApiPropertyOptional = ApiPropertyOptional;
function ApiResponseProperty(options = {}) {
return ApiProperty(Object.assign({ readOnly: true }, options));
}
exports.ApiResponseProperty = ApiResponseProperty;

@@ -9,2 +9,3 @@ import { Type } from '@nestjs/common';

enum?: SwaggerEnumType;
enumName?: string;
}

@@ -11,0 +12,0 @@ interface ApiQuerySchemaHost extends ParameterOptions {

@@ -11,3 +11,4 @@ "use strict";

function ApiQuery(options) {
const [type, isArray] = helpers_1.getTypeIsArrayTuple(options.type, options.isArray);
const apiQueryMetadata = options;
const [type, isArray] = helpers_1.getTypeIsArrayTuple(apiQueryMetadata.type, apiQueryMetadata.isArray);
const param = Object.assign(Object.assign({ name: lodash_1.isNil(options.name) ? defaultQueryOptions.name : options.name, in: 'query' }, lodash_1.omit(options, 'enum')), { type,

@@ -14,0 +15,0 @@ isArray });

@@ -18,1 +18,2 @@ export * from './api-basic.decorator';

export * from './api-use-tags.decorator';
export * from './api-extension.decorator';

@@ -26,1 +26,2 @@ "use strict";

__export(require("./api-use-tags.decorator"));
__export(require("./api-extension.decorator"));
import { Type } from '@nestjs/common';
import { SchemaObject } from '../interfaces/open-api-spec.interface';
export declare const exploreApiParametersMetadata: (schemas: SchemaObject[], instance: object, prototype: Type<unknown>, method: Function) => {
export declare const exploreApiParametersMetadata: (schemas: SchemaObject[], schemaRefsStack: [], instance: object, prototype: Type<unknown>, method: Function) => {
parameters: any;
};

@@ -15,3 +15,3 @@ "use strict";

const schemaObjectFactory = new schema_object_factory_1.SchemaObjectFactory(modelPropertiesAccessor, swaggerTypesMapper);
exports.exploreApiParametersMetadata = (schemas, instance, prototype, method) => {
exports.exploreApiParametersMetadata = (schemas, schemaRefsStack, instance, prototype, method) => {
const explicitParameters = Reflect.getMetadata(constants_1.DECORATORS.API_PARAMETERS, method);

@@ -33,3 +33,3 @@ const parametersMetadata = parameterMetadataAccessor.explore(instance, prototype, method);

}
const paramsWithDefinitions = schemaObjectFactory.createFromModel(properties, schemas);
const paramsWithDefinitions = schemaObjectFactory.createFromModel(properties, schemas, schemaRefsStack);
const parameters = swaggerTypesMapper.mapParamTypes(paramsWithDefinitions);

@@ -36,0 +36,0 @@ return parameters ? { parameters } : undefined;

@@ -150,4 +150,3 @@ export interface OpenAPIObject {

export declare type LinkParametersObject = Record<string, any>;
export interface HeaderObject extends BaseParameterObject {
}
export declare type HeaderObject = BaseParameterObject;
export interface TagObject {

@@ -154,0 +153,0 @@ name: string;

@@ -8,2 +8,3 @@ import { Type } from '@nestjs/common';

name?: string;
enumName?: string;
}

@@ -10,2 +10,3 @@ import { Type } from '@nestjs/common';

enum?: unknown[];
enumName?: string;
}

@@ -12,0 +13,0 @@ export declare type ParamsWithType = Record<string, ParamWithTypeMetadata>;

@@ -24,5 +24,4 @@ "use strict";

switch (Number(keyPair[0])) {
case route_paramtypes_enum_1.RouteParamtypes.BODY: {
case route_paramtypes_enum_1.RouteParamtypes.BODY:
return 'body';
}
case route_paramtypes_enum_1.RouteParamtypes.PARAM:

@@ -29,0 +28,0 @@ return 'path';

@@ -13,3 +13,3 @@ import { ApiResponseSchemaHost } from '../decorators';

description?: string;
headers?: Record<string, import("../interfaces/open-api-spec.interface").ReferenceObject | import("../interfaces/open-api-spec.interface").HeaderObject>;
headers?: Record<string, import("../interfaces/open-api-spec.interface").ReferenceObject | import("../interfaces/open-api-spec.interface").BaseParameterObject>;
links?: Record<string, import("../interfaces/open-api-spec.interface").ReferenceObject | import("../interfaces/open-api-spec.interface").LinkObject>;

@@ -16,0 +16,0 @@ status?: number;

@@ -11,5 +11,131 @@ import { Type } from '@nestjs/common';

constructor(modelPropertiesAccessor: ModelPropertiesAccessor, swaggerTypesMapper: SwaggerTypesMapper);
createFromModel(parameters: ParamWithTypeMetadata[], schemas: SchemaObject[]): Array<ParamWithTypeMetadata | BaseParameterObject>;
createFromModel(parameters: ParamWithTypeMetadata[], schemas: SchemaObject[], schemaRefsStack?: string[]): Array<ParamWithTypeMetadata | BaseParameterObject>;
exploreModelSchema(type: Type<unknown> | Function, schemas: SchemaObject[], schemaRefsStack?: string[]): string;
mergePropertyWithMetadata(key: string, prototype: Type<unknown>, schemas: SchemaObject[], schemaRefsStack?: string[], metadata?: SchemaObjectMetadata): SchemaObjectMetadata | ReferenceObject;
createEnumParam(param: ParamWithTypeMetadata & BaseParameterObject, schemas: SchemaObject[], schemaRefsStack: string[]): Partial<ParamWithTypeMetadata & BaseParameterObject>;
createEnumSchemaType(key: string, metadata: SchemaObjectMetadata, schemas: SchemaObject[], schemaRefsStack: string[]): Pick<{
items: {
$ref: string;
};
$ref?: undefined;
name: string;
type: string;
isArray?: boolean;
required?: boolean;
enumName?: string;
externalDocs?: import("../interfaces/open-api-spec.interface").ExternalDocumentationObject;
nullable?: boolean;
discriminator?: import("../interfaces/open-api-spec.interface").DiscriminatorObject;
readOnly?: boolean;
writeOnly?: boolean;
xml?: import("../interfaces/open-api-spec.interface").XmlObject;
example?: any;
examples?: any[];
deprecated?: boolean;
allOf?: (SchemaObject | ReferenceObject)[];
oneOf?: (SchemaObject | ReferenceObject)[];
anyOf?: (SchemaObject | ReferenceObject)[];
not?: SchemaObject | ReferenceObject;
properties?: Record<string, SchemaObject | ReferenceObject>;
additionalProperties?: boolean | SchemaObject | ReferenceObject;
description?: string;
format?: string;
default?: any;
title?: string;
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
exclusiveMinimum?: boolean;
maxLength?: number;
minLength?: number;
pattern?: string;
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
maxProperties?: number;
minProperties?: number;
enum?: any[];
} | {
$ref: string;
items?: SchemaObject | ReferenceObject;
name: string;
type: string;
isArray?: boolean;
required?: boolean;
enumName?: string;
externalDocs?: import("../interfaces/open-api-spec.interface").ExternalDocumentationObject;
nullable?: boolean;
discriminator?: import("../interfaces/open-api-spec.interface").DiscriminatorObject;
readOnly?: boolean;
writeOnly?: boolean;
xml?: import("../interfaces/open-api-spec.interface").XmlObject;
example?: any;
examples?: any[];
deprecated?: boolean;
allOf?: (SchemaObject | ReferenceObject)[];
oneOf?: (SchemaObject | ReferenceObject)[];
anyOf?: (SchemaObject | ReferenceObject)[];
not?: SchemaObject | ReferenceObject;
properties?: Record<string, SchemaObject | ReferenceObject>;
additionalProperties?: boolean | SchemaObject | ReferenceObject;
description?: string;
format?: string;
default?: any;
title?: string;
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
exclusiveMinimum?: boolean;
maxLength?: number;
minLength?: number;
pattern?: string;
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
maxProperties?: number;
minProperties?: number;
enum?: any[];
}, "externalDocs" | "required" | "nullable" | "discriminator" | "readOnly" | "writeOnly" | "xml" | "example" | "examples" | "deprecated" | "allOf" | "oneOf" | "anyOf" | "not" | "items" | "properties" | "additionalProperties" | "description" | "format" | "default" | "title" | "multipleOf" | "maximum" | "exclusiveMaximum" | "minimum" | "exclusiveMinimum" | "maxLength" | "minLength" | "pattern" | "maxItems" | "minItems" | "uniqueItems" | "maxProperties" | "minProperties" | "isArray" | "enumName" | "name" | "$ref"> | {
name: string;
type?: string | Function | Record<string, any> | Type<unknown> | [Function];
isArray?: boolean;
required?: boolean;
enumName?: string;
externalDocs?: import("../interfaces/open-api-spec.interface").ExternalDocumentationObject;
nullable?: boolean;
discriminator?: import("../interfaces/open-api-spec.interface").DiscriminatorObject;
readOnly?: boolean;
writeOnly?: boolean;
xml?: import("../interfaces/open-api-spec.interface").XmlObject;
example?: any;
examples?: any[];
deprecated?: boolean;
allOf?: (SchemaObject | ReferenceObject)[];
oneOf?: (SchemaObject | ReferenceObject)[];
anyOf?: (SchemaObject | ReferenceObject)[];
not?: SchemaObject | ReferenceObject;
items?: SchemaObject | ReferenceObject;
properties?: Record<string, SchemaObject | ReferenceObject>;
additionalProperties?: boolean | SchemaObject | ReferenceObject;
description?: string;
format?: string;
default?: any;
title?: string;
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
exclusiveMinimum?: boolean;
maxLength?: number;
minLength?: number;
pattern?: string;
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
maxProperties?: number;
minProperties?: number;
enum?: any[];
};
createNotBuiltInTypeReference(key: string, metadata: SchemaObjectMetadata, schemas: SchemaObject[], schemaRefsStack: string[]): BaseParameterObject & Record<string, any>;

@@ -16,0 +142,0 @@ transformToArraySchemaProperty(metadata: SchemaObjectMetadata, key: string, type: string | Record<string, any>): BaseParameterObject & Record<string, any>;

@@ -18,5 +18,8 @@ "use strict";

}
createFromModel(parameters, schemas) {
createFromModel(parameters, schemas, schemaRefsStack = []) {
return parameters.map(param => {
if (!is_body_parameter_util_1.isBodyParameter(param)) {
if (param.enumName) {
return this.createEnumParam(param, schemas, schemaRefsStack);
}
return param;

@@ -30,3 +33,3 @@ }

}
const modelName = this.exploreModelSchema(param.type, schemas);
const modelName = this.exploreModelSchema(param.type, schemas, schemaRefsStack);
const name = param.name || modelName;

@@ -68,3 +71,3 @@ const schema = Object.assign(Object.assign({}, (param.schema || {})), { $ref: utils_1.getSchemaPath(modelName) });

type: 'object',
properties: lodash_1.mapValues(lodash_1.keyBy(propertiesWithType, 'name'), property => lodash_1.omit(property, ['name', 'isArray', 'required']))
properties: lodash_1.mapValues(lodash_1.keyBy(propertiesWithType, 'name'), property => lodash_1.omit(property, ['name', 'isArray', 'required', 'enumName']))
};

@@ -96,2 +99,5 @@ const typeDefinitionRequiredFields = propertiesWithType

if (lodash_1.isString(metadata.type)) {
if (enum_utils_1.isEnumMetadata(metadata)) {
return this.createEnumSchemaType(key, metadata, schemas, schemaRefsStack);
}
return Object.assign(Object.assign({}, metadata), { name: metadata.name || key });

@@ -126,2 +132,48 @@ }

}
createEnumParam(param, schemas, schemaRefsStack) {
var _a;
const enumName = param.enumName;
const $ref = utils_1.getSchemaPath(enumName);
if (!lodash_1.includes(schemaRefsStack, enumName)) {
schemaRefsStack.push(enumName);
const _enum = param.enum
? param.enum
: param.schema['items']
? param.schema['items']['enum']
: param.schema['enum'];
schemas.push({
[enumName]: {
type: 'string',
enum: _enum
}
});
}
param.schema =
param.isArray || ((_a = param.schema) === null || _a === void 0 ? void 0 : _a['items'])
? { type: 'array', items: { $ref } }
: { $ref };
return lodash_1.omit(param, ['isArray', 'items', 'enumName', 'enum']);
}
createEnumSchemaType(key, metadata, schemas, schemaRefsStack) {
if (!metadata.enumName) {
return Object.assign(Object.assign({}, metadata), { name: metadata.name || key });
}
const enumName = metadata.enumName;
const $ref = utils_1.getSchemaPath(enumName);
if (!lodash_1.includes(schemaRefsStack, enumName)) {
schemaRefsStack.push(enumName);
schemas.push({
[enumName]: {
type: 'string',
enum: metadata.isArray && metadata.items
? metadata.items['enum']
: metadata.enum
}
});
}
const _schemaObject = Object.assign(Object.assign({}, metadata), { name: metadata.name || key, type: metadata.isArray ? 'array' : 'string' });
const refHost = metadata.isArray ? { items: { $ref } } : { $ref };
const paramObject = Object.assign(Object.assign({}, _schemaObject), refHost);
return lodash_1.omit(paramObject, ['enum', 'type']);
}
createNotBuiltInTypeReference(key, metadata, schemas, schemaRefsStack) {

@@ -142,3 +194,13 @@ if (shared_utils_1.isUndefined(metadata.type)) {

const validMetadataObject = lodash_1.omit(metadata, keysToRemove);
return Object.assign(Object.assign({}, validMetadataObject), { name: metadata.name || key, $ref });
const extraMetadataKeys = Object.keys(validMetadataObject);
if (extraMetadataKeys.length > 0) {
return {
name: metadata.name || key,
allOf: [{ $ref }, Object.assign({}, validMetadataObject)]
};
}
return {
name: metadata.name || key,
$ref
};
}

@@ -145,0 +207,0 @@ transformToArraySchemaProperty(metadata, key, type) {

@@ -11,2 +11,3 @@ import { Controller } from '@nestjs/common/interfaces';

private readonly schemas;
private readonly schemaRefsStack;
constructor(schemaObjectFactory: SchemaObjectFactory);

@@ -13,0 +14,0 @@ exploreController(wrapper: InstanceWrapper<Controller>, modulePath?: string, globalPrefix?: string): DenormalizedDoc[];

@@ -27,2 +27,3 @@ "use strict";

this.schemas = [];
this.schemaRefsStack = [];
}

@@ -36,3 +37,3 @@ exploreController(wrapper, modulePath, globalPrefix) {

api_operation_explorer_1.exploreApiOperationMetadata,
api_parameters_explorer_1.exploreApiParametersMetadata.bind(null, this.schemas)
api_parameters_explorer_1.exploreApiParametersMetadata.bind(null, this.schemas, this.schemaRefsStack)
],

@@ -105,7 +106,4 @@ security: [api_security_explorer_1.exploreApiSecurityMetadata],

const fullPath = globalPath + this.validateRoutePath(routePath);
return {
method: common_1.RequestMethod[requestMethod].toLowerCase(),
path: fullPath === '' ? '/' : fullPath,
operationId: this.getOperationId(instance, method)
};
const apiExtension = Reflect.getMetadata(constants_2.DECORATORS.API_EXTENSION, method);
return Object.assign({ method: common_1.RequestMethod[requestMethod].toLowerCase(), path: fullPath === '' ? '/' : fullPath, operationId: this.getOperationId(instance, method) }, apiExtension);
}

@@ -112,0 +110,0 @@ getOperationId(instance, method) {

@@ -0,7 +1,9 @@

import { SchemaObjectMetadata } from '../interfaces/schema-object-metadata.interface';
import { SwaggerEnumType } from '../types/swagger-enum.type';
export declare function getEnumValues(enumType: SwaggerEnumType): string[] | number[];
export declare function getEnumType(values: (string | number)[]): 'string' | 'number';
export declare function addEnumArraySchema(paramDefinition: Record<'schema' | 'isArray', any>, decoratorOptions: Partial<Record<'enum', any>>): void;
export declare function addEnumSchema(paramDefinition: Record<'schema', any>, decoratorOptions: Partial<Record<'enum', any>>): void;
export declare const isEnumArray: <T extends Partial<Record<"isArray" | "enum", any>>>(obj: Record<string, any>) => obj is T;
export declare function addEnumArraySchema(paramDefinition: Record<'schema' | 'isArray' | 'enumName', any>, decoratorOptions: Partial<Record<'enum' | 'enumName', any>>): void;
export declare function addEnumSchema(paramDefinition: Record<'schema' | 'enumName', any>, decoratorOptions: Partial<Record<'enum' | 'enumName', any>>): void;
export declare const isEnumArray: <T extends Partial<Record<"enum" | "isArray", any>>>(obj: Record<string, any>) => obj is T;
export declare const isEnumDefined: <T extends Partial<Record<"enum", any>>>(obj: Record<string, any>) => obj is T;
export declare const isEnumMetadata: (metadata: SchemaObjectMetadata) => any;

@@ -39,2 +39,5 @@ "use strict";

};
if (decoratorOptions.enumName) {
paramDefinition.enumName = decoratorOptions.enumName;
}
}

@@ -48,2 +51,5 @@ exports.addEnumArraySchema = addEnumArraySchema;

paramSchema.type = getEnumType(enumValues);
if (decoratorOptions.enumName) {
paramDefinition.enumName = decoratorOptions.enumName;
}
}

@@ -53,1 +59,2 @@ exports.addEnumSchema = addEnumSchema;

exports.isEnumDefined = (obj) => obj.enum;
exports.isEnumMetadata = (metadata) => { var _a; return metadata.enum || (metadata.isArray && ((_a = metadata.items) === null || _a === void 0 ? void 0 : _a['enum'])); };
{
"name": "@nestjs/swagger",
"version": "4.2.12",
"version": "4.3.0",
"description": "Nest - modern, fast, powerful node.js web framework (@swagger)",

@@ -11,2 +11,3 @@ "author": "Kamil Mysliwiec",

"format": "prettier \"lib/**/*.ts\" --write",
"lint": "eslint 'lib/**/*.ts' --fix",
"prepublish:next": "npm run build",

@@ -36,2 +37,7 @@ "publish:next": "npm publish --access public --tag next",

"@types/node": "11.15.0",
"@typescript-eslint/eslint-plugin": "2.20.0",
"@typescript-eslint/parser": "2.20.0",
"eslint": "6.8.0",
"eslint-config-prettier": "6.10.0",
"eslint-plugin-import": "2.20.1",
"express": "4.17.1",

@@ -44,3 +50,3 @@ "fastify-swagger": "2.5.0",

"reflect-metadata": "0.1.13",
"release-it": "12.6.0",
"release-it": "12.6.1",
"swagger-parser": "8.0.4",

@@ -47,0 +53,0 @@ "swagger-ui-express": "4.1.3",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc