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

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-03-29 19:54:14 +01:00
import { type NextRequest } from 'next/server'
export const runtime = 'edge'
const apiUrl = 'https://api.1inch.dev/swap/v6.0/1/quote'
const config: RequestInit = {
headers: {
Authorization: `Bearer ${process.env.ONEINCH_API_KEY}`,
'content-type': 'application/json'
},
method: 'GET',
next: { revalidate: 60 }
}
export async function GET(request: NextRequest) {
const searchParams = request?.nextUrl?.searchParams
const src = searchParams?.get('src')
const dst = searchParams?.get('dst')
const amount = searchParams?.get('amount')
if (!src || !dst || !amount) {
return Response.json(null, { status: 400 })
}
const url = `${apiUrl}/?src=${src}&dst=${dst}&amount=${amount}&includeTokensInfo=true&includeProtocols=true`
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: {
'Content-Type': 'application/json',
'Cache-Control': 'public, s-maxage=10'
}
})
2024-03-29 19:54:14 +01:00
}