-
Notifications
You must be signed in to change notification settings - Fork 128
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
- Loading branch information
Showing
11 changed files
with
201 additions
and
108 deletions.
There are no files selected for viewing
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 { | ||
...definedQueryParams, | ||
...definedBodyParams, | ||
}; | ||
} | ||
|
||
function isNotEmptyString(str) { | ||
return !_.isString(str) || !_.isEmpty(str); | ||
} | ||
|
||
module.exports = { combineBodyQueryParameters }; |
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
25 changes: 7 additions & 18 deletions
25
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; |
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
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
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
Oops, something went wrong.