Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support DTO required fields as not optional properties on interface in TS. #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>2.11.1</VersionPrefix>
<VersionPrefix>3.0.0</VersionPrefix>
<PackageValidationBaselineVersion>2.8.0</PackageValidationBaselineVersion>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
Expand Down
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

These are the NuGet package releases. See also [npm Release Notes](ReleaseNotesNpm.md).

## 3.0.0

* Support required fields as not optional in TypeScript.

## 2.11.1

* Ignore events for now.
Expand Down
12 changes: 6 additions & 6 deletions conformance/src/conformanceApiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface ICreateWidgetResponse {
/** Request for GetWidget. */
export interface IGetWidgetRequest {
/** The widget ID. */
id?: number;
id: number;

/** Don't get the widget if it has this ETag. */
ifNotETag?: string;
Expand Down Expand Up @@ -127,7 +127,7 @@ export interface IDeleteWidgetResponse {
/** Request for GetWidgetBatch. */
export interface IGetWidgetBatchRequest {
/** The IDs of the widgets to return. */
ids?: number[];
ids: number[];
}

/** Response for GetWidgetBatch. */
Expand Down Expand Up @@ -258,9 +258,9 @@ export interface IMixedResponse {

/** Request for Required. */
export interface IRequiredRequest {
query?: string;
query: string;

normal?: string;
normal: string;

widget?: IWidget;

Expand All @@ -281,7 +281,7 @@ export interface IRequiredRequest {

/** Response for Required. */
export interface IRequiredResponse {
normal?: string;
normal: string;
}

/** Request for MirrorBytes. */
Expand Down Expand Up @@ -328,7 +328,7 @@ export interface IWidget {
id?: number;

/** The name of the widget. */
name?: string;
name: string;
}

export interface IAny {
Expand Down
12 changes: 6 additions & 6 deletions conformance/src/fastify/conformanceApiPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ export interface ICreateWidgetResponse {
/** Request for GetWidget. */
export interface IGetWidgetRequest {
/** The widget ID. */
id?: number;
id: number;

/** Don't get the widget if it has this ETag. */
ifNotETag?: string;
Expand Down Expand Up @@ -944,7 +944,7 @@ export interface IDeleteWidgetResponse {
/** Request for GetWidgetBatch. */
export interface IGetWidgetBatchRequest {
/** The IDs of the widgets to return. */
ids?: number[];
ids: number[];
}

/** Response for GetWidgetBatch. */
Expand Down Expand Up @@ -1075,9 +1075,9 @@ export interface IMixedResponse {

/** Request for Required. */
export interface IRequiredRequest {
query?: string;
query: string;

normal?: string;
normal: string;

widget?: IWidget;

Expand All @@ -1098,7 +1098,7 @@ export interface IRequiredRequest {

/** Response for Required. */
export interface IRequiredResponse {
normal?: string;
normal: string;
}

/** Request for MirrorBytes. */
Expand Down Expand Up @@ -1145,7 +1145,7 @@ export interface IWidget {
id?: number;

/** The name of the widget. */
name?: string;
name: string;
}

export interface IAny {
Expand Down
2 changes: 1 addition & 1 deletion src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ private static void WriteDto(CodeWriter code, ServiceDtoInfo dtoInfo, ServiceInf
{
code.WriteLineSkipOnce();
WriteJsDoc(code, fieldInfo);
code.WriteLine($"{fieldInfo.Name}?: {RenderFieldType(service.GetFieldType(fieldInfo)!)};");
code.WriteLine($"{fieldInfo.Name}{(fieldInfo.IsRequired ? "" : "?")}: {RenderFieldType(service.GetFieldType(fieldInfo)!)};");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ public void GenerateExampleApiTypeScript_DataPropertiesRequired()

var typesFile = result.Files.Single(f => f.Name == "testApiTypes.ts");
Assert.That(typesFile.Text, Does.Contain("export interface IWidget {"));
Assert.That(typesFile.Text, Does.Contain("id?: string;"));
Assert.That(typesFile.Text, Does.Contain("name?: string;"));
Assert.That(typesFile.Text, Does.Contain("id: string;"));
Assert.That(typesFile.Text, Does.Contain("name: string;"));
Assert.That(typesFile.Text, Does.Contain("price?: number;"));
Assert.That(typesFile.Text, Does.Contain("}"));
}
Expand Down
Loading