@spitch/reader
Version:
Embeddable audio reader component to render a rich audio reader player in African languages.
47 lines (46 loc) • 1.64 kB
JavaScript
export const API_URL = import.meta.env.VITE_SPITCH_API_URL || 'https://api.spi-tch.com/v1';
class SpitchAPI {
async generateStreamingAudio(config) {
const headers = {
"Content-Type": "application/json",
Accept: "*/*",
Origin: config.origin,
};
const body = {
url: config.url,
language: config.language,
voice: config.voice,
};
const url = `${API_URL}/reader/${config.id}/audio`;
const response = await fetch(url, {
method: "POST",
headers: new Headers(headers),
body: JSON.stringify(body),
});
if (!response.ok) {
let respText = "";
try {
respText = await response.text();
if (response.status === 401) {
let userMsg = "This feature is not available on this website.";
try {
const details = JSON.parse(respText);
userMsg += details.detail ? `\nDetails: ${details.detail}` : "";
}
catch { }
throw new Error(userMsg);
}
}
catch { }
throw new Error(`Streaming audio failed: ${response.status}`);
}
return response;
}
async getReaderConfig(readerId) {
const res = await fetch(`${API_URL}/reader/${readerId}/config`);
if (!res.ok)
throw new Error('Failed to fetch reader config');
return await res.json();
}
}
export const spitchAPI = new SpitchAPI();