stations route
This commit is contained in:
parent
9c1d930a6a
commit
99f15760a8
3 changed files with 52 additions and 1 deletions
2
api.js
2
api.js
|
@ -11,6 +11,7 @@ const nocache = require('nocache')
|
|||
const pkg = require('./package.json')
|
||||
const departures = require('./lib/departures')
|
||||
const journeys = require('./lib/journeys')
|
||||
const stations = require('./lib/stations')
|
||||
const allStations = require('./lib/all-stations')
|
||||
|
||||
|
||||
|
@ -40,6 +41,7 @@ const noCache = nocache()
|
|||
|
||||
api.get('/stations/:id/departures', noCache, departures)
|
||||
api.get('/journeys', noCache, journeys)
|
||||
api.get('/stations', stations)
|
||||
api.get('/stations/all', allStations)
|
||||
|
||||
|
||||
|
|
47
lib/stations.js
Normal file
47
lib/stations.js
Normal 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
|
|
@ -31,11 +31,13 @@
|
|||
"compression": "^1.7.1",
|
||||
"corser": "^2.0.1",
|
||||
"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",
|
||||
"hafas-departures-in-direction": "^0.1.0",
|
||||
"hsts": "^2.1.0",
|
||||
"morgan": "^1.9.0",
|
||||
"ndjson": "^1.5.0",
|
||||
"nocache": "^2.0.0",
|
||||
"parse-messy-time": "^2.1.0",
|
||||
"shorthash": "^0.0.2"
|
||||
|
|
Loading…
Reference in a new issue