Skip to content

DataFetcher

Defined in: packages/core/src/data/data-fetcher.ts:146

HTTP data fetcher with caching and retry support.

Features:

  • In-memory caching with TTL
  • Conditional requests (If-None-Match, If-Modified-Since)
  • Automatic retry with exponential backoff
  • Request timeout and cancellation
  • GeoJSON validation
  • Lifecycle callbacks
const fetcher = new DataFetcher({
cache: { enabled: true, defaultTTL: 300000, maxSize: 100 },
retry: { enabled: true, maxRetries: 3, initialDelay: 1000, maxDelay: 10000 },
timeout: 30000,
});
// Fetch with caching
const result = await fetcher.fetch('https://example.com/data.geojson');
console.log(`Fetched ${result.featureCount} features in ${result.duration}ms`);
// Prefetch for later use
await fetcher.prefetch('https://example.com/data2.geojson');
// Force fresh fetch
const fresh = await fetcher.fetch(url, { skipCache: true });

new DataFetcher(config?): DataFetcher

Defined in: packages/core/src/data/data-fetcher.ts:175

Create a new DataFetcher instance

ParameterTypeDescription
config?Partial<FetcherConfig>Fetcher configuration

DataFetcher

abortAll(): void

Defined in: packages/core/src/data/data-fetcher.ts:316

Abort all active requests

void


clearCache(): void

Defined in: packages/core/src/data/data-fetcher.ts:300

Clear all cached entries

void


fetch(url, options?): Promise<FetchResult>

Defined in: packages/core/src/data/data-fetcher.ts:212

Fetch GeoJSON data from a URL

ParameterTypeDescription
urlstringURL to fetch from
optionsFetchOptionsFetch options

Promise<FetchResult>

Fetch result with data and metadata

On network error, timeout, invalid JSON, or non-GeoJSON response

const result = await fetcher.fetch(
'https://example.com/data.geojson',
{
ttl: 60000, // 1 minute cache
onRetry: (attempt, delay, error) => {
console.log(`Retry ${attempt} in ${delay}ms: ${error.message}`);
},
}
);

getCacheStats(): CacheStats

Defined in: packages/core/src/data/data-fetcher.ts:309

Get cache statistics

CacheStats

Cache stats including size, hits, misses, and hit rate


invalidate(url): void

Defined in: packages/core/src/data/data-fetcher.ts:293

Invalidate cached entry for a URL

ParameterTypeDescription
urlstringURL to invalidate

void

// Force next fetch to get fresh data
fetcher.invalidate('https://example.com/data.geojson');

prefetch(url, ttl?): Promise<void>

Defined in: packages/core/src/data/data-fetcher.ts:278

Prefetch data and store in cache

ParameterTypeDescription
urlstringURL to prefetch
ttl?numberOptional custom TTL for cached entry

Promise<void>

Useful for preloading data that will be needed soon. Does not return the data.

// Prefetch data for quick access later
await fetcher.prefetch('https://example.com/data.geojson', 600000);