Skip to content

Commit

Permalink
refactor: use restInfo handler directly
Browse files Browse the repository at this point in the history
  • Loading branch information
rgwozdz committed Apr 11, 2024
1 parent 6a808d7 commit eb4b156
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 108 deletions.
17 changes: 17 additions & 0 deletions packages/featureserver/src/helpers/combine-body-query-params.js
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 };
1 change: 1 addition & 0 deletions packages/featureserver/src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ module.exports = {
...require('./renderers'),
...require('./validate-inputs'),
...require('./normalize-request-params'),
...require('./combine-body-query-params')

Check failure on line 15 in packages/featureserver/src/helpers/index.js

View workflow job for this annotation

GitHub Actions / Install, lint, test, report coverage changes (18.x, ubuntu-latest)

Insert `,`
};
25 changes: 7 additions & 18 deletions packages/featureserver/src/helpers/normalize-request-params.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
const _ = require('lodash');
const defaults = require('../metadata-defaults');
const { combineBodyQueryParameters } = require('./combine-body-query-params');

function normalizeRequestParameters(
query,
body,
maxRecordCount = defaults.maxRecordCount(),
) {
const definedQueryParams = _.chain(query)
.pickBy(isNotEmptyString)
.mapValues(coerceStrings)
.value();

const definedBodyParams = _.chain(body)
.pickBy(isNotEmptyString)
.mapValues(coerceStrings)
.value();

const { resultRecordCount, ...params } = {
...definedQueryParams,
...definedBodyParams,
};
const requestParams = combineBodyQueryParameters(body, query);

const { resultRecordCount, ...params } = _.mapValues(
requestParams,
coerceStrings,
);

return {
...params,
resultRecordCount: resultRecordCount || maxRecordCount,
};
}

function isNotEmptyString(str) {
return !_.isString(str) || !_.isEmpty(str);
}

