barikoiapis
Version:
Bangladeshi location data provider API library from barikoi.com
67 lines (58 loc) • 1.47 kB
text/typescript
import axios, { AxiosResponse } from "axios";
import { getConfig } from "./config";
interface Geometry {
geometry: GeoJSON.Geometry | string;
legs: {
steps: any[];
distance: number;
duration: number;
summary: string;
weight: number;
}[];
distance: number;
duration: number;
weight_name: string;
weight: number;
}
interface Waypoint {
hint: string;
distance: number;
name: string;
location: [number, number];
}
interface RouteResponse {
code: string;
routes: Geometry[];
waypoints: Waypoint[];
}
interface routeOptions {
geometry?: string;
}
export async function route(
source: [number, number],
destination: [number, number],
options: routeOptions
): Promise<RouteResponse> {
const apiKey = getConfig().apiKey;
const version = getConfig().version;
let apiUrl = "";
if (version === "v2") {
apiUrl = `https://barikoi.xyz/v2/api/route/${source[0]},${source[1]};${destination[0]},${destination[1]}`;
} else {
apiUrl = `https://barikoi.xyz/v2/api/route/${apiKey}/${source[0]},${source[1]};${destination[0]},${destination[1]}`;
}
const params =
version === "v1" ? { ...options } : { api_key: apiKey, ...options };
try {
const response: AxiosResponse<{ route: RouteResponse }> = await axios.get(
apiUrl,
{
params,
}
);
return response.data.route;
} catch (error) {
console.error("Route request failed:", error.message);
throw error;
}
}