import axios , { AxiosRequestConfig } from 'axios' ; import { stringify , parseUrl , stringifyUrl } from 'query-string' ; import { parseStringPromise } from 'xml2js' ; import { OgcServiceTypes , SH_SERVICE_HOSTNAMES_V1_OR_V2 , SH_SERVICE_HOSTNAMES_V3 } from './const' ; import { getAxiosReqParams , RequestConfiguration } from '../utils/cancelRequests' ; import { CACHE_CONFIG_30MIN } from '../utils/cacheHandlers' ; import { GetCapabilitiesWmtsXml } from './wmts.utils' ; export type GetCapabilitiesWmsXml = { WMS_Capabilities : { Service : [ ] ; Capability : [ { Layer : [ GetCapabilitiesXmlLayer ] ; } ] ; } ; } ; export type GetCapabilitiesXmlLayer = { Name ? : string [ ] ; Title : string [ ] ; Abstract : string [ ] ; Style : any [ ] ; Dimension ? : any [ ] ; Layer ? : GetCapabilitiesXmlLayer [ ] ; ResourceUrl ? : string ; } ; export function createGetCapabilitiesXmlUrl ( baseUrl , ogcServiceType ) { const defaultQueryParams = { service : ogcServiceType , request : 'GetCapabilities' , format : 'text/xml' , } ; const { url , query } = parseUrl ( baseUrl ) ; return stringifyUrl ( { url : url , query : { ... defaultQueryParams , ... query } } , { sort : false } ) ; } export async function fetchGetCapabilitiesXml ( baseUrl , ogcServiceType , reqConfig ) { const axiosReqConfig = { responseType : 'text' , ... getAxiosReqParams ( reqConfig , CACHE_CONFIG_30MIN ) , } ; const url = createGetCapabilitiesXmlUrl ( baseUrl , ogcServiceType ) ; const res = await axios . get ( url , axiosReqConfig ) ; const parsedXml = await parseStringPromise ( res . data ) ; return parsedXml ; } function _flattenLayers ( layers , result = [ ] ) { layers . forEach ( l => { result . push ( l ) ; if ( l . Layer ) { _flattenLayers ( l . Layer , result ) ; } } ) ; return result ; } export async function fetchLayersFromGetCapabilitiesXml ( baseUrl , ogcServiceType , reqConfig ) { const parsedXml = ( await fetchGetCapabilitiesXml ( baseUrl , ogcServiceType , reqConfig ) ) as GetCapabilitiesWmsXml ; const layersInfos = _flattenLayers ( parsedXml . WMS_Capabilities . Capability [ 0 ] . Layer ) . filter ( layerInfo => layerInfo . Name ) ; return layersInfos ; } export async function fetchGetCapabilitiesJson ( baseUrl , reqConfig ) { const query = { request : 'GetCapabilities' , format : 'application/json' , } ; const queryString = stringify ( query , { sort : false } ) ; const url = `${ baseUrl }?${ queryString }` ; const axiosReqConfig = { responseType : 'json' , ... getAxiosReqParams ( reqConfig , CACHE_CONFIG_30MIN ) , } ; const res = await axios . get ( url , axiosReqConfig ) ; return res . data . layers ; } export async function fetchGetCapabilitiesJsonV1 ( baseUrl , reqConfig ) { const instanceId = parseSHInstanceId ( baseUrl ) ; const url = `https://eocloud.sentinel-hub.com/v1/config/instance/instance.${ instanceId }?scope=ALL` ; const axiosReqConfig = { responseType : 'json' , ... getAxiosReqParams ( reqConfig , CACHE_CONFIG_30MIN ) , } ; const res = await axios . get ( url , axiosReqConfig ) ; return res . data . layers ; } export function parseSHInstanceId ( baseUrl ) { const INSTANCE_ID_LENGTH = 36 ; for ( let hostname of SH_SERVICE_HOSTNAMES_V3 ) { const prefix = `${ hostname }ogc/wms/` ; if ( ! baseUrl . startsWith ( prefix ) ) { continue ; } const instanceId = baseUrl . substr ( prefix . length , INSTANCE_ID_LENGTH ) ; return instanceId ; } for ( let hostname of SH_SERVICE_HOSTNAMES_V1_OR_V2 ) { const prefix = `${ hostname }v1/wms/` ; if ( ! baseUrl . startsWith ( prefix ) ) { continue ; } const instanceId = baseUrl . substr ( prefix . length , INSTANCE_ID_LENGTH ) ; return instanceId ; } throw new Error ( `Could not parse instanceId from URL: ${ baseUrl }` ) ; }