public TypeMapping( ITypeConverter customTypeConverter = null, IEnumerable <ITypeDescriptorProvider> typeDescriptorProviders = null, bool appendIPrefix = false, TypeMappingConfig config = default) { _customTypeConverter = customTypeConverter; _typeDescriptorProviders = typeDescriptorProviders?.ToArray() ?? Array.Empty <ITypeDescriptorProvider>(); _appendIPrefix = appendIPrefix; _config = config; _nullabilityHandler = NullabilityHandlerResolver.FromConfig(config); }
public void GenerateApiClients( IEnumerable <ApiClientModule> apiClientModules, IEnumerable <TSStaticContentAttribute> staticContentModules, string transportModuleName, string enumsModuleName, HashSet <Type> allEnums) { var sw = new Stopwatch(); sw.Start(); var moduleNames = new HashSet <string>(); var staticContentByModuleName = staticContentModules.ToDictionary(attr => attr.ModuleName); // generating client modules for api controllers foreach (var module in apiClientModules) { if (moduleNames.Contains(module.Name)) { throw new Exception("Duplicate module name - " + module.Name); } moduleNames.Add(module.Name); var typeMapping = new TypeMapping( _customTypeConverter, _typeDescriptorProviders, _arguments.AppendIPrefix, TypeMappingConfig.FromArgs(_arguments)); foreach (var type in module.ExplicitlyRequiredTypes.Where(t => !t.IsEnum)) { typeMapping.AddType(type); } var generator = new ApiModuleGenerator(module, typeMapping, _customApiClientWriter, _serializeToJson, transportModuleName); generator.WriteApiClientClass(); generator.WriteTypeDefinitions(); generator.WriteEnumImports(enumsModuleName); var explictlyRequiredEnums = module.ExplicitlyRequiredTypes.Where(t => t.IsEnum); foreach (var enumType in typeMapping.GetEnums().Union(explictlyRequiredEnums)) { allEnums.Add(enumType); } if (staticContentByModuleName.TryGetValue(module.Name, out var staticContentModule)) { generator.WriteStaticContent(staticContentModule); } _resultFileWriter.WriteFile($"{module.Name}.ts", generator.GetResult()); Console.WriteLine($"TypeScript client `{module.Name}` generated in {sw.ElapsedMilliseconds} ms"); sw.Restart(); } foreach (var staticContent in staticContentByModuleName.Values.Where(attr => !moduleNames.Contains(attr.ModuleName))) { if (moduleNames.Contains(staticContent.ModuleName)) { throw new Exception("Duplicate module name - " + staticContent.ModuleName); } moduleNames.Add(staticContent.ModuleName); var content = string.Join("\r\n", staticContent.Content.Select(entry => $"export let {entry.Key} = {_serializeToJson(entry.Value)};")); _resultFileWriter.WriteFile($"{staticContent.ModuleName}.ts", content); } }