Documentation
¶
Overview ¶
Package generator creates TypeScript client code for Go WASM functions.
Two modes are supported:
- Sync mode: class that calls globalThis functions directly
- Worker mode: class that communicates via Web Worker
Go functions returning (T, error) automatically throw in TypeScript using the __error field convention defined by ErrorFieldName.
Index ¶
- Constants
- func DeriveClassName(dirName string) string
- func Generate(parsed *parser.ParsedFile, outputFile, className string) string
- func GenerateClient(parsed *parser.ParsedFile, outputFile, className string) string
- func GenerateGoBindings(parsed *parser.ParsedFile, workerMode bool) string
- func GenerateWorker(wasmPath string) string
- func GenerateWorkerClassMethod(fn parser.GoFunction) string
- func LowerFirst(s string) string
- func ToKebabCase(s string) string
Constants ¶
const ErrorFieldName = "__error"
ErrorFieldName is the JSON field used to pass Go errors through the WASM boundary. When a Go function returns (T, error), the generated binding returns map[string]interface{}{ErrorFieldName: err.Error()} on error. The TypeScript client checks for this field and throws it as a JavaScript Error.
Variables ¶
This section is empty.
Functions ¶
func DeriveClassName ¶ added in v1.1.0
DeriveClassName generates a TypeScript class name from a directory name. It prepends "Go" and converts to TitleCase, unless the directory name is "go" or starts with "go-" or "go_" (to avoid "GoGoWasm" from "go-wasm").
func Generate ¶
func Generate(parsed *parser.ParsedFile, outputFile, className string) string
Generate creates TypeScript class-based client for sync mode. This generates a class that wraps globalThis function calls.
func GenerateClient ¶
func GenerateClient(parsed *parser.ParsedFile, outputFile, className string) string
GenerateClient creates client.ts with a class-based API for worker mode.
func GenerateGoBindings ¶
func GenerateGoBindings(parsed *parser.ParsedFile, workerMode bool) string
GenerateGoBindings generates Go wrapper code for WASM export. workerMode determines whether callbacks use postMessage-based invocation (true) or direct JS function invocation (false).
func GenerateWorker ¶
GenerateWorker creates worker.js content that runs Go WASM in a Web Worker. The wasmPath parameter specifies the path to the WASM file (e.g., "module.wasm").
func GenerateWorkerClassMethod ¶
func GenerateWorkerClassMethod(fn parser.GoFunction) string
GenerateWorkerClassMethod creates a single async instance method for worker mode.
func LowerFirst ¶ added in v1.1.0
LowerFirst converts first letter to lowercase. Used for converting Go function names to JavaScript conventions.
func ToKebabCase ¶ added in v1.1.0
ToKebabCase converts "GoMain" to "go-main". Used for generating TypeScript filenames from class names.
Types ¶
This section is empty.