diff --git a/cloudflare/fetch/client.go b/cloudflare/fetch/client.go index 4b9c573..4a36d19 100644 --- a/cloudflare/fetch/client.go +++ b/cloudflare/fetch/client.go @@ -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 +}