{"id":13413798,"url":"https://github.com/csweichel/bel","last_synced_at":"2025-08-02T18:07:03.927Z","repository":{"id":57496959,"uuid":"171746931","full_name":"csweichel/bel","owner":"csweichel","description":"Generate TypeScript interfaces from Go structs/interfaces - useful for JSON RPC","archived":false,"fork":false,"pushed_at":"2024-08-01T10:07:06.000Z","size":89,"stargazers_count":42,"open_issues_count":4,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-12T05:43:40.383Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/csweichel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-02-20T20:48:24.000Z","updated_at":"2024-11-17T05:12:21.000Z","dependencies_parsed_at":"2023-07-13T19:00:21.580Z","dependency_job_id":null,"html_url":"https://github.com/csweichel/bel","commit_stats":null,"previous_names":["32leaves/bel"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csweichel%2Fbel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csweichel%2Fbel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csweichel%2Fbel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csweichel%2Fbel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/csweichel","download_url":"https://codeload.github.com/csweichel/bel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230136114,"owners_count":18178812,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:49.584Z","updated_at":"2024-12-19T23:13:28.686Z","avatar_url":"https://github.com/csweichel.png","language":"Go","funding_links":[],"categories":["Serialization","Relational Databases"],"sub_categories":["HTTP Clients"],"readme":"# [bel](https://en.wikipedia.org/wiki/Bel_(mythology))\nGenerate TypeScript interfaces from Go structs/interfaces - useful for JSON RPC\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/32leaves/bel)](https://goreportcard.com/report/github.com/32leaves/bel)\n[![GoDoc](https://godoc.org/github.com/32leaves/bel?status.svg)](https://godoc.org/github.com/32leaves/bel)\n[![gocover.run](https://gocover.run/github.com/32leaves/bel.svg?style=flat\u0026tag=1.10)](https://gocover.run?tag=1.10\u0026repo=github.com%2F32leaves%2Fbel)\n[![Stability: Active](https://masterminds.github.io/stability/active.svg)](https://masterminds.github.io/stability/active.html)\n\n[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io#github.com/32leaves/bel)\n\n`bel` is used in production in https://gitpod.io.\n\n## Getting started\n`bel` is easy to use. There are two steps involved: extract the Typescript information, and generate the Typescript code.\n```Go\npackage main\n\nimport (\n    \"github.com/32leaves/bel\"\n)\n\ntype Demo struct {\n    Foo string `json:\"foo,omitempty\"`\n    Bar uint32\n    Baz struct {\n        FirstField  bool\n        SecondField *string\n    }\n}\n\nfunc main() {\n    ts, err := bel.Extract(Demo{})\n    if err != nil {\n        panic(err)\n    }\n\n    err = bel.Render(ts)\n    if err != nil {\n        panic(err)\n    }\n}\n```\n\nproduces something akin to (sans formatting):\n\n```TypeScript\nexport interface Demo {\n    foo?: string\n    Bar: number\n    Baz: {\n        FirstField: boolean\n        SecondField: string\n    }\n}\n```\n\n### Converting interfaces\nYou can also convert Golang interfaces to TypeScript interfaces. This is particularly handy for JSON RPC:\n```Go\npackage main\n\nimport (\n    \"os\"\n    \"github.com/32leaves/bel\"\n)\n\ntype DemoService interface {\n    SayHello(name, msg string) (string, error)\n}\n\nfunc main() {\n    ts, err := bel.Extract((*DemoService)(nil))\n    if err != nil {\n        panic(err)\n    }\n\n    err = bel.Render(ts)\n    if err != nil {\n        panic(err)\n    }\n}\n```\n\nproduces something akin to (sans formatting):\n\n```TypeScript\nexport interface DemoService {\n    SayHello(arg0: string, arg1: string): string\n}\n```\n\n## Advanced Usage\nYou can try all the examples mentioned below in [Gitpod](https://gitpod.io#github.com/32leaves/bel).\n\n### FollowStructs\nFollow structs enable the transitive generation of types. See [examples/embed-structs.go](examples/follow-structs.go).\n\nwould produce code for the interface `UserService`, as well as the struct it refers to `AddUserRequest`, and `User` because it's referenced by `AddUserRequest`.\nWithout `FollowStructs` we'd simply refer to the types by name, but would not generate code for them.\n\n### SortAlphabetically\nSortAlphabetically sorts all types and their members by alphabetical order.\nThis produces more deterministic/stable output, which is great for making things comparable across pull requests.\nSee [examples/sort-alphabetically.go](examples/sort-alphabetically.go).\n\n### EmbedStructs\nEmbed structs is similar to `FollowStructs` except that it produces a single canonical type for each structure.\nWhenever one struct references another, that reference is resolved and the definition of the other is embedded.\nSee [examples/embed-structs.go](examples/embed-structs.go).\n\n### NameAnonStructs\n`NameAnonStructs` is kind of the opposite of `EmbedStructs`. When we encounter a nested anonymous struct, we make give this previously anonymous structure a name and refer to it using this name.\nSee [examples/name-anon-structs.go](examples/name-anon-structs.go).\n\n### CustomNamer\n`CustomNamer` enables full control over the TypeScript type names. This is handy to enforce a custom coding guideline, or to add a prefix/suffix to the generated type names.\nSee [examples/custom-namer.go](examples/custom-namer.go).\n\n### Enums\nSee [examples/enums.go](examples/enums.go).\n\nGo famously does not have enums, but rather type aliases and consts. Using reflection alone there is no way to obtain a comprehensive list of type values, as the linker might optimize and remove some.\n_bel_ supports the extraction of enums by parsing the Go source code. Note that this is merely a heuristic and may fail in your case. If it does not work, _bel_ falls back to the underlying type.\n\nEnums can be generated as TypeScript `enum` or as sum types. Use the `bel.GenerateEnumsAsSumTypes` flag to change this behaviour.\n\n### Code Generation\nSee [examples/code-generation.go](examples/code-generation.go).\n\nWhen generating the TypeScript code you might want to wrap everything in a namespace. To that end `bel.GenerateNamespace(\"myNamespaceName\")` can be used.\n\nBy default _bel_ adds a comment to the files it generates. You can influcence this comment (and any other code that comes before the generated code)\nusing `bel.GeneratePreamble` and `bel.GenerateAdditionalPreamble`.\n\nYou can configure the `io.Writer` that _bel_ uses using `bel.GenerateOutputTo`.\n\n# Contributing\nAll contributions/PR/issue/beer are welcome ❤️.\n\nIt's easiest to work with _bel_ using Gitpod: [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io#github.com/32leaves/bel)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsweichel%2Fbel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcsweichel%2Fbel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsweichel%2Fbel/lists"}