All files / metadata metadataGenerator.ts

88.24% Statements 30/34
100% Branches 0/0
73.33% Functions 11/15
90.32% Lines 28/31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1311x 1x   1x   1x     1x 1x     1x 1x 1x     1x 1x 24x 2152x       1x   1x   1x           1x       1x 2x     1x       1x       1x 1x 2152x 32x 32x 1x   1x                                                                                                                                                  
import * as ts from 'typescript';
import { ControllerGenerator } from './controllerGenerator';
 
export class MetadataGenerator {
    public static current: MetadataGenerator;
    public readonly nodes = new Array<ts.Node>();
    public readonly typeChecker: ts.TypeChecker;
    private readonly program: ts.Program;
    private referenceTypes: { [typeName: string]: ReferenceType } = {};
    private circularDependencyResolvers = new Array<(referenceTypes: { [typeName: string]: ReferenceType }) => void>();
 
    constructor(entryFile: string) {
        this.program = ts.createProgram([entryFile], {});
        this.typeChecker = this.program.getTypeChecker();
        MetadataGenerator.current = this;
    }
 
    public generate(): Metadata {
        this.program.getSourceFiles().forEach(sf => {
            ts.forEachChild(sf, node => {
                this.nodes.push(node);
            });
        });
 
        const controllers = this.buildControllers();
 
        this.circularDependencyResolvers.forEach(c => c(this.referenceTypes));
 
        return {
            Controllers: controllers,
            ReferenceTypes: this.referenceTypes
        };
    }
 
    public TypeChecker() {
        return this.typeChecker;
    }
 
    public addReferenceType(referenceType: ReferenceType) {
        this.referenceTypes[referenceType.typeName] = referenceType;
    }
 
    public getReferenceType(typeName: string) {
        return this.referenceTypes[typeName];
    }
 
    public onFinish(callback: (referenceTypes: { [typeName: string]: ReferenceType }) => void) {
        this.circularDependencyResolvers.push(callback);
    }
 
    private buildControllers() {
        return this.nodes
            .filter(node => node.kind === ts.SyntaxKind.ClassDeclaration)
            .map((classDeclaration: ts.ClassDeclaration) => new ControllerGenerator(classDeclaration))
            .filter(generator => generator.IsValid())
            .map(generator => generator.Generate());
    }
}
 
export interface Metadata {
    Controllers: Controller[];
    ReferenceTypes: { [typeName: string]: ReferenceType };
}
 
export interface Controller {
    location: string;
    methods: Method[];
    name: string;
    path: string;
}
 
export interface Method {
    deprecated?: boolean;
    description: string;
    method: string;
    name: string;
    parameters: Parameter[];
    path: string;
    type: Type;
    tags: string[];
    responses: ResponseType[];
    security?: Security;
    summary?: string;
}
 
export interface Parameter {
    parameterName: string;
    description: string;
    in: string;
    name: string;
    required: boolean;
    type: Type;
}
 
export interface Security {
    name: string;
    scopes?: string[];
}
 
export interface Type {
    typeName: string;
}
 
export interface EnumerateType extends Type {
    enumMembers: string[];
}
 
export interface ReferenceType extends Type {
    description: string;
    properties: Property[];
    additionalProperties?: Property[];
}
 
export interface ArrayType extends Type {
    elementType: Type;
}
 
export interface ResponseType {
    description: string;
    name: string;
    schema?: Type;
    examples?: any;
}
 
export interface Property {
    description: string;
    name: string;
    type: Type;
    required: boolean;
}