Changed from axios to fetch.

This commit is contained in:
Michael Mainguy 2023-08-10 14:15:19 -05:00
parent 9a23df34fb
commit 1902219450

View File

@ -2,21 +2,36 @@ interface Env {
VOICE_TOKEN: string; VOICE_TOKEN: string;
} }
addEventListener('fetch', event => { const handler: ExportedHandler<Env> = {
event.respondWith(eventHandler(event)); async fetch(request, env: Env, context) {
}); async function gatherResponse(response) {
const {headers} = response;
const contentType = headers.get("content-type") || "";
if (contentType.includes("application/json")) {
return JSON.stringify(await response.json());
}
return response.text();
}
async function eventHandler(event) { const init = {
try {
const res = await fetch('https://api.assemblyai.com/v2/realtime/token',
{
method: 'POST', method: 'POST',
body: JSON.stringify({expires_in: 3600}), body: JSON.stringify({expires_in: 3600}),
headers: {authorization: event.context.env.VOICE_TOKEN} headers: {authorization: env.VOICE_TOKEN}
}); };
const response = await fetch('https://api.assemblyai.com/v2/realtime/token', init);
const results = await gatherResponse(response);
return new Response(results, init);
}
}
export default handler;
/*
export const onRequest: PagesFunction<Env> = async (context) => {
try {
const res = await fetch('https://api.assemblyai.com/v2/realtime/token',
{method: 'POST', body: JSON.stringify({expires_in: 3600}), headers: {authorization: context.env.VOICE_TOKEN}});
const response = await res.json(); const response = await res.json();
return new Response(JSON.stringify(response), {status: 200}); return Response.json(response);
} catch (error) { } catch (error) {
return new Response(error.message, {status: 500}); return new Response(error.message, {status: 500});
} }
} }*/