UNPKG

@respeecher/respeecher-js

Version:

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Frespeecher%2Frespeecher-js) [![npm shield](

139 lines (138 loc) 5.98 kB
/** * This file was auto-generated by Fern from our API Definition. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as core from "../../../../core/index.mjs"; import { ContextfulGenerationRequest } from "../../../../serialization/resources/tts/types/ContextfulGenerationRequest.mjs"; import { CancellationRequest } from "../../../../serialization/resources/tts/types/CancellationRequest.mjs"; import { fromJson } from "../../../../core/json.mjs"; import * as serializers from "../../../../serialization/index.mjs"; export class TtsSocket { constructor(args) { this.eventHandlers = {}; this.handleOpen = () => { var _a, _b; (_b = (_a = this.eventHandlers).open) === null || _b === void 0 ? void 0 : _b.call(_a); }; this.handleMessage = (event) => { var _a, _b, _c, _d; const data = fromJson(event.data); const parsedResponse = serializers.tts.WebSocketSocketResponse.parse(data, { unrecognizedObjectKeys: "strip", omitUndefined: true, }); if (parsedResponse.ok) { (_b = (_a = this.eventHandlers).message) === null || _b === void 0 ? void 0 : _b.call(_a, parsedResponse.value); } else { (_d = (_c = this.eventHandlers).error) === null || _d === void 0 ? void 0 : _d.call(_c, new Error("Received unknown message type")); } }; this.handleClose = (event) => { var _a, _b; (_b = (_a = this.eventHandlers).close) === null || _b === void 0 ? void 0 : _b.call(_a, event); }; this.handleError = (event) => { var _a, _b; const message = event.message; (_b = (_a = this.eventHandlers).error) === null || _b === void 0 ? void 0 : _b.call(_a, new Error(message)); }; this.socket = args.socket; this.socket.addEventListener("open", this.handleOpen); this.socket.addEventListener("message", this.handleMessage); this.socket.addEventListener("close", this.handleClose); this.socket.addEventListener("error", this.handleError); } /** The current state of the connection; this is one of the readyState constants. */ get readyState() { return this.socket.readyState; } /** * @param event - The event to attach to. * @param callback - The callback to run when the event is triggered. * Usage: * ```typescript * this.on('open', () => { * console.log('The websocket is open'); * }); * ``` */ on(event, callback) { this.eventHandlers[event] = callback; } sendGenerate(message) { this.assertSocketIsOpen(); const jsonPayload = ContextfulGenerationRequest.jsonOrThrow(message, { unrecognizedObjectKeys: "strip", allowUnrecognizedUnionMembers: true, allowUnrecognizedEnumValues: true, skipValidation: true, }); this.socket.send(JSON.stringify(jsonPayload)); } sendCancel(message) { this.assertSocketIsOpen(); const jsonPayload = CancellationRequest.jsonOrThrow(message, { unrecognizedObjectKeys: "strip", allowUnrecognizedUnionMembers: true, allowUnrecognizedEnumValues: true, skipValidation: true, }); this.socket.send(JSON.stringify(jsonPayload)); } /** Connect to the websocket and register event handlers. */ connect() { this.socket.reconnect(); this.socket.addEventListener("open", this.handleOpen); this.socket.addEventListener("message", this.handleMessage); this.socket.addEventListener("close", this.handleClose); this.socket.addEventListener("error", this.handleError); return this; } /** Close the websocket and unregister event handlers. */ close() { this.socket.close(); this.handleClose({ code: 1000 }); this.socket.removeEventListener("open", this.handleOpen); this.socket.removeEventListener("message", this.handleMessage); this.socket.removeEventListener("close", this.handleClose); this.socket.removeEventListener("error", this.handleError); } /** Returns a promise that resolves when the websocket is open. */ waitForOpen() { return __awaiter(this, void 0, void 0, function* () { if (this.socket.readyState === core.ReconnectingWebSocket.OPEN) { return this.socket; } return new Promise((resolve, reject) => { this.socket.addEventListener("open", () => { resolve(this.socket); }); this.socket.addEventListener("error", (event) => { reject(event); }); }); }); } /** Asserts that the websocket is open. */ assertSocketIsOpen() { if (!this.socket) { throw new Error("Socket is not connected."); } if (this.socket.readyState !== core.ReconnectingWebSocket.OPEN) { throw new Error("Socket is not open."); } } /** Send a binary payload to the websocket. */ sendBinary(payload) { this.socket.send(payload); } }