-
Notifications
You must be signed in to change notification settings - Fork 37
/
client.go
50 lines (42 loc) · 1010 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package fetch
import (
"net/http"
"syscall/js"
)
// Client is an HTTP client.
type Client struct {
// namespace - Objects that Fetch API belongs to. Default is Global
namespace js.Value
}
// applyOptions applies client options.
func (c *Client) applyOptions(opts []ClientOption) {
for _, opt := range opts {
opt(c)
}
}
// HTTPClient returns *http.Client.
func (c *Client) HTTPClient(redirect RedirectMode) *http.Client {
return &http.Client{
Transport: &transport{
namespace: c.namespace,
redirect: redirect,
},
}
}
// ClientOption is a type that represents an optional function.
type ClientOption func(*Client)
// WithBinding changes the objects that Fetch API belongs to.
// This is useful for service bindings, mTLS, etc.
func WithBinding(bind js.Value) ClientOption {
return func(c *Client) {
c.namespace = bind
}
}
// NewClient returns new Client
func NewClient(opts ...ClientOption) *Client {
c := &Client{
namespace: js.Global(),
}
c.applyOptions(opts)
return c
}