-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: use restInfo handler directly in output-geoservice (#967)
* refactor: use restInfo handler directly
- Loading branch information
Showing
17 changed files
with
1,167 additions
and
123 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env sh | ||
echo "\nRunning code linting...\n" | ||
npm run lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions
17
packages/featureserver/src/helpers/combine-body-query-params.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const _ = require('lodash'); | ||
|
||
function combineBodyQueryParameters(body, query) { | ||
const definedQueryParams = _.pickBy(query, isNotEmptyString); | ||
const definedBodyParams = _.pickBy(body, isNotEmptyString); | ||
|
||
return { | ||
...definedBodyParams, | ||
...definedQueryParams, | ||
}; | ||
} | ||
|
||
function isNotEmptyString(str) { | ||
return !_.isString(str) || !_.isEmpty(str); | ||
} | ||
|
||
module.exports = { combineBodyQueryParameters }; |
25 changes: 25 additions & 0 deletions
25
packages/featureserver/src/helpers/combine-body-query-params.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const should = require('should'); // eslint-disable-line | ||
const { combineBodyQueryParameters } = require('./combine-body-query-params'); | ||
|
||
describe('combineBodyQueryParameters', () => { | ||
it('should merge objects with deference to query', () => { | ||
combineBodyQueryParameters( | ||
{ foo: 'bar', hello: 'body-param' }, | ||
{ foo: 'baz', test: 'query-param' }, | ||
).should.deepEqual({ | ||
foo: 'baz', | ||
test: 'query-param', | ||
hello: 'body-param', | ||
}); | ||
}); | ||
|
||
it('should merge objects with deference to query, strip empty strings', () => { | ||
combineBodyQueryParameters( | ||
{ foo: 'bar', hello: '' }, | ||
{ foo: 'baz', test: 'query-param' }, | ||
).should.deepEqual({ | ||
foo: 'baz', | ||
test: 'query-param', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 11 additions & 15 deletions
26
packages/featureserver/src/helpers/normalize-request-params.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,60 @@ | ||
const _ = require('lodash'); | ||
const defaults = require('./metadata-defaults'); | ||
const joi = require('joi'); | ||
const metadataDefaults = require('./metadata-defaults'); | ||
const { generalResponseHandler } = require('./response-handlers'); | ||
const { combineBodyQueryParameters } = require('./helpers'); | ||
|
||
function restInfo(data = {}, req) { | ||
const versionDefaults = defaults.restInfoDefaults(); | ||
const parameterSchema = joi | ||
.object({ | ||
f: joi.string().valid('json', 'pjson').default('json'), | ||
}) | ||
.unknown(); | ||
|
||
function restInfo(req, res, data = {}) { | ||
const { currentVersion, fullVersion } = getVersions(req.app.locals); | ||
|
||
const requestParams = combineBodyQueryParameters(req.body, req.query); | ||
|
||
validate(requestParams); | ||
|
||
return generalResponseHandler( | ||
res, | ||
{ | ||
currentVersion, | ||
fullVersion, | ||
owningSystemUrl: data.owningSystemUrl, | ||
authInfo: { | ||
...data.authInfo, | ||
}, | ||
}, | ||
requestParams, | ||
); | ||
} | ||
|
||
function getVersions(locals) { | ||
const versionDefaults = metadataDefaults.restInfoDefaults(); | ||
const currentVersion = _.get( | ||
req, | ||
'app.locals.config.featureServer.currentVersion', | ||
locals, | ||
'config.featureServer.currentVersion', | ||
versionDefaults.currentVersion, | ||
); | ||
|
||
const fullVersion = _.get( | ||
req, | ||
'app.locals.config.featureServer.fullVersion', | ||
locals, | ||
'config.featureServer.fullVersion', | ||
versionDefaults.fullVersion, | ||
); | ||
return { currentVersion, fullVersion }; | ||
} | ||
|
||
return { | ||
currentVersion, | ||
fullVersion, | ||
owningSystemUrl: data.owningSystemUrl, | ||
authInfo: { | ||
...data.authInfo, | ||
}, | ||
}; | ||
function validate(parameters) { | ||
const { error } = parameterSchema.validate(parameters); | ||
|
||
if (error) { | ||
const err = new Error('Invalid format'); | ||
err.code = 400; | ||
throw err; | ||
} | ||
} | ||
|
||
module.exports = restInfo; |
Oops, something went wrong.