js

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 3

Documentation

Index

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 WithBody added in v0.0.2

func WithBody(body any) FetchOption

WithBody sets the request body.

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.

func NewFunc added in v0.0.2

func NewFunc(fn func(this Value, args []Value) any) Func

NewFunc creates a no-op function wrapper for non-WASM builds.

func (Func) Release added in v0.0.2

func (Func) Release()

Release is a no-op 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

func FromJSPromise(jsPromise Value) *Promise

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

func NewPromise(tryfn func() (Value, error)) *Promise

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

func (p *Promise) Catch(catchfn func(err error) error) *Promise

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

func (p *Promise) Done(donefn func(value Value, err error)) *Promise

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

func (p *Promise) Finally(finallyfn func()) *Promise

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

func (p *Promise) Then(thenfn func(value Value) (Value, error)) *Promise

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

func (p *Promise) Wait() (Value, error)

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 Proto

type Proto uint

Proto is the type representing the type

const (
	UndefinedProto Proto = iota
	NullProto
	ArrayProto
	ObjectProto
	MapProto
	TextProto
	CommentProto
	WindowProto
	DocumentProto
	DocumentTypeProto
	ElementProto
	AttrProto
	NodeProto
	EventProto
	CustomEventProto
)

func TypeOf

func TypeOf(v Value) Proto

Returns the type of the given Value as a Proto.

func (Proto) Equal

func (p Proto) Equal(other Proto) bool

Equal returns true if two Proto values are equal.

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

func SetInterval(interval time.Duration, fn func()) *Timer

SetInterval calls fn immediately and then repeatedly at the given interval until Cancel is called.

func SetTimeout added in v0.0.2

func SetTimeout(delay time.Duration, fn func()) *Timer

SetTimeout calls fn once after the given delay.

func (*Timer) Cancel added in v0.0.2

func (t *Timer) Cancel()

Cancel stops the timer. Safe to call multiple times.

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is a wrapper around any value

func GetProto

func GetProto(path string) Value

GetProto returns js.Undefined() for the non-wasm build.

func Global

func Global() Value

Global returns an undefined global object in the non-wasm build.

func NewArray

func NewArray() Value

NewArray creates a new array with the given length.

func NewMap

func NewMap() Value

NewMap creates a new JavaScript Map.

func NewObject

func NewObject() Value

NewObject creates a new empty object.

func Null

func Null() Value

Null returns the JavaScript null value.

func Undefined

func Undefined() Value

Undefined returns the JavaScript undefined value.

func ValueOf added in v0.0.2

func ValueOf(v any) Value

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) Bool added in v0.0.2

func (v Value) Bool() bool

Bool returns the value as a bool (stub for non-WASM builds).

func (Value) Call added in v0.0.2

func (v Value) Call(method string, args ...any) Value

Call is a stub that returns undefined in non-WASM builds.

func (Value) Get added in v0.0.2

func (v Value) Get(key string) Value

Get is a stub that returns undefined in non-WASM builds.

func (Value) Invoke added in v0.0.2

func (v Value) Invoke(args ...any) Value

Invoke is a stub that returns undefined in non-WASM builds.

func (Value) IsNull added in v0.0.2

func (v Value) IsNull() bool

IsNull returns true if the value is null.

func (Value) IsUndefined added in v0.0.2

func (v Value) IsUndefined() bool

IsUndefined returns true if the value is undefined.

func (Value) New added in v0.0.2

func (v Value) New(args ...any) Value

New is a stub that returns undefined in non-WASM builds.

func (Value) Set added in v0.0.2

func (v Value) Set(key string, value any)

Set is a stub that does nothing in non-WASM builds.

func (Value) String added in v0.0.2

func (v Value) String() string

String returns the value as a string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL