forked from openmultiplayer/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
62 lines (51 loc) · 1.53 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"io/fs"
"io/ioutil"
"strings"
"github.com/Southclaws/supervillain"
"github.com/openmultiplayer/web/server/src/api/auth/discord"
"github.com/openmultiplayer/web/server/src/api/auth/github"
"github.com/openmultiplayer/web/server/src/resources/forum"
"github.com/openmultiplayer/web/server/src/resources/server"
"github.com/openmultiplayer/web/server/src/resources/user"
"github.com/openmultiplayer/web/server/src/web"
)
// -
//
// What is this!?
//
// This generates TypeScript types from Go structures. This is useful for
// keeping the frontend's type signatures in line with the backend's structures.
//
// Run this task after modifying any the Prisma schema or any of the response
// payloads used by the API.
//
// If you use the Taskfile task `generate` this will be done automatically.
//
// -
func convert(name, prefix string, objs ...interface{}) {
out := fmt.Sprintf("frontend/src/types/_generated_%s.ts", name)
fmt.Println("Generating schemas for", out)
output := strings.Builder{}
output.WriteString(`import * as z from "zod"
`)
for _, v := range objs {
output.WriteString(supervillain.StructToZodSchemaWithPrefix(prefix, v))
}
ioutil.WriteFile(
out,
[]byte(output.String()),
fs.ModePerm,
)
}
func main() {
convert("Error", "API", web.Error{})
convert("Server", "", server.All{})
convert("User", "", user.User{})
convert("Forum", "", forum.Post{})
convert("GitHub", "", github.Link{}, github.Callback{})
convert("Discord", "", discord.Link{}, discord.Callback{})
fmt.Println("DONE")
}