diff --git a/lib/station.js b/lib/station.js index 38fb82d..354fdc4 100644 --- a/lib/station.js +++ b/lib/station.js @@ -21,7 +21,7 @@ const route = (req, res, next) => { stream.removeListener('data', onStation) res.json(station) - next() + next('/station/:id') } stream.on('data', onStation) diff --git a/lib/stations.js b/lib/stations.js index 6d1d5fe..ceebdfc 100644 --- a/lib/stations.js +++ b/lib/stations.js @@ -1,12 +1,13 @@ 'use strict' const autocomplete = require('db-stations-autocomplete') -const stations = require('db-stations') +const allStations = require('db-stations/full.json') const parse = require('cli-native').to const createFilter = require('db-stations/create-filter') -const filterStream = require('stream-filter') const ndjson = require('ndjson') +const hasProp = (o, k) => Object.prototype.hasOwnProperty.call(o, k) + const err400 = (msg) => { const err = new Error(msg) err.statusCode = 400 @@ -14,10 +15,21 @@ const err400 = (msg) => { } const complete = (req, res, next) => { - const fuzzy = req.query.fuzzy === 'true' - const completion = req.query.completion !== 'false' + const limit = req.query.results && parseInt(req.query.results) || 3 + const fuzzy = parse(req.query.fuzzy) === true + const completion = parse(req.query.completion) !== false + const results = autocomplete(req.query.query, limit, fuzzy, completion) - res.json(autocomplete(req.query.query, fuzzy, completion)) + const data = [] + for (let result of results) { + // todo: make this more efficient + const station = allStations.find(s => s.id === result.id) + if (!station) continue + + data.push(Object.assign(result, station)) + } + + res.json(data) next() } @@ -27,17 +39,25 @@ const filter = (req, res, next) => { } const selector = Object.create(null) - for (let prop in req.query) selector[prop] = parse(req.query[prop]) + for (let prop in req.query) { + if (prop.slice(0, 12) === 'coordinates.') { // derhuerst/db-rest#2 + prop = 'location.' + prop.slice(12) + } + selector[prop] = parse(req.query[prop]) + } const filter = createFilter(selector) - stations.full() - .on('error', next) - .pipe(filterStream.obj(filter)) - .on('error', next) - .pipe(ndjson.stringify()) - .on('error', next) + res.type('application/x-ndjson') + const out = ndjson.stringify() + out + .once('error', next) .pipe(res) .once('finish', () => next()) + + for (let station of allStations) { + if (filter(station)) out.write(station) + } + out.end() } const route = (req, res, next) => { diff --git a/package.json b/package.json index 63a161a..5cb680d 100644 --- a/package.json +++ b/package.json @@ -28,13 +28,12 @@ }, "dependencies": { "cli-native": "^1.0.0", - "db-hafas": "^2.0.0-alpha.2", - "db-stations": "^1.24.0", - "db-stations-autocomplete": "^1.0.0", - "hafas-rest-api": "0.1.0-alpha.10", + "db-hafas": "^2.1.0", + "db-stations": "^2.2.0", + "db-stations-autocomplete": "^2.0.1", + "hafas-rest-api": "^0.3.3", "hsts": "^2.1.0", - "ndjson": "^1.5.0", - "stream-filter": "^2.1.0" + "ndjson": "^1.5.0" }, "scripts": { "start": "node index.js"