eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.6 kB
JavaScript
import{resumeHook}from"#internal/workflow/runtime.js";import{buildAuthorizationCompletePage}from"#runtime/connections/authorization-complete-page.js";async function handleConnectionCallbackRequest(e,t){return handleCallbackRequest(e,t,!1)}async function handleLegacyConnectionCallbackRequest(e,t){return handleCallbackRequest(e,t,!0)}async function handleCallbackRequest(e,t,n){let r=t.params.name,i=t.params.attemptId,a=t.params.token;if(typeof r!=`string`||r.length===0)return Response.json({error:`Missing connection name.`,ok:!1},{status:400});if(typeof a!=`string`||a.length===0)return Response.json({error:`Missing callback token.`,ok:!1},{status:400});if(!n&&(typeof i!=`string`||i.length===0))return Response.json({error:`Missing authorization attempt ID.`,ok:!1},{status:400});let o=await projectAuthorizationCallback(e);try{await resumeHook(a,{kind:`deliver`,payloads:[{authorizationCallback:n?{callback:o,connectionName:r,legacy:!0}:{attemptId:i,callback:o,connectionName:r}}]})}catch{return Response.json({error:`Connection callback not pending.`,ok:!1},{status:404})}return buildAuthorizationCompletePage()}async function projectAuthorizationCallback(e){let t={};for(let[n,r]of new URL(e.url).searchParams)t[n]=r;let n;if(e.method!==`GET`&&e.method!==`HEAD`){try{n=await e.text()}catch{n=void 0}let r=e.headers.get(`content-type`)??``;if(n&&r.includes(`application/x-www-form-urlencoded`))for(let[e,r]of new URLSearchParams(n))t[e]=r}return n===void 0?{params:t,method:e.method}:{params:t,method:e.method,body:n}}export{handleConnectionCallbackRequest,handleLegacyConnectionCallbackRequest};