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; |
@ -1,30 +1,33 @@ |
|||||||
var type_mapping = require('../helper/type_mapping'); |
var type_mapping = require('../helper/type_mapping'); |
||||||
|
var sanitizeAll = require('../sanitizer/sanitizeAll'); |
||||||
|
// middleware
|
||||||
|
module.exports.middleware = (_api_pelias_config) => { |
||||||
|
var sanitizers = { |
||||||
|
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
||||||
|
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), |
||||||
|
text: require('../sanitizer/_text'), |
||||||
|
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), |
||||||
|
city_name_standardizer: require('../sanitizer/_city_name_standardizer'), |
||||||
|
size: require('../sanitizer/_size')(/* use defaults*/), |
||||||
|
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
||||||
|
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
||||||
|
// depends on the layers and sources sanitizers, must be run after them
|
||||||
|
sources_and_layers: require('../sanitizer/_sources_and_layers'), |
||||||
|
private: require('../sanitizer/_flag_bool')('private', false), |
||||||
|
location_bias: require('../sanitizer/_location_bias')(_api_pelias_config.defaultParameters), |
||||||
|
geo_search: require('../sanitizer/_geo_search'), |
||||||
|
boundary_country: require('../sanitizer/_boundary_country'), |
||||||
|
categories: require('../sanitizer/_categories'), |
||||||
|
// this can go away once geonames has been abrogated
|
||||||
|
geonames_warnings: require('../sanitizer/_geonames_warnings') |
||||||
|
}; |
||||||
|
|
||||||
var sanitizeAll = require('../sanitizer/sanitizeAll'), |
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
||||||
sanitizers = { |
|
||||||
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
|
||||||
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), |
|
||||||
text: require('../sanitizer/_text'), |
|
||||||
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), |
|
||||||
city_name_standardizer: require('../sanitizer/_city_name_standardizer'), |
|
||||||
size: require('../sanitizer/_size')(/* use defaults*/), |
|
||||||
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
|
||||||
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
|
||||||
// depends on the layers and sources sanitizers, must be run after them
|
|
||||||
sources_and_layers: require('../sanitizer/_sources_and_layers'), |
|
||||||
private: require('../sanitizer/_flag_bool')('private', false), |
|
||||||
geo_search: require('../sanitizer/_geo_search'), |
|
||||||
boundary_country: require('../sanitizer/_boundary_country'), |
|
||||||
categories: require('../sanitizer/_categories'), |
|
||||||
// this can go away once geonames has been abrogated
|
|
||||||
geonames_warnings: require('../sanitizer/_geonames_warnings') |
|
||||||
}; |
|
||||||
|
|
||||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
return function( req, res, next ){ |
||||||
|
sanitize( req, function( err, clean ){ |
||||||
|
next(); |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
// middleware
|
|
||||||
module.exports.middleware = function( req, res, next ){ |
|
||||||
sanitize( req, function( err, clean ){ |
|
||||||
next(); |
|
||||||
}); |
|
||||||
}; |
}; |
||||||
|
@ -1,28 +1,30 @@ |
|||||||
var type_mapping = require('../helper/type_mapping'); |
var type_mapping = require('../helper/type_mapping'); |
||||||
|
var sanitizeAll = require('../sanitizer/sanitizeAll'); |
||||||
var sanitizeAll = require('../sanitizer/sanitizeAll'), |
|
||||||
sanitizers = { |
|
||||||
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
|
||||||
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), |
|
||||||
synthesize_analysis: require('../sanitizer/_synthesize_analysis'), |
|
||||||
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), |
|
||||||
city_name_standardizer: require('../sanitizer/_city_name_standardizer'), |
|
||||||
size: require('../sanitizer/_size')(/* use defaults*/), |
|
||||||
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
|
||||||
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
|
||||||
// depends on the layers and sources sanitizers, must be run after them
|
|
||||||
sources_and_layers: require('../sanitizer/_sources_and_layers'), |
|
||||||
private: require('../sanitizer/_flag_bool')('private', false), |
|
||||||
geo_search: require('../sanitizer/_geo_search'), |
|
||||||
boundary_country: require('../sanitizer/_boundary_country'), |
|
||||||
categories: require('../sanitizer/_categories') |
|
||||||
}; |
|
||||||
|
|
||||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
|
||||||
|
|
||||||
// middleware
|
// middleware
|
||||||
module.exports.middleware = function( req, res, next ){ |
module.exports.middleware = (_api_pelias_config) => { |
||||||
sanitize( req, function( err, clean ){ |
var sanitizers = { |
||||||
next(); |
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), |
||||||
}); |
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), |
||||||
|
synthesize_analysis: require('../sanitizer/_synthesize_analysis'), |
||||||
|
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), |
||||||
|
city_name_standardizer: require('../sanitizer/_city_name_standardizer'), |
||||||
|
size: require('../sanitizer/_size')(/* use defaults*/), |
||||||
|
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), |
||||||
|
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), |
||||||
|
// depends on the layers and sources sanitizers, must be run after them
|
||||||
|
sources_and_layers: require('../sanitizer/_sources_and_layers'), |
||||||
|
private: require('../sanitizer/_flag_bool')('private', false), |
||||||
|
location_bias: require('../sanitizer/_location_bias')(_api_pelias_config.defaultParameters), |
||||||
|
geo_search: require('../sanitizer/_geo_search'), |
||||||
|
boundary_country: require('../sanitizer/_boundary_country'), |
||||||
|
categories: require('../sanitizer/_categories') |
||||||
|
}; |
||||||
|
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
||||||
|
|
||||||
|
return function( req, res, next ){ |
||||||
|
sanitize( req, function( err, clean ){ |
||||||
|
next(); |
||||||
|
}); |
||||||
|
}; |
||||||
}; |
}; |
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -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