Skip to content

Commit 9f58aa6

Browse files
gwaxsoedirgo
andauthored
feat: add constant arrays of enum values for typescript (#901)
* Add constant arrays of enum values for typescript * chore: reorder generated typescript exports --------- Co-authored-by: Bobbie Soedirgo <[email protected]>
1 parent 8be8982 commit 9f58aa6

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/server/templates/typescript.ts

+20
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,26 @@ export type CompositeTypes<
495495
: PublicCompositeTypeNameOrOptions extends keyof DefaultSchema["CompositeTypes"]
496496
? DefaultSchema["CompositeTypes"][PublicCompositeTypeNameOrOptions]
497497
: never
498+
499+
export const Constants = {
500+
${schemas
501+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
502+
.map((schema) => {
503+
const schemaEnums = types
504+
.filter((type) => type.schema === schema.name && type.enums.length > 0)
505+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
506+
return `${JSON.stringify(schema.name)}: {
507+
Enums: {
508+
${schemaEnums.map(
509+
(enum_) =>
510+
`${JSON.stringify(enum_.name)}: [${enum_.enums
511+
.map((variant) => JSON.stringify(variant))
512+
.join(', ')}]`
513+
)}
514+
}
515+
}`
516+
})}
517+
} as const
498518
`
499519

500520
output = await prettier.format(output, {

test/server/typegen.ts

+27
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,15 @@ test('typegen: typescript', async () => {
535535
: PublicCompositeTypeNameOrOptions extends keyof DefaultSchema["CompositeTypes"]
536536
? DefaultSchema["CompositeTypes"][PublicCompositeTypeNameOrOptions]
537537
: never
538+
539+
export const Constants = {
540+
public: {
541+
Enums: {
542+
meme_status: ["new", "old", "retired"],
543+
user_status: ["ACTIVE", "INACTIVE"],
544+
},
545+
},
546+
} as const
538547
"
539548
`)
540549
})
@@ -1090,6 +1099,15 @@ test('typegen w/ one-to-one relationships', async () => {
10901099
: PublicCompositeTypeNameOrOptions extends keyof DefaultSchema["CompositeTypes"]
10911100
? DefaultSchema["CompositeTypes"][PublicCompositeTypeNameOrOptions]
10921101
: never
1102+
1103+
export const Constants = {
1104+
public: {
1105+
Enums: {
1106+
meme_status: ["new", "old", "retired"],
1107+
user_status: ["ACTIVE", "INACTIVE"],
1108+
},
1109+
},
1110+
} as const
10931111
"
10941112
`)
10951113
})
@@ -1645,6 +1663,15 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
16451663
: PublicCompositeTypeNameOrOptions extends keyof DefaultSchema["CompositeTypes"]
16461664
? DefaultSchema["CompositeTypes"][PublicCompositeTypeNameOrOptions]
16471665
: never
1666+
1667+
export const Constants = {
1668+
public: {
1669+
Enums: {
1670+
meme_status: ["new", "old", "retired"],
1671+
user_status: ["ACTIVE", "INACTIVE"],
1672+
},
1673+
},
1674+
} as const
16481675
"
16491676
`)
16501677
})

0 commit comments

Comments
 (0)