convert to ESM 💥
This commit is contained in:
parent
fe469cd658
commit
6f2e5f134b
11 changed files with 67 additions and 62 deletions
|
@ -1,12 +1,11 @@
|
||||||
{
|
{
|
||||||
"extends": "eslint:recommended",
|
"extends": "eslint:recommended",
|
||||||
"env": {
|
"env": {
|
||||||
"commonjs": true,
|
"es2022": true,
|
||||||
"es6": true,
|
|
||||||
"node": true
|
"node": true
|
||||||
},
|
},
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
"ecmaVersion": 2018
|
"sourceType": "module"
|
||||||
},
|
},
|
||||||
"ignorePatterns": [
|
"ignorePatterns": [
|
||||||
"node_modules"
|
"node_modules"
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
'use strict'
|
import generateApiDocs from 'hafas-rest-api/tools/generate-docs.js'
|
||||||
|
import {api} from './api.js'
|
||||||
const generateApiDocs = require('hafas-rest-api/tools/generate-docs')
|
|
||||||
const {api} = require('./api')
|
|
||||||
|
|
||||||
const HEAD = `\
|
const HEAD = `\
|
||||||
# \`v6.db.transport.rest\` API documentation
|
# \`v6.db.transport.rest\` API documentation
|
||||||
|
|
34
api.js
34
api.js
|
@ -1,19 +1,25 @@
|
||||||
'use strict'
|
// todo: use import assertions once they're supported by Node.js & ESLint
|
||||||
|
// https://github.com/tc39/proposal-import-assertions
|
||||||
|
import {createRequire} from 'node:module'
|
||||||
|
const require = createRequire(import.meta.url)
|
||||||
|
|
||||||
|
import {dirname, join as pathJoin} from 'node:path'
|
||||||
|
import {fileURLToPath} from 'node:url'
|
||||||
|
import createHafas from 'db-hafas'
|
||||||
|
import createApi from 'hafas-rest-api'
|
||||||
|
import createHealthCheck from 'hafas-client-health-check'
|
||||||
|
import Redis from 'ioredis'
|
||||||
|
import withCache from 'cached-hafas-client'
|
||||||
|
import redisStore from 'cached-hafas-client/stores/redis.js'
|
||||||
|
import serveStatic from 'serve-static'
|
||||||
|
import {parseBoolean} from 'hafas-rest-api/lib/parse.js'
|
||||||
|
import {loyaltyCardParser} from './lib/loyalty-cards.js'
|
||||||
|
import {route as stations} from './routes/stations.js'
|
||||||
|
import {route as station} from './routes/station.js'
|
||||||
|
|
||||||
const createHafas = require('db-hafas')
|
|
||||||
const createApi = require('hafas-rest-api')
|
|
||||||
const createHealthCheck = require('hafas-client-health-check')
|
|
||||||
const Redis = require('ioredis')
|
|
||||||
const withCache = require('cached-hafas-client')
|
|
||||||
const redisStore = require('cached-hafas-client/stores/redis')
|
|
||||||
const {join: pathJoin} = require('path')
|
|
||||||
const serveStatic = require('serve-static')
|
|
||||||
const {parseBoolean} = require('hafas-rest-api/lib/parse')
|
|
||||||
const pkg = require('./package.json')
|
const pkg = require('./package.json')
|
||||||
const {loyaltyCardParser} = require('./lib/loyalty-cards')
|
|
||||||
const stations = require('./routes/stations')
|
|
||||||
const station = require('./routes/station')
|
|
||||||
|
|
||||||
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||||
const docsRoot = pathJoin(__dirname, 'docs')
|
const docsRoot = pathJoin(__dirname, 'docs')
|
||||||
|
|
||||||
const berlinHbf = '8011160'
|
const berlinHbf = '8011160'
|
||||||
|
@ -87,7 +93,7 @@ const api = createApi(hafas, config, (api) => {
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = {
|
export {
|
||||||
hafas,
|
hafas,
|
||||||
config,
|
config,
|
||||||
api,
|
api,
|
||||||
|
|
4
index.js
4
index.js
|
@ -1,6 +1,4 @@
|
||||||
'use strict'
|
import {api, config} from './api.js'
|
||||||
|
|
||||||
const {api, config} = require('./api')
|
|
||||||
|
|
||||||
api.listen(config.port, (err) => {
|
api.listen(config.port, (err) => {
|
||||||
const {logger} = api.locals
|
const {logger} = api.locals
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
'use strict'
|
import {createRequire} from 'node:module'
|
||||||
|
const require = createRequire(import.meta.url)
|
||||||
|
|
||||||
const {statSync} = require('fs')
|
import {statSync} from 'node:fs'
|
||||||
const {full: readRawStations} = require('db-stations')
|
import _dbStations from 'db-stations'
|
||||||
|
const {full: readRawStations} = _dbStations
|
||||||
|
|
||||||
// We don't have access to the publish date+time of the npm package,
|
// We don't have access to the publish date+time of the npm package,
|
||||||
// so we use the ctime of db-stations/full.ndjson as an approximation.
|
// so we use the ctime of db-stations/full.ndjson as an approximation.
|
||||||
|
// Also require() doesn't make much sense in ES Modules.
|
||||||
// todo: this is brittle, find a better way, e.g. a build script
|
// todo: this is brittle, find a better way, e.g. a build script
|
||||||
const timeModified = statSync(require.resolve('db-stations/full.ndjson')).ctime
|
const timeModified = statSync(require.resolve('db-stations/full.ndjson')).ctime
|
||||||
|
|
||||||
|
@ -37,4 +40,6 @@ pStations.catch((err) => {
|
||||||
process.exit(1) // todo: is this appropriate?
|
process.exit(1) // todo: is this appropriate?
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = pStations
|
export {
|
||||||
|
pStations,
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const {data: cards} = require('hafas-client/p/db/loyalty-cards')
|
import _loyaltyCards from 'hafas-client/p/db/loyalty-cards.js'
|
||||||
|
const {data: cards} = _loyaltyCards
|
||||||
|
|
||||||
const typesByName = new Map([
|
const typesByName = new Map([
|
||||||
// https://github.com/public-transport/hafas-client/blob/68ecd7c5e976dd2f51c5c64a81600e7e181a8996/p/db/loyalty-cards.js#L6-L11
|
// https://github.com/public-transport/hafas-client/blob/68ecd7c5e976dd2f51c5c64a81600e7e181a8996/p/db/loyalty-cards.js#L6-L11
|
||||||
|
@ -29,8 +30,8 @@ const loyaltyCardParser = {
|
||||||
parse: parseLoyaltyCard,
|
parse: parseLoyaltyCard,
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export {
|
||||||
loyaltyCards: cards,
|
cards as loyaltyCards,
|
||||||
parseLoyaltyCard,
|
parseLoyaltyCard,
|
||||||
loyaltyCardParser,
|
loyaltyCardParser,
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
"name": "db-rest",
|
"name": "db-rest",
|
||||||
"description": "A clean REST API wrapping around the Deutsche Bahn API.",
|
"description": "A clean REST API wrapping around the Deutsche Bahn API.",
|
||||||
"version": "5.0.2",
|
"version": "5.0.2",
|
||||||
|
"type": "module",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "Jannis R <mail@jannisr.de>",
|
"author": "Jannis R <mail@jannisr.de>",
|
||||||
"homepage": "https://github.com/derhuerst/db-rest/tree/6",
|
"homepage": "https://github.com/derhuerst/db-rest/tree/6",
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
'use strict'
|
import {pStations} from '../lib/db-stations.js'
|
||||||
|
|
||||||
const pStations = require('../lib/db-stations')
|
|
||||||
|
|
||||||
const err404 = (msg) => {
|
const err404 = (msg) => {
|
||||||
const err = new Error(msg)
|
const err = new Error(msg)
|
||||||
|
@ -58,4 +56,6 @@ Returns a stop/station from [\`db-stations@3\`](https://github.com/derhuerst/db-
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = stationRoute
|
export {
|
||||||
|
stationRoute as route,
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
'use strict'
|
import computeEtag from 'etag'
|
||||||
|
import serveBuffer from 'serve-buffer'
|
||||||
const computeEtag = require('etag')
|
import autocomplete from 'db-stations-autocomplete'
|
||||||
const serveBuffer = require('serve-buffer')
|
import _cliNative from 'cli-native'
|
||||||
const autocomplete = require('db-stations-autocomplete')
|
const {to: parse} = _cliNative
|
||||||
const parse = require('cli-native').to
|
import createFilter from 'db-stations/create-filter.js'
|
||||||
const createFilter = require('db-stations/create-filter')
|
import {pStations} from '../lib/db-stations.js'
|
||||||
let pAllStations = require('../lib/db-stations')
|
|
||||||
|
|
||||||
const JSON_MIME = 'application/json'
|
const JSON_MIME = 'application/json'
|
||||||
const NDJSON_MIME = 'application/x-ndjson'
|
const NDJSON_MIME = 'application/x-ndjson'
|
||||||
|
@ -24,7 +23,7 @@ const toNdjsonBuf = (data) => {
|
||||||
return Buffer.concat(chunks, bytes)
|
return Buffer.concat(chunks, bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
pAllStations = pAllStations.then(({data, timeModified}) => {
|
const pAllStations = pStations.then(({data, timeModified}) => {
|
||||||
const asJson = Buffer.from(JSON.stringify(data), 'utf8')
|
const asJson = Buffer.from(JSON.stringify(data), 'utf8')
|
||||||
const asNdjson = toNdjsonBuf(data)
|
const asNdjson = toNdjsonBuf(data)
|
||||||
return {
|
return {
|
||||||
|
@ -202,4 +201,6 @@ stationsRoute.queryParameters = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = stationsRoute
|
export {
|
||||||
|
stationsRoute as route,
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
'use strict'
|
import tape from 'tape'
|
||||||
|
import _ndjson from 'ndjson'
|
||||||
const tape = require('tape')
|
const {parse: ndjsonParser} = _ndjson
|
||||||
const {parse: ndjsonParser} = require('ndjson')
|
import {loyaltyCards} from '../lib/loyalty-cards.js'
|
||||||
const {loyaltyCards} = require('../lib/loyalty-cards')
|
import {fetchWithTestApi} from './util.js'
|
||||||
const {fetchWithTestApi} = require('./util')
|
import {pStations as pAllStations} from '../lib/db-stations.js'
|
||||||
const pAllStations = require('../lib/db-stations')
|
|
||||||
|
|
||||||
const NO_JOURNEYS = {
|
const NO_JOURNEYS = {
|
||||||
// todo?
|
// todo?
|
||||||
|
|
17
test/util.js
17
test/util.js
|
@ -1,12 +1,9 @@
|
||||||
'use strict'
|
import createApi from 'hafas-rest-api'
|
||||||
|
import getPort from 'get-port'
|
||||||
const createApi = require('hafas-rest-api')
|
import {createServer} from 'node:http'
|
||||||
const getPort = require('get-port')
|
import {promisify} from 'node:util'
|
||||||
const {createServer} = require('http')
|
import axios from 'axios'
|
||||||
const {promisify} = require('util')
|
import {config, hafas as unmockedHafas} from '../api.js'
|
||||||
const axios = require('axios')
|
|
||||||
const pkg = require('../package.json')
|
|
||||||
const {config, hafas: unmockedHafas} = require('../api')
|
|
||||||
|
|
||||||
// adapted from https://github.com/public-transport/hafas-rest-api/blob/60335eacd8332d7f448da875a7498dd97934e360/test/util.js#L40-L77
|
// adapted from https://github.com/public-transport/hafas-rest-api/blob/60335eacd8332d7f448da875a7498dd97934e360/test/util.js#L40-L77
|
||||||
const createTestApi = async (mocks, cfg) => {
|
const createTestApi = async (mocks, cfg) => {
|
||||||
|
@ -50,7 +47,7 @@ const fetchWithTestApi = async (mocks, cfg, path, opt = {}) => {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export {
|
||||||
createTestApi,
|
createTestApi,
|
||||||
fetchWithTestApi,
|
fetchWithTestApi,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue