Options
All
  • Public
  • Public/Protected
  • All
Menu

Class UnbluStaticApi

The central entry point that allows to configure an initialize the Unblu Visitor JS API.

The static Unblu API works without actually loading the rest of Unblu. It can do some general checks and load Unblu or connect the API to a loaded version of Unblu. The JS API is an optional add-on to the Unblu visitor site integration.

Depending on how Unblu is integrated into the local website the API has to be initialized differently.

a.) API-only integration If no unblu-snippet is loaded into the page, Unblu can be fully initialized with the API. In this case, both the configure and the initialize methods have to be called. Example:

 const api = await unblu.api
     // configure the unblu server
     .configure({
         apiKey: "<your-api-key>",
         serverUrl: "<unblu-server-url>"
     })
     // initialize the api.
     .initialize();

This implementation will load the Unblu snippet and initialize both Unblu and the JS API.

b.) Snippet and JS API integration If the Unblu snippet is already present in the local website, Unblu doesn't have to be loaded and only the API has to be initialized. Example:

// directly initialize the api without configuring.
const api = await unblu.api.initialize();

Hierarchy

  • UnbluStaticApi

Index

Events

Static ERROR

ERROR: "error" = "error"

Event emitted if the API initialization fails.

It usually makes sense to use this event if there is some general action that has to be triggered when the API initialization fails, but there are several places in the integration code that may trigger the initialization.

In most cases however, it is better to use

unblu.api.initialize().catch(error=> { //handle error here });

or

try{
     let api = await unblu.api.initialize();
}catch(e){
    // handle error here
}
see

on for listener registration

Static READY

READY: "ready" = "ready"

Event emitted as soon as the API is initialized.

It usually makes sense to use this event if there is some general action that has to be triggered when the API is initialized, but there are several places in the integration code that may trigger the initialization.

In most cases however, it is better to use

unblu.api.initialize().then(api => { //use api here });

or

let api = await unblu.api.initialize();
// use api here
see

on for listener registration

Methods

configure

  • Configures the way that Unblu should be initialized.

    The configuration of the Unblu API is needed when, and only when no Unblu snippet is already present in the website.

    Note:

    • Calling this method when there already is an unblu-snippet will result in an UnbluApiError.
    • This method must be called BEFORE initialize. If it is called afterwards an UnbluApiError will be thrown.
    see

    isConfigurationNeeded to check if configuration is needed or not.

    Parameters

    Returns UnbluStaticApi

    an instance of this allowing chaining like unblu.api.configure({...}).initialize();

getApiState

  • Returns the current state of the API

    see

    isInitialized for a simpler check

    Returns ApiState

    the current API state.

initialize

  • Initializes the API and resolves to the fully initialized API.

    If the API has already been initialized or is already in the initializing process, the existing API will be returned. There is only ever one instance of the API which will be returned by any call of this method which makes it safe to call this multiple times.

    The initialization may fail with a UnbluApiError for the following reasons

    Returns Promise<UnbluApi>

isConfigurationNeeded

  • isConfigurationNeeded(): boolean
  • Checks whether the API has to be configured or not.

    • If no snippet is present and the API state is still INITIAL a configuration is necessary.
    • If a snippet is present or the API is already loaded, configuration is not necessary.
    see

    configure to configure the API

    see

    initialize to initialize the API

    Returns boolean

    true if a configuration is needed to initialize the API, false otherwise.

isInitialized

  • isInitialized(): boolean
  • Checks whether the API is initialized or not.

    see

    getApiState for the full state

    Returns boolean

    true if the API is initialized, false for any other state.

off

  • off(event: string, listener: Listener): boolean
  • Removes a previously registered listener.

    Parameters

    • event: string

      The event unregister.

    • listener: Listener

      The listener to be removed.

    Returns boolean

    true if the listener was removed, false otherwise.

on

  • Registers an event listener for the given event.

    Note If the API is already initialized, this listener will be called directly.

    see

    READY

    Parameters

    • event: "ready"

      The ready event

    • listener: ReadyListener

      The listener to be called.

    Returns void

  • Registers an event listener for the given event.

    Note If the API has already failed, this listener will be called directly.

    see

    ERROR

    Parameters

    • event: "error"

      The error event

    • listener: ErrorListener

      The listener to be called.

    Returns void