Example #1
0
        public CodeArtifactCollection GenerateArtifacts(SigSpecDocument document)
        {
            var resolver = new TypeScriptTypeResolver(_settings.TypeScriptGeneratorSettings);

            resolver.RegisterSchemaDefinitions(document.Definitions);

            var artifacts = new List <CodeArtifact>();

            foreach (var hub in document.Hubs)
            {
                var hubModel = new HubModel(hub.Key, hub.Value, resolver);
                var template = _settings.TypeScriptGeneratorSettings.TemplateFactory.CreateTemplate("TypeScript", "Hub", hubModel);
                artifacts.Add(new CodeArtifact(hubModel.Name, CodeArtifactType.Class, CodeArtifactLanguage.TypeScript, template));
            }

            if (_settings.GenerateDtoTypes)
            {
                var generator = new TypeScriptGenerator(document, _settings.TypeScriptGeneratorSettings, resolver);
                var types     = generator.GenerateTypes();

                return(new CodeArtifactCollection(artifacts.Concat(types.Artifacts), types.ExtensionCode));
            }
            else
            {
                var extensionCode = new TypeScriptExtensionCode(_settings.TypeScriptGeneratorSettings.ExtensionCode, _settings.TypeScriptGeneratorSettings.ExtendedClasses);
                return(new CodeArtifactCollection(artifacts, extensionCode));
            }
        }
Example #2
0
        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(beDest.Text))
            {
                MessageBox.Show("The target directory does not exist.");
                return;
            }

            var schemaSwagger = await SwaggerDocument.FromUrlAsync(meURL.Text);

            TypeScriptGeneratorSettings tsGenSettings = new TypeScriptGeneratorSettings()
            {
                ConvertConstructorInterfaceData = true,
                TypeStyle = TypeScriptTypeStyle.KnockoutClass,
                MarkOptionalProperties = true,
                GenerateDefaultValues  = false,
                PropertyNameGenerator  = new myPropertyNameGenerator(),
                TypeNameGenerator      = new myTypeNameGenerator(),
                TypeScriptVersion      = 2.4m
            };

            var _resolver = new TypeScriptTypeResolver(tsGenSettings);

            _resolver.RegisterSchemaDefinitions(schemaSwagger.Definitions);
            var generatorJ = new TypeScriptGenerator(schemaSwagger, tsGenSettings, _resolver);
            var typeDef    = generatorJ.GenerateTypes();

            foreach (CodeArtifact codeArtifact in typeDef.Artifacts)
            {
                File.WriteAllText(Path.Combine(beDest.Text, codeArtifact.TypeName + ".ts"), meFileHeader.Text + "\n\n" + codeArtifact.Code);
            }
        }
        public IEnumerable <CodeArtifact> GenerateArtifacts(SigSpecDocument document)
        {
            var resolver = new TypeScriptTypeResolver(this._settings.TypeScriptGeneratorSettings);

            resolver.RegisterSchemaDefinitions(document.Definitions);

            var artifacts = new List <CodeArtifact>();

            foreach (var hub in document.Hubs)
            {
                var hubModel = new HubModel(hub.Key, hub.Value, resolver);
                var template = this._settings.TypeScriptGeneratorSettings.TemplateFactory.CreateTemplate("TypeScript", "Hub", hubModel);
                artifacts.Add(new CodeArtifact(hubModel.Name, CodeArtifactType.Class, CodeArtifactLanguage.TypeScript, CodeArtifactCategory.Client, template));
            }

            if (this._settings.GenerateDtoTypes)
            {
                var generator = new TypeScriptGenerator(document, this._settings.TypeScriptGeneratorSettings, resolver);
                var types     = generator.GenerateTypes();

                return(artifacts.Concat(types));
            }
            else
            {
                return(artifacts);
            }
        }
Example #4
0
        /// <summary>Initializes a new instance of the <see cref="SwaggerToTypeScriptClientGenerator" /> class.</summary>
        /// <param name="document">The Swagger document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="resolver">The resolver.</param>
        /// <exception cref="ArgumentNullException"><paramref name="document" /> is <see langword="null" />.</exception>
        public SwaggerToTypeScriptClientGenerator(SwaggerDocument document, SwaggerToTypeScriptClientGeneratorSettings settings, TypeScriptTypeResolver resolver)
            : base(document, settings.CodeGeneratorSettings, resolver)
        {
            Settings = settings;

            _document = document ?? throw new ArgumentNullException(nameof(document));
            _resolver = resolver;
            _resolver.RegisterSchemaDefinitions(_document.Definitions);
            _extensionCode = new TypeScriptExtensionCode(
                Settings.TypeScriptGeneratorSettings.ExtensionCode,
                (Settings.TypeScriptGeneratorSettings.ExtendedClasses ?? new string[] { }).Concat(new[] { Settings.ConfigurationClass }).ToArray(),
                new[] { Settings.ClientBaseClass });
        }
