32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
import type { YandexLocationDetectRequest, YandexLocationDetectResponse } from '#shared/yandex_location_detect'
|
||
import axios from 'axios'
|
||
import { defineEventHandler, readBody } from 'h3'
|
||
|
||
export default defineEventHandler(async (event): Promise<YandexLocationDetectResponse | { error: string }> => {
|
||
try {
|
||
const data = await readBody<YandexLocationDetectRequest>(event)
|
||
const apiUrl = import.meta.env.VITE_YANDEX_B2B_BASE_URL!
|
||
const token = import.meta.env.VITE_YANDEX_B2B_TOKEN!
|
||
|
||
const response = await axios.post<YandexLocationDetectResponse>(
|
||
`${apiUrl}/location/detect`,
|
||
{
|
||
location: data?.location,
|
||
},
|
||
{
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
'Accept-Language': 'ru-RU',
|
||
'Authorization': `Bearer ${token}`,
|
||
},
|
||
},
|
||
)
|
||
|
||
return response.data
|
||
}
|
||
catch (error) {
|
||
console.error('Ошибка при запросе к Яндекс API:', error)
|
||
return { error: `Не удалось получить координаты: ${error}` }
|
||
}
|
||
})
|