flight-planner
Version:
Plan and route VFR flights
44 lines (43 loc) • 1.76 kB
JavaScript
import { bbox, buffer, point } from "@turf/turf";
/**
* RepositoryBase class provides a base implementation for repositories.
*
* @abstract
* @class RepositoryBase<T>
* @template T - The type of data to be fetched.
*/
export class RepositoryBase {
/**
* Fetches data by geographic location within specified radius.
*
* @param location - The location coordinates [longitude, latitude].
* @param radius - The radius in kilometers (default: 100, max: 1000).
* @returns A promise that resolves to an array of data.
*/
async fetchByLocation(location, radius = 100) {
if (!Array.isArray(location) || location.length < 2 ||
typeof location[0] !== 'number' || typeof location[1] !== 'number') {
throw new Error('Invalid location format. Expected [longitude, latitude].');
}
const radiusRange = Math.min(1000, Math.max(1, radius));
const resultList = [];
if (this.fetchByRadius) {
const result = await this.fetchByRadius(location, radiusRange);
resultList.push(...result);
}
else if (this.fetchByBbox) {
const locationPoint = point(location);
const buffered = buffer(locationPoint, radiusRange, { units: 'kilometers' });
if (buffered) {
const searchBbox = bbox(buffered);
const result = await this.fetchByBbox(searchBbox);
resultList.push(...result);
}
}
else {
throw new Error('This repository does not implement fetchByRadius or fetchByBbox. At least one of these methods must be implemented to use fetchByLocation.');
}
return resultList;
}
}
export default RepositoryBase;