From 6420c959e828c918923d4d7da0a0b242756fe4a9 Mon Sep 17 00:00:00 2001 From: aki-0421 <118268728+aki-0421@users.noreply.github.com> Date: Tue, 11 Apr 2023 21:33:52 +0900 Subject: [PATCH] F: add transport --- cloudflare/fetch/client.go | 10 ++++++++++ cloudflare/fetch/transport.go | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 cloudflare/fetch/transport.go diff --git a/cloudflare/fetch/client.go b/cloudflare/fetch/client.go index 4a36d19..c460a80 100644 --- a/cloudflare/fetch/client.go +++ b/cloudflare/fetch/client.go @@ -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) diff --git a/cloudflare/fetch/transport.go b/cloudflare/fetch/transport.go new file mode 100644 index 0000000..142b2e3 --- /dev/null +++ b/cloudflare/fetch/transport.go @@ -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) +}