@beamimpact/web-sdk
Version:
The Beam SDK enables brands to connect with their customers over shared values, not transactional discounts, to build stronger loyalty. Our integration achieves this by allowing customers to (a) choose a nonprofit where the brand will donate part of their
91 lines (88 loc) • 3.34 kB
TypeScript
import { b as TApiKey, c as TUrl, T as TNumericId } from '../chunks/types-CPxMwnoR.esm.js';
type BeamConfigOptions = {
apiKey: TApiKey;
domain?: TUrl;
chainId: TNumericId;
storeId?: TNumericId;
baseUrl?: TUrl;
logUrl?: TUrl;
plugins?: BeamPlugin[];
};
type BeamPlugin = {
name: string;
init: (config: BeamConfigOptions) => Promise<void>;
};
/**
* BeamConfig is not exported, it should be accessed as a singleton via getConfig & init
* @example
* import { init, getConfig } from '@beamimpact/web-sdk/dist/integrations/beam'
* // In setup script
* const beam = await init({ apiKey: '', chainId: 1, storeId: 1, plugins: [] })
* // In other scripts that need to wait for Beam to be ready
* const beam = getConfig()
* await beam.readyPromise.then(() => doSomething())
* // OR
* if (beam.status !== 'ready') {
* beam.addEventListener('beamstatuschange', event => {
* if (event.detail.status === 'ready') doSomething()
* else if (event.detail.status === 'error') handleBeamError()
* })
* }
*/
declare class BeamConfig extends EventTarget {
#private;
apiKey?: TApiKey;
chainId?: TNumericId;
storeId?: TNumericId;
/** Domain to set Beam cookies on, used if store and checkout are on different subdomains */
domain?: TUrl;
/** Beam server URL to make API requests */
baseUrl?: TUrl;
/** Beam server URL for logging / errors */
logUrl?: TUrl;
/** Alternative to adding event listener for beamstatuschange - resolves when Beam and all plugins are ready */
readyPromise: Promise<boolean>;
/** getConfig().addEventListener("beamstatuschange", ({detail}) => { console.log(detail.status) }) */
status: "pending" | "ready" | "error";
/** Plugins such as Statsig for A/B tests - Beam waits for all plugins to initialize before emitting ready status */
plugins: {
[pluginName: string]: BeamPlugin;
};
/** Sets up Beam in pending state. This is used for initial value of Beam config singleton,
* which allows scripts to access and wait for "ready" promise or event before initialization script runs.
* Use init() to set up Beam for use. */
constructor(options: Partial<BeamConfigOptions>);
/** Used by end users to set up Beam with config options and plugins. */
init(options: BeamConfigOptions): Promise<BeamConfig>;
}
/**
* @example
* import { init, getConfig } from '@beamimpact/web-sdk/dist/integrations/beam'
*
* const beam = await init({
* apiKey: '',
* chainId: 1,
* storeId: 1,
* plugins: [ new StatsigPlugin({ statsigApiKey: '' }) ]
* })
*
* beam.plugins.statsig.logEvent('cart_updated', 100.00)
*/
declare const init: (beamConfigOptions: BeamConfigOptions) => Promise<BeamConfig>;
/**
* @example
* // In scripts that need to wait for Beam to be ready
* const beam = getConfig()
* await beam.readyPromise
* .then(() => doSomething(beam))
* .catch(err => handleBeamError(err))
* // OR
* if (beam.status !== 'ready') {
* beam.addEventListener('beamstatuschange', event => {
* if (event.detail.status === 'ready') doSomething(beam)
* else if (event.detail.status === 'error') handleBeamError(event.detail.error)
* })
* }
*/
declare const getConfig: () => BeamConfig;
export { type BeamConfigOptions, type BeamPlugin, getConfig, init };