@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
64 lines • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const sdk_1 = require("./sdk");
const errors_1 = require("./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 (0, errors_1.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 sdk_1.default(configuration);
}
return KontentSmartLink.instance;
}
destroy = () => {
this.sdk?.destroy();
this.sdk = null;
};
setConfiguration = (configuration) => {
if (!this.sdk) {
throw (0, errors_1.NotInitializedError)('KontentSmartLink is not initialized or has already been destroyed.');
}
else {
this.sdk.updateConfiguration(configuration);
}
};
on = (event, handler) => {
if (!this.sdk) {
throw (0, errors_1.NotInitializedError)('KontentSmartLink is not initialized or has already been destroyed.');
}
else {
this.sdk.on(event, handler);
}
};
off = (event, handler) => {
if (!this.sdk) {
throw (0, errors_1.NotInitializedError)('KontentSmartLink is not initialized or has already been destroyed.');
}
else {
this.sdk.off(event, handler);
}
};
}
exports.default = KontentSmartLink;
//# sourceMappingURL=smartlink.js.map