UNPKG

@benshi.ai/js-sdk

Version:

Benshi SDK

131 lines (106 loc) 4.27 kB
import DeferredRunner from "./deferredRunner"; import ECommerce from "./ECommerce"; import ELearning from "./ELearning"; import Loyalty from "./Loyalty"; import Navigation from "./Navigation"; import Payments from "./Payments"; import { default as BsLogFallback } from '../modules/BsLog' import { default as ECommerceFallback } from '../modules/ECommerce' import { default as ELearningFallback } from '../modules/ELearning' import { default as LoyaltyFallback } from '../modules/Loyalty' import { default as NavigationFallback } from '../modules/Navigation' import { default as PaymentsFallback } from '../modules/Payments' import { EventEmitter } from 'events'; import { BsLogEvents } from "../core/commonTypes"; import { getFilenameForVersion, getLatestPatch, getListOfVersionsFromManifest } from "../cdn"; import { version } from '../../package.json' function setExternalScript(cdnUrl) { return new Promise((resolve, reject) => { const load = async () => { try { const fetchedManifestResponse = await fetch(`${cdnUrl}/manifest.json`) const manifest = await fetchedManifestResponse.json() const versions = getListOfVersionsFromManifest(manifest) const desiredVersion = getLatestPatch(version, versions) const scriptTag = document.createElement('script'); scriptTag.type = 'text/javascript'; scriptTag.src = `${cdnUrl}${getFilenameForVersion(desiredVersion)}`; scriptTag.onload = () => resolve(undefined); scriptTag.onerror = () => reject() document.body.appendChild(scriptTag); } catch (e) { console.warn('error loading sdk', e) reject() } } load() }); } const moduleName = 'internalBsLog' export default class BsLog extends EventEmitter { private key private options private deferredRunner = new DeferredRunner() private DEFAULT_CDN_URL = 'https://cdn.benshi.ai/' private cdnUrl private bsLog constructor(key, options) { super() this.key = key this.options = options this.cdnUrl = this.options.cdnUrl || this.DEFAULT_CDN_URL this.cdnUrl = this.cdnUrl[this.cdnUrl.length - 1] === '/' ? this.cdnUrl : `${this.cdnUrl}/` delete this.options.cdnUrl ECommerce.init(this.deferredRunner) ELearning.init(this.deferredRunner) Loyalty.init(this.deferredRunner) Navigation.init(this.deferredRunner) Payments.init(this.deferredRunner) if (this.options.skipDynamicLoading) { this.useFallback() } else { setExternalScript(this.cdnUrl) .then(() => { this.useExternalLibrary() }) .catch(e => { console.log('error loading external script: ', e) this.useFallback() console.log(`using fallback SDK@${version}`) }) } } private subscribe() { this.bsLog.on(BsLogEvents.NudgeAction, (...args) => { this.emit(BsLogEvents.NudgeAction, ...args) }) } private useExternalLibrary() { (window as any).internalBsLog = new (window as any).BsLog(this.key, this.options) this.bsLog = (window as any).internalBsLog this.subscribe() this.deferredRunner.activate(window); } private useFallback() { this.bsLog = new BsLogFallback(this.key, this.options) const modules = { Loyalty: LoyaltyFallback, Navigation: NavigationFallback, Payments: PaymentsFallback, ELearning: ELearningFallback, ECommerce: ECommerceFallback, [moduleName]: this.bsLog } this.subscribe() this.deferredRunner.activate(modules); } public identify(...args) { this.deferredRunner.execute(moduleName, 'identify', args) } public setUserId(...args) { this.deferredRunner.execute(moduleName, 'setUserId', args) } public flush(...args) { this.deferredRunner.execute(moduleName, 'flush', args) } }