UNPKG

barikoiapis

Version:

Bangladeshi location data provider API library from barikoi.com

36 lines (31 loc) 1.06 kB
import { getConfig } from './config'; import axios, { AxiosResponse } from 'axios'; import { AutocompleteResult, AutocompleteOptions } from './types'; export async function autocomplete( options: AutocompleteOptions ): Promise<{ places: AutocompleteResult[]; status: number }> { const apiKey = getConfig().apiKey; const version = getConfig().version; let apiUrl = ''; if (version === 'v2') { apiUrl = `https://barikoi.xyz/v2/api/search/autocomplete/place`; } else { apiUrl = `https://barikoi.xyz/v1/api/search/autocomplete/${apiKey}/place`; } const params = version === 'v1' ? { ...options } : { api_key: apiKey, ...options }; try { const response: AxiosResponse<{ places: AutocompleteResult[]; status: number; }> = await axios.get(apiUrl, { params, }); // Return the full `data` object from the response return response.data; } catch (error) { // Handle errors, log them, or throw an exception console.error('Autocomplete request failed:', error.message); throw error; } }