From 96250fd4cd175ea356906351a9ca96c99a48346a Mon Sep 17 00:00:00 2001 From: lizacoma Date: Wed, 8 Nov 2023 22:00:06 +0200 Subject: [PATCH] refactor: text to speech api --- .../text-to-speech/api/TextToSpeechAPI.js | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/modules/lookups/modules/media/modules/text-to-speech/api/TextToSpeechAPI.js b/src/modules/lookups/modules/media/modules/text-to-speech/api/TextToSpeechAPI.js index cd0a119cb..fa5f27116 100644 --- a/src/modules/lookups/modules/media/modules/text-to-speech/api/TextToSpeechAPI.js +++ b/src/modules/lookups/modules/media/modules/text-to-speech/api/TextToSpeechAPI.js @@ -1,7 +1,10 @@ +import applyTransform, { + notify, + snakeToCamel, +} from '@webitel/ui-sdk/src/api/transformers'; import { objCamelToSnake } from '@webitel/ui-sdk/src/scripts/caseConverters'; -import eventBus from '@webitel/ui-sdk/src/scripts/eventBus'; import qs from 'query-string'; -import instance from '../../../../../../../app/api/old/instance'; +import instance from '../../../../../../../app/api/instance'; const getTtsStreamUrl = (params, apiUrl = false) => { const baseUrl = '/storage/tts/stream'; @@ -9,22 +12,35 @@ const getTtsStreamUrl = (params, apiUrl = false) => { ...objCamelToSnake(params), access_token: instance.defaults.headers['X-Webitel-Access'], })}`; + // console.log('getTtsStreamUrl url1:', url1); + // let url = applyTransform(params, [ + // merge(getDefaultGetParams()), + // starToSearch('search'), + // (params) => ({ ...params, q: params.search }), + // sanitize(fieldsToSend), + // camelToSnake(), + // generateUrl(baseUrl), + // ]); + // console.log('getTtsStreamUrl url:', url); if (apiUrl) url = `${import.meta.env.VITE_API_URL}${url}`; return url; }; const getTts = async (params) => { + const url = getTtsStreamUrl(params, true); + try { - const url = getTtsStreamUrl(params, true); - const response = await fetch(url); - if (!response.ok) throw new Error(response.status); - return response.blob(); + const response = await instance.get(url); + return applyTransform(response.data, [ + snakeToCamel(), + ]); } catch (err) { - eventBus.$emit('notification', { - type: 'error', - text: `Failed to process Text-to-Speech: ${err}`, - }); - throw err; + throw applyTransform(err, [ + notify(({ callback }) => callback({ + type: 'error', + text: `Failed to process Text-to-Speech: ${err}`, + })), + ]); } };