private/apigen: use correct TS type for nillable fields

This change gives the proper type to TS class fields generated from
nillable Go struct fields. Previously, Go struct fields having a nil
representation ([]Type, *Type, etc.) were translated into TypeScript as
"Type | undefined". This isn't correct because these fields, when nil,
are given the value "null" when marshalled into JSON. This change fixes
this issue by giving these fields the type "Type | null".

Change-Id: I5a1a83eb3810a3cba10895bb2f0f75ca5fd7d1b5
This commit is contained in:
Jeremy Wharton 2023-11-20 23:51:53 -06:00 committed by Storj Robot
parent 0f4f1ddde8
commit 587fa8fdff
5 changed files with 21 additions and 19 deletions

View File

@ -13,7 +13,7 @@ export class Document {
export class Metadata { export class Metadata {
owner?: string; owner?: string;
tags?: string[][]; tags: string[][] | null;
} }
export class NewDocument { export class NewDocument {

View File

@ -15,7 +15,7 @@ export class Document {
export class Metadata { export class Metadata {
owner?: string; owner?: string;
tags?: string[][]; tags: string[][] | null;
} }
export class NewDocument { export class NewDocument {

View File

@ -135,12 +135,14 @@ func (types *Types) GenerateTypescriptDefinitions() string {
continue continue
} }
isOptional := "" var isOptional, isNullable string
if isNillableType(field.Type) || jsonInfo.OmitEmpty { if jsonInfo.OmitEmpty {
isOptional = "?" isOptional = "?"
} else if isNillableType(field.Type) {
isNullable = " | null"
} }
pf("\t%s%s: %s;", jsonInfo.FieldName, isOptional, TypescriptTypeName(field.Type)) pf("\t%s%s: %s%s;", jsonInfo.FieldName, isOptional, TypescriptTypeName(field.Type), isNullable)
} }
}() }()
} }

View File

@ -8,13 +8,13 @@ export class APIKeyInfo {
id: UUID; id: UUID;
projectId: UUID; projectId: UUID;
projectPublicId: UUID; projectPublicId: UUID;
userAgent?: string; userAgent: string | null;
name: string; name: string;
createdAt: Time; createdAt: Time;
} }
export class APIKeyPage { export class APIKeyPage {
apiKeys?: APIKeyInfo[]; apiKeys: APIKeyInfo[] | null;
search: string; search: string;
limit: number; limit: number;
order: number; order: number;
@ -46,7 +46,7 @@ export class CreateAPIKeyRequest {
export class CreateAPIKeyResponse { export class CreateAPIKeyResponse {
key: string; key: string;
keyInfo?: APIKeyInfo; keyInfo: APIKeyInfo | null;
} }
export class Project { export class Project {
@ -54,18 +54,18 @@ export class Project {
publicId: UUID; publicId: UUID;
name: string; name: string;
description: string; description: string;
userAgent?: string; userAgent: string | null;
ownerId: UUID; ownerId: UUID;
rateLimit?: number; rateLimit: number | null;
burstLimit?: number; burstLimit: number | null;
maxBuckets?: number; maxBuckets: number | null;
createdAt: Time; createdAt: Time;
memberCount: number; memberCount: number;
storageLimit?: MemorySize; storageLimit: MemorySize | null;
bandwidthLimit?: MemorySize; bandwidthLimit: MemorySize | null;
userSpecifiedStorageLimit?: MemorySize; userSpecifiedStorageLimit: MemorySize | null;
userSpecifiedBandwidthLimit?: MemorySize; userSpecifiedBandwidthLimit: MemorySize | null;
segmentLimit?: number; segmentLimit: number | null;
defaultPlacement: number; defaultPlacement: number;
} }
@ -74,7 +74,7 @@ export class ResponseUser {
fullName: string; fullName: string;
shortName: string; shortName: string;
email: string; email: string;
userAgent?: string; userAgent: string | null;
projectLimit: number; projectLimit: number;
isProfessional: boolean; isProfessional: boolean;
position: string; position: string;