From 19022194501814df3e81f8bbe276aa84cf49730f Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Thu, 10 Aug 2023 14:15:19 -0500 Subject: [PATCH] Changed from axios to fetch. --- functions/api/voice/token.ts | 37 +++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/functions/api/voice/token.ts b/functions/api/voice/token.ts index 500a1d8..2db8fe7 100644 --- a/functions/api/voice/token.ts +++ b/functions/api/voice/token.ts @@ -2,21 +2,36 @@ interface Env { VOICE_TOKEN: string; } -addEventListener('fetch', event => { - event.respondWith(eventHandler(event)); -}); +const handler: ExportedHandler = { + 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 = { + 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); + const results = await gatherResponse(response); + return new Response(results, init); + } +} +export default handler; +/* +export const onRequest: PagesFunction = async (context) => { try { const res = await fetch('https://api.assemblyai.com/v2/realtime/token', - { - method: 'POST', - body: JSON.stringify({expires_in: 3600}), - headers: {authorization: event.context.env.VOICE_TOKEN} - }); + {method: 'POST', body: JSON.stringify({expires_in: 3600}), headers: {authorization: context.env.VOICE_TOKEN}}); const response = await res.json(); - return new Response(JSON.stringify(response), {status: 200}); + return Response.json(response); } catch (error) { return new Response(error.message, {status: 500}); } -} +}*/