2024-03-29 19:54:14 +01:00
|
|
|
import { type NextRequest } from 'next/server'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
const config: RequestInit = {
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
method: 'GET',
|
2024-03-30 16:49:14 +01:00
|
|
|
next: { revalidate: 30 }
|
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-03-30 16:49:14 +01: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}`
|
2024-03-29 21:37:30 +01:00
|
|
|
let data
|
|
|
|
let status
|
2024-03-29 19:54:14 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetch(url, config)
|
|
|
|
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-03-30 18:05:01 +01:00
|
|
|
'Content-Type': 'application/json'
|
2024-03-29 21:37:30 +01:00
|
|
|
}
|
|
|
|
})
|
2024-03-29 19:54:14 +01:00
|
|
|
}
|