import { Service } from 'typedi' ; import { Class , ClassLiteral , isPrimitive } from '@mockingbird/common' ; import { MockGeneratorOptions } from '../types/mock-generator-options.interface' ; import { ClassParser } from '../parser/class-parser' ; @ Service ( ) export class MockGenerator { public constructor ( private readonly classParser ) { } private static classToPlain < TClass > ( targetClass ) { const toPlain = ( target ) : ClassLiteral < TClass > => { for ( const key of Object . keys ( target ) ) { const value = target [ key ] ; if ( value instanceof Object ) { if ( isPrimitive ( value . constructor . name ) ) { toPlain ( value ) ; } else { target [ key ] = toPlain ( { ... value } ) ; } } } return target ; } ; return toPlain ( { ... targetClass } ) ; } example Mock param targetClass { Class < TClass > } returns { TClass } public generate < TClass = any > ( targetClass ) ; example Mock param targetClass param options public generate < TClass = any > ( targetClass , options ) ; public generate < TClass = any > ( targetClass , options ) ; param targetClass param options public generate < TClass = any > ( targetClass , options = { } ) { const { count = 1 , plain = false , locale = 'en' , ... config } = options || { } ; this . classParser . setLocale ( locale ) ; if ( count === 1 ) { const parsedClass = this . classParser . parse ( targetClass , config ) ; return plain ? MockGenerator . classToPlain < TClass > ( parsedClass ) : parsedClass ; } const classInstances = [ ] ; for ( let i = 1 ; i <= count ; i ++ ) { const parsedClass = this . classParser . parse ( targetClass , config ) ; classInstances . push ( plain ? MockGenerator . classToPlain < TClass > ( parsedClass ) : parsedClass ) ; } return classInstances ; } }