public void MyTest() { var engine = new ConvertContext(); engine.Configuration.AddConverter(new HttpRequestInterfaceConverter()); engine.Configuration.AddConverter(new RequestDispatcherConverter(DispatcherResponseType.Observable)); engine.Configuration.AddConverter(new RequestConverter()); engine.GenerateForTypes(typeof(TestRequest)); var files = engine.GetFiles().ToDictionary(x => x.Key, x => x.Value); var expected = new Dictionary <string, string> { { "IHttpRequest.ts", @"export interface IHttpRequest<TResponse> { method: string; route: string; body: any; queryString: { [key: string]: string | string[]; }; }" }, { "IRequestDispatcher.ts", @"import { Observable } from 'rxjs/index'; import { IHttpRequest } from './IHttpRequest'; export interface IRequestDispatcher { execute<TResponse>(request: IHttpRequest<TResponse>): Observable<TResponse>; }" }, { "TestRequest.ts", @"import { TestFilterDto } from './TestFilterDto'; import { IHttpRequest } from './IHttpRequest'; import { TestResponse } from './TestResponse'; import { IRequestDispatcher } from './IRequestDispatcher'; export class TestRequest { constructor( public id?: string, public numbers?: Array<number>, public filter?: TestFilterDto) { } public __name = 'TestRequest'; private __request = () => { const req: IHttpRequest<TestResponse> = { method: 'get', route: 'api/values/{Id}'.replace('{Id}', this.id ? this.id.toString() : ''), body: undefined, queryString: {} }; if (this.numbers) req.queryString['numbers'] = this.numbers.map(i => i ? i.toString() : null).filter(i => typeof i === 'string'); if (this.filter) { if (this.filter.search) req.queryString['filter.search'] = this.filter.search; if (this.filter.page) { if (this.filter.page.size) req.queryString['filter.page.size'] = this.filter.page.size.toString(); if (this.filter.page.index) req.queryString['filter.page.index'] = this.filter.page.index.toString(); } } return req; } public execute = (dispatcher: IRequestDispatcher) => dispatcher.execute(this.__request()); } " }, { "TestResponse.ts", @"export class TestResponse { constructor( public date?: Date) { } } " }, { "TestFilterDto.ts", @"import { PageDto } from './PageDto'; import { PagedFilterDto } from './PagedFilterDto'; export class TestFilterDto extends PagedFilterDto { constructor( page?: PageDto, public search?: string) { super(page); } } " }, { "PagedFilterDto.ts", @"import { PageDto } from './PageDto'; export class PagedFilterDto { constructor( public page?: PageDto) { } } " }, { "PageDto.ts", @"export class PageDto { constructor( public size?: number, public index?: number) { } } " } }; expected.All(x => files.ContainsKey(x.Key)).Should().BeTrue("Expected file is not found in actual"); files.All(x => expected.ContainsKey(x.Key)).Should().BeTrue("Actual file is not found in expected"); foreach (var expectedFile in expected) { expectedFile.Value.Should().BeLike(files[expectedFile.Key], $"File content for {expectedFile.Key} does not match the expected value."); } }
public void GenerateTypeScript() { var engine = new ConvertContext(); engine.Configuration.GetFileDirectory = type => type == typeof(Person) ? "/dto/person/" : type == typeof(Employee) ? "/dto/person/employee/" : "/"; engine.GenerateForTypes( typeof(Employee), typeof(Employer)); var files = engine.GetFiles().ToDictionary(x => x.Key, x => x.Value); var expected = new Dictionary <string, string> { { "dto/person/employee/Employee.ts", @"import { Person } from '../Person'; import { GenericInheritance } from '../../../GenericInheritance'; import { MyGenericTest } from '../../../MyGenericTest'; export class Employee extends Person { constructor( firstName?: string, lastName?: string, children?: Array<Person>, public salary?: number, public hello?: GenericInheritance<MyGenericTest<string>, string>) { super(firstName, lastName, children); } }" }, { "Employer.ts", @"import { Person } from './dto/person/Person'; export class Employer extends Person { constructor( firstName?: string, lastName?: string, children?: Array<Person>, public companyName?: string) { super(firstName, lastName, children); } }" }, { "GenericInheritance.ts", @"import { MyGenericTest } from './MyGenericTest'; export class GenericInheritance<T1, T2> extends MyGenericTest<T2> { constructor( genericProperty?: T2, public anotherProperty?: Array<T1>) { super(genericProperty); } }" }, { "MyGenericTest.ts", @"export class MyGenericTest<T> { constructor( public genericProperty?: T) { } } " }, { "dto/person/Person.ts", @"export abstract class Person { constructor( public firstName?: string, public lastName?: string, public children?: Array<Person>) { } } " }, }; expected.All(x => files.ContainsKey(x.Key)).Should().BeTrue("Expected file is not found in actual"); files.All(x => expected.ContainsKey(x.Key)).Should().BeTrue("Actual file is not found in expected"); expected.ToList().ForEach(x => files[x.Key].Should().BeLike(x.Value)); }