Files
3x-ui/tools/openapigen/main.go
MHSanaei 7bd281d26d feat(codegen): Go-first tool emitting Zod schemas and TS types
Add tools/openapigen — a single-binary Go program that walks the
exported structs in database/model, web/entity, and xray via go/parser
and emits two committed artifacts under frontend/src/generated:

  - zod.ts   shared Zod schemas keyed off `validate:` tags (ports get
             .min(1).max(65535), Inbound.protocol becomes a z.enum,
             Node.scheme too, etc.)
  - types.ts plain TS interfaces inferred from the same walk, so
             consumers can import Inbound without dragging Zod along

The walker flattens embedded structs (AllSettingView.AllSetting),
honors json:"-" and omitempty, and accepts per-struct overrides so
the JSON-string-inside-JSON columns (Inbound.Settings/StreamSettings/
Sniffing, ClientRecord.Reverse, InboundClientIps.Ips) render as
z.unknown() instead of leaking the DB-storage type into the API
contract. Type aliases like model.Protocol are emitted as TS aliases
and Zod schemas in their own right.

Wires `npm run gen:zod` in frontend/package.json so the generator can
be re-run without leaving the frontend tree. The existing openapi.json
build (gen:api) is left alone for now; migrating the OpenAPI surface
to this generator is a follow-up.

PR2 of the planned Zod end-to-end rollout.
2026-05-25 19:29:44 +02:00

116 lines
2.4 KiB
Go

package main
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
)
func main() {
root := flag.String("root", ".", "repository root containing database/model and web/entity")
outDir := flag.String("out", "frontend/src/generated", "output directory relative to root")
flag.Parse()
if err := run(*root, *outDir); err != nil {
fmt.Fprintln(os.Stderr, "openapigen:", err)
os.Exit(1)
}
}
func run(root, outDir string) error {
requests := []packageRequest{
{
Path: resolveRel(root, "database/model"),
StructAllow: setOf(
"User",
"Inbound",
"FallbackParentInfo",
"OutboundTraffics",
"InboundClientIps",
"ApiToken",
"HistoryOfSeeders",
"Setting",
"Node",
"CustomGeoResource",
"ClientReverse",
"Client",
"ClientRecord",
"ClientInbound",
"InboundFallback",
),
AliasAllow: setOf("Protocol"),
Overrides: map[string][]walkOverride{
"Inbound": {
{Field: "Settings", Kind: KindAny},
{Field: "StreamSettings", Kind: KindAny},
{Field: "Sniffing", Kind: KindAny},
},
"ClientRecord": {
{Field: "Reverse", Kind: KindAny},
},
"InboundClientIps": {
{Field: "Ips", Kind: KindAny},
},
},
},
{
Path: resolveRel(root, "web/entity"),
StructAllow: setOf(
"Msg",
"AllSetting",
"AllSettingView",
),
},
{
Path: resolveRel(root, "xray"),
StructAllow: setOf(
"ClientTraffic",
),
},
}
schemas, aliases, err := walkPackages(requests)
if err != nil {
return err
}
schemas = flattenEmbedded(schemas)
if len(schemas) == 0 {
return fmt.Errorf("no schemas produced; nothing to write")
}
target := filepath.Join(root, outDir)
if err := os.MkdirAll(target, 0o755); err != nil {
return err
}
zodBuf := &bytes.Buffer{}
if err := emitZod(zodBuf, schemas, aliases); err != nil {
return err
}
typesBuf := &bytes.Buffer{}
if err := emitTypes(typesBuf, schemas, aliases); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(target, "zod.ts"), zodBuf.Bytes(), 0o644); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(target, "types.ts"), typesBuf.Bytes(), 0o644); err != nil {
return err
}
fmt.Printf("openapigen: wrote %d schemas to %s\n", len(schemas), target)
return nil
}
func setOf(names ...string) map[string]bool {
m := make(map[string]bool, len(names))
for _, n := range names {
m[n] = true
}
return m
}