UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

103 lines (102 loc) 3.73 kB
import StateManager from '../state/statemanager'; const default_encoding = 'application/json'; /** * This class covers the common part of the OGC API standard described in https://docs.ogc.org/is/19-072/19-072.html. * It contains shared methods between clients of different OGC API standards. * Any OGC API (Maps, Records, Features, STAC...) shall extend this class. */ export default class OgcApiClient { get state() { return this.stateManager.state; } constructor(options) { Object.defineProperty(this, "stateManager", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "url", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.url = options.url; this.stateManager = StateManager.getInstance(); } async serviceDescription() { throw new Error('Not implemented'); } async conformance() { const url = await this.getLink('conformance'); let response; if (url) { response = await fetch(url, this.getFetchOptions()); } if (!response?.ok) { throw new Error(`Failed to get conformance: ${response?.status} - ${response?.statusText}`); } const conformance = await response.json(); return conformance.conformsTo ?? []; } async getByRelationAndType(collectionId, relation, type) { const url = await this.getLink(relation, `${this.url}/collections/${collectionId}`, type); let response; if (url) { response = await fetch(url, this.getFetchOptions()); } if (!response?.ok) { throw new Error(`Failed to get ${relation}: ${response?.status} - ${response?.statusText}`); } if (type.includes('json')) { return await response.json(); } else if (type.includes('xml')) { return response; } else { return response; } } async getLinks(path) { const response = await fetch(path, this.getFetchOptions()); if (!response.ok) { throw new Error(`Failed to get links: ${response.status} - ${response.statusText}`); } const jsonResponse = await response.json(); return jsonResponse.links ?? []; } async getLink(relation, path = this.url, encodingType = default_encoding) { const links = await this.getLinks(path); const link = links.find((link) => link.rel === relation && (!('type' in link) || link.type === encodingType))?.href; if (!link) { throw new Error(`OGC API resource at '${path}' does not support '${relation}'}`); } return link; } async fetchAll(url, fetchOptions) { const responses = []; while (url) { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`Failed to fetch all: ${response.status} - ${response.statusText}`); } const responseJson = await response.json(); responses.push(responseJson); // Get next page URL if it exists url = this.getNextUrl(responseJson, url); } return responses; } getNextUrl(responseJson, _currentUrl) { return responseJson.links?.find((link) => link.rel === 'next')?.href ?? ''; } getFetchOptions(method = 'GET') { const fetchOptions = { method: method }; fetchOptions.headers = new Headers(); return fetchOptions; } }