asi-calculator/app/api/quote/route.ts

40 lines
996 B
TypeScript
Raw Normal View History

import type { NextRequest } from 'next/server'
2024-03-29 19:54:14 +01:00
export const runtime = 'edge'
2024-03-30 16:49:14 +01:00
const apiUrl = process.env.WEB3_API_URL
2024-03-29 19:54:14 +01:00
export async function GET(request: NextRequest) {
const searchParams = request?.nextUrl?.searchParams
2024-03-30 16:49:14 +01:00
const tokenIn = searchParams?.get('tokenIn')
const tokenOut = searchParams?.get('tokenOut')
const amountIn = searchParams?.get('amountIn')
2024-03-29 19:54:14 +01:00
2024-04-01 03:25:23 +02:00
if (!tokenIn || !tokenOut || !amountIn)
2024-03-29 19:54:14 +01:00
return Response.json(null, { status: 400 })
2024-03-30 16:49:14 +01:00
const url = `${apiUrl}/quote?tokenIn=${tokenIn}&tokenOut=${tokenOut}&amountIn=${amountIn}`
let data: unknown
let status: number
2024-03-29 19:54:14 +01:00
try {
2024-04-01 04:25:28 +02:00
const res = await fetch(url)
2024-03-29 19:54:14 +01:00
const json = await res.json()
data = json
2024-03-29 21:37:30 +01:00
status = res.status
2024-03-29 19:54:14 +01:00
} catch (error: unknown) {
console.error((error as Error).message)
data = null
2024-03-29 21:37:30 +01:00
status = 500
2024-03-29 19:54:14 +01:00
}
2024-03-29 21:37:30 +01:00
return new Response(JSON.stringify(data), {
status,
headers: {
2024-04-05 01:15:38 +02:00
'Content-Type': 'application/json',
'Cache-Control': 'public, s-maxage=10'
2024-03-29 21:37:30 +01:00
}
})
2024-03-29 19:54:14 +01:00
}