Skip to content

Commit c04a700

Browse files
committed
feat: Express embedded structs via extends in TypeScript
1 parent 2c46b37 commit c04a700

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

codersdk/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (c *Client) UpdateActiveTemplateVersion(ctx context.Context, template uuid.
7373
// TemplateVersionsByTemplate.
7474
type TemplateVersionsByTemplateRequest struct {
7575
TemplateID uuid.UUID `json:"template_id" validate:"required"`
76-
Pagination `json:"pagination"`
76+
Pagination
7777
}
7878

7979
// TemplateVersionsByTemplate lists versions associated with a template.

codersdk/users.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const (
2323
type UsersRequest struct {
2424
Search string `json:"search"`
2525
// Filter users by status
26-
Status string `json:"status"`
27-
Pagination `json:"pagination"`
26+
Status string `json:"status"`
27+
Pagination
2828
}
2929

3030
// User represents a user in Coder.

scripts/apitypings/main.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,28 @@ func (g *Generator) posLine(obj types.Object) string {
237237
func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, error) {
238238
var s strings.Builder
239239
_, _ = s.WriteString(g.posLine(obj))
240+
_, _ = s.WriteString(fmt.Sprintf("export interface %s ", obj.Name()))
240241

241-
_, _ = s.WriteString(fmt.Sprintf("export interface %s {\n", obj.Name()))
242+
// Handle named embedded structs in the codersdk package via extension.
243+
var extends []string
244+
extendedFields := make(map[int]bool)
245+
for i := 0; i < st.NumFields(); i++ {
246+
field := st.Field(i)
247+
if field.Embedded() && field.Pkg().Name() == "codersdk" {
248+
extendedFields[i] = true
249+
extends = append(extends, field.Name())
250+
}
251+
}
252+
if len(extends) > 0 {
253+
_, _ = s.WriteString(fmt.Sprintf("extends %s ", strings.Join(extends, ", ")))
254+
}
255+
256+
_, _ = s.WriteString("{\n")
242257
// For each field in the struct, we print 1 line of the typescript interface
243258
for i := 0; i < st.NumFields(); i++ {
259+
if extendedFields[i] {
260+
continue
261+
}
244262
field := st.Field(i)
245263
tag := reflect.StructTag(st.Tag(i))
246264

@@ -326,7 +344,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
326344
return TypescriptType{
327345
ValueType: "any",
328346
AboveTypeLine: fmt.Sprintf("%s\n%s",
329-
indentedComment("Embedded struct, please fix by naming it"),
347+
indentedComment("Embedded anonymous struct, please fix by naming it"),
330348
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any"),
331349
),
332350
}, nil

site/src/api/typesGenerated.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,8 @@ export interface TemplateVersionParameterSchema {
258258
}
259259

260260
// From codersdk/templates.go:74:6
261-
export interface TemplateVersionsByTemplateRequest {
261+
export interface TemplateVersionsByTemplateRequest extends Pagination {
262262
readonly template_id: string
263-
readonly pagination: Pagination
264263
}
265264

266265
// From codersdk/templates.go:28:6
@@ -311,10 +310,9 @@ export interface UserRoles {
311310
}
312311

313312
// From codersdk/users.go:23:6
314-
export interface UsersRequest {
313+
export interface UsersRequest extends Pagination {
315314
readonly search: string
316315
readonly status: string
317-
readonly pagination: Pagination
318316
}
319317

320318
// From codersdk/workspaces.go:18:6

0 commit comments

Comments
 (0)