UNPKG

@kontent-ai/smart-link

Version:

Kontent.ai Smart Link SDK allowing to automatically inject [smart links](https://docs.kontent.ai/tutorials/develop-apps/build-strong-foundation/set-up-editing-from-preview#a-using-smart-links) to Kontent.ai according to manually specified [HTML data attri

62 lines 2.35 kB
import KontentSmartLinkSDK from './sdk'; import { InvalidEnvironmentError, NotInitializedError } from './utils/errors'; /** * KontentSmartLink is the main entry point for the Kontent Smart Link SDK. * It provides a singleton wrapper around the SDK instance to manage initialization, * configuration updates, and cleanup while maintaining a consistent API for users. */ class KontentSmartLink { // This wrapper is publicly exposed instead of the SDK to avoid exposing the SDK to the user // so they can't accidentally create multiple instances of the SDK as it might duplicate events or custom elements initialization. static instance; sdk = null; static initializeOnLoad(configuration) { if (typeof window === 'undefined') { throw InvalidEnvironmentError('KontentSmartLink can only be initialized in a browser environment.'); } return new Promise((resolve) => { window.addEventListener('load', () => { resolve(KontentSmartLink.initialize(configuration)); }); }); } static initialize(configuration) { if (!KontentSmartLink.instance) { KontentSmartLink.instance = new KontentSmartLink(); } if (!KontentSmartLink.instance.sdk) { KontentSmartLink.instance.sdk = new KontentSmartLinkSDK(configuration); } return KontentSmartLink.instance; } destroy = () => { this.sdk?.destroy(); this.sdk = null; }; setConfiguration = (configuration) => { if (!this.sdk) { throw NotInitializedError('KontentSmartLink is not initialized or has already been destroyed.'); } else { this.sdk.updateConfiguration(configuration); } }; on = (event, handler) => { if (!this.sdk) { throw NotInitializedError('KontentSmartLink is not initialized or has already been destroyed.'); } else { this.sdk.on(event, handler); } }; off = (event, handler) => { if (!this.sdk) { throw NotInitializedError('KontentSmartLink is not initialized or has already been destroyed.'); } else { this.sdk.off(event, handler); } }; } export default KontentSmartLink; //# sourceMappingURL=smartlink.js.map