fetchgo

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: MIT Imports: 9 Imported by: 0

README

fetchgo

fetchgo is a minimal Go library designed to simplify HTTP requests across different runtimes. It provides a unified API that works in the browser with TinyGo + WebAssembly using syscall/js, and on the server using Go’s standard net/http package. With fetchgo, you can write cross-platform HTTP logic once and run it anywhere.

Documentation

Index

Constants

View Source
const (
	// RequestJSON indicates that the request body should be encoded as JSON.
	RequestJSON requestType = "json"
	// RequestForm indicates that the request body should be form-urlencoded.
	RequestForm requestType = "form"
	// RequestMultipart indicates that the request body should be multipart/form-data.
	RequestMultipart requestType = "multipart"
	// RequestRaw indicates that the request body should be passed as-is.
	RequestRaw requestType = "raw"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v0.0.3

type Client struct {
	BaseURL string // Base URL for all requests, e.g., "https://api.example.com"

	TimeoutMS   int         // Request timeout in milliseconds.
	RequestType requestType // Default request type (e.g., RequestJSON, RequestRaw).
	// contains filtered or unexported fields
}

Client is a configurable HTTP client that works across WASM and standard Go. To create a client, initialize the struct directly, for example: client := &fetchgo.Client{ BaseURL: "https://api.example.com", RequestType: fetchgo.RequestJSON }

func (*Client) AddHeader added in v0.0.3

func (c *Client) AddHeader(key, value string)

AddHeader adds a header to the client's default headers. It will add the (key, value) pair even if the key already exists, potentially creating duplicate header keys.

func (*Client) SendRequest added in v0.0.3

func (c *Client) SendRequest(method, url string, body any, callback func(any, error))

SendRequest sends an HTTP request and invokes the callback with the result. It delegates the actual request logic to the environment-specific doRequest method. The entire operation, including the callback, runs in a new goroutine.

func (*Client) SetHeader added in v0.0.3

func (c *Client) SetHeader(key, value string)

SetHeader sets a header, ensuring there is at most one entry for the given key. If the header already exists, its value is replaced. If it does not exist, it is appended.

type Encoder added in v0.0.3

type Encoder interface {
	Encode(data any) ([]byte, error)
	Decode(data []byte, v any) error
}

Encoder defines the interface for encoding and decoding data. This allows for pluggable serialization strategies (e.g., JSON, form, raw).

type Fetchgo

type Fetchgo struct{}

func New

func New() *Fetchgo

type JSONEncoder added in v0.0.3

type JSONEncoder struct{}

JSONEncoder implements the Encoder interface for JSON data.

func (JSONEncoder) Decode added in v0.0.3

func (e JSONEncoder) Decode(data []byte, v any) error

Decode unmarshals the given JSON byte slice into the provided variable.

func (JSONEncoder) Encode added in v0.0.3

func (e JSONEncoder) Encode(data any) ([]byte, error)

Encode marshals the given data into a JSON byte slice.

type RawEncoder added in v0.0.3

type RawEncoder struct{}

RawEncoder implements the Encoder interface for raw byte data. It acts as a pass-through for []byte and converts string to []byte.

func (RawEncoder) Decode added in v0.0.3

func (e RawEncoder) Decode(data []byte, v any) error

Decode handles raw data. It expects the destination `v` to be a pointer to a byte slice (*[]byte).

func (RawEncoder) Encode added in v0.0.3

func (e RawEncoder) Encode(data any) ([]byte, error)

Encode handles raw data. It expects data to be either []byte or string.

Jump to

Keyboard shortcuts

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