UNPKG

@magicbell/core

Version:

Official MagicBell API wrapper

77 lines 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const isNil_js_1 = tslib_1.__importDefault(require("lodash/isNil.js")); const ajax_js_1 = require("./lib/ajax.js"); const realtime_js_1 = require("./lib/realtime.js"); const index_js_1 = tslib_1.__importDefault(require("./models/Config/index.js")); const NotificationStoreWithContext_js_1 = tslib_1.__importDefault(require("./stores/NotificationStoreWithContext.js")); class MagicBellClient { config; _store; static async createInstance(options) { const client = new MagicBellClient(options); await client.config?.fetch(); return client; } static configure(options) { const { _baseURL: baseURL, ...otherOptions } = options; (0, ajax_js_1.setupAjax)({ ...otherOptions, baseURL }); } /** * Create a MagicBell instance. * * @param apiKey API key of your MagicBell project * @param userEmail Email of the user whose notifications will be displayed * @param userExternalId External ID of the user whose notifications will be displayed * @param userKey Computed HMAC of the user whose notifications will be displayed, compute this with the secret of the magicbell project * @param apiSecret API secret of your MagicBell project (required to create notifications) */ constructor(options) { // Setup the axios instance MagicBellClient.configure(options); const isFrontendClient = !(0, isNil_js_1.default)(options.userEmail) || !(0, isNil_js_1.default)(options.userExternalId); if (isFrontendClient) { const { apiKey, userEmail, userExternalId, userKey, apiSecret } = options; this.config = new index_js_1.default({ apiKey, userEmail, userExternalId, userKey, apiSecret }); } } /** * @deprecated To create notifications pUse Notification.create() instead */ get store() { if (!this._store) this._store = new NotificationStoreWithContext_js_1.default({}); return this._store; } /** * @deprecated To create notifications pUse Notification.create() instead */ getStore() { return this.store; } /** * Connect to a magicbell websocket to get real-time events. * * @return {Function} Function to close the connection and stop the listeners. YOU MUST CALL THIS FUNCTION in order to prevent memory leaks. */ startRealTimeListener() { if (!this.config) throw Error('Set a user email or id for this client'); const ablyClient = (0, realtime_js_1.connectToAbly)(this.config); const emitWakeup = () => realtime_js_1.pushEventAggregator.emit('wakeup'); ablyClient.connection.on('disconnected', emitWakeup); ablyClient.connection.on('suspended', emitWakeup); const ablyChannel = ablyClient.channels.get(this.config.ws.channel); ablyChannel.subscribe(realtime_js_1.handleAblyEvent); return () => { ablyClient.connection.off('disconnected', emitWakeup); ablyClient.connection.off('suspended', emitWakeup); ablyChannel.unsubscribe(realtime_js_1.handleAblyEvent); ablyChannel.detach(); ablyClient.close(); }; } } exports.default = MagicBellClient; //# sourceMappingURL=MagicBellClient.js.map