README
¶
WASM-FETCH
A go-wasm library that wraps the Fetch API
Install
go get github.com/mlctrez/wasm-fetch
Motivation
Importing net/http adds ~4 MBs to your wasm binary. If that's an issue for you, you can use this library to make fetch calls.
Fork
Forked from https://github.com/marwan-at-work/wasm-fetch to allow use in https://github.com/maxence-charriere/go-app. This fork provides implementations for wasm and a stub for !wasm that allows importing and usage into multi-architecture source.
Example
package main
import (
"context"
"time"
"github.com/mlctrez/wasm-fetch"
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := fetch.Fetch("/some/api/call", &fetch.Opts{
Body: strings.NewReader(`{"one": "two"}`),
Method: fetch.MethodPost,
Signal: ctx,
})
// use response...
Status
GO-WASM is currently experimental and therefore this package is experimental as well, things can break unexpectedly.
Documentation
¶
Overview ¶
Package fetch is a Web Assembly fetch wrapper that avoids importing net/http.
package main import ( "context" "time" "github.com/mlctrez/wasm-fetch" ) ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() resp, err := fetch.Fetch("/some/api/call", &fetch.Opts{ Body: strings.NewReader(`{"one": "two"}`), Method: fetch.MethodPost, Signal: ctx, })
Index ¶
Constants ¶
const ( CacheDefault = "default" CacheNoStore = "no-store" CacheReload = "reload" CacheNone = "no-cache" CacheForce = "force-cache" CacheOnlyIfCached = "only-if-cached" )
cache enums
const ( CredentialsOmit = "omit" CredentialsSameOrigin = "same-origin" CredentialsInclude = "include" )
credentials enums
const ( MethodGet = "GET" MethodHead = "HEAD" MethodPost = "POST" MethodPut = "PUT" MethodPatch = "PATCH" // RFC 5789 MethodDelete = "DELETE" MethodConnect = "CONNECT" MethodOptions = "OPTIONS" MethodTrace = "TRACE" )
Common HTTP methods.
Unless otherwise noted, these are defined in RFC 7231 section 4.3.
const ( ModeSameOrigin = "same-origin" ModeNoCORS = "no-cors" ModeCORS = "cors" )
Mode enums
const ( RedirectFollow = "follow" RedirectError = "error" RedirectManual = "manual" )
Redirect enums
const ( ReferrerNone = "no-referrer" ReferrerClient = "client" )
Referrer enums
const ( ReferrerPolicyNone = "no-referrer" ReferrerPolicyNoDowngrade = "no-referrer-when-downgrade" ReferrerPolicyOrigin = "origin" ReferrerPolicyCrossOrigin = "origin-when-cross-origin" ReferrerPolicyUnsafeURL = "unsafe-url" )
ReferrerPolicy enums
Variables ¶
This section is empty.
Functions ¶
func CanonicalHeaderKey ¶
CanonicalHeaderKey returns the canonical format of the header key s. The canonicalization converts the first letter and any letter following a hyphen to upper case; the rest are converted to lowercase. For example, the canonical key for "accept-encoding" is "Accept-Encoding". If s contains a space or invalid header field bytes, it is returned without modifications.
Types ¶
type Header ¶
A Header represents the key-value pairs in an HTTP header.
func (Header) Add ¶
Add adds the key, value pair to the header. It appends to any existing values associated with key.
func (Header) Get ¶
Get gets the first value associated with the given key. It is case insensitive; textproto.CanonicalMIMEHeaderKey is used to canonicalize the provided key. If there are no values associated with the key, Get returns "". To access multiple values of a key, or to use non-canonical keys, access the map directly.
func (Header) Set ¶
Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.
type Opts ¶
type Opts struct { // Method is the http verb (constants are copied from net/http to avoid import) Method string // Headers is a map of http headers to send. Headers map[string]string // Body is the body request Body io.Reader // Mode docs https://developer.mozilla.org/en-US/docs/Web/API/Request/mode Mode string // Credentials docs https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials Credentials string // Cache docs https://developer.mozilla.org/en-US/docs/Web/API/Request/cache Cache string // Redirect docs https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch Redirect string // Referrer docs https://developer.mozilla.org/en-US/docs/Web/API/Request/referrer Referrer string // ReferrerPolicy docs https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch ReferrerPolicy string // Integrity docs https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity Integrity string // KeepAlive docs https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch KeepAlive *bool // Signal docs https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal Signal context.Context }
Opts are the options you can pass to the fetch call.