Skip to content

Commit

Permalink
refactor: text to speech api
Browse files Browse the repository at this point in the history
  • Loading branch information
lizacoma committed Nov 8, 2023
1 parent d09fb34 commit 96250fd
Showing 1 changed file with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
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';
let url = `${baseUrl}?${qs.stringify({
...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}`,
})),
]);
}
};

Expand Down

0 comments on commit 96250fd

Please sign in to comment.