fetch

package module
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: MIT Imports: 8 Imported by: 0

README

fetchgo

Minimal Go HTTP library for unified requests in Browser (WASM) and Server.

Features

  • Unified API: Write once, run on Server (net/http) and Browser (syscall/js).
  • TinyGo Compatible: Optimized for small WASM binaries (~45KB gzipped).
  • Encoding Support: Automatic JSON and TinyBin encoding for structs (raw bytes sent as-is).

Installation

go get github.com/tinywasm/fetch

Usage

package main

import (
    "github.com/tinywasm/fetch"
)

func main() {
    fg := fetchgo.New()
    // Client with 5s timeout
    client := fg.NewClient("https://api.example.com", 5000)

    userData := User{ Name: "Alice", Age: 30 }

    // Send JSON request
    client.SendJSON("GET", "/data", userData, func(resp []byte, err error) {
        if err != nil {
            println("Error:", err)
            return
        }
        println("Response:", string(resp))
    })

    // or Send Binary request
    // client.SendBinary("POST", "/upload", userData, ...
}

snapshots

browser

browser

servidor (golite)

server

Documentation

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// SendJSON performs an HTTP request with JSON encoding.
	// url MUST be absolute (e.g., "https://api.example.com/users")
	// body is encoded to JSON, Content-Type: application/json
	// The callback receives the raw response body as []byte.
	// The user is responsible for decoding it using json.Unmarshal.
	SendJSON(method, url string, body any, callback func([]byte, error))

	// SendBinary performs an HTTP request with TinyBin encoding.
	// url MUST be absolute (e.g., "https://api.example.com/users")
	// body is encoded with TinyBin, Content-Type: application/octet-stream
	// The callback receives the raw response body as []byte.
	// The user is responsible for decoding it using gobin.Decode.
	SendBinary(method, url string, body any, callback func([]byte, error))

	// SetHeader sets a default header for all requests from this client.
	SetHeader(key, value string)
}

Client defines the public interface for an HTTP client.

type Fetchgo

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

Fetchgo manages HTTP clients with explicit codec methods.

func New

func New() *Fetchgo

New creates a new Fetchgo instance with sensible defaults.

func (*Fetchgo) NewClient

func (f *Fetchgo) NewClient(baseURL string, timeoutMS int) Client

NewClient creates a configured HTTP client.

func (*Fetchgo) SetCORS

func (f *Fetchgo) SetCORS(mode string, credentials bool) *Fetchgo

SetCORS configures CORS behavior for WASM/browser requests.

Directories

Path Synopsis
example
web command

Jump to

Keyboard shortcuts

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