fetch

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 7 Imported by: 2

README

WASM-FETCH

GoDoc

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

View Source
const (
	CacheDefault      = "default"
	CacheNoStore      = "no-store"
	CacheReload       = "reload"
	CacheNone         = "no-cache"
	CacheForce        = "force-cache"
	CacheOnlyIfCached = "only-if-cached"
)

cache enums

View Source
const (
	CredentialsOmit       = "omit"
	CredentialsSameOrigin = "same-origin"
	CredentialsInclude    = "include"
)

credentials enums

View Source
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.

View Source
const (
	ModeSameOrigin = "same-origin"
	ModeNoCORS     = "no-cors"
	ModeCORS       = "cors"
	ModeNavigate   = "navigate"
)

Mode enums

View Source
const (
	RedirectFollow = "follow"
	RedirectError  = "error"
	RedirectManual = "manual"
)

Redirect enums

View Source
const (
	ReferrerNone   = "no-referrer"
	ReferrerClient = "client"
)

Referrer enums

View Source
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

func CanonicalHeaderKey(s string) string

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 map[string][]string

A Header represents the key-value pairs in an HTTP header.

func (Header) Add

func (h Header) Add(key, value string)

Add adds the key, value pair to the header. It appends to any existing values associated with key.

func (Header) Del

func (h Header) Del(key string)

Del deletes the values associated with key.

func (Header) Get

func (h Header) Get(key string) string

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

func (h Header) Set(key, value string)

Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.

func (Header) Write

func (h Header) Write(w io.Writer) error

Write writes a header in wire format.

func (Header) WriteSubset

func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error

WriteSubset writes a header in wire format. If exclude is not nil, keys where exclude[key] == true are not written.

type Opts

Opts are the options you can pass to the fetch call.

type Response

type Response struct {
	Headers    Header
	OK         bool
	Redirected bool
	Status     int
	StatusText string
	Type       string
	URL        string
	Body       []byte
	BodyUsed   bool
}

Response is the response that return from the fetch promise.

func Fetch

func Fetch(url string, opts *Opts) (*Response, error)

Jump to

Keyboard shortcuts

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