45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
type DadataAddressSuggestion = {
|
|
value: string;
|
|
unrestricted_value?: string;
|
|
data?: {
|
|
fias_id?: string;
|
|
};
|
|
};
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const config = useRuntimeConfig(event);
|
|
const token = String(config.dadataApiToken || '').trim();
|
|
if (!token) {
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'DADATA_API_TOKEN is not configured.',
|
|
});
|
|
}
|
|
|
|
const body = await readBody<{ query?: string }>(event);
|
|
const query = String(body?.query ?? '').trim();
|
|
if (query.length < 2) {
|
|
return { suggestions: [] as DadataAddressSuggestion[] };
|
|
}
|
|
|
|
const response = await $fetch<{ suggestions?: DadataAddressSuggestion[] }>(
|
|
'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address',
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json',
|
|
Authorization: `Token ${token}`,
|
|
},
|
|
body: {
|
|
query,
|
|
count: 10,
|
|
},
|
|
},
|
|
);
|
|
|
|
return {
|
|
suggestions: response.suggestions ?? [],
|
|
};
|
|
});
|