Skip to content

Commit

Permalink
Merge pull request #84 from annarieger/proxyfn-for-capabilities
Browse files Browse the repository at this point in the history
Apply optional `proxyFn` by parsing of layers from GetCapabilities document.
  • Loading branch information
annarieger authored Feb 27, 2019
2 parents d80e294 + a15de7f commit a25eb6e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions src/CapabilitiesUtil/CapabilitiesUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand All @@ -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'),
Expand Down
7 changes: 7 additions & 0 deletions src/CapabilitiesUtil/CapabilitiesUtil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

0 comments on commit a25eb6e

Please sign in to comment.