Changed from axios to fetch.

This commit is contained in:
Michael Mainguy 2023-08-10 16:05:42 -05:00
parent eed47ba48c
commit 5beb36cf45

View File

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