fetch

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT, MIT Imports: 8 Imported by: 0

README

@mygo-plugins/fetch

A fetch for MyGo apps that makes HTTP requests from Go: not subject to CORS, free to set any header (Origin, Cookie, User-Agent, ...), with the standard options and a standard Response whose body streams as Go reads it.

Use the plugin in Go:

import "github.com/egoist/mygo/plugins/fetch"

mygo.Use(fetch.Plugin) // or fetch.New(fetch.Options{Client: ..., Allow: ...})

and fetch in the frontend:

import { fetch } from "@mygo-plugins/fetch";

const ctrl = new AbortController();
const res = await fetch("https://api.example.com/items", {
  headers: { authorization: `Bearer ${token}` },
  signal: ctrl.signal, // aborting cancels the request in Go
});
const items = await res.json();
  • Aborting the signal, canceling the body, navigating away or closing the window cancels the request in Go.
  • redirect: "manual" returns the redirect response itself, with its Location, instead of an opaque response.
  • mode, credentials, cache and the like do not apply; cookies are those of the Go side's http.Client (give it a Jar to keep them).
  • Every response header is readable but Set-Cookie, which a Response cannot hold.
  • URLs that are not http or https (the app's own pages, data:, blob:) go to the webview's fetch.

The documentation covers streaming, errors and the options of the Go side.

Documentation

Overview

Package fetch is the fetch plugin of MyGo: pages make HTTP requests from Go with the fetch of its JavaScript package, @mygo-plugins/fetch, which takes and returns the standard Request options and Response. Requests made this way are not subject to CORS, may set any header (Origin, Cookie, User-Agent, ...) and read every response header but Set-Cookie, which a Response cannot hold.

mygo.Use(fetch.Plugin)

and in the frontend:

import { fetch } from "@mygo-plugins/fetch";

const res = await fetch("https://api.example.com/items", {
  headers: { authorization: `Bearer ${token}` },
});
const items = await res.json();

The response body streams to the page as Go reads it, and aborting the request's signal cancels it in Go. URLs that are not http or https, such as the app's own pages, go to the webview's fetch.

Index

Constants

This section is empty.

Variables

View Source
var Plugin = New(Options{})

Plugin is the plugin with the default options.

Functions

func New

func New(opts Options) mygo.Plugin

New returns the plugin with options.

Types

type Options

type Options struct {
	// Client makes the requests; nil means http.DefaultClient. Its
	// CheckRedirect limits the redirects followed when the request's
	// redirect mode is "follow".
	Client *http.Client
	// Allow, when not nil, decides whether pages may make a request; a
	// request it refuses rejects with an error.
	Allow func(r *http.Request) bool
}

Options configure the plugin.

Jump to

Keyboard shortcuts

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