Documentation
¶
Index ¶
- func NewTokenList(values ...string) *tokenlist
- type FetchOption
- type FetchRequest
- type FetchResponse
- type Func
- type Promise
- func Delete(url string, opts ...FetchOption) *Promise
- func Fetch(url string, opts ...FetchOption) *Promise
- func FromJSPromise(jsPromise Value) *Promise
- func Get(url string, opts ...FetchOption) *Promise
- func NewPromise(tryfn func() (Value, error)) *Promise
- func Post(url string, body any, opts ...FetchOption) *Promise
- func Put(url string, body any, opts ...FetchOption) *Promise
- func (p *Promise) Catch(catchfn func(err error) error) *Promise
- func (p *Promise) Done(donefn func(value Value, err error)) *Promise
- func (p *Promise) Finally(finallyfn func()) *Promise
- func (p *Promise) Run()
- func (p *Promise) Then(thenfn func(value Value) (Value, error)) *Promise
- func (p *Promise) Wait() (Value, error)
- type Proto
- type Timer
- type Value
- func (v Value) Bool() bool
- func (v Value) Call(method string, args ...any) Value
- func (v Value) Get(key string) Value
- func (v Value) Invoke(args ...any) Value
- func (v Value) IsNull() bool
- func (v Value) IsUndefined() bool
- func (v Value) New(args ...any) Value
- func (v Value) Set(key string, value any)
- func (v Value) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewTokenList ¶
func NewTokenList(values ...string) *tokenlist
Types ¶
type FetchOption ¶ added in v0.0.2
type FetchOption func(*FetchRequest)
FetchOption is a function that configures a FetchRequest.
func WithHeader ¶ added in v0.0.2
func WithHeader(key, value string) FetchOption
WithHeader adds a header to the request.
func WithHeaders ¶ added in v0.0.2
func WithHeaders(headers map[string]string) FetchOption
WithHeaders sets multiple headers on the request.
func WithJSON ¶ added in v0.0.2
func WithJSON(body string) FetchOption
WithJSON sets the request body as JSON and adds Content-Type header.
func WithMethod ¶ added in v0.0.2
func WithMethod(method string) FetchOption
WithMethod sets the HTTP method for the request.
func WithQuery ¶ added in v0.0.2
func WithQuery(params url.Values) FetchOption
WithQuery appends the given query parameters to the request URL. Existing parameters in the URL are preserved; new keys are added.
type FetchRequest ¶ added in v0.0.2
type FetchRequest struct {
// contains filtered or unexported fields
}
FetchRequest represents an HTTP request configuration.
type FetchResponse ¶ added in v0.0.2
type FetchResponse struct {
// contains filtered or unexported fields
}
FetchResponse wraps an HTTP response.
func ResponseFrom ¶ added in v0.0.2
func ResponseFrom(v Value) *FetchResponse
ResponseFrom extracts a *FetchResponse from a Value. In native Go, the Value should wrap a *FetchResponse. Returns nil if the Value does not contain a *FetchResponse.
func (*FetchResponse) Blob ¶ added in v0.0.2
func (r *FetchResponse) Blob() *Promise
Blob returns a Promise that resolves to the response body as a Blob. Note: In native Go, this returns the raw body as a []byte wrapped in a Value, since the Blob type is browser-specific and not available here.
func (*FetchResponse) Headers ¶ added in v0.0.2
func (r *FetchResponse) Headers() map[string]string
Headers returns the response headers.
func (*FetchResponse) JSON ¶ added in v0.0.2
func (r *FetchResponse) JSON() *Promise
JSON returns a Promise that resolves to the parsed JSON body. Note: In native Go builds, this method is not implemented and will return an error. Use Text() followed by encoding/json.Unmarshal instead.
func (*FetchResponse) OK ¶ added in v0.0.2
func (r *FetchResponse) OK() bool
OK returns true if the response status is in the 200-299 range.
func (*FetchResponse) Status ¶ added in v0.0.2
func (r *FetchResponse) Status() int
Status returns the HTTP status code.
func (*FetchResponse) StatusText ¶ added in v0.0.2
func (r *FetchResponse) StatusText() string
StatusText returns the HTTP status text.
func (*FetchResponse) Text ¶ added in v0.0.2
func (r *FetchResponse) Text() *Promise
Text returns a Promise that resolves to the response body as text.
type Func ¶ added in v0.0.2
type Func struct{}
Func is a no-op function wrapper used for non-WASM builds.
type Promise ¶ added in v0.0.2
type Promise struct {
// contains filtered or unexported fields
}
Promise represents an asynchronous operation that can be chained with Then, Catch, and Finally handlers, similar to JavaScript Promises. A Promise can only be executed once (via Run, Wait, or Done). Subsequent execution attempts are ignored due to an internal sync.Once guard.
func Delete ¶ added in v0.0.2
func Delete(url string, opts ...FetchOption) *Promise
Delete is a convenience method for Fetch with DELETE method.
func Fetch ¶ added in v0.0.2
func Fetch(url string, opts ...FetchOption) *Promise
Fetch creates a new HTTP request and returns a Promise that resolves to a *FetchResponse.
func FromJSPromise ¶ added in v0.0.2
FromJSPromise wraps an existing JavaScript Promise value. In native Go, this creates a promise that resolves immediately with the value. This exists for API compatibility with the WASM build.
func Get ¶ added in v0.0.2
func Get(url string, opts ...FetchOption) *Promise
Get is a convenience method for Fetch with GET method.
func NewPromise ¶
NewPromise creates a new Promise with the given executor function. The executor is the async operation that produces a value or error.
func Post ¶ added in v0.0.2
func Post(url string, body any, opts ...FetchOption) *Promise
Post is a convenience method for Fetch with POST method.
func Put ¶ added in v0.0.2
func Put(url string, body any, opts ...FetchOption) *Promise
Put is a convenience method for Fetch with PUT method.
func (*Promise) Catch ¶ added in v0.0.2
Catch sets the error handler which is called when the promise rejects or when the Then handler returns an error. The handler can recover by returning nil, or propagate/transform the error. Returns the promise for chaining.
func (*Promise) Done ¶ added in v0.0.2
Done sets a completion callback that receives the final value and error, and immediately starts executing the promise asynchronously. In native Go, Wait() is preferred, but Done() provides API compatibility with WASM. Returns the promise for chaining.
func (*Promise) Finally ¶ added in v0.0.2
Finally sets a handler that is always called after the promise completes, regardless of success or failure. Useful for cleanup operations. Returns the promise for chaining.
func (*Promise) Run ¶ added in v0.0.2
func (p *Promise) Run()
Run executes the promise asynchronously. Handlers are called in order: tryfn -> thenfn (on success) or catchfn (on error) -> finallyfn. Can only be called once per Promise instance.
func (*Promise) Then ¶ added in v0.0.2
Then sets the success handler which is called when the promise resolves successfully. The handler receives the resolved value and can transform it or return an error. Returns the promise for chaining.
func (*Promise) Wait ¶ added in v0.0.2
Wait executes the promise synchronously and blocks until completion. Returns the final value and any error that occurred. This is useful for testing or when you need the result immediately. Can only be called once per Promise instance (subsequent calls return zero values).
type Timer ¶ added in v0.0.2
type Timer struct {
// contains filtered or unexported fields
}
Timer fires a callback once (after a delay) or repeatedly (on an interval). Mirrors the browser's setTimeout/setInterval API.
func SetInterval ¶ added in v0.0.2
SetInterval calls fn immediately and then repeatedly at the given interval until Cancel is called.
func SetTimeout ¶ added in v0.0.2
SetTimeout calls fn once after the given delay.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a wrapper around any value
func ValueOf ¶ added in v0.0.2
ValueOf wraps any Go value in a Value for use in non-WASM builds. Uses ObjectProto as the type since the native build has no JavaScript type system - all wrapped values are treated as generic objects. To retrieve the original Go value, use type assertion:
if str, ok := v.v.(string); ok { ... }
In WASM builds, ValueOf converts Go values to their JavaScript equivalents.
func (Value) IsUndefined ¶ added in v0.0.2
IsUndefined returns true if the value is undefined.