function coerceStrings(val) {
if (val === 'false') {
return false;
Expand Down
63 changes: 48 additions & 15 deletions packages/featureserver/src/rest-info-route-handler.js
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;
78 changes: 54 additions & 24 deletions packages/featureserver/src/rest-info-route-handler.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
const should = require('should'); // eslint-disable-line
const restInfo = require('./rest-info-route-handler');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const CURRENT_VERSION = 11.2;
const FULL_VERSION = '11.2.0';

describe('rest/info handler', () => {
const handlerSpy = sinon.spy();
const restInfo = proxyquire('./rest-info-route-handler', {
'./response-handlers': {
generalResponseHandler: handlerSpy,
},
});

afterEach(() => {
handlerSpy.resetHistory();
});

it('should return default info', () => {
const req = {
app: {
locals: {},
},
query: {},
body: {},
};
const result = restInfo(undefined, req);
result.should.deepEqual({
currentVersion: CURRENT_VERSION,
fullVersion: FULL_VERSION,
authInfo: {},
owningSystemUrl: undefined,
});

restInfo(req, {});
handlerSpy.callCount.should.equal(1);
handlerSpy.firstCall.args.should.deepEqual([
{},
{
currentVersion: CURRENT_VERSION,
fullVersion: FULL_VERSION,
authInfo: {},
owningSystemUrl: undefined,
},
{},
]);
});

it('should return default plus supplied info', () => {
Expand All @@ -29,13 +49,18 @@ describe('rest/info handler', () => {
locals: {},
},
};
const result = restInfo(data, req);
result.should.deepEqual({
currentVersion: CURRENT_VERSION,
fullVersion: FULL_VERSION,
authInfo: { foo: 'bar' },
owningSystemUrl: 'helloworld',
});
restInfo(req, {}, data);
handlerSpy.callCount.should.equal(1);
handlerSpy.firstCall.args.should.deepEqual([
{},
{
currentVersion: CURRENT_VERSION,
fullVersion: FULL_VERSION,
authInfo: { foo: 'bar' },
owningSystemUrl: 'helloworld',
},
{},
]);
});

it('should return versions from app.locals', () => {
Expand All @@ -51,17 +76,22 @@ describe('rest/info handler', () => {
},
},
};
const result = restInfo(
{ authInfo: { foo: 'bar' }, owningSystemUrl: 'helloworld' },

restInfo(
req,
{},
{ authInfo: { foo: 'bar' }, owningSystemUrl: 'helloworld' },
);
result.should.deepEqual({
currentVersion: 10.81,
fullVersion: '10.8.1',
authInfo: {
foo: 'bar',
handlerSpy.callCount.should.equal(1);
handlerSpy.firstCall.args.should.deepEqual([
{},
{
currentVersion: 10.81,
fullVersion: '10.8.1',
authInfo: { foo: 'bar' },
owningSystemUrl: 'helloworld',
},
owningSystemUrl: 'helloworld',
});
{},
]);
});
});
3 changes: 1 addition & 2 deletions packages/featureserver/src/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ module.exports = function route(req, res, geojson = {}) {
geojson.metadata = geojson.metadata || { maxRecordCount: 2000 };

if (isRestInfoRequest(route)) {
const result = restInfo(geojson, req);
return generalResponseHandler(res, result, req.query);
return restInfo(req, res, geojson);
}

if (isServerMetadataRequest(route)) {
Expand Down
19 changes: 7 additions & 12 deletions packages/featureserver/src/route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,15 @@ describe('Route module unit tests', () => {
);
restInfoSpy.calledOnce.should.equal(true);
restInfoSpy.firstCall.args.should.deepEqual([
{
metadata: { maxRecordCount: 2000 },
},
{
params: {},
query: { resultRecordCount: 2000 },
url: '/rest/info',
},
]);

responseHandlerSpy.calledOnce.should.equal(true);
responseHandlerSpy.firstCall.args.should.deepEqual([
{},
{ restInfo: true },
{ resultRecordCount: 2000 },
{
metadata: { maxRecordCount: 2000 },
},
]);
});

Expand All @@ -149,14 +143,15 @@ describe('Route module unit tests', () => {
);
restInfoSpy.calledOnce.should.equal(true);
restInfoSpy.firstCall.args.should.deepEqual([
{
metadata: { maxRecordCount: 2000 },
},
{
params: {},
query: { resultRecordCount: 2000 },
url: '/rest/info',
},
{},
{
metadata: { maxRecordCount: 2000 },
},
]);

responseHandlerSpy.calledOnce.should.equal(true);
Expand Down
24 changes: 0 additions & 24 deletions packages/featureserver/test/integration/info.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@ const _ = require('lodash');
const CURRENT_VERSION = 11.2;

describe('Info operations', () => {
describe('rest info', () => {
it('should conform to the prescribed schema', () => {
const req = {
app: {
locals: {},
},
};

const supplementalRestInfo = {
authInfo: {
isTokenBasedSecurity: true,
tokenServicesUrl: 'http://localhost/provider/generateToken',
},
};
const restInfo = FeatureServer.restInfo(supplementalRestInfo, req);
restInfo.should.have.property('currentVersion', CURRENT_VERSION);
restInfo.should.have.property('authInfo');
restInfo.authInfo.should.have.property('isTokenBasedSecurity', true);
restInfo.authInfo.should.have
.property('tokenServicesUrl')
.be.type('string');
});
});

describe('server info', () => {
it('should conform to the prescribed schema', () => {
const result = FeatureServer.serverInfo(data);
Expand Down
13 changes: 12 additions & 1 deletion packages/output-geoservices/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const FeatureServer = require('@koopjs/featureserver');
const {
restInfo,
// serverInfo,
// layerInfo,
// layersInfo,
// query,
} = require('@koopjs/featureserver');
const Logger = require('@koopjs/logger');
let logger = new Logger();
const ARCGIS_UNAUTHORIZED_MESSAGE = 'Item does not exist or is inaccessible.';
Expand Down Expand Up @@ -182,7 +189,11 @@ class GeoServices {
);
}

FeatureServer.route(req, res, {
/*
const result = restInfo(geojson, req);
return generalResponseHandler(res, result, req.query);
*/
restInfo(req, res, {
owningSystemUrl: this.#buildOwningSystemUrl(
req.headers.host,
req.baseUrl,
Expand Down
Loading

0 comments on commit eb4b156

Please sign in to comment.