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.0.0-next.12 to 4.0.0-next.13

dist/plugin/utils/ast-utils.d.ts

5

dist/plugin/utils/plugin-utils.d.ts

@@ -1,7 +0,6 @@

import { Decorator, Type } from 'ts-morph';
import * as ts from 'typescript';
export declare function getDecoratorOrUndefinedByNames(names: string[], decorators: Decorator[]): Decorator | undefined;
export declare function getTypeReferenceAsString(type: Type): string;
export declare function getDecoratorOrUndefinedByNames(names: string[], decorators: ts.NodeArray<ts.Decorator>): ts.Decorator | undefined;
export declare function getTypeReferenceAsString(type: ts.Type, typeChecker: ts.TypeChecker): string;
export declare function isPromiseOrObservable(type: string): boolean;
export declare function hasPropertyKey(key: string, properties: ts.PropertyAssignment[]): boolean;
export declare function replaceImportPath(typeReference: string, fileName: string): string;

26

dist/plugin/utils/plugin-utils.js

@@ -5,10 +5,12 @@ "use strict";

const path_1 = require("path");
const ts = require("typescript");
const ast_utils_1 = require("./ast-utils");
function getDecoratorOrUndefinedByNames(names, decorators) {
return decorators.find(item => names.includes(item.getName()));
return (decorators || ts.createNodeArray()).find(item => names.includes(ast_utils_1.getDecoratorName(item)));
}
exports.getDecoratorOrUndefinedByNames = getDecoratorOrUndefinedByNames;
function getTypeReferenceAsString(type) {
if (type.isArray()) {
const arrayType = type.getArrayElementType();
const elementType = this.getTypeReferenceAsString(arrayType);
function getTypeReferenceAsString(type, typeChecker) {
if (ast_utils_1.isArray(type)) {
const arrayType = ast_utils_1.getTypeArguments(type)[0];
const elementType = getTypeReferenceAsString(arrayType, typeChecker);
if (!elementType) {

@@ -19,14 +21,14 @@ return undefined;

}
if (type.isBoolean()) {
if (ast_utils_1.isBoolean(type)) {
return Boolean.name;
}
if (type.isNumber()) {
if (ast_utils_1.isNumber(type)) {
return Number.name;
}
if (type.isString()) {
if (ast_utils_1.isString(type)) {
return String.name;
}
if (isPromiseOrObservable(type.getText())) {
const typeArguments = type.getTypeArguments();
const elementType = this.getTypeReferenceAsString(lodash_1.head(typeArguments));
if (isPromiseOrObservable(ast_utils_1.getText(type, typeChecker))) {
const typeArguments = ast_utils_1.getTypeArguments(type);
const elementType = getTypeReferenceAsString(lodash_1.head(typeArguments), typeChecker);
if (!elementType) {

@@ -38,3 +40,3 @@ return undefined;

if (type.isClass()) {
return type.getText();
return ast_utils_1.getText(type, typeChecker);
}

@@ -41,0 +43,0 @@ return undefined;

@@ -1,4 +0,4 @@

import { ts } from 'ts-morph';
import * as ts from 'typescript';
export declare class AbstractFileVisitor {
updateImports(sourceFile: ts.SourceFile): ts.SourceFile;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts_morph_1 = require("ts-morph");
const ts = require("typescript");
const plugin_constants_1 = require("../plugin-constants");
class AbstractFileVisitor {
updateImports(sourceFile) {
return ts_morph_1.ts.updateSourceFileNode(sourceFile, [
ts_morph_1.ts.createImportEqualsDeclaration(undefined, undefined, plugin_constants_1.OPENAPI_NAMESPACE, ts_morph_1.ts.createExternalModuleReference(ts_morph_1.ts.createLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME))),
return ts.updateSourceFileNode(sourceFile, [
ts.createImportEqualsDeclaration(undefined, undefined, plugin_constants_1.OPENAPI_NAMESPACE, ts.createExternalModuleReference(ts.createLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME))),
...sourceFile.statements

@@ -10,0 +10,0 @@ ]);

@@ -1,2 +0,1 @@

import { MethodDeclaration } from 'ts-morph';
import * as ts from 'typescript';

@@ -6,7 +5,7 @@ import { AbstractFileVisitor } from './abstract.visitor';

visit(sourceFile: ts.SourceFile, ctx: ts.TransformationContext, program: ts.Program): ts.SourceFile;
addDecoratorToNode(node: MethodDeclaration, hostFilename: string): ts.MethodDeclaration;
createDecoratorObjectLiteralExpr(node: MethodDeclaration, existingProperties: ts.PropertyAssignment[], hostFilename: string): ts.ObjectLiteralExpression;
createTypePropertyAssignment(node: MethodDeclaration, existingProperties: ts.PropertyAssignment[], hostFilename: string): ts.PropertyAssignment;
createStatusPropertyAssignment(node: MethodDeclaration, existingProperties: ts.PropertyAssignment[]): ts.PropertyAssignment;
getStatusCodeIdentifier(node: MethodDeclaration): ts.Identifier | ts.PropertyAccessExpression;
addDecoratorToNode(compilerNode: ts.MethodDeclaration, typeChecker: ts.TypeChecker, hostFilename: string): ts.MethodDeclaration;
createDecoratorObjectLiteralExpr(node: ts.MethodDeclaration, typeChecker: ts.TypeChecker, existingProperties: ts.PropertyAssignment[], hostFilename: string): ts.ObjectLiteralExpression;
createTypePropertyAssignment(node: ts.MethodDeclaration, typeChecker: ts.TypeChecker, existingProperties: ts.PropertyAssignment[], hostFilename: string): ts.PropertyAssignment;
createStatusPropertyAssignment(node: ts.MethodDeclaration, existingProperties: ts.PropertyAssignment[]): ts.PropertyAssignment;
getStatusCodeIdentifier(node: ts.MethodDeclaration): any;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const ts_morph_1 = require("ts-morph");
const ts = require("typescript");
const decorators_1 = require("../../decorators");
const plugin_constants_1 = require("../plugin-constants");
const ast_utils_1 = require("../utils/ast-utils");
const plugin_utils_1 = require("../utils/plugin-utils");

@@ -16,6 +16,3 @@ const abstract_visitor_1 = require("./abstract.visitor");

if (ts.isMethodDeclaration(node)) {
const wrappedNode = ts_morph_1.createWrappedNode(node, {
typeChecker
});
return this.addDecoratorToNode(wrappedNode, sourceFile.fileName);
return this.addDecoratorToNode(node, typeChecker, sourceFile.fileName);
}

@@ -26,28 +23,30 @@ return ts.visitEachChild(node, visitNode, ctx);

}
addDecoratorToNode(node, hostFilename) {
const compilerNode = ts.getMutableClone(node.compilerNode);
addDecoratorToNode(compilerNode, typeChecker, hostFilename) {
const { pos, end } = compilerNode.decorators || ts.createNodeArray();
compilerNode.decorators = Object.assign([
...(compilerNode.decorators || ts.createNodeArray()),
ts.createDecorator(ts.createCall(ts.createIdentifier(`${plugin_constants_1.OPENAPI_NAMESPACE}.${decorators_1.ApiResponse.name}`), undefined, [this.createDecoratorObjectLiteralExpr(node, [], hostFilename)]))
ts.createDecorator(ts.createCall(ts.createIdentifier(`${plugin_constants_1.OPENAPI_NAMESPACE}.${decorators_1.ApiResponse.name}`), undefined, [
this.createDecoratorObjectLiteralExpr(compilerNode, typeChecker, [], hostFilename)
]))
], { pos, end });
return compilerNode;
}
createDecoratorObjectLiteralExpr(node, existingProperties = [], hostFilename) {
createDecoratorObjectLiteralExpr(node, typeChecker, existingProperties = [], hostFilename) {
const properties = [
...existingProperties,
this.createStatusPropertyAssignment(node, existingProperties),
this.createTypePropertyAssignment(node, existingProperties, hostFilename)
this.createTypePropertyAssignment(node, typeChecker, existingProperties, hostFilename)
];
return ts.createObjectLiteral(lodash_1.compact(properties));
}
createTypePropertyAssignment(node, existingProperties, hostFilename) {
createTypePropertyAssignment(node, typeChecker, existingProperties, hostFilename) {
if (plugin_utils_1.hasPropertyKey('type', existingProperties)) {
return undefined;
}
const type = node.getReturnType();
const signature = typeChecker.getSignatureFromDeclaration(node);
const type = typeChecker.getReturnTypeOfSignature(signature);
if (!type) {
return undefined;
}
let typeReference = plugin_utils_1.getTypeReferenceAsString(type);
let typeReference = plugin_utils_1.getTypeReferenceAsString(type, typeChecker);
if (!typeReference) {

@@ -67,7 +66,7 @@ return undefined;

getStatusCodeIdentifier(node) {
const decorators = node.getDecorators();
const decorators = node.decorators;
const httpCodeDecorator = plugin_utils_1.getDecoratorOrUndefinedByNames(['HttpCode'], decorators);
if (httpCodeDecorator) {
const argument = lodash_1.head(httpCodeDecorator.getArguments());
return argument && argument.compilerNode;
const argument = lodash_1.head(ast_utils_1.getDecoratorArguments(httpCodeDecorator));
return argument;
}

@@ -74,0 +73,0 @@ const postDecorator = plugin_utils_1.getDecoratorOrUndefinedByNames(['Post'], decorators);

@@ -1,2 +0,1 @@

import { Decorator, PropertyDeclaration } from 'ts-morph';
import * as ts from 'typescript';

@@ -7,10 +6,10 @@ import { PluginOptions } from '../merge-options';

visit(sourceFile: ts.SourceFile, ctx: ts.TransformationContext, program: ts.Program, options: PluginOptions): ts.SourceFile;
addDecoratorToNode(node: PropertyDeclaration, options: PluginOptions, hostFilename: string): ts.PropertyDeclaration;
addPropertiesToExisitingDecorator(existingDecorator: Decorator, wrappedNode: PropertyDeclaration, originalNode: ts.Node, options: PluginOptions, hostFilename: string): ts.Node;
createDecoratorObjectLiteralExpr(node: PropertyDeclaration, existingProperties?: ts.PropertyAssignment[], options?: PluginOptions, hostFilename?: string): ts.ObjectLiteralExpression;
createTypePropertyAssignment(node: PropertyDeclaration, existingProperties: ts.PropertyAssignment[], hostFilename: string): ts.PropertyAssignment;
createEnumPropertyAssignment(node: PropertyDeclaration, existingProperties: ts.PropertyAssignment[], hostFilename: string): ts.PropertyAssignment | ts.PropertyAssignment[];
createDefaultPropertyAssignment(node: PropertyDeclaration, existingProperties: ts.PropertyAssignment[]): ts.PropertyAssignment;
createValidationPropertyAssignments(node: PropertyDeclaration): ts.PropertyAssignment[];
addPropertyByValidationDecorator(decoratorName: string, propertyKey: string, decorators: Decorator[], assignments: ts.PropertyAssignment[]): void;
addDecoratorToNode(compilerNode: ts.PropertyDeclaration, typeChecker: ts.TypeChecker, options: PluginOptions, hostFilename: string): ts.PropertyDeclaration;
addPropertiesToExisitingDecorator(compilerNode: ts.Decorator, originalNode: ts.PropertyDeclaration, typeChecker: ts.TypeChecker, options: PluginOptions, hostFilename: string): ts.Node;
createDecoratorObjectLiteralExpr(node: ts.PropertyDeclaration, typeChecker: ts.TypeChecker, existingProperties?: ts.PropertyAssignment[], options?: PluginOptions, hostFilename?: string): ts.ObjectLiteralExpression;
createTypePropertyAssignment(node: ts.PropertyDeclaration, typeChecker: ts.TypeChecker, existingProperties: ts.PropertyAssignment[], hostFilename: string): ts.PropertyAssignment;
createEnumPropertyAssignment(node: ts.PropertyDeclaration, typeChecker: ts.TypeChecker, existingProperties: ts.PropertyAssignment[], hostFilename: string): ts.PropertyAssignment | ts.PropertyAssignment[];
createDefaultPropertyAssignment(node: ts.PropertyDeclaration, existingProperties: ts.PropertyAssignment[]): ts.PropertyAssignment;
createValidationPropertyAssignments(node: ts.PropertyDeclaration): ts.PropertyAssignment[];
addPropertyByValidationDecorator(decoratorName: string, propertyKey: string, decorators: ts.NodeArray<ts.Decorator>, assignments: ts.PropertyAssignment[]): void;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const ts_morph_1 = require("ts-morph");
const ts = require("typescript");
const decorators_1 = require("../../decorators");
const plugin_constants_1 = require("../plugin-constants");
const ast_utils_1 = require("../utils/ast-utils");
const plugin_utils_1 = require("../utils/plugin-utils");

@@ -16,6 +16,3 @@ const abstract_visitor_1 = require("./abstract.visitor");

if (ts.isPropertyDeclaration(node)) {
const wrappedNode = ts_morph_1.createWrappedNode(node, {
typeChecker
});
const decorators = wrappedNode.getDecorators();
const decorators = node.decorators;
const hidePropertyDecorator = plugin_utils_1.getDecoratorOrUndefinedByNames([decorators_1.ApiHideProperty.name], decorators);

@@ -27,5 +24,5 @@ if (hidePropertyDecorator) {

if (!propertyDecorator) {
return this.addDecoratorToNode(wrappedNode, options, sourceFile.fileName);
return this.addDecoratorToNode(node, typeChecker, options, sourceFile.fileName);
}
return this.addPropertiesToExisitingDecorator(propertyDecorator, wrappedNode, node, options, sourceFile.fileName);
return this.addPropertiesToExisitingDecorator(propertyDecorator, node, typeChecker, options, sourceFile.fileName);
}

@@ -36,4 +33,3 @@ return ts.visitEachChild(node, visitNode, ctx);

}
addDecoratorToNode(node, options, hostFilename) {
const compilerNode = ts.getMutableClone(node.compilerNode);
addDecoratorToNode(compilerNode, typeChecker, options, hostFilename) {
const { pos, end } = compilerNode.decorators || ts.createNodeArray();

@@ -43,3 +39,3 @@ compilerNode.decorators = Object.assign([

ts.createDecorator(ts.createCall(ts.createIdentifier(`${plugin_constants_1.OPENAPI_NAMESPACE}.${decorators_1.ApiProperty.name}`), undefined, [
this.createDecoratorObjectLiteralExpr(node, [], options, hostFilename)
this.createDecoratorObjectLiteralExpr(compilerNode, typeChecker, [], options, hostFilename)
]))

@@ -49,4 +45,3 @@ ], { pos, end });

}
addPropertiesToExisitingDecorator(existingDecorator, wrappedNode, originalNode, options, hostFilename) {
const compilerNode = ts.getMutableClone(existingDecorator.compilerNode);
addPropertiesToExisitingDecorator(compilerNode, originalNode, typeChecker, options, hostFilename) {
const callExpr = compilerNode.expression;

@@ -64,3 +59,3 @@ if (!callExpr) {

callExpr.arguments = Object.assign([
this.createDecoratorObjectLiteralExpr(wrappedNode, [], options, hostFilename)
this.createDecoratorObjectLiteralExpr(originalNode, typeChecker, [], options, hostFilename)
], { pos, end });

@@ -70,3 +65,3 @@ }

callExpr.arguments = Object.assign([
this.createDecoratorObjectLiteralExpr(wrappedNode, decoratorProperties, options, hostFilename)
this.createDecoratorObjectLiteralExpr(originalNode, typeChecker, decoratorProperties, options, hostFilename)
], {

@@ -78,4 +73,4 @@ pos,

}
createDecoratorObjectLiteralExpr(node, existingProperties = [], options = {}, hostFilename = '') {
const isRequired = !node.hasQuestionToken();
createDecoratorObjectLiteralExpr(node, typeChecker, existingProperties = [], options = {}, hostFilename = '') {
const isRequired = !node.questionToken;
let properties = [

@@ -85,5 +80,5 @@ ...existingProperties,

ts.createPropertyAssignment('required', ts.createLiteral(isRequired)),
this.createTypePropertyAssignment(node, existingProperties, hostFilename),
this.createTypePropertyAssignment(node, typeChecker, existingProperties, hostFilename),
this.createDefaultPropertyAssignment(node, existingProperties),
this.createEnumPropertyAssignment(node, existingProperties, hostFilename)
this.createEnumPropertyAssignment(node, typeChecker, existingProperties, hostFilename)
];

@@ -95,3 +90,3 @@ if (options.classValidatorShim) {

}
createTypePropertyAssignment(node, existingProperties, hostFilename) {
createTypePropertyAssignment(node, typeChecker, existingProperties, hostFilename) {
const key = 'type';

@@ -101,7 +96,7 @@ if (plugin_utils_1.hasPropertyKey(key, existingProperties)) {

}
const type = node.getType();
const type = typeChecker.getTypeAtLocation(node);
if (!type) {
return undefined;
}
let typeReference = plugin_utils_1.getTypeReferenceAsString(type);
let typeReference = plugin_utils_1.getTypeReferenceAsString(type, typeChecker);
if (!typeReference) {

@@ -113,3 +108,3 @@ return undefined;

}
createEnumPropertyAssignment(node, existingProperties, hostFilename) {
createEnumPropertyAssignment(node, typeChecker, existingProperties, hostFilename) {
const key = 'enum';

@@ -119,10 +114,10 @@ if (plugin_utils_1.hasPropertyKey(key, existingProperties)) {

}
let type = node.getType();
let type = typeChecker.getTypeAtLocation(node);
if (!type) {
return undefined;
}
let isArray = false;
if (type.isArray()) {
type = type.getArrayElementType();
isArray = true;
let isArrayType = false;
if (ast_utils_1.isArray(type)) {
type = ast_utils_1.getTypeArguments(type)[0];
isArrayType = true;
if (!type) {

@@ -132,8 +127,8 @@ return undefined;

}
if (!type.isEnum()) {
if (!ast_utils_1.isEnum(type)) {
return undefined;
}
const enumRef = plugin_utils_1.replaceImportPath(type.getText(), hostFilename);
const enumRef = plugin_utils_1.replaceImportPath(ast_utils_1.getText(type, typeChecker), hostFilename);
const enumProperty = ts.createPropertyAssignment(key, ts.createIdentifier(enumRef));
if (isArray) {
if (isArrayType) {
const isArrayKey = 'isArray';

@@ -150,3 +145,3 @@ const isArrayProperty = ts.createPropertyAssignment(isArrayKey, ts.createIdentifier('true'));

}
const initializer = node.getInitializer();
const initializer = node.initializer;
if (!initializer) {

@@ -159,3 +154,3 @@ return undefined;

const assignments = [];
const decorators = node.getDecorators();
const decorators = node.decorators;
this.addPropertyByValidationDecorator('Min', 'minimum', decorators, assignments);

@@ -172,3 +167,3 @@ this.addPropertyByValidationDecorator('Max', 'maximum', decorators, assignments);

}
const argument = lodash_1.head(decoratorRef.getArguments());
const argument = lodash_1.head(ast_utils_1.getDecoratorArguments(decoratorRef));
assignments.push(ts.createPropertyAssignment(propertyKey, ts.createIdentifier(argument && argument.getText())));

@@ -175,0 +170,0 @@ }

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

constructor(schemaObjectFactory: SchemaObjectFactory);
exploreController(wrapper: InstanceWrapper<Controller>, modulePath: string): DenormalizedDoc[];
exploreController(wrapper: InstanceWrapper<Controller>, modulePath?: string, globalPrefix?: string): DenormalizedDoc[];
getSchemas(): SchemaObject[];

@@ -15,0 +15,0 @@ private generateDenormalizedDocument;

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

}
exploreController(wrapper, modulePath) {
exploreController(wrapper, modulePath, globalPrefix) {
const { instance, metatype } = wrapper;

@@ -42,3 +42,3 @@ const prototype = Object.getPrototypeOf(instance);

};
return this.generateDenormalizedDocument(metatype, prototype, instance, documentResolvers, modulePath);
return this.generateDenormalizedDocument(metatype, prototype, instance, documentResolvers, modulePath, globalPrefix);
}

@@ -48,3 +48,3 @@ getSchemas() {

}
generateDenormalizedDocument(metatype, prototype, instance, documentResolvers, modulePath) {
generateDenormalizedDocument(metatype, prototype, instance, documentResolvers, modulePath, globalPrefix) {
let path = this.validateRoutePath(this.reflectControllerPath(metatype));

@@ -54,2 +54,5 @@ if (modulePath) {

}
if (globalPrefix) {
path = this.validateRoutePath(globalPrefix + path);
}
const self = this;

@@ -56,0 +59,0 @@ const globalMetadata = this.exploreGlobalMetadata(metatype);

@@ -11,5 +11,6 @@ import { INestApplication } from '@nestjs/common';

scanApplication(app: INestApplication, options: SwaggerDocumentOptions): Omit<OpenAPIObject, 'openapi' | 'info'>;
scanModuleRoutes(routes: Map<string, InstanceWrapper>, modulePath?: string): Omit<OpenAPIObject, 'openapi' | 'info'>;
scanModuleRoutes(routes: Map<string, InstanceWrapper>, modulePath?: string, globalPrefix?: string): Array<Omit<OpenAPIObject, 'openapi' | 'info'> & Record<'root', any>>;
getModules(modulesContainer: Map<string, Module>, include: Function[]): Module[];
addExtraModels(schemas: SchemaObject[], extraModels: Function[]): void;
private getGlobalPrefix;
}

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

const swagger_transformer_1 = require("./swagger-transformer");
const strip_last_slash_util_1 = require("./utils/strip-last-slash.util");
class SwaggerScanner {

@@ -21,2 +22,3 @@ constructor() {

const modules = this.getModules(container.getModules(), includedModules);
const globalPrefix = strip_last_slash_util_1.stripLastSlash(this.getGlobalPrefix(app));
const denormalizedPaths = modules.map(({ routes, metatype, relatedModules }) => {

@@ -36,3 +38,3 @@ let allRoutes = new Map(routes);

: undefined;
return this.scanModuleRoutes(allRoutes, path);
return this.scanModuleRoutes(allRoutes, path, globalPrefix);
});

@@ -45,4 +47,4 @@ const schemas = this.explorer.getSchemas();

}
scanModuleRoutes(routes, modulePath) {
const denormalizedArray = [...routes.values()].map(ctrl => this.explorer.exploreController(ctrl, modulePath));
scanModuleRoutes(routes, modulePath, globalPrefix) {
const denormalizedArray = [...routes.values()].map(ctrl => this.explorer.exploreController(ctrl, modulePath, globalPrefix));
return lodash_1.flatten(denormalizedArray);

@@ -61,3 +63,7 @@ }

}
getGlobalPrefix(app) {
const internalConfigRef = app.config;
return (internalConfigRef && internalConfigRef.getGlobalPrefix()) || '';
}
}
exports.SwaggerScanner = SwaggerScanner;
{
"name": "@nestjs/swagger",
"version": "4.0.0-next.12",
"version": "4.0.0-next.13",
"description": "Nest - modern, fast, powerful node.js web framework (@swagger)",

@@ -40,3 +40,3 @@ "author": "Kamil Mysliwiec",

"ts-morph": "^4.2.0",
"typescript": "3.6.3"
"typescript": "^3.7.2"
},

@@ -43,0 +43,0 @@ "peerDependencies": {

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