Esempio n. 1
0
        private void Process(CoderStringBuilder output, Specification specification)
        {
            output.AppendLine($"export module {CleanClassName(specification.Info.Title, false)} {{");
            output.Indent();
            foreach (var defination in specification.Definations)
            {
                AddTypes(output, defination.Name, defination.Properties);
            }

            foreach (var path in specification.Paths)
            {
                AddAPIRequest(output, path.Delete);
                AddAPIRequest(output, path.Get);
                AddAPIRequest(output, path.Head);
                AddAPIRequest(output, path.Options);
                AddAPIRequest(output, path.Patch);
                AddAPIRequest(output, path.Post);
                AddAPIRequest(output, path.Put);
            }

            output.AppendLine("export interface API {");
            output.Indent();
            output.AppendLine("setToken(value: string, headerOrQueryName: string, isQuery: boolean): void;");

            foreach (var path in specification.Paths)
            {
                AddAPICall(output, path, HTTPAction.Delete);
                AddAPICall(output, path, HTTPAction.Get);
                AddAPICall(output, path, HTTPAction.Head);
                AddAPICall(output, path, HTTPAction.Options);
                AddAPICall(output, path, HTTPAction.Patch);
                AddAPICall(output, path, HTTPAction.Post);
                AddAPICall(output, path, HTTPAction.Put);
            }

            output.Outdent();
            output.AppendLine("}");

            output.Outdent();
            output.AppendLine("}");
        }
Esempio n. 2
0
        private void AddParameterInterface(CoderStringBuilder output, string sourceName, Parameter[] parameters)
        {
            var name = CleanClassName(sourceName, false);

            if (existingInterfaces.Contains(name))
            {
                return;
            }

            existingInterfaces.Add(name);

            output.AppendLine($"export interface I{name} {{");
            output.Indent();
            foreach (var parameter in parameters)
            {
                var propertyName = parameter.Name;
                if (!parameter.Required)
                {
                    propertyName += "?";
                }

                var propertyType  = "any";
                var bodyParameter = parameter as BodyParameter;
                if (bodyParameter != null)
                {
                    propertyType = SchemaTypeCleaner(bodyParameter.Schema);
                }

                var otherParameter = parameter as OtherParameter;
                if (otherParameter != null)
                {
                    var arrayParameter = parameter as OtherArrayParameter;
                    if (arrayParameter != null)
                    {
                        propertyType = $"Array<{CleanClassName(arrayParameter.Items[0].Type)}>";
                    }
                    else
                    {
                        propertyType = CleanClassName(otherParameter.Type);
                    }
                }

                output.AppendLine($"{propertyName}: {propertyType};");
            }

            output.Outdent();
            output.AppendLine("}");
            output.AppendLine();
        }
Esempio n. 3
0
        public void Run(Configuration swaggerConfig, Specification[] specifications)
        {
            existingInterfaces.Add("any");
            var output = new CoderStringBuilder();

            output.AppendLine($"//{Messages.VersionIdentifierPrefix}:{Configuration.nSwaggerVersion}");
            output.AppendLine($"// {Messages.Notice}");
            output.AppendLine($"// {Messages.LastGenerated} {DateTime.UtcNow:o}");
            output.AppendLine($"namespace {swaggerConfig.Namespace} {{");
            output.Indent();
            foreach (var specification in specifications.Where(_ => !_.Error))
            {
                Process(output, specification);
            }

            output.Outdent();
            output.AppendLine("}");

            if (!swaggerConfig.DoNotWriteTargetFile)
            {
                File.WriteAllText(swaggerConfig.Target, output.ToString());
            }
        }
Esempio n. 4
0
 private void OutputBasicTypeClose(CoderStringBuilder output)
 {
     output.Outdent();
     output.AppendLine("}");
     output.AppendLine();
 }
Esempio n. 5
0
        private void AddTypes(CoderStringBuilder output, string sourceName, Property[] properties)
        {
            var enums = new List <EnumInfo>();
            var name  = CleanClassName(sourceName, false);

            if (existingInterfaces.Contains(name))
            {
                return;
            }

            existingInterfaces.Add(name);

            OutputBasicType(output, false, name, properties, true, enums);

            var enumProperties = (properties?.Any(_ => _.Enum != null));

            if (enumProperties.HasValue && enumProperties.Value)
            {
                OutputBasicType(output, true, name, properties);

                output.AppendLine();
                output.AppendLine("constructor(source?: any) {");
                output.Indent();
                output.AppendLine("if (source !== undefined) {");
                output.Indent();
                output.AppendLine("Object.assign(this, source);");
                output.Outdent();
                output.AppendLine("}");
                output.Outdent();
                output.AppendLine("}");

                foreach (var property in properties.Where(_ => _.Enum != null))
                {
                    var propertyName = property.Name;
                    var enumName     = name + propertyName;
                    output.AppendLine();
                    output.AppendLine($"get {propertyName}AsEnum(): {enumName} {{");
                    output.Indent();
                    output.AppendLine($"return {enumName}[this.{propertyName}];");
                    output.Outdent();
                    output.AppendLine("}");

                    output.AppendLine();
                    output.AppendLine($"set {propertyName}AsEnum(value:{enumName}) {{");
                    output.Indent();
                    output.AppendLine($"this.{propertyName} = {enumName}[value];");
                    output.Outdent();
                    output.AppendLine("}");
                }

                OutputBasicTypeClose(output);
            }

            foreach (var @enum in enums)
            {
                output.AppendLine($"export enum {@enum.EnumClassName} {{");
                output.Indent();
                var addComma = false;
                foreach (var enumValue in @enum.EnumValues)
                {
                    if (!addComma)
                    {
                        addComma = true;
                    }
                    else
                    {
                        output.AppendLine(",", true);
                    }

                    output.Append(enumValue);
                }
                output.AppendLine();
                output.Outdent();
                output.AppendLine("}");
                output.AppendLine();
            }
        }