convert to ESM 💥

This commit is contained in:
Jannis R 2022-12-19 13:23:13 +01:00
parent fe469cd658
commit 6f2e5f134b
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
11 changed files with 67 additions and 62 deletions

View file

@ -1,12 +1,11 @@
{
"extends": "eslint:recommended",
"env": {
"commonjs": true,
"es6": true,
"es2022": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 2018
"sourceType": "module"
},
"ignorePatterns": [
"node_modules"

View file

@ -1,7 +1,5 @@
'use strict'
const generateApiDocs = require('hafas-rest-api/tools/generate-docs')
const {api} = require('./api')
import generateApiDocs from 'hafas-rest-api/tools/generate-docs.js'
import {api} from './api.js'
const HEAD = `\
# \`v6.db.transport.rest\` API documentation

34
api.js
View file

@ -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 {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 berlinHbf = '8011160'
@ -87,7 +93,7 @@ const api = createApi(hafas, config, (api) => {
}))
})
module.exports = {
export {
hafas,
config,
api,

View file

@ -1,6 +1,4 @@
'use strict'
const {api, config} = require('./api')
import {api, config} from './api.js'
api.listen(config.port, (err) => {
const {logger} = api.locals

View file

@ -1,10 +1,13 @@
'use strict'
import {createRequire} from 'node:module'
const require = createRequire(import.meta.url)
const {statSync} = require('fs')
const {full: readRawStations} = require('db-stations')
import {statSync} from 'node:fs'
import _dbStations from 'db-stations'
const {full: readRawStations} = _dbStations
// 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.
// Also require() doesn't make much sense in ES Modules.
// todo: this is brittle, find a better way, e.g. a build script
const timeModified = statSync(require.resolve('db-stations/full.ndjson')).ctime
@ -37,4 +40,6 @@ pStations.catch((err) => {
process.exit(1) // todo: is this appropriate?
})
module.exports = pStations
export {
pStations,
}

View file

@ -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([
// https://github.com/public-transport/hafas-client/blob/68ecd7c5e976dd2f51c5c64a81600e7e181a8996/p/db/loyalty-cards.js#L6-L11
@ -29,8 +30,8 @@ const loyaltyCardParser = {
parse: parseLoyaltyCard,
}
module.exports = {
loyaltyCards: cards,
export {
cards as loyaltyCards,
parseLoyaltyCard,
loyaltyCardParser,
}

View file

@ -3,6 +3,7 @@
"name": "db-rest",
"description": "A clean REST API wrapping around the Deutsche Bahn API.",
"version": "5.0.2",
"type": "module",
"main": "index.js",
"author": "Jannis R <mail@jannisr.de>",
"homepage": "https://github.com/derhuerst/db-rest/tree/6",

View file

@ -1,6 +1,4 @@
'use strict'
const pStations = require('../lib/db-stations')
import {pStations} from '../lib/db-stations.js'
const err404 = (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,
}

View file

@ -1,11 +1,10 @@
'use strict'
const computeEtag = require('etag')
const serveBuffer = require('serve-buffer')
const autocomplete = require('db-stations-autocomplete')
const parse = require('cli-native').to
const createFilter = require('db-stations/create-filter')
let pAllStations = require('../lib/db-stations')
import computeEtag from 'etag'
import serveBuffer from 'serve-buffer'
import autocomplete from 'db-stations-autocomplete'
import _cliNative from 'cli-native'
const {to: parse} = _cliNative
import createFilter from 'db-stations/create-filter.js'
import {pStations} from '../lib/db-stations.js'
const JSON_MIME = 'application/json'
const NDJSON_MIME = 'application/x-ndjson'
@ -24,7 +23,7 @@ const toNdjsonBuf = (data) => {
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 asNdjson = toNdjsonBuf(data)
return {
@ -202,4 +201,6 @@ stationsRoute.queryParameters = {
},
}
module.exports = stationsRoute
export {
stationsRoute as route,
}

View file

@ -1,10 +1,9 @@
'use strict'
const tape = require('tape')
const {parse: ndjsonParser} = require('ndjson')
const {loyaltyCards} = require('../lib/loyalty-cards')
const {fetchWithTestApi} = require('./util')
const pAllStations = require('../lib/db-stations')
import tape from 'tape'
import _ndjson from 'ndjson'
const {parse: ndjsonParser} = _ndjson
import {loyaltyCards} from '../lib/loyalty-cards.js'
import {fetchWithTestApi} from './util.js'
import {pStations as pAllStations} from '../lib/db-stations.js'
const NO_JOURNEYS = {
// todo?

View file

@ -1,12 +1,9 @@
'use strict'
const createApi = require('hafas-rest-api')
const getPort = require('get-port')
const {createServer} = require('http')
const {promisify} = require('util')
const axios = require('axios')
const pkg = require('../package.json')
const {config, hafas: unmockedHafas} = require('../api')
import createApi from 'hafas-rest-api'
import getPort from 'get-port'
import {createServer} from 'node:http'
import {promisify} from 'node:util'
import axios from 'axios'
import {config, hafas as unmockedHafas} from '../api.js'
// adapted from https://github.com/public-transport/hafas-rest-api/blob/60335eacd8332d7f448da875a7498dd97934e360/test/util.js#L40-L77
const createTestApi = async (mocks, cfg) => {
@ -50,7 +47,7 @@ const fetchWithTestApi = async (mocks, cfg, path, opt = {}) => {
return res
}
module.exports = {
export {
createTestApi,
fetchWithTestApi,
}