Skip to content

Commit e959395

Browse files
fix(lib): Handle enums as query parameters #133
1 parent f418b21 commit e959395

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Better support for generic types (#123)
1313

14+
### Fixed
15+
16+
- Allow enums to be used as query parameters in API clients (#133)
17+
1418
## [0.16.0] - 2024-12-17
1519

1620
### Added

TypeContractor.Tests/TypeScript/ApiClientWriterTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,59 @@ public void Writes_Get_With_Route()
185185
.And.NotContain("url.searchParams.append(");
186186
}
187187

188+
[Fact]
189+
public void Handles_Get_With_Route_And_Query_Parameters()
190+
{
191+
// Arrange
192+
var apiClient = new ApiClient("TestClient", "TestController", "test", null);
193+
apiClient.AddEndpoint(new ApiClientEndpoint("getLatestId", "latest/{year}", EndpointMethod.GET, null, typeof(Guid), false,
194+
[new EndpointParameter("year", typeof(int), null, false, false, true, false, false, false, false, false),
195+
new EndpointParameter("reverse", typeof(bool?), null, false, false, false, true, false, false, false, true)], null));
196+
197+
// Act
198+
var result = Sut.Write(apiClient, [], _converter, true, _templateFn, Casing.Pascal);
199+
200+
// Assert
201+
var file = File.ReadAllText(result).Trim();
202+
file.Should()
203+
.NotBeEmpty()
204+
.And.Contain("import { z } from 'zod';")
205+
.And.Contain("export class TestClient {")
206+
.And.Contain("public async getLatestId(year: number, reverse: boolean | undefined, cancellationToken: AbortSignal = null): Promise<string> {")
207+
.And.Contain("const url = new URL(`test/latest/${year}`, window.location.origin);")
208+
.And.Contain("if (!!reverse)")
209+
.And.Contain("url.searchParams.append('reverse', reverse.toString());");
210+
}
211+
212+
[Fact]
213+
public void Handles_Enum_As_Query_Parameter()
214+
{
215+
// Arrange
216+
var outputTypes = new List<OutputType>
217+
{
218+
_converter.Convert(typeof(ReportType))
219+
};
220+
221+
var apiClient = new ApiClient("TestClient", "TestController", "test", null);
222+
apiClient.AddEndpoint(new ApiClientEndpoint("getLatestId", "latest/{year}", EndpointMethod.GET, null, typeof(Guid), false,
223+
[new EndpointParameter("year", typeof(int), null, false, false, true, false, false, false, false, false),
224+
new EndpointParameter("reportType", typeof(ReportType), null, false, false, false, true, false, false, false, false)], null));
225+
226+
// Act
227+
var result = Sut.Write(apiClient, outputTypes, _converter, true, _templateFn, Casing.Pascal);
228+
229+
// Assert
230+
var file = File.ReadAllText(result).Trim();
231+
file.Should()
232+
.NotBeEmpty()
233+
.And.Contain("import { z } from 'zod';")
234+
.And.Contain("export class TestClient {")
235+
.And.Contain("public async getLatestId(year: number, reportType: ReportType, cancellationToken: AbortSignal = null): Promise<string> {")
236+
.And.Contain("const url = new URL(`test/latest/${year}`, window.location.origin);")
237+
.And.NotContain("if (!!reportType)")
238+
.And.Contain("url.searchParams.append('reportType', reportType.toString());");
239+
}
240+
188241
[Fact]
189242
public void Unpacks_Complex_Object_To_Query()
190243
{
@@ -290,6 +343,12 @@ private class PaginatedRequest
290343
public int PageSize { get; set; }
291344
}
292345

346+
private enum ReportType
347+
{
348+
Summary = 0,
349+
Detailed = 1,
350+
}
351+
293352
private MetadataLoadContext BuildMetadataLoadContext()
294353
{
295354
// Get the array of runtime assemblies.

TypeContractor/Helpers/CasingHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Globalization;
1+
using System.Globalization;
22
using System.Text;
33
using System.Text.RegularExpressions;
44

TypeContractor/TypeScript/ApiClientWriter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public string Write(ApiClient apiClient, IEnumerable<OutputType> allTypes, TypeS
9191
continue;
9292
}
9393

94+
if (outputType.IsEnum)
95+
{
96+
queryParamsDto.Add(new QueryParameterTemplateDto(queryParam.Name, true, false, false, queryParam.IsOptional, null));
97+
continue;
98+
}
99+
94100
foreach (var property in outputType.Properties ?? [])
95101
{
96102
queryParamsDto.Add(new QueryParameterTemplateDto(queryParam.Name, false, property.IsNullable, false, queryParam.IsOptional, property.DestinationName));

0 commit comments

Comments
 (0)