stations route

This commit is contained in:
Jannis R 2017-11-10 00:06:15 +01:00
parent 9c1d930a6a
commit 99f15760a8
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
3 changed files with 52 additions and 1 deletions

2
api.js
View file

@ -11,6 +11,7 @@ const nocache = require('nocache')
const pkg = require('./package.json') const pkg = require('./package.json')
const departures = require('./lib/departures') const departures = require('./lib/departures')
const journeys = require('./lib/journeys') const journeys = require('./lib/journeys')
const stations = require('./lib/stations')
const allStations = require('./lib/all-stations') const allStations = require('./lib/all-stations')
@ -40,6 +41,7 @@ const noCache = nocache()
api.get('/stations/:id/departures', noCache, departures) api.get('/stations/:id/departures', noCache, departures)
api.get('/journeys', noCache, journeys) api.get('/journeys', noCache, journeys)
api.get('/stations', stations)
api.get('/stations/all', allStations) api.get('/stations/all', allStations)

47
lib/stations.js Normal file
View file

@ -0,0 +1,47 @@
'use strict'
const autocomplete = require('db-stations-autocomplete')
const stations = require('db-stations')
const parse = require('cli-native').to
const createFilter = require('db-stations/create-filter')
const ndjson = require('ndjson')
const err400 = (msg) => {
const err = new Error(msg)
err.statusCode = 400
return err
}
const complete = (req, res, next) => {
const fuzzy = req.query.fuzzy === 'true'
const completion = req.query.completion !== 'false'
res.json(autocomplete(req.query.query, fuzzy, completion))
next()
}
const filter = (req, res, next) => {
if (Object.keys(req.query).length === 0) {
return next(err400('Missing properties.'))
}
const selector = Object.create(null)
for (let prop in req.query) selector[prop] = parse(req.query[prop])
const filter = createFilter(selector)
stations.full()
.on('error', next)
.pipe(filter)
.on('error', next)
.pipe(ndjson.stringify())
.on('error', next)
.pipe(res)
.once('finish', () => next())
}
const route = (req, res, next) => {
if (req.query.query) complete(req, res, next)
else filter(req, res, next)
}
module.exports = route

View file

@ -31,11 +31,13 @@
"compression": "^1.7.1", "compression": "^1.7.1",
"corser": "^2.0.1", "corser": "^2.0.1",
"db-hafas": "^1.1.0", "db-hafas": "^1.1.0",
"db-stations": "^1.22.0", "db-stations": "^1.24.0",
"db-stations-autocomplete": "^1.0.0",
"express": "^4.16.2", "express": "^4.16.2",
"hafas-departures-in-direction": "^0.1.0", "hafas-departures-in-direction": "^0.1.0",
"hsts": "^2.1.0", "hsts": "^2.1.0",
"morgan": "^1.9.0", "morgan": "^1.9.0",
"ndjson": "^1.5.0",
"nocache": "^2.0.0", "nocache": "^2.0.0",
"parse-messy-time": "^2.1.0", "parse-messy-time": "^2.1.0",
"shorthash": "^0.0.2" "shorthash": "^0.0.2"