public void WriteTo(TypeScriptWriter writer) { _options.WriteHeader(writer); var rWriter = new ReflectiveWriter(_options); if (!_options.RequestHelperTypeOption.ShouldEmitTypes) { foreach (var type in _options.RequestHelperTypeOption.Types) _options.Types.AddLiteralImport(_options.RequestHelperTypeOption.ImportFrom, type); } AddTypeDependencies(rWriter); rWriter.WriteImports(writer); if (_options.RequestHelperTypeOption.ShouldEmitTypes) { WriteRequestTypesAndHelpers(writer); } foreach (var factory in _requests.GroupBy(r => r.FactoryName)) { writer.WriteClass(factory.Key) .Configure(c => { c.MakeAbstract(); foreach (var req in factory) { var methodBuilder = c.AddMethod($"{req.Name}") .Static(); if (req.Method.HasRequestBody()) { var requestBodyType = req.GetRequestBodyKind() == RequestBodyKind.Json ? _options.TypeConverter.Convert(req.GetRequestBodyType()) : (req.GetRequestBodyKind() == RequestBodyKind.FormData ? new TypeReference("FormData") : _options.EmptyRequestBodyType); if (requestBodyType.Optional) { requestBodyType = new TypeReference($"{requestBodyType.FullName} | undefined"); } methodBuilder .WithReturnType(new TypeReference($"{req.Method.GetName()}Request", new[] { requestBodyType, _options.TypeConverter.Convert(req.ResponseType) })); } else { methodBuilder .WithReturnType(new TypeReference($"{req.Method.GetName()}Request", new[] { _options.TypeConverter.Convert(req.ResponseType) })); } methodBuilder.WithParams(p => { foreach (var rp in req.RequestParams.Where(x => x.Kind != ParameterKind.Body)) { p.Param(rp.NameOrDestructureExpression, _options.TypeConverter.Convert(rp.Type) .MakeOptional(rp.Kind == ParameterKind.Query && rp.Type.IsNullable())); } if (req.Method.HasRequestBody() && req.GetRequestBodyKind() == RequestBodyKind.Json) p.Param("data", _options.TypeConverter.Convert(req.GetRequestBodyType())); }) .WithBody(w => { var queryParams = req.RequestParams.Where(x => x.Kind == ParameterKind.Query) .ToArray(); if (queryParams.Any()) { w.Write($"const {_options.QueryVariableName} = toQuery({{ ", true); w.WriteDelimited(queryParams, (p, wr) => wr.Write(_options.TypeConverter.TreatAsPrimitive(p.Type.UnwrapNullable()) || p.Type.IsCollection() ? p.Name : $"...{p.Name}"), ", "); w.WriteLine(" });", false); } var formParams = req.RequestParams.Where(x => x.Kind == ParameterKind.Form) .ToArray(); if ((formParams.Any())) { w.WriteLine("const data = new FormData();"); foreach (var formParam in formParams) { w.WriteLine($"data.append('{formParam.Name}', {formParam.Name});"); } } w.WriteLine("return {"); w.Indent(); w.WriteLine($"method: '{req.Method.GetName().ToUpper()}',"); if (req.Method.HasRequestBody() && req.GetRequestBodyKind() != RequestBodyKind.Empty) w.WriteLine("data,"); else if (req.Method.HasRequestBody()) w.WriteLine("data: undefined,"); w.WriteLine( $"url: `{req.Path.Replace("{", "${")}{(queryParams.Any() ? $"${{{_options.QueryVariableName}}}" : "")}`"); w.Deindent(); w.WriteLine("};"); }); } }); } rWriter.WriteTo(writer, false); }
public void WriteTo(TypeScriptWriter writer) { CheckConfigured(); _options.WriteHeader(writer); var rWriter = new ReflectiveWriter(_options); if (!_options.RequestHelperTypeOption.ShouldEmitTypes) { foreach (var type in _options.RequestHelperTypeOption.Types) { _options.Types.AddLiteralImport(_options.RequestHelperTypeOption.ImportFrom, type); } } AddTypeDependencies(rWriter); rWriter.WriteImports(writer); if (_options.RequestHelperTypeOption.ShouldEmitTypes) { WriteRequestTypesAndHelpers(writer); } if (_requests.Any()) { writer.WriteClass("RequestFactory") .Configure(c => { c.MakeAbstract(); foreach (var req in _requests) { var methodBuilder = c.AddMethod($"{req.Name}") .Static(); if (req.Method.HasRequestBody()) { var requestBodyType = _options.TypeConverter.Convert(req.GetRequestBodyType(), null); if (requestBodyType.Optional) { requestBodyType = new TypeReference($"{requestBodyType.FullName} | undefined"); } methodBuilder .WithReturnType(new TypeReference($"{req.Method.GetName()}Request", new[] { requestBodyType, _options.TypeConverter.Convert(req.ResponseType, null) })); } else { methodBuilder .WithReturnType(new TypeReference($"{req.Method.GetName()}Request", new[] { _options.TypeConverter.Convert(req.ResponseType, null) })); } methodBuilder.WithParams(p => { foreach (var rp in req.RequestParams.Where(x => x.Kind != ParameterKind.Body)) { p.Param(rp.Name, _options.TypeConverter.Convert(rp.Type) .MakeOptional(rp.Kind == ParameterKind.Query)); } if (req.Method.HasRequestBody()) { p.Param("data", _options.TypeConverter.Convert(req.GetRequestBodyType(), null)); } }) .WithBody(w => { var queryParams = req.RequestParams.Where(x => x.Kind == ParameterKind.Query) .ToArray(); if (queryParams.Any()) { w.Write("const query = toQuery({", true); w.WriteDelimited(queryParams, (p, wr) => wr.Write(p.Name), ", "); w.WriteLine("});", false); } w.WriteLine("return {"); w.Indent(); w.WriteLine($"method: '{req.Method.GetName().ToUpper()}',"); if (req.Method.HasRequestBody()) { w.WriteLine("data,"); } w.WriteLine( $"url: `{req.Path.Replace("{", "${")}{(queryParams.Any() ? "${query}" : "")}`"); w.Deindent(); w.WriteLine("};"); }); } }); } rWriter.WriteTo(writer, false); }