UNPKG

n8n-nodes-salutespeech

Version:

User-friendly SaluteSpeech (Sber) community node

136 lines 4.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SaluteSpeechApiClient = void 0; const axios_1 = __importDefault(require("axios")); const Settings_1 = require("./Settings"); const PostAuth_1 = require("./PostAuth"); const exception_1 = require("./exception"); const PostSpeechRecognize_1 = require("./PostSpeechRecognize"); const PostTextSynthesize_1 = require("./PostTextSynthesize"); class SaluteSpeechApiClientInstance { constructor(config) { this._settings = { ...(0, Settings_1.getDefaultSettings)(), ...config, }; if (this._settings.accessToken) { this._accessToken = { access_token: this._settings.accessToken, expires_at: 0, }; } this._client = axios_1.default.create(this._getAxiosConfig()); this._authClient = axios_1.default.create(this._getAuthAxiosConfig()); } async updateConfig(config) { var _a; if (this._settings.credentials !== config.authKey) { this._settings = { ...this._settings, ...config }; this._settings.credentials = (_a = config.authKey) !== null && _a !== void 0 ? _a : undefined; const axiosConfig = { timeout: this._settings.timeout * 1000, httpsAgent: this._settings.httpsAgent, validateStatus: () => true, }; this._client = axios_1.default.create({ baseURL: this._settings.baseUrl, ...axiosConfig, }); this._authClient = axios_1.default.create(axiosConfig); this._accessToken = undefined; this._settings.accessToken = undefined; await this.updateToken(); } } get token() { var _a; return (_a = this._accessToken) === null || _a === void 0 ? void 0 : _a.access_token; } get useAuth() { return Boolean(this._settings.credentials); } checkValidityToken() { return !!this._accessToken; } resetToken() { this._accessToken = undefined; } _getAxiosConfig() { return { baseURL: this._settings.baseUrl, timeout: this._settings.timeout * 1000, httpsAgent: this._settings.httpsAgent, validateStatus: () => true, }; } _getAuthAxiosConfig() { return { timeout: this._settings.timeout * 1000, httpsAgent: this._settings.httpsAgent, validateStatus: () => true, }; } async updateToken() { if (this._settings.credentials) { this._accessToken = await (0, PostAuth_1.post_auth)(this._authClient, { url: this._settings.authUrl, credentials: this._settings.credentials, scope: this._settings.scope, }); console.info('OAUTH UPDATE TOKEN'); } } async _decorator(call) { console.log('_decorator.useAuth', this.useAuth); if (this.useAuth) { console.log('_decorator.checkValidityToken', this.checkValidityToken()); if (this.checkValidityToken()) { try { return await call(); } catch (error) { console.error('_decorator.error', error); if (error instanceof exception_1.AuthenticationError) { console.warn('AUTHENTICATION ERROR'); this.resetToken(); } else { throw error; } } } await this.updateToken(); } return await call(); } async speechRecognize(file, lang, profanity, model, channelsCount) { return this._decorator(() => { var _a; return (0, PostSpeechRecognize_1.post_speech_recognize)(this._client, { file, language: lang, profanity, model, channelsCount, accessToken: (_a = this._accessToken) === null || _a === void 0 ? void 0 : _a.access_token, }); }); } async textSynthesize(text, voiceId, format, freq) { return this._decorator(() => { var _a; return (0, PostTextSynthesize_1.post_text_synthesize)(this._client, { text, voiceId, format, freq, accessToken: (_a = this._accessToken) === null || _a === void 0 ? void 0 : _a.access_token, }); }); } } exports.SaluteSpeechApiClient = new SaluteSpeechApiClientInstance({}); //# sourceMappingURL=SaluteSpeechApiClient.js.map