Reference
Classes
BoundBucketClient
A client bound with a specific user, company, and other context.
Constructors
new BoundBucketClient()
Parameters
client
__namedParameters
Returns
Accessors
company
Get Signature
Gets the company associated with the client.
Returns
| undefined
| { [k: string]
: any
; id
: undefined
| string
| number
; name
: string
; }
The company or undefined
if it is not set.
otherContext
Get Signature
Gets the "other" context associated with the client.
Returns
undefined
| Record
<string
, any
>
The "other" context or undefined
if it is not set.
user
Get Signature
Gets the user associated with the client.
Returns
| undefined
| { [k: string]
: any
; email
: string
; id
: undefined
| string
| number
; name
: string
; }
The user or undefined
if it is not set.
Methods
bindClient()
Create a new client bound with the additional context. Note: This performs a shallow merge for user/company/other individually.
Parameters
__namedParameters
Returns
new client bound with the additional context
flush()
Flushes the batch buffer.
Returns
Promise
<void
>
getFeature()
Get a specific feature for the user/company/other context bound to this client. Using the isEnabled
property sends a check
event to Bucket.
Parameters
key
string
Returns
Features for the given user/company and whether each one is enabled or not
getFeatureRemote()
Get remotely evaluated feature for the user/company/other context bound to this client.
Parameters
key
string
Returns
Promise
<Feature
>
Feature for the given user/company and key and whether it's enabled or not
getFeatures()
Get features for the user/company/other context bound to this client. Meant for use in serialization of features for transferring to the client-side/browser.
Returns
Record
<string
, Feature
>
Features for the given user/company and whether each one is enabled or not
getFeaturesRemote()
Get remotely evaluated feature for the user/company/other context bound to this client.
Returns
Promise
<Record
<string
, Feature
>>
Features for the given user/company and whether each one is enabled or not
track()
Track an event in Bucket.
Parameters
event
string
The event to track.
opts
?
‐
Returns
Promise
<void
>
Throws
An error if the event is invalid or the options are invalid.
BucketClient
The SDK client.
Constructors
new BucketClient()
Creates a new SDK client. See README for configuration options.
Parameters
options
The options for the client or an existing client to clone.
Returns
Throws
An error if the options are invalid.
Accessors
featureOverrides
Set Signature
Parameters
overrides
Returns
void
logger
Get Signature
Gets the logger associated with the client.
Returns
undefined
| Logger
The logger or undefined
if it is not set.
Methods
bindClient()
Returns a new BoundBucketClient with the user/company/otherContext set to be used in subsequent calls. For example, for evaluating feature targeting or tracking events.
Parameters
__namedParameters
Returns
A new client bound with the arguments given.
Throws
An error if the user/company is given but their ID is not a string.
Remarks
The updateUser
/ updateCompany
methods will automatically be called when the user/company is set respectively.
flush()
Flushes the batch buffer.
Returns
Promise
<void
>
Remarks
It is recommended to call this method when the application is shutting down to ensure all events are sent before the process exits.
getFeature()
Gets the evaluated feature for the current context which includes the user, company, and custom context. Using the isEnabled
property sends a check
event to Bucket.
Parameters
__namedParameters
key
string
Returns
The evaluated features.
Remarks
Call initialize
before calling this method to ensure the feature definitions are cached, no features will be returned otherwise.
getFeatureRemote()
Gets evaluated feature with the usage of remote context. This method triggers a network request every time it's called.
Parameters
key
string
userId
?
companyId
?
additionalContext
?
Returns
Promise
<Feature
>
evaluated feature
getFeatures()
Gets the evaluated feature for the current context which includes the user, company, and custom context.
Parameters
__namedParameters
Returns
Record
<string
, Feature
>
The evaluated features.
Remarks
Call initialize
before calling this method to ensure the feature definitions are cached, no features will be returned otherwise.
getFeaturesRemote()
Gets evaluated features with the usage of remote context. This method triggers a network request every time it's called.
Parameters
userId
?
companyId
?
additionalContext
?
Returns
Promise
<Record
<string
, Feature
>>
evaluated features
initialize()
Initializes the client by caching the features definitions.
Returns
Promise
<void
>
void
Remarks
Call this method before calling getFeatures
to ensure the feature definitions are cached. The client will ignore subsequent calls to this method.
track()
Tracks an event in Bucket.
Parameters
userId
The userId of the user who performed the event
event
string
The event to track.
opts
?
The options.
Returns
Promise
<void
>
Throws
An error if the user is not set or the event is invalid or the options are invalid.
Remarks
If the company is set, the event will be associated with the company.
updateCompany()
Updates the associated company in Bucket.
Parameters
companyId
opts
?
Returns
Promise
<void
>
Throws
An error if the company is not set or the options are invalid.
Remarks
The company must be set using withCompany
before calling this method. If the user is set, the company will be associated with the user.
updateUser()
Updates the associated user in Bucket.
Parameters
userId
opts
?
Returns
Promise
<void
>
Throws
An error if the company is not set or the options are invalid.
Remarks
The company must be set using withCompany
before calling this method. If the user is set, the company will be associated with the user.
Interfaces
ContextWithTracking
A context with tracking option.
Extends
Properties
company?
{ [k: string]
: any
; id
: undefined
| string
| number
; name
: string
; }
The company context. If no id
key is set, the whole object is ignored.
company.id
undefined
| string
| number
‐
company.name?
string
‐
enableTracking?
boolean
Enable tracking for the context. If set to false
, tracking will be disabled for the context. Default is true
.
other?
Record
<string
, any
>
The other context. This is used for any additional context that is not related to user or company.
user?
{ [k: string]
: any
; email
: string
; id
: undefined
| string
| number
; name
: string
; }
The user context. If no id
key is set, the whole object is ignored.
user.email?
string
‐
user.id
undefined
| string
| number
‐
user.name?
string
‐
Feature
Describes a feature
Properties
isEnabled
boolean
If the feature is enabled.
key
string
The key of the feature.
Methods
track()
Track feature usage in Bucket.
Returns
Promise
<void
>
Features
Describes a collection of evaluated features.
Remarks
You should extend the Features interface to define the available features.
HttpClient
Defines the interface for an HTTP client.
Remarks
This interface is used to abstract the HTTP client implementation from the SDK. Define your own implementation of this interface to use a different HTTP client.
Methods
get()
Sends a GET request to the specified URL.
Type Parameters
TResponse
Parameters
url
string
The URL to send the request to.
headers
Record
<string
, string
>
The headers to include in the request.
Returns
Promise
<HttpClientResponse
<TResponse
>>
The response from the server.
post()
Sends a POST request to the specified URL.
Type Parameters
TBody
TResponse
Parameters
url
string
The URL to send the request to.
headers
Record
<string
, string
>
The headers to include in the request.
body
TBody
The body of the request.
Returns
Promise
<HttpClientResponse
<TResponse
>>
The response from the server.
Logger
Logger interface for logging messages
Properties
debug
(message
: string
, data
?: any
) => void
Log a debug messages
error
(message
: string
, data
?: any
) => void
Log an error messages
info
(message
: string
, data
?: any
) => void
Log an info messages
warn
(message
: string
, data
?: any
) => void
Log a warning messages
RawFeature
Describes a feature
Properties
isEnabled
boolean
If the feature is enabled.
key
string
The key of the feature.
missingContextFields?
string
[]
The missing fields in the evaluation context (optional).
targetingVersion?
number
The version of the targeting used to evaluate if the feature is enabled (optional).
Type Aliases
Attributes
Describes the attributes of a user, company or event.
BatchBufferOptions<T>
Options for configuring the BatchBuffer.
Type Parameters
T
The type of items in the buffer.
Type declaration
flushHandler
(items
: T
[]) => Promise
<void
>
A function that handles flushing the items in the buffer.
intervalMs
?
number
The interval in milliseconds at which the buffer is flushed.
logger
?
The logger to use for logging (optional).
maxSize
?
number
The maximum size of the buffer before it is flushed.
ClientOptions
Defines the options for the SDK client.
Type declaration
apiBaseUrl
?
string
The host to send requests to (optional).
batchOptions
?
The options for the batch buffer (optional). If not provided, the default options are used.
configFile
?
string
The path to the config file. If supplied, the config file will be loaded. Defaults to bucket.json
when NODE_ENV is not production. Can also be set through the environment variable BUCKET_CONFIG_FILE.
fallbackFeatures
?
The features to "enable" as fallbacks when the API is unavailable (optional).
featureOverrides
?
If a filename is specified, feature targeting results be overridden with the values from this file. The file should be a JSON object with feature keys as keys and boolean values as values.
If a function is specified, the function will be called with the context and should return a record of feature keys and boolean values.
Defaults to "bucketFeatures.json".
host
?
string
Deprecated
Use apiBaseUrl
instead.
httpClient
?
The HTTP client to use for sending requests (optional). Default is the built-in fetch client.
logger
?
The logger to use for logging (optional). Default is info level logging to console.
logLevel
?
Use the console logger, but set a log level. Ineffective if a custom logger is provided.
offline
?
boolean
In offline mode, no data is sent or fetched from the the Bucket API. This is useful for testing or development.
secretKey
?
string
The secret key used to authenticate with the Bucket API.
Context
Describes the current user context, company context, and other context. This is used to determine if feature targeting matches and to track events.
Type declaration
company
?
{ [k: string]
: any
; id
: string
| number
| undefined
; name
: string
; }
The company context. If no id
key is set, the whole object is ignored.
company.id
string
| number
| undefined
‐
company.name
?
string
‐
other
?
Record
<string
, any
>
The other context. This is used for any additional context that is not related to user or company.
user
?
{ [k: string]
: any
; email
: string
; id
: string
| number
| undefined
; name
: string
; }
The user context. If no id
key is set, the whole object is ignored.
user.email
?
string
‐
user.id
string
| number
| undefined
‐
user.name
?
string
‐
FeatureOverrides
Describes the feature overrides.
FeatureOverridesFn()
Parameters
context
Returns
HttpClientResponse<TResponse>
Describes the response of a HTTP client.
Type Parameters
TResponse
The type of the response body.
Type declaration
body
TResponse
| undefined
The body of the response if available.
ok
boolean
Indicates that the request succeeded.
status
number
The status code of the response.
IdType
LogLevel
TrackingMeta
Describes the meta context associated with tracking.
Type declaration
active
?
boolean
Whether the user or company is active.
TrackOptions
Defines the options for tracking of entities.
Type declaration
attributes
?
The attributes associated with the event.
meta
?
The meta context associated with the event.
TypedFeatures
Describes a collection of evaluated feature.
Remarks
This types falls back to a generic Record<string, Feature> if the Features interface has not been extended.
Variables
LOG_LEVELS
Last updated