R: add WithBinding in fetch

This commit is contained in:
aki-0421 2023-04-09 09:23:07 +09:00
parent 8b39614bbd
commit ef831af9d7
No known key found for this signature in database
GPG Key ID: 64A8CF6D437D166A

View File

@ -12,9 +12,30 @@ type Client struct {
namespace js.Value
}
// NewClient returns new Client
func NewClient() *Client {
return &Client{
namespace: jsutil.Global,
// applyOptions applies client options.
func (c *Client) applyOptions(opts []ClientOption) {
for _, opt := range opts {
opt(c)
}
}
// 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: jsutil.Global,
}
c.applyOptions(opts)
return c
}