mirror of https://github.com/pelias/api.git
Diana Shkolnikov
7 years ago
committed by
GitHub
49 changed files with 2440 additions and 2214 deletions
@ -1,34 +0,0 @@
|
||||
var proxy = require('express-http-proxy'); |
||||
|
||||
function addRoutes(app, peliasConfig) { |
||||
var sendToLegacy; |
||||
|
||||
if (!peliasConfig.hasOwnProperty('legacyUrl')) { |
||||
sendToLegacy = function redirectToV1(req, res, next) { |
||||
res.redirect(301, '/v1'); |
||||
}; |
||||
} |
||||
else { |
||||
sendToLegacy = proxy(peliasConfig.legacyUrl); |
||||
} |
||||
|
||||
// api root
|
||||
app.get( '/', sendToLegacy ); |
||||
|
||||
// place API
|
||||
app.get( '/place', sendToLegacy); |
||||
|
||||
// suggest APIs
|
||||
app.get( '/suggest', sendToLegacy ); |
||||
app.get( '/suggest/nearby', sendToLegacy ); |
||||
app.get( '/suggest/coarse',sendToLegacy ); |
||||
|
||||
// search APIs
|
||||
app.get( '/search', sendToLegacy); |
||||
app.get( '/search/coarse', sendToLegacy); |
||||
|
||||
// reverse API
|
||||
app.get( '/reverse', sendToLegacy ); |
||||
} |
||||
|
||||
module.exports.addRoutes = addRoutes; |
@ -1,32 +0,0 @@
|
||||
|
||||
var _ = require('lodash'), |
||||
check = require('check-types'); |
||||
|
||||
var DEFAULT_DETAILS_BOOL = true; |
||||
|
||||
// validate inputs, convert types and apply defaults
|
||||
function sanitize( raw, clean ){ |
||||
|
||||
// error & warning messages
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
if( !check.undefined( raw.details ) ){ |
||||
clean.details = isTruthy( raw.details ); |
||||
} else { |
||||
clean.details = DEFAULT_DETAILS_BOOL; |
||||
} |
||||
|
||||
return messages; |
||||
} |
||||
|
||||
// be lenient with 'truthy' values
|
||||
function isTruthy(val) { |
||||
if( check.string( val ) ){ |
||||
return _.includes( ['true', '1'], val ); |
||||
} |
||||
|
||||
return val === 1 || val === true; |
||||
} |
||||
|
||||
// export function
|
||||
module.exports = sanitize; |
@ -0,0 +1,31 @@
|
||||
const _ = require('lodash'); |
||||
/** |
||||
Set a focus.lat and focus.lon if specified in pelias config |
||||
* @param {object} defaultParameters property of pelias config |
||||
*/ |
||||
|
||||
function setup(defaultParameters){ |
||||
|
||||
return function setLocationBias(raw, clean){ |
||||
/* |
||||
check that: |
||||
1. {object} raw exists |
||||
2. pelias-config included the properties focus.point.lat and focus.point.lon |
||||
3. raw.focus.point.lon and raw.focus.point.lat have not been set |
||||
*/ |
||||
if (!_.isUndefined(raw) && |
||||
!_.isUndefined(defaultParameters['focus.point.lat']) && |
||||
!_.isUndefined(defaultParameters['focus.point.lon']) && |
||||
!_.has(raw, 'focus.point.lon') && |
||||
!_.has(raw, 'focus.point.lat') ) { |
||||
|
||||
raw['focus.point.lat'] = defaultParameters['focus.point.lat']; |
||||
raw['focus.point.lon'] = defaultParameters['focus.point.lon']; |
||||
} |
||||
|
||||
return { errors: [], warnings: [] }; |
||||
}; |
||||
} |
||||
|
||||
// if focus.point.lat and focus.point.lon already exists, don't change
|
||||
module.exports = setup; |
@ -0,0 +1,109 @@
|
||||
const setup = require('../../../sanitizer/_location_bias'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.setLocationBias = function(test, common) { |
||||
test('set focus point', t => { |
||||
const defaultParameters = { // specify focus point latitude and longitude
|
||||
'focus.point.lat': 12.12121212, |
||||
'focus.point.lon': 21.21212121 |
||||
}; |
||||
const locationBias = setup(defaultParameters); |
||||
const raw = {}; |
||||
const expected = { |
||||
'focus.point.lat': 12.12121212, |
||||
'focus.point.lon': 21.21212121 |
||||
}; |
||||
|
||||
locationBias(raw, undefined); |
||||
t.deepEqual(raw, expected, 'focus point should be set'); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('undefined raw', t => { |
||||
const defaultParameters = { |
||||
'focus.point.lat': 12.12121212, |
||||
'focus.point.lon': 21.21212121 |
||||
}; |
||||
const locationBias = setup(defaultParameters); |
||||
|
||||
locationBias(undefined, undefined); |
||||
t.deepEqual(undefined, undefined, 'should be unmodified' ); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('focusPointLat is undefined', t => { |
||||
const defaultParameters = { |
||||
'focus.point.lon': 12.2121212 |
||||
}; |
||||
const locationBias = setup(defaultParameters); |
||||
const raw = {}; |
||||
const expected = {}; |
||||
|
||||
locationBias(raw, undefined); |
||||
t.deepEqual(raw, expected, 'should be unmodified' ); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('focusPointLon is undefined', t => { |
||||
const defaultParameters = { |
||||
'focus.point.lat': 12.2121212 |
||||
}; |
||||
const locationBias = setup(defaultParameters); |
||||
const raw = {}; |
||||
const expected = {}; |
||||
|
||||
locationBias(raw, undefined); |
||||
t.deepEqual(raw, expected, 'should be unmodified' ); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('raw has focus.point.lon already', t => { |
||||
const defaultParameters = { |
||||
'focus.point.lon': 12.2121212, |
||||
'focus.point.lat': 12.2121212 |
||||
}; |
||||
const locationBias = setup(defaultParameters); |
||||
const raw = { |
||||
'focus.point.lon': 43.4343434 |
||||
}; |
||||
const expected = { |
||||
'focus.point.lon': 43.4343434 |
||||
}; |
||||
|
||||
locationBias(raw, undefined); |
||||
t.deepEqual(raw, expected, 'should be unmodified' ); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('raw has focus.point.lat already', t => { |
||||
const defaultParameters = { |
||||
'focus.point.lon': 12.2121212, |
||||
'focus.point.lat': 12.2121212 |
||||
}; |
||||
const locationBias = setup(defaultParameters); |
||||
const raw = { |
||||
'focus.point.lat': 34.3434343 |
||||
}; |
||||
const expected = { |
||||
'focus.point.lat': 34.3434343 |
||||
}; |
||||
|
||||
locationBias(raw, undefined); |
||||
t.deepEqual(raw, expected, 'should be unmodified' ); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = (tape, common) => { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape(`SANITIZE _location_bias: ${name}`, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue