UNPKG

@point-api/js-sdk

Version:

Javascript SDK for Point API

172 lines 6.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ioProxy = require("socket.io-client"); const io = ioProxy.default || ioProxy; /** * Point Websockets Api Instance */ class AutocompleteSessionImpl { /** * @param emailAddress Email address of Point user * @param authCode Auth code (JWT) provider */ constructor(emailAddress, authManager, searchType = "standard", apiUrl = "https://v1.pointapi.com") { /** @private Reconnect counter */ this.reconnectCount = 0; /** @private Max reconnect attempts */ this.maxReconnects = 10; /** * Registers error handler callback that will be invoked * on any socket.io session errors sent from the server. * * @param callback Error callback method */ this.setOnErrorHandler = (callback) => { this.onErrorHandler = callback; }; /** * Callback function that handles JWT changed events. */ this.onJwtChange = () => { this.reconnect(); }; this.emailAddress = emailAddress; this.authManager = authManager; this.searchType = searchType; this.ApiUrl = apiUrl; } /** * Reconnects to the Point API socket.io */ async reconnect() { this.disconnect(); this.authManager.onJwtChange(this.onJwtChange); const jwt = await this.authManager.getJwt(); this.socket = io(this.ApiUrl, { reconnection: false, query: { emailAddress: this.emailAddress, searchType: this.searchType }, transportOptions: { polling: { extraHeaders: { Authorization: "Bearer " + jwt } } } }); this.socket.on("connect", () => { this.reconnectCount = 0; }); this.socket.on("error", (error) => { if (this.onErrorHandler) { this.onErrorHandler(error); } }); this.socket.on("disconnect", (reason) => { // If client was the one that disconnected, // don't reconnect automatically. if (reason === "io client disconnect") return; // If server was the one that disconnected, // the authentication credentials are probably // invalid. Don't reconnect automatically. if (reason === "io server disconnect") return; // Try to reconnect maxReconnect times using exponentially // growing delays starting from 100ms if (this.reconnectCount < this.maxReconnects) { const delay = 100 * Math.pow(2, this.reconnectCount); this.reconnectCount++; setTimeout(() => { this.reconnect(); }, delay); } }); } /** * Disconnects from the Point API manually */ disconnect() { if (this.socket != null) { // Remove event listeners this.socket.removeAllListeners(); // Close the connection this.socket.disconnect(); } this.authManager.offJwtChange(this.onJwtChange); } /** * Query PointApi with seed text to get predicted suggestions * @param seedText The text to base suggestion predictions off of * @returns A list of the predicted suggestion objects */ queryByContent(seedText, currentContext) { return new Promise((resolve, reject) => { if (!this.socket || this.socket.disconnected) { reject("Socket is disconnected"); } this.socket.emit("querySnippetContents", { seedText: seedText.trim(), currentContext }, (response) => { if (!response || !response.snippets || !response.snippets.length) { resolve(null); } resolve(response); }); }); } /** * Query PointApi with a hotkey trigger to get a full hotkey suggestion * @param trigger String that is a shortcut for the full hotkey text * @returns A list of the predicted suggestion objects */ queryByName(query) { return new Promise((resolve, reject) => { if (!this.socket || this.socket.disconnected) { reject("Socket is disconnected"); } this.socket.emit("querySnippetNames", { query }, (response) => { if (!response || !response.snippets || !response.snippets.length) { resolve(null); } resolve(response); }); }); } /** * Give feedback on Point Api's suggestions. * This is like chosenSuggestion/Hotkey interaction. */ async feedback(responseId, snippet, origin) { this.socket.emit("feedback", { responseId, snippet, origin }, (response) => { if (!response || response.status !== "success") { if (response.message) { throw new Error(response.message); } throw new Error("Could not record feedback"); } }); } /** * Get reply suggestions given some recieved text */ reply(previousMessage, contextType = "text") { return new Promise((resolve, reject) => { if (!this.socket || this.socket.disconnected) { reject("Socket is disconnected"); } this.socket.emit("reply", { previousMessage, contextType }, (response) => { if (!response || !response.replies || !response.replies.length) { resolve(null); } resolve(response); }); }); } } exports.default = AutocompleteSessionImpl; //# sourceMappingURL=autocompleteSession.js.map