import { FeatureLike } from "ol/Feature"; import { Extent } from "ol/extent"; import FeatureFormat from "ol/format/Feature"; import { Projection } from "ol/proj"; import { ulid } from "ulid"; export const featureId = (f: FeatureLike | number) => { return typeof f === "number" ? f : ((f.getId() ?? f.get("id")) as number); }; /** * This is a rewrite of loadFeaturesXhr() in ol/featureloader.js to use fetch() instead of XMLHttpRequest. * This is needed so that patchFetch() from lol-auth-js can inject a bearer token into the auth header. */ export const loadFeatures = async ( url: string, format: FeatureFormat, extent: Extent, projection: Projection ) => { const type = format.getType(); const response = await fetch(url, { method: "GET", headers: { "x-linz-correlation-id": ulid(), }, }); let data: any; if (type === "json") { data = await response.json(); } else if (type === "arraybuffer") { data = await response.arrayBuffer(); } else if (type === "xml") { data = new DOMParser().parseFromString( await response.text(), "application/xml" ); } else { data = await response.text(); } return format.readFeatures(data, { extent: extent, featureProjection: projection, }); };