Skip to content

Commit bfba63f

Browse files
committed
Add client name option to TypeScript client generator
1 parent 2b7b990 commit bfba63f

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

TypescriptClientGenerator/Generators/TypeScriptGenerator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ namespace TypescriptClientGenerator.Generators;
77
public class TypeScriptGenerator
88
{
99
private readonly OpenApiSpec _spec;
10+
private readonly string? _clientName;
1011

11-
public TypeScriptGenerator(OpenApiSpec spec)
12+
public TypeScriptGenerator(OpenApiSpec spec, string? clientName = null)
1213
{
1314
_spec = spec;
15+
_clientName = clientName;
1416
}
1517

1618
public string Generate()
@@ -131,6 +133,9 @@ private void GenerateApiMethods(StringBuilder sb)
131133

132134
private string GetClientName()
133135
{
136+
if (!string.IsNullOrEmpty(_clientName))
137+
return _clientName;
138+
134139
var title = _spec.Info.Title
135140
.Replace(" ", "")
136141
.Replace("|", "")

TypescriptClientGenerator/Program.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.CommandLine;
1+
using System.CommandLine;
22
using System.CommandLine.Invocation;
33
using System.Text.Json;
44
using TypescriptClientGenerator.Generators;
@@ -16,31 +16,40 @@
1616
DefaultValueFactory = _ => new FileInfo("generated.ts")
1717
};
1818

19+
var clientNameOption = new Option<string?>("--client-name", "-c")
20+
{
21+
Description = "The name of the generated client class (default: derived from OpenAPI title)"
22+
};
23+
1924
var rootCommand = new RootCommand("Generates a TypeScript client from an OpenAPI specification")
2025
{
2126
inputOption,
22-
outputOption
27+
outputOption,
28+
clientNameOption
2329
};
2430

25-
rootCommand.Action = new GenerateAction(inputOption, outputOption);
31+
rootCommand.Action = new GenerateAction(inputOption, outputOption, clientNameOption);
2632

2733
return await rootCommand.Parse(args).InvokeAsync();
2834

2935
class GenerateAction : AsynchronousCommandLineAction
3036
{
3137
private readonly Option<FileInfo> _inputOption;
3238
private readonly Option<FileInfo> _outputOption;
39+
private readonly Option<string?> _clientNameOption;
3340

34-
public GenerateAction(Option<FileInfo> inputOption, Option<FileInfo> outputOption)
41+
public GenerateAction(Option<FileInfo> inputOption, Option<FileInfo> outputOption, Option<string?> clientNameOption)
3542
{
3643
_inputOption = inputOption;
3744
_outputOption = outputOption;
45+
_clientNameOption = clientNameOption;
3846
}
3947

4048
public override async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken = default)
4149
{
4250
var inputFile = parseResult.GetValue(_inputOption)!;
4351
var outputFile = parseResult.GetValue(_outputOption)!;
52+
var clientName = parseResult.GetValue(_clientNameOption);
4453

4554
if (!inputFile.Exists)
4655
{
@@ -66,7 +75,7 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
6675
return 1;
6776
}
6877

69-
var generator = new TypeScriptGenerator(openApi);
78+
var generator = new TypeScriptGenerator(openApi, clientName);
7079
var generatedCode = generator.Generate();
7180

7281
await File.WriteAllTextAsync(outputFile.FullName, generatedCode, cancellationToken);

0 commit comments

Comments
 (0)