Skip to content

Commit

Permalink
Set default descriptions via app.locals (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgwozdz authored Jun 13, 2022
1 parent 225f672 commit 457fbb2
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased
### Added
- override default currentVersion and fullVersion via app.locals.config.featureServer
- override default service-description and layer-description via app.locals.config.featureServer

## [3.5.1] - 06-01-2022
### Changed
Expand Down
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@ routes.forEach(route => {
})
```

### Version Configuration
By default, the service and layer metadata endpoints will respond with the following version attributes:
```json
currentVersion: 10.51,
fullVersion: '10.5.1'
### Setting defaults at runtime
FeatureServer allows several defaults to be set at runtime via Express's `app.locals` method. Specifically, you will need to set:

```js
app.locals.config = {
featureServer: {
// define default here
}
}
```

You can alter these defaults by setting properties on Express's `app.locals`:
If you are using FeatureServer as part of a Koop instance, the equivalent of Express's `app.locals` is `koop.server.locals`.

The follow properties can be set at runtime with the noted method:

```js
app.locals.config = {
featureServer: {
currentVersion: 10.81,
fullVersion: '10.8.1'
currentVersion: 11.01, // defaults to 10.51
fullVersion: '11.0.1', // defaults to '10.5.1'
serviceDescription: 'default service description',
description: 'default layer description'
}
}
```
Note, if you are using FeatureServer as part of the Koop platform, the equivalent of Express's `app.locals` is `koop.server.locals`.


## API
* [FeatureServer.route](#FeatureServer.route)
Expand Down
18 changes: 12 additions & 6 deletions lib/helpers/table-layer-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@ class TableLayerMetadata {
} = geojson

const {
params: { layer: layerId } = {},
params: {
layer: layerId
} = {},
query = {}
} = req

const currentVersion = _.get(req, 'app.locals.config.featureServer.currentVersion', CURRENT_VERSION)
const fullVersion = _.get(req, 'app.locals.config.featureServer.fullVersion', FULL_VERSION)
const {
currentVersion,
fullVersion,
description
} = _.get(req, 'app.locals.config.featureServer', {})

const normalizedOptions = {
layerId,
const normalizedOptions = _.pickBy({
currentVersion,
fullVersion,
description,
layerId,
...query,
...metadata,
capabilities: normalizeCapabilities(capabilities, metadata.capabilities)
}
}, (value) => value)

if (!normalizedGeojson.features) {
normalizedGeojson.features = []
Expand Down
21 changes: 15 additions & 6 deletions lib/server-info-route-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,25 @@ function serverMetadata (json, req = {}) {
const {
query = {}
} = req
const currentVersion = _.get(req, 'app.locals.config.featureServer.currentVersion', CURRENT_VERSION)
const fullVersion = _.get(req, 'app.locals.config.featureServer.fullVersion', FULL_VERSION)

const { extent, metadata, ...rest } = json
const { maxRecordCount, hasStaticData, description, copyrightText } = { ...metadata, ...rest }
const { extent, metadata = {}, ...rest } = json
const {
maxRecordCount,
hasStaticData,
copyrightText,
description: providerLayerDescription,
serviceDescription: providerServiceDescription
} = { ...metadata, ...rest }
const spatialReference = getSpatialReference(json, query)
const { layers, tables, relationships } = normalizeInputData(json)
// TODO reproject default extents when non WGS84 CRS is found or passed
const fullExtent = getServiceExtent({ extent, metadata, layers, spatialReference })

// TODO reproject default extents when non WGS84 CRS is found or passed
const {
currentVersion = CURRENT_VERSION,
fullVersion = FULL_VERSION,
serviceDescription
} = _.get(req, 'app.locals.config.featureServer', {})

return _.defaults({
currentVersion,
Expand All @@ -41,7 +50,7 @@ function serverMetadata (json, req = {}) {
}),
relationships: relationships.map(relationshipInfo),
supportsRelationshipsResource: relationships && relationships.length > 0,
serviceDescription: description,
serviceDescription: serviceDescription || providerServiceDescription || providerLayerDescription,
copyrightText: copyrightText,
maxRecordCount: maxRecordCount || _.get(layers, '[0].metadata.maxRecordCount'),
hasStaticData: typeof hasStaticData === 'boolean' ? hasStaticData : false
Expand Down
60 changes: 45 additions & 15 deletions test/unit/helpers/table-layer-metadata.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,36 +281,55 @@ describe('TableLayerMetadata', () => {
geojson.should.deepEqual({ features: ['feature'] })
options.should.deepEqual({
capabilities: {},
layerId: '99',
currentVersion: CURRENT_VERSION,
fullVersion: FULL_VERSION
layerId: '99'
})
})

it('static method "normalizeInput" should create expected geojson and options', () => {
it('static method "normalizeInput" should merge capabilities', () => {
const { geojson, options } = TableLayerMetadata.normalizeInput({
features: ['feature'],
metadata: {
foo: 'bar',
capabilities: 'list,of,stuff'
},
capabilities: {
world: 'hellow'
}
}, {
params: { layer: '99' },
app: { locals: { config: { featureServer: { currentVersion: 90.99, fullVersion: '90.9.9' } } } }
params: { layer: '99' }
})
geojson.should.deepEqual({ features: ['feature'] })
options.should.deepEqual({
foo: 'bar',
layerId: '99',
capabilities: {
list: 'list,of,stuff',
world: 'hellow'
},
currentVersion: 90.99,
fullVersion: '90.9.9'
}
})
})

it('static method "normalizeInput" should defer to metadata description', () => {
const { geojson, options } = TableLayerMetadata.normalizeInput({
features: ['feature'],
metadata: {
description: 'Metadata description'
}
}, {
params: { layer: '99' },
app: {
locals: {
config: {
featureServer: {
description: 'Overrides default layer description.'
}
}
}
}
})
geojson.should.deepEqual({ features: ['feature'] })
options.should.deepEqual({
layerId: '99',
description: 'Metadata description',
capabilities: {}
})
})

Expand All @@ -326,17 +345,28 @@ describe('TableLayerMetadata', () => {
world: 'hellow'
}
}, {

params: { layer: '99' }
params: { layer: '99' },
app: {
locals: {
config: {
featureServer: {
currentVersion: 90.99,
fullVersion: '90.9.9',
description: 'Overrides default layer description.'
}
}
}
}
})
tableLayerMetadata.should.deepEqual({
...defaultFixture,
capabilities: 'list,of,stuff',
displayField: 'myField',
fields: ['fields'],
id: 99,
currentVersion: CURRENT_VERSION,
fullVersion: FULL_VERSION
currentVersion: 90.99,
fullVersion: '90.9.9',
description: 'Overrides default layer description.'
})
})
})
10 changes: 7 additions & 3 deletions test/unit/server-info-route-handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ describe('server info', () => {
})
})

it('should use req.app.locals.config.featureServer for version', () => {
it('should use req.app.locals.config.featureServer for version and serviceDescription', () => {
const simpleCollectionFixture = {
type: 'FeatureCollection',
crs: {
Expand Down Expand Up @@ -782,7 +782,11 @@ describe('server info', () => {
app: {
locals: {
config: {
featureServer: { currentVersion: 11.01, fullVersion: '11.0.1' }
featureServer: {
currentVersion: 11.01,
fullVersion: '11.0.1',
serviceDescription: 'Overrides default service description'
}
}
}
}
Expand All @@ -803,7 +807,7 @@ describe('server info', () => {
serverInfo.should.deepEqual({
foo: 'bar',
maxRecordCount: 'max-record-count',
serviceDescription: 'service-description',
serviceDescription: 'Overrides default service description',
copyrightText: 'copyright-text',
hasStaticData: false,
supportsRelationshipsResource: false,
Expand Down

0 comments on commit 457fbb2

Please sign in to comment.