mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
F: add transport
This commit is contained in:
parent
9e97a4a812
commit
6420c959e8
@ -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)
|
||||
|
||||
|
17
cloudflare/fetch/transport.go
Normal file
17
cloudflare/fetch/transport.go
Normal 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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user