Ejemplo n.º 1
0
 public GenSdk(
     IApiDescriptionGroupCollectionProvider apiDescriptionsProvider,
     IOptions <MvcJsonOptions> mvcJsonOpts,
     ILogger <GenSdk> logger)
 {
     _logger = logger;
     _apiDescriptionsProvider = apiDescriptionsProvider;
     _jsonSerializerSettings  = mvcJsonOpts.Value.SerializerSettings;
     _tsGen = new TypescriptGenerator(mvcJsonOpts.Value?.SerializerSettings?.ContractResolver
                                      ?? new DefaultContractResolver());
 }
Ejemplo n.º 2
0
            public string GenTypescript(GenSdkOptions opts, TypescriptGenerator tsGen)
            {
                var parentItems = new List <string>();

                foreach (var p in Children)
                {
                    var item = p.Value.GenTypescript(opts, tsGen);
                    if (p.Value is DictItem)
                    {
                        item = indentAllButFirstLine(item, opts.Indent);
                    }
                    parentItems.Add($"{opts.Indent}{p.Key} : {item}");
                }
                return($"{{\r\n{string.Join(",\r\n", parentItems)}\r\n}}");
            }
Ejemplo n.º 3
0
        private static string getApiErrors(GenSdkOptions opts, TypescriptGenerator tsGen)
        {
            IEnumerable <Type> apiErrors = typeof(ApiError).GetTypeInfo().Assembly
                                           .GetTypes()
                                           .Where(t => typeof(ApiError).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract)
                                           .Select(t => t);

            var errorDefinitions = new List <ErrorDefinition>();

            foreach (var t in apiErrors)
            {
                var bt = t.GetTypeInfo().BaseType;

                if (bt == typeof(ApiError))
                {
                    errorDefinitions.Add(new ErrorDefinition()
                    {
                        Error    = t.Name,
                        DataType = null
                    });
                    continue;
                }
                else if (bt.IsConstructedGenericType &&
                         bt.GetTypeInfo().GetGenericTypeDefinition() == typeof(ApiError <>))
                {
                    var dataType = bt.GenericTypeArguments[0];
                    tsGen.Generate(dataType);
                    errorDefinitions.Add(new ErrorDefinition()
                    {
                        Error    = t.Name,
                        DataType = dataType
                    });
                    continue;
                }
            }

            var errs = new List <string>();

            foreach (var def in errorDefinitions)
            {
                errs.Add(opts.GetApiPromiseErrorFormat(def.Error, def.DataType != null ? tsGen.Generate(def.DataType) : "null"));
            }

            return(opts.GetApiPromiseFormat(apiErrors: indentAllButFirstLine(string.Join("\r\n", errs))));
        }
Ejemplo n.º 4
0
            public string GenTypescript(GenSdkOptions opts, TypescriptGenerator tsGen)
            {
                var inputParams = new List <string>();
                var outputValue = "void";

                if (InputFormType != null)
                {
                    inputParams.Add($"form: {tsGen.Generate(InputFormType)}");
                }
                if (InputBodyType != null)
                {
                    inputParams.Add($"body: {tsGen.Generate(InputBodyType)}");
                }
                if (ResultType != null)
                {
                    outputValue = tsGen.Generate(ResultType);
                }
                var resultType      = outputValue;
                var requestFunction = opts.GetApiRequestFunctionFormat(RelativePath, HttpMethod, InputBodyType != null ? "body" : "null", InputFormType != null ? "form" : "null", resultType);

                return(indentAllButFirstLine(opts.GetApiFunctionFormat(string.Join(", ", inputParams), resultType, requestFunction)));
                // return indentAllButFirstLine($"({string.Join(", ", inputParams)}): {outputFormat} =>\r\n{opts.Indent}{requestFormat}");
            }