diff --git a/README.md b/README.md index c2bda17e9..e8c178893 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,9 @@ Returns the layers from a parsed WMS GetCapabilities object. - `capabilities` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** A capabilities object. - `nameField` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Configure the field which should be set as the 'name' property in the openlayers layer. (optional, default `'Name'`) +- `proxyFn` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Optional proxy function which can be applied to + `GetMap`, `GetFeatureInfo` and `GetLegendGraphic` + requests to avoid CORS issues. Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<OlLayerTile>** Array of OlLayerTile diff --git a/src/CapabilitiesUtil/CapabilitiesUtil.js b/src/CapabilitiesUtil/CapabilitiesUtil.js index a14ed0351..da774c3f5 100644 --- a/src/CapabilitiesUtil/CapabilitiesUtil.js +++ b/src/CapabilitiesUtil/CapabilitiesUtil.js @@ -3,6 +3,7 @@ import OlSourceImageWMS from 'ol/source/ImageWMS'; import OlLayerImage from 'ol/layer/Image'; import get from 'lodash/get.js'; +import isFunction from 'lodash/isFunction'; /** * Helper class to parse capabilities of WMS layers @@ -32,9 +33,12 @@ class CapabilitiesUtil { * @param {Object} capabilities A capabilities object. * @param {string} nameField Configure the field which should be set as the * 'name' property in the openlayers layer. + * @param {Function} proxyFn Optional proxy function which can be applied to + * `GetMap`, `GetFeatureInfo` and `GetLegendGraphic` + * requests to avoid CORS issues. * @return {OlLayerTile[]} Array of OlLayerTile */ - static getLayersFromWmsCapabilties(capabilities, nameField = 'Name') { + static getLayersFromWmsCapabilties(capabilities, nameField = 'Name', proxyFn) { const wmsVersion = get(capabilities,'version'); const wmsAttribution = get(capabilities,'Service.AccessConstraints'); const layersInCapabilities = get(capabilities,'Capability.Layer.Layer'); @@ -49,12 +53,12 @@ class CapabilitiesUtil { title: get(layerObj, 'Title'), name: get(layerObj, nameField), abstract: get(layerObj, 'Abstract'), - getFeatureInfoUrl: getFeatureInfoUrl, + getFeatureInfoUrl: isFunction(proxyFn) ? proxyFn(getFeatureInfoUrl) : getFeatureInfoUrl, getFeatureInfoFormats: get(wmsGetFeatureInfoConfig, 'Format'), - legendUrl: legendUrl, + legendUrl: isFunction(proxyFn) ? proxyFn(legendUrl) : legendUrl, queryable: get(layerObj, 'queryable'), source: new OlSourceImageWMS({ - url: getMapUrl, + url: isFunction(proxyFn) ? proxyFn(getMapUrl) : getMapUrl, attributions: wmsAttribution, params: { 'LAYERS': get(layerObj, 'Name'), diff --git a/src/CapabilitiesUtil/CapabilitiesUtil.spec.js b/src/CapabilitiesUtil/CapabilitiesUtil.spec.js index 15f74070e..94a68d0eb 100644 --- a/src/CapabilitiesUtil/CapabilitiesUtil.spec.js +++ b/src/CapabilitiesUtil/CapabilitiesUtil.spec.js @@ -176,6 +176,13 @@ describe('CapabilitiesUtil', () => { expect(layerSource.getParams()['LAYERS']).toBe(layerName); expect(layerSource.getParams()['VERSION']).toBe(capVersion); }); + + it('applies proxy function if provided', () => { + const proxyFn = jest.fn(); + CapabilitiesUtil.getLayersFromWmsCapabilties(capabilitiesObj, 'name', proxyFn); + expect.assertions(1); + expect(proxyFn).toBeCalledTimes(3); + }); }); }); });