تايب سكريبت / جافا سكريبت
Node.js 18+, or any runtime with native fetch. Works with the Edge runtime.
The reference implementation. Typed models generated from the same OpenAPI contract this website renders, a maps client and a tracking client, retries with jitter, and an SSE reader for tracking streams.
Install
npm install @bardiyamap/sdk@0.1.0What it supports
- Places search, autocomplete and details
- Forward and reverse geocoding
- Directions and distance matrix
- Address validation
- Style and tile URL helpers with optional key embedding
- Tracking sessions, ingest, history and state
- Client tokens, webhooks and subscriptions
- Server-sent events with resume
Initialise the client
ts
import { createClient } from "@bardiyamap/sdk";
const client = createClient({
mapsApiKey: process.env.BARDIYA_MAPS_API_KEY,
trackingApiKey: process.env.BARDIYA_TRACKING_API_KEY,
timeoutMs: 15_000,
maxRetries: 2,
});Typical operations
Places search and directions
ts
const places = await client.maps.places.search({
q: "coffee",
latitude: 30.0444,
longitude: 31.2357,
limit: 5,
});
const route = await client.maps.routing.directions({
origin: { latitude: 30.0444, longitude: 31.2357 },
destination: { latitude: 30.0626, longitude: 31.2497 },
});
// isDegradedDirections is a package export, not a client method:
// import { isDegradedDirections } from "@bardiyamap/sdk";
if (isDegradedDirections(route)) {
console.warn("degraded route:", route.geometry_source, route.distance_source);
}Tracking session and stream
ts
const session = await client.tracking.createSession(
{ subjectType: "vehicle", externalReference: "order-1042" },
{ idempotencyKey: "order-1042" },
);
await client.tracking.sendLocation(session.id, {
latitude: 30.0444,
longitude: 31.2357,
accuracyM: 8,
recordedAt: new Date().toISOString(),
});
for await (const frame of client.tracking.streamSession(session.id)) {
if (frame.type === "location") console.log(frame.id, frame.data);
}Error handling
ts
import { BardiyaApiError } from "@bardiyamap/sdk";
try {
await client.maps.places.search({ q: "coffee" });
} catch (error) {
if (error instanceof BardiyaApiError) {
error.status; // 429
error.code; // "rate_limited"
error.requestId; // quote this to support
error.retryable; // true for 429/5xx, false for 401/403/422
}
}Realtime
ts
// Resumable: pass the last event id you processed.
for await (const frame of client.tracking.streamSession(id, { lastEventId })) {
lastEventId = frame.id ?? lastEventId;
}Things that catch people out
- The package re-exports a tracking client that predates the maps client, so older imports keep working.
- Cancellation uses an AbortSignal per call; pass signal in the request options.
- Server-side only: a bundler will happily inline your key into a browser bundle, so keep the import in server code.