Example #5
0
 public SelfTypeScriptGenerator(TypeScriptClientGeneratorSettings clientGeneratorSettings,
                                OpenApiDocument openApiDocument)
 {
     _clientGeneratorSettings = clientGeneratorSettings;
     if (_clientGeneratorSettings.ExcludedParameterNames == null)
     {
         _clientGeneratorSettings.ExcludedParameterNames = new string[0];
     }
     Constant.TsBaseType.AddRange(_clientGeneratorSettings.ExcludedParameterNames);
     _resolver        = new TypeScriptTypeResolver(clientGeneratorSettings.TypeScriptGeneratorSettings);
     _openApiDocument = openApiDocument;
     _resolver.RegisterSchemaDefinitions(_openApiDocument.Definitions);
     _typeScriptClientGenerator =
         new TypeScriptClientGenerator(_openApiDocument, _clientGeneratorSettings, _resolver);
     _extensionCode = new TypeScriptExtensionCode(
         clientGeneratorSettings.TypeScriptGeneratorSettings.ExtensionCode,
         (clientGeneratorSettings.TypeScriptGeneratorSettings.ExtendedClasses ?? new string[] { })
         .Concat(new[] { clientGeneratorSettings.ConfigurationClass }).ToArray(),
         new[] { clientGeneratorSettings.ClientBaseClass });
 }
        public async Task CreateClient(TextWriter writer)
        {
            var interfacesToWrite = new InterfaceManager();

            writer.WriteLine(
                @"import * as hal from 'htmlrapier.halcyon/src/EndpointClient';
import { Fetcher } from 'htmlrapier/src/fetcher';"
                );

            await WriteClient(interfacesToWrite, writer);

            //Write interfaces, kind of weird, no good docs for this
            var settings = new TypeScriptGeneratorSettings()
            {
                TypeStyle = TypeScriptTypeStyle.Interface,
                ForceAllPropertiesOptional = true,
                DateTimeType      = TypeScriptDateTimeType.String,
                EnumNameGenerator = new EnumValueEnumNameGenerator(),
            };

            //Gather up everything to write, skip duplicate instances of the same thing
            Dictionary <String, CodeArtifact> codeArtifacts = new Dictionary <String, CodeArtifact>();
            ExtensionCode lastExtensionCode = null;

            foreach (var item in interfacesToWrite.Interfaces)
            {
                //Remove any properties from item that are hal embeds
                var propertiesToRemove = item.Value.Properties.Where(i => i.Value.IsHalEmbedded()).ToList();
                foreach (var remove in propertiesToRemove)
                {
                    item.Value.Properties.Remove(remove.Key);
                }

                var resolver = new TypeScriptTypeResolver(settings);
                resolver.RegisterSchemaDefinitions(new Dictionary <String, JsonSchema4>()
                {
                    { item.Key, item.Value }
                });                                                                                                     //Add all discovered generators

                var generator = new TypeScriptGenerator(item.Value, settings, resolver);
                var artifacts = generator.GenerateTypes();
                foreach (var artifact in artifacts.Artifacts)
                {
                    if (!codeArtifacts.ContainsKey(artifact.TypeName))
                    {
                        codeArtifacts.Add(artifact.TypeName, artifact);
                    }
                }
                lastExtensionCode = artifacts.ExtensionCode;
            }

            //Write the classes officially
            //From TypeScriptGenerator.cs GenerateFile, (NJsonSchema 9.10.49)
            var model = new FileTemplateModel(settings)
            {
                Types         = ConversionUtilities.TrimWhiteSpaces(string.Join("\n\n", CodeArtifactCollection.OrderByBaseDependency(codeArtifacts.Values).Select(p => p.Code))),
                ExtensionCode = (TypeScriptExtensionCode)lastExtensionCode
            };

            var template = settings.TemplateFactory.CreateTemplate("TypeScript", "File", model);
            var classes  = ConversionUtilities.TrimWhiteSpaces(template.Render());

            writer.WriteLine(classes);

            //Write out common interfaces we reuse in all clients
            writer.WriteLine(@"
export interface HalEndpointDocQuery {
    includeRequest?: boolean;
    includeResponse?: boolean;
}");

            //End Write Interfaces
        }