F: add transport

This commit is contained in:
aki-0421 2023-04-11 21:33:52 +09:00
parent 9e97a4a812
commit 6420c959e8
No known key found for this signature in database
GPG Key ID: 64A8CF6D437D166A
2 changed files with 27 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package fetch
import (
"net/http"
"syscall/js"
"github.com/syumai/workers/internal/jsutil"
@ -19,6 +20,15 @@ func (c *Client) applyOptions(opts []ClientOption) {
}
}
// HTTPClient returns *http.Client.
func (c *Client) HTTPClient() *http.Client {
return &http.Client{
Transport: &transport{
namespace: c.namespace,
},
}
}
// ClientOption is a type that represents an optional function.
type ClientOption func(*Client)

View File

@ -0,0 +1,17 @@
package fetch
import (
"net/http"
"syscall/js"
)
// transport is an implementation of http.RoundTripper
type transport struct {
// namespace - Objects that Fetch API belongs to. Default is Global
namespace js.Value
}
// RoundTrip replaces http.DefaultTransport.RoundTrip to use cloudflare fetch
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
return fetch(t.namespace, req)
}