diff --git a/.eslintrc.json b/.eslintrc.json index dbb60da4..4fd76634 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,7 +1,6 @@ { "env": { - "commonjs": true, - "es6": true, + "es2021": true, "node": true }, "extends": "eslint:recommended", @@ -11,7 +10,8 @@ }, "ignorePatterns": ["node_modules", "*example.js"], "parserOptions": { - "ecmaVersion": 2018 + "ecmaVersion": 2021, + "sourceType": "module" }, "rules": { "no-unused-vars": [ diff --git a/docs/departures.md b/docs/departures.md index 180ddf84..79317bb5 100644 --- a/docs/departures.md +++ b/docs/departures.md @@ -43,8 +43,9 @@ With `opt`, you can override the default options, which look like this: If you pass an object `opt.products`, its fields will partially override the default products defined in the profile. An example with the [BVG profile](../p/bvg): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' + const client = createClient(vbbProfile, 'my-awesome-program') // will query with these products: suburban, subway, bus, express, regional @@ -60,8 +61,8 @@ You may pass a departure's `tripId` into [`trip(id, lineName, [opt])`](trip.md) As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/docs/journeys-from-trip.md b/docs/journeys-from-trip.md index ce9bf623..264679a9 100644 --- a/docs/journeys-from-trip.md +++ b/docs/journeys-from-trip.md @@ -22,8 +22,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [*Deutsche Bahn* profile](../p/db): ```js -const createClient = require('hafas-client') -const dbProfile = require('hafas-client/p/db') +import {createClient} from 'hafas-client' +import {dbProfile} from 'hafas-client/p/db.js' const berlinSüdkreuz = '8011113' const münchenHbf = '8000261' diff --git a/docs/journeys.md b/docs/journeys.md index 2c80f208..285f20a1 100644 --- a/docs/journeys.md +++ b/docs/journeys.md @@ -85,8 +85,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} 'hafas-client' +import {vbbProfile} 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/docs/lines.md b/docs/lines.md index 765e4ff3..6b767e1f 100644 --- a/docs/lines.md +++ b/docs/lines.md @@ -7,8 +7,8 @@ As an example, we're going to use the [SVV profile](../p/svv): ```js -const createClient = require('hafas-client') -const svvProfile = require('hafas-client/p/svv') +import {createClient} from 'hafas-client' +import {svvProfile} from 'hafas-client/p/svv.js' const client = createClient(svvProfile, 'my-awesome-program') diff --git a/docs/locations.md b/docs/locations.md index 1b189ba2..48bf4222 100644 --- a/docs/locations.md +++ b/docs/locations.md @@ -23,8 +23,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/docs/nearby.md b/docs/nearby.md index cc9332a1..bf5a6976 100644 --- a/docs/nearby.md +++ b/docs/nearby.md @@ -24,8 +24,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/docs/profile-boilerplate.js b/docs/profile-boilerplate.js index 529ee2df..925a36fd 100644 --- a/docs/profile-boilerplate.js +++ b/docs/profile-boilerplate.js @@ -1,4 +1,3 @@ -'use strict' // Refer to the the ./writing-a-profile.md guide. const products = [ @@ -41,4 +40,6 @@ const insaProfile = { radar: false } -module.exports = insaProfile +export { + insaProfile, +} diff --git a/docs/radar.md b/docs/radar.md index 4f8ff5d6..b2bcdc43 100644 --- a/docs/radar.md +++ b/docs/radar.md @@ -25,8 +25,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/docs/reachable-from.md b/docs/reachable-from.md index c0883555..f654ef48 100644 --- a/docs/reachable-from.md +++ b/docs/reachable-from.md @@ -31,8 +31,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/docs/readme.md b/docs/readme.md index 84fed8b4..6a3425a8 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -11,9 +11,9 @@ There's opt-in support for throttling requests to the endpoint. ```js -const createClient = require('hafas-client') -const withThrottling = require('hafas-client/throttle') -const dbProfile = require('hafas-client/p/db') +import {createClient} from 'hafas-client' +import {withThrottling} from 'hafas-client/throttle.js' +import {dbProfile} from 'hafas-client/p/db.js' // create a throttled HAFAS client with Deutsche Bahn profile const client = createClient(withThrottling(dbProfile), 'my-awesome-program') @@ -35,9 +35,9 @@ const client = createClient(throttledDbProfile, 'my-awesome-program') There's opt-in support for retrying failed requests to the endpoint. ```js -const createClient = require('hafas-client') -const withRetrying = require('hafas-client/retry') -const dbProfile = require('hafas-client/p/db') +import {createClient} from 'hafas-client' +import {withRetrying} from 'hafas-client/retry.js' +import {dbProfile} from 'hafas-client/p/db.js' // create a client with Deutsche Bahn profile that will retry on HAFAS errors const client = createClient(withRetrying(dbProfile), 'my-awesome-program') @@ -65,7 +65,7 @@ const client = createClient(retryingDbProfile, 'my-awesome-program') By default, `hafas-client` randomizes the client name that you pass into `createClient`, and sends it as [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) in a randomized form: ```js -const createClient('hafas-client') +import {createClient} from 'hafas-client' // … const client = createClient(someProfile, 'my-awesome-program') @@ -158,7 +158,7 @@ Each error has the following properties: To check **if an error from `hafas-client` is HAFAS-specific, use `error instanceof HafasError`**: ```js -const {HafasError} = require('hafas-client/lib/errors') +import {HafasError} from 'hafas-client/lib/errors.js' try { await client.journeys(/* … */) diff --git a/docs/refresh-journey.md b/docs/refresh-journey.md index b8ed6bdb..86852945 100644 --- a/docs/refresh-journey.md +++ b/docs/refresh-journey.md @@ -21,8 +21,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile) diff --git a/docs/remarks.md b/docs/remarks.md index 708add6c..4aa0ad85 100644 --- a/docs/remarks.md +++ b/docs/remarks.md @@ -20,8 +20,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [SVV profile](../p/svv): ```js -const createClient = require('hafas-client') -const svvProfile = require('hafas-client/p/svv') +import {createClient} from 'hafas-client' +import {svvProfile} from 'hafas-client/p/svv.js' const client = createClient(svvProfile, 'my-awesome-program') diff --git a/docs/server-info.md b/docs/server-info.md index a2d2938c..2b8c8ee2 100644 --- a/docs/server-info.md +++ b/docs/server-info.md @@ -16,8 +16,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [SVV profile](../p/svv): ```js -const createClient = require('hafas-client') -const svvProfile = require('hafas-client/p/svv') +import {createClient} from 'hafas-client' +import {svvProfile} from 'hafas-client/p/svv.js' const client = createClient(svvProfile, 'my-awesome-program') diff --git a/docs/stop.md b/docs/stop.md index e0b4c1ea..de65116a 100644 --- a/docs/stop.md +++ b/docs/stop.md @@ -35,8 +35,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/docs/trip.md b/docs/trip.md index 307c6c25..d5d23d32 100644 --- a/docs/trip.md +++ b/docs/trip.md @@ -7,8 +7,8 @@ This method can be used to refetch information about a trip – a vehicle stopp Let's say you used [`journeys`](journeys.md) and now want to get more up-to-date data about the arrival/departure of a leg. You'd pass in the trip ID from `leg.tripId`, e.g. `'1|24983|22|86|18062017'`, and the name of the line from `leg.line.name` like this: ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') @@ -39,8 +39,8 @@ With `opt`, you can override the default options, which look like this: As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb/index.js' const client = createClient(vbbProfile) diff --git a/docs/trips-by-name.md b/docs/trips-by-name.md index 40ed61ab..aac4a367 100644 --- a/docs/trips-by-name.md +++ b/docs/trips-by-name.md @@ -7,8 +7,8 @@ Get all trips matching one or more criteria, e.g. a specific name. As an example, we're going to use the [VBB profile](../p/vbb): ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb.js' const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/docs/writing-a-profile.md b/docs/writing-a-profile.md index e4947b5c..d831f782 100644 --- a/docs/writing-a-profile.md +++ b/docs/writing-a-profile.md @@ -35,7 +35,7 @@ Assuming their HAFAS endpoint returns all line names prefixed with `foo `, we ca ```js // get the default line parser -const parseLine = require('hafas-client/parse/line') +import {parseLine} from 'hafas-client/parse/line.js' // wrapper function with additional logic const parseLineWithoutFoo = (ctx, rawLine) => { @@ -52,7 +52,7 @@ If you pass this profile into `hafas-client`, the `parseLine` method will overri You can also use the `parseHook` helper to reduce boilerplate: ```js -const {parseHook} = require('hafas-client/lib/profile-hooks') +import {parseHook} from 'hafas-client/lib/profile-hooks.js' const removeFoo = (ctx, rawLine) => ({ ...ctx.parsed, diff --git a/format/address.js b/format/address.js index b5ffb7d2..74c73ce6 100644 --- a/format/address.js +++ b/format/address.js @@ -1,7 +1,5 @@ -'use strict' - -const formatLocationIdentifier = require('./location-identifier') -const formatCoord = require('./coord') +import {formatLocationIdentifier} from './location-identifier.js' +import {formatCoord} from './coord.js' const formatAddress = (a) => { if (a.type !== 'location' || !a.latitude || !a.longitude || !a.address) { @@ -22,4 +20,6 @@ const formatAddress = (a) => { } } -module.exports = formatAddress +export { + formatAddress, +} diff --git a/format/coord.js b/format/coord.js index 7d1ff695..bc8404d6 100644 --- a/format/coord.js +++ b/format/coord.js @@ -1,5 +1,5 @@ -'use strict' - const formatCoord = x => Math.round(x * 1000000) -module.exports = formatCoord +export { + formatCoord, +} diff --git a/format/date.js b/format/date.js index 983ab952..aa61d81c 100644 --- a/format/date.js +++ b/format/date.js @@ -1,6 +1,4 @@ -'use strict' - -const {DateTime, IANAZone} = require('luxon') +import {DateTime, IANAZone} from 'luxon' const timezones = new WeakMap() @@ -19,4 +17,6 @@ const formatDate = (profile, when) => { }).toFormat('yyyyMMdd') } -module.exports = formatDate +export { + formatDate, +} diff --git a/format/filters.js b/format/filters.js index 45a17f7c..92630998 100644 --- a/format/filters.js +++ b/format/filters.js @@ -1,5 +1,3 @@ -'use strict' - const bike = {type: 'BC', mode: 'INC'} const accessibility = { @@ -8,4 +6,7 @@ const accessibility = { complete: {type: 'META', mode: 'INC', meta: 'completeBarrierfree'} } -module.exports = {bike, accessibility} +export { + bike, + accessibility, +} diff --git a/format/index.js b/format/index.js deleted file mode 100644 index a119dd37..00000000 --- a/format/index.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict' - -module.exports = { - date: require('./date'), - time: require('./time'), - filters: require('./filters'), - station: require('./station'), - address: require('./address'), - poi: require('./poi'), - location: require('./location'), - locationFilter: require('./location-filter'), - rectangle: require('./rectangle') -} diff --git a/format/lines-req.js b/format/lines-req.js index d56cb402..8eab450f 100644 --- a/format/lines-req.js +++ b/format/lines-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatLinesReq = (ctx, query) => { return { meth: 'LineMatch', @@ -9,4 +7,6 @@ const formatLinesReq = (ctx, query) => { } } -module.exports = formatLinesReq +export { + formatLinesReq, +} diff --git a/format/location-filter.js b/format/location-filter.js index 67dfa6e4..49fdc4c1 100644 --- a/format/location-filter.js +++ b/format/location-filter.js @@ -1,8 +1,8 @@ -'use strict' - const formatLocationFilter = (stops, addresses, poi) => { if (stops && addresses && poi) return 'ALL' return (stops ? 'S' : '') + (addresses ? 'A' : '') + (poi ? 'P' : '') } -module.exports = formatLocationFilter +export { + formatLocationFilter, +} diff --git a/format/location-identifier.js b/format/location-identifier.js index 87cfcde6..15a471fc 100644 --- a/format/location-identifier.js +++ b/format/location-identifier.js @@ -1,5 +1,3 @@ -'use strict' - const sep = '@' const formatLocationIdentifier = (data) => { @@ -13,4 +11,6 @@ const formatLocationIdentifier = (data) => { return str } -module.exports = formatLocationIdentifier +export { + formatLocationIdentifier, +} diff --git a/format/location.js b/format/location.js index 3baf2396..65962e3b 100644 --- a/format/location.js +++ b/format/location.js @@ -1,5 +1,3 @@ -'use strict' - const formatLocation = (profile, l, name = 'location') => { if ('string' === typeof l) return profile.formatStation(l) if ('object' === typeof l && !Array.isArray(l)) { @@ -14,4 +12,6 @@ const formatLocation = (profile, l, name = 'location') => { throw new TypeError(name + ': valid station, address or poi required.') } -module.exports = formatLocation +export { + formatLocation, +} diff --git a/format/locations-req.js b/format/locations-req.js index 9f4a58df..c5192f8c 100644 --- a/format/locations-req.js +++ b/format/locations-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatLocationsReq = (ctx, query) => { const {profile, opt} = ctx @@ -17,4 +15,6 @@ const formatLocationsReq = (ctx, query) => { } } -module.exports = formatLocationsReq +export { + formatLocationsReq, +} diff --git a/format/nearby-req.js b/format/nearby-req.js index 03f875c8..94e9b7e8 100644 --- a/format/nearby-req.js +++ b/format/nearby-req.js @@ -1,6 +1,4 @@ -'use strict' - -const nearbyReq = (ctx, location) => { +const formatNearbyReq = (ctx, location) => { const {profile, opt} = ctx return { @@ -25,4 +23,6 @@ const nearbyReq = (ctx, location) => { } } -module.exports = nearbyReq +export { + formatNearbyReq, +} diff --git a/format/poi.js b/format/poi.js index 6678fc5d..6172759b 100644 --- a/format/poi.js +++ b/format/poi.js @@ -1,7 +1,5 @@ -'use strict' - -const formatLocationIdentifier = require('./location-identifier') -const formatCoord = require('./coord') +import {formatLocationIdentifier} from './location-identifier.js' +import {formatCoord} from './coord.js' const formatPoi = (p) => { if (p.type !== 'location' || !p.latitude || !p.longitude || !p.id || !p.name) { @@ -21,4 +19,6 @@ const formatPoi = (p) => { } } -module.exports = formatPoi +export { + formatPoi, +} diff --git a/format/products-filter.js b/format/products-filter.js index ad1344d0..569b0614 100644 --- a/format/products-filter.js +++ b/format/products-filter.js @@ -1,6 +1,4 @@ -'use strict' - -const isObj = require('lodash/isObject') +import isObj from 'lodash/isObject.js' const hasProp = (o, k) => Object.prototype.hasOwnProperty.call(o, k) @@ -32,4 +30,6 @@ const formatProductsFilter = (ctx, filter) => { } } -module.exports = formatProductsFilter +export { + formatProductsFilter, +} diff --git a/format/radar-req.js b/format/radar-req.js index a5b015d3..c4aaa631 100644 --- a/format/radar-req.js +++ b/format/radar-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatRadarReq = (ctx, north, west, south, east) => { const {profile, opt} = ctx @@ -26,4 +24,6 @@ const formatRadarReq = (ctx, north, west, south, east) => { } } -module.exports = formatRadarReq +export { + formatRadarReq, +} diff --git a/format/reachable-from-req.js b/format/reachable-from-req.js index 23e27b45..95dd7d96 100644 --- a/format/reachable-from-req.js +++ b/format/reachable-from-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatReachableFromReq = (ctx, address) => { const {profile, opt} = ctx @@ -19,4 +17,6 @@ const formatReachableFromReq = (ctx, address) => { } } -module.exports = formatReachableFromReq +export { + formatReachableFromReq, +} diff --git a/format/rectangle.js b/format/rectangle.js index 51f83e9e..eb12da17 100644 --- a/format/rectangle.js +++ b/format/rectangle.js @@ -1,5 +1,3 @@ -'use strict' - const formatRectangle = (profile, north, west, south, east) => { return { llCrd: { @@ -13,4 +11,6 @@ const formatRectangle = (profile, north, west, south, east) => { } } -module.exports = formatRectangle +export { + formatRectangle, +} diff --git a/format/refresh-journey-req.js b/format/refresh-journey-req.js index 3fdf8ff1..b91a7604 100644 --- a/format/refresh-journey-req.js +++ b/format/refresh-journey-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatRefreshJourneyReq = (ctx, refreshToken) => { // eslint-disable-next-line no-unused-vars const {profile, opt} = ctx @@ -22,4 +20,6 @@ const formatRefreshJourneyReq = (ctx, refreshToken) => { } } -module.exports = formatRefreshJourneyReq +export { + formatRefreshJourneyReq, +} diff --git a/format/remarks-req.js b/format/remarks-req.js index cd122ac7..bf203ac3 100644 --- a/format/remarks-req.js +++ b/format/remarks-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatRemarksReq = (ctx) => { const {profile, opt} = ctx @@ -31,4 +29,6 @@ const formatRemarksReq = (ctx) => { return {meth: 'HimSearch', req} } -module.exports = formatRemarksReq +export { + formatRemarksReq, +} diff --git a/format/station-board-req.js b/format/station-board-req.js index 35fdd967..e4bbc001 100644 --- a/format/station-board-req.js +++ b/format/station-board-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatStationBoardReq = (ctx, station, type) => { const {profile, opt} = ctx @@ -31,4 +29,6 @@ const formatStationBoardReq = (ctx, station, type) => { } } -module.exports = formatStationBoardReq +export { + formatStationBoardReq, +} diff --git a/format/station.js b/format/station.js index 26c42dda..1e033639 100644 --- a/format/station.js +++ b/format/station.js @@ -1,6 +1,4 @@ -'use strict' - -const formatLocationIdentifier = require('./location-identifier') +import {formatLocationIdentifier} from './location-identifier.js' const formatStation = (id) => { return { @@ -13,4 +11,6 @@ const formatStation = (id) => { } } -module.exports = formatStation +export { + formatStation, +} diff --git a/format/stop-req.js b/format/stop-req.js index 4769c562..6a98a911 100644 --- a/format/stop-req.js +++ b/format/stop-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatStopReq = (ctx, stopRef) => { return { // todo: there's also `StationDetails`, are there differences? @@ -10,4 +8,6 @@ const formatStopReq = (ctx, stopRef) => { } } -module.exports = formatStopReq +export { + formatStopReq, +} diff --git a/format/time.js b/format/time.js index c2474ee9..975888cc 100644 --- a/format/time.js +++ b/format/time.js @@ -1,6 +1,4 @@ -'use strict' - -const {DateTime, IANAZone} = require('luxon') +import {DateTime, IANAZone} from 'luxon' const timezones = new WeakMap() @@ -19,4 +17,6 @@ const formatTime = (profile, when) => { }).toFormat('HHmmss') } -module.exports = formatTime +export { + formatTime, +} diff --git a/format/trip-req.js b/format/trip-req.js index 5ed826bb..bca4f25c 100644 --- a/format/trip-req.js +++ b/format/trip-req.js @@ -1,5 +1,3 @@ -'use strict' - const formatTripReq = ({opt}, id) => { return { cfg: {polyEnc: 'GPA'}, @@ -15,4 +13,6 @@ const formatTripReq = ({opt}, id) => { } } -module.exports = formatTripReq +export { + formatTripReq, +} diff --git a/index.js b/index.js index 9c438d8d..bb050802 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,12 @@ -'use strict' +import isObj from 'lodash/isObject.js' +import sortBy from 'lodash/sortBy.js' +import omit from 'lodash/omit.js' -const isObj = require('lodash/isObject') -const sortBy = require('lodash/sortBy') -const omit = require('lodash/omit') - -const defaultProfile = require('./lib/default-profile') -const validateProfile = require('./lib/validate-profile') -const {INVALID_REQUEST} = require('./lib/errors') -const sliceLeg = require('./lib/slice-leg') -const {HafasError} = require('./lib/errors') +import {defaultProfile} from './lib/default-profile.js' +import {validateProfile} from './lib/validate-profile.js' +import {INVALID_REQUEST} from './lib/errors.js' +import {sliceLeg} from './lib/slice-leg.js' +import {HafasError} from './lib/errors.js' const isNonEmptyString = str => 'string' === typeof str && str.length > 0 @@ -795,4 +793,6 @@ const createClient = (profile, userAgent, opt = {}) => { return client } -module.exports = createClient +export { + createClient, +} diff --git a/lib/check-if-res-is-ok.js b/lib/check-if-res-is-ok.js deleted file mode 100644 index d441c708..00000000 --- a/lib/check-if-res-is-ok.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict' - -const {HafasError, byErrorCode} = require('./errors') - -const checkIfResponseIsOk = (_) => { - const { - body, - errProps: baseErrProps, - } = _ - - const errProps = { - ...baseErrProps, - } - if (body.id) errProps.hafasResponseId = body.id - - // Because we want more accurate stack traces, we don't construct the error here, - // but only return the constructor & error message. - const getError = (_) => { - // mutating here is ugly but pragmatic - if (_.errTxt) errProps.hafasMessage = _.errTxt - if (_.errTxtOut) errProps.hafasDescription = _.errTxtOut - - if (_.err in byErrorCode) return byErrorCode[_.err] - return { - Error: HafasError, - message: body.errTxt || 'unknown error', - props: {}, - } - } - - if (body.err && body.err !== 'OK') { - const {Error: HafasError, message, props} = getError(body) - throw new HafasError(message, body.err, {...errProps, ...props}) - } - if (!body.svcResL || !body.svcResL[0]) { - throw new HafasError('invalid/unsupported response structure', null, errProps) - } - if (body.svcResL[0].err !== 'OK') { - const {Error: HafasError, message, props} = getError(body.svcResL[0]) - throw new HafasError(message, body.svcResL[0].err, {...errProps, ...props}) - } -} - -module.exports = checkIfResponseIsOk diff --git a/lib/default-profile.js b/lib/default-profile.js index 4f1178a0..2578bd70 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -1,52 +1,50 @@ -'use strict' +import {request} from '../lib/request.js' -const request = require('../lib/request') +import {formatStationBoardReq} from '../format/station-board-req.js' +import {formatLocationsReq} from '../format/locations-req.js' +import {formatStopReq} from '../format/stop-req.js' +import {formatNearbyReq} from '../format/nearby-req.js' +import {formatTripReq} from '../format/trip-req.js' +import {formatRadarReq} from '../format/radar-req.js' +import {formatReachableFromReq} from '../format/reachable-from-req.js' +import {formatRefreshJourneyReq} from '../format/refresh-journey-req.js' +import {formatRemarksReq} from '../format/remarks-req.js' +import {formatLinesReq} from '../format/lines-req.js' -const formatStationBoardReq = require('../format/station-board-req') -const formatLocationsReq = require('../format/locations-req') -const formatStopReq = require('../format/stop-req') -const formatNearbyReq = require('../format/nearby-req') -const formatTripReq = require('../format/trip-req') -const formatRadarReq = require('../format/radar-req') -const formatReachableFromReq = require('../format/reachable-from-req') -const formatRefreshJourneyReq = require('../format/refresh-journey-req') -const formatRemarksReq = require('../format/remarks-req') -const formatLinesReq = require('../format/lines-req') +import {parseDateTime} from '../parse/date-time.js' +import {parsePlatform} from '../parse/platform.js' +import {parseBitmask as parseProductsBitmask} from '../parse/products-bitmask.js' +import {parseIcon} from '../parse/icon.js' +import {parseWhen} from '../parse/when.js' +import {parsePrognosisType} from '../parse/prognosis-type.js' +import {parseScheduledDays} from '../parse/scheduled-days.js' +import {parseDeparture} from '../parse/departure.js' +import {parseArrival} from '../parse/arrival.js' +import {parseTrip} from '../parse/trip.js' +import {parseJourneyLeg} from '../parse/journey-leg.js' +import {parseJourney} from '../parse/journey.js' +import {parseLine} from '../parse/line.js' +import {parseLocation} from '../parse/location.js' +import {parseCommonData as parseCommon} from '../parse/common.js' +import {parsePolyline} from '../parse/polyline.js' +import {parseMovement} from '../parse/movement.js' +import {parseNearby} from '../parse/nearby.js' +import {parseOperator} from '../parse/operator.js' +import {parseHint} from '../parse/hint.js' +import {parseWarning} from '../parse/warning.js' +import {parseStopover} from '../parse/stopover.js' -const parseDateTime = require('../parse/date-time') -const parsePlatform = require('../parse/platform') -const parseProductsBitmask = require('../parse/products-bitmask') -const parseIcon = require('../parse/icon') -const parseWhen = require('../parse/when') -const parsePrognosisType = require('../parse/prognosis-type') -const parseScheduledDays = require('../parse/scheduled-days') -const parseDeparture = require('../parse/departure') -const parseArrival = require('../parse/arrival') -const parseTrip = require('../parse/trip') -const parseJourneyLeg = require('../parse/journey-leg') -const parseJourney = require('../parse/journey') -const parseLine = require('../parse/line') -const parseLocation = require('../parse/location') -const parseCommon = require('../parse/common') -const parsePolyline = require('../parse/polyline') -const parseMovement = require('../parse/movement') -const parseNearby = require('../parse/nearby') -const parseOperator = require('../parse/operator') -const parseHint = require('../parse/hint') -const parseWarning = require('../parse/warning') -const parseStopover = require('../parse/stopover') - -const formatAddress = require('../format/address') -const formatCoord = require('../format/coord') -const formatDate = require('../format/date') -const formatLocationFilter = require('../format/location-filter') -const formatProductsFilter = require('../format/products-filter') -const formatPoi = require('../format/poi') -const formatStation = require('../format/station') -const formatTime = require('../format/time') -const formatLocation = require('../format/location') -const formatRectangle = require('../format/rectangle') -const filters = require('../format/filters') +import {formatAddress} from '../format/address.js' +import {formatCoord} from '../format/coord.js' +import {formatDate} from '../format/date.js' +import {formatLocationFilter} from '../format/location-filter.js' +import {formatProductsFilter} from '../format/products-filter.js' +import {formatPoi} from '../format/poi.js' +import {formatStation} from '../format/station.js' +import {formatTime} from '../format/time.js' +import {formatLocation} from '../format/location.js' +import {formatRectangle} from '../format/rectangle.js' +import * as filters from '../format/filters.js' const DEBUG = /(^|,)hafas-client(,|$)/.test(process.env.DEBUG || '') const logRequest = DEBUG @@ -137,4 +135,6 @@ const defaultProfile = { lines: true, } -module.exports = defaultProfile +export { + defaultProfile, +} diff --git a/lib/errors.js b/lib/errors.js index 942131a7..4e197b82 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -1,5 +1,3 @@ -'use strict' - const ACCESS_DENIED = 'ACCESS_DENIED' const INVALID_REQUEST = 'INVALID_REQUEST' const NOT_FOUND = 'NOT_FOUND' @@ -283,7 +281,7 @@ const byErrorCode = Object.assign(Object.create(null), { } }) -module.exports = { +export { ACCESS_DENIED, INVALID_REQUEST, NOT_FOUND, diff --git a/lib/find-in-tree.js b/lib/find-in-tree.js index aab4d736..fae8b331 100644 --- a/lib/find-in-tree.js +++ b/lib/find-in-tree.js @@ -1,6 +1,4 @@ -'use strict' - -const objectScan = require('object-scan') +import objectScan from 'object-scan' const createFindInTree = (needles) => { const scanner = objectScan(needles, { @@ -20,4 +18,6 @@ const createFindInTree = (needles) => { } } -module.exports = createFindInTree +export { + createFindInTree, +} diff --git a/lib/profile-hooks.js b/lib/profile-hooks.js index dd6a8089..bc5aa981 100644 --- a/lib/profile-hooks.js +++ b/lib/profile-hooks.js @@ -1,5 +1,3 @@ -'use strict' - // For any type of "thing to parse", there's >=1 parse functions. // By composing custom parse function(s) with the default ones, one // can customize the behaviour of hafas-client. Profiles extensively @@ -20,6 +18,6 @@ const parseHook = (oldParse, newParse) => { } } -module.exports = { - parseHook +export { + parseHook, } diff --git a/lib/request.js b/lib/request.js index d71adc6c..dad0d91e 100644 --- a/lib/request.js +++ b/lib/request.js @@ -1,16 +1,13 @@ -'use strict' - -const ProxyAgent = require('https-proxy-agent') -const {isIP} = require('net') -const {Agent: HttpsAgent} = require('https') -const roundRobin = require('@derhuerst/round-robin-scheduler') -const {randomBytes} = require('crypto') -const createHash = require('create-hash') -const {stringify} = require('qs') -const {Request, fetch} = require('cross-fetch') -const {parse: parseContentType} = require('content-type') -const {HafasError} = require('./errors') -const checkIfResponseIsOk = require('./check-if-res-is-ok') +import ProxyAgent from 'https-proxy-agent' +import {isIP} from 'net' +import {Agent as HttpsAgent} from 'https' +import roundRobin from '@derhuerst/round-robin-scheduler' +import {randomBytes} from 'crypto' +import createHash from 'create-hash' +import {stringify} from 'qs' +import {Request, fetch} from 'cross-fetch' +import {parse as parseContentType} from 'content-type' +import {HafasError, byErrorCode} from './errors.js' const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null const localAddresses = process.env.LOCAL_ADDRESS || null @@ -62,6 +59,45 @@ const randomizeUserAgent = (userAgent) => { const md5 = input => createHash('md5').update(input).digest() +const checkIfResponseIsOk = (_) => { + const { + body, + errProps: baseErrProps, + } = _ + + const errProps = { + ...baseErrProps, + } + if (body.id) errProps.hafasResponseId = body.id + + // Because we want more accurate stack traces, we don't construct the error here, + // but only return the constructor & error message. + const getError = (_) => { + // mutating here is ugly but pragmatic + if (_.errTxt) errProps.hafasMessage = _.errTxt + if (_.errTxtOut) errProps.hafasDescription = _.errTxtOut + + if (_.err in byErrorCode) return byErrorCode[_.err] + return { + Error: HafasError, + message: body.errTxt || 'unknown error', + props: {}, + } + } + + if (body.err && body.err !== 'OK') { + const {Error: HafasError, message, props} = getError(body) + throw new HafasError(message, body.err, {...errProps, ...props}) + } + if (!body.svcResL || !body.svcResL[0]) { + throw new HafasError('invalid/unsupported response structure', null, errProps) + } + if (body.svcResL[0].err !== 'OK') { + const {Error: HafasError, message, props} = getError(body.svcResL[0]) + throw new HafasError(message, body.svcResL[0].err, {...errProps, ...props}) + } +} + const request = async (ctx, userAgent, reqData) => { const {profile, opt} = ctx @@ -163,4 +199,7 @@ const request = async (ctx, userAgent, reqData) => { } } -module.exports = request +export { + checkIfResponseIsOk, + request, +} diff --git a/lib/slice-leg.js b/lib/slice-leg.js index f77327cc..7723bf27 100644 --- a/lib/slice-leg.js +++ b/lib/slice-leg.js @@ -1,5 +1,3 @@ -'use strict' - const findById = (needle) => { const needleStopId = needle.id const needleStationId = needle.station ? needle.station.id : null @@ -44,4 +42,6 @@ const sliceLeg = (leg, from, to) => { return newLeg } -module.exports = sliceLeg +export { + sliceLeg, +} diff --git a/lib/validate-profile.js b/lib/validate-profile.js index 80b670ec..55fb3513 100644 --- a/lib/validate-profile.js +++ b/lib/validate-profile.js @@ -1,5 +1,3 @@ -'use strict' - const types = { locale: 'string', timezone: 'string', @@ -92,4 +90,6 @@ const validateProfile = (profile) => { } } -module.exports = validateProfile +export { + validateProfile, +} diff --git a/p/avv/example.js b/p/avv/example.js index 43f0d1bd..1b21a1c2 100644 --- a/p/avv/example.js +++ b/p/avv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vosProfile = require('.') - -const client = createClient(vosProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const rwth = '1057' const kronenberg = '1397' @@ -48,6 +47,6 @@ client.locations('kronenberg', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/avv/index.js b/p/avv/index.js index e069370f..d79592b3 100644 --- a/p/avv/index.js +++ b/p/avv/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -81,7 +84,7 @@ const products = [{ default: true, }] -const avvProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -97,4 +100,6 @@ const avvProfile = { remarksGetPolyline: false, } -module.exports = avvProfile +export { + profile, +} diff --git a/p/avv/readme.md b/p/avv/readme.md index dcfdd12a..f3e799f3 100644 --- a/p/avv/readme.md +++ b/p/avv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const avvProfile = require('hafas-client/p/AVV') +import {createClient} from 'hafas-client' +import {avvProfile} from 'hafas-client/p/avv/index.js' // create a client with AVV profile const client = createClient(avvProfile, 'my-awesome-program') diff --git a/p/bart/example.js b/p/bart/example.js index 433ee661..4e749d24 100644 --- a/p/bart/example.js +++ b/p/bart/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vosProfile = require('.') - -const client = createClient(vosProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const fremont = '100013296' const embarcadero = '100013295' @@ -48,6 +47,6 @@ client.locations('embarcadero', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/bart/index.js b/p/bart/index.js index fa410967..191e4f90 100644 --- a/p/bart/index.js +++ b/p/bart/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -46,7 +49,7 @@ const products = [{ default: true, }] -const bartProfile = { +const profile = { ...baseProfile, locale: 'en-US', timezone: 'America/Los_Angeles', @@ -61,4 +64,6 @@ const bartProfile = { refreshJourneyUseOutReconL: true, } -module.exports = bartProfile +export { + profile, +} diff --git a/p/bart/readme.md b/p/bart/readme.md index e7f47692..a305d237 100644 --- a/p/bart/readme.md +++ b/p/bart/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const bartProfile = require('hafas-client/p/BART') +import {createClient} from 'hafas-client' +import {bartProfile} from 'hafas-client/p/bart/index.js' // create a client with BART profile const client = createClient(bartProfile, 'my-awesome-program') diff --git a/p/bls/example.js b/p/bls/example.js index a6088f16..c077d9a8 100644 --- a/p/bls/example.js +++ b/p/bls/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const blsProfile = require('.') - -const client = createClient(blsProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const bernDennigkofengässli = '8590093' const münsingenSpital = '8578932' @@ -48,6 +47,6 @@ client.journeys(bernDennigkofengässli, münsingenSpital, {results: 1, stopovers // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/bls/index.js b/p/bls/index.js index 8092829f..ddf63b1b 100644 --- a/p/bls/index.js +++ b/p/bls/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ default: true, }] -const blsProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -88,4 +91,6 @@ const blsProfile = { reachableFrom: true, } -module.exports = blsProfile +export { + profile, +} diff --git a/p/bls/readme.md b/p/bls/readme.md index eae5effe..5694540c 100644 --- a/p/bls/readme.md +++ b/p/bls/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const blsProfile = require('hafas-client/p/BLS') +import {createClient} from 'hafas-client' +import {blsProfile} from 'hafas-client/p/bls/index.js' // create a client with BLS profile const client = createClient(blsProfile, 'my-awesome-program') diff --git a/p/bvg/example.js b/p/bvg/example.js index 21e58edd..06c97969 100644 --- a/p/bvg/example.js +++ b/p/bvg/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const bvgProfile = require('.') - -const client = createClient(bvgProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Hauptbahnhof to Charlottenburg client.journeys('900000003201', '900000024101', {results: 1, polylines: true}) @@ -43,6 +42,6 @@ client.journeys('900000003201', '900000024101', {results: 1, polylines: true}) // return client.refreshJourney(journey.refreshToken, {stopovers: true, remarks: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/bvg/index.js b/p/bvg/index.js index d47acff2..0e92abbf 100644 --- a/p/bvg/index.js +++ b/p/bvg/index.js @@ -1,19 +1,20 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const {parseHook} = require('../../lib/profile-hooks') +import {parseHook} from '../../lib/profile-hooks.js' -const parseAndAddLocationDHID = require('../vbb/parse-loc-dhid') +import {parseAndAddLocationDHID} from '../vbb/parse-loc-dhid.js' -const { - parseLocation: _parseLocation, - parseArrival: _parseArrival, - parseDeparture: _parseDeparture, - parseStopover: _parseStopover, - parseJourneyLeg: _parseJourneyLeg, -} = require('../../lib/default-profile') +import {parseLocation as _parseLocation} from '../../parse/location.js' +import {parseArrival as _parseArrival} from '../../parse/arrival.js' +import {parseDeparture as _parseDeparture} from '../../parse/departure.js' +import {parseStopover as _parseStopover} from '../../parse/stopover.js' +import {parseJourneyLeg as _parseJourneyLeg} from '../../parse/journey-leg.js' const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' // todo: there's also a referenced icon `{"res":"occup_fig_{low,mid}"}` const addOccupancy = (item, occupancyCodes) => { @@ -131,7 +132,7 @@ const requestJourneysWithBerlkoenig = ({opt}, query) => { // todo: adapt/extend `vbb-parse-ticket` to support the BVG markup -const bvgProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -162,4 +163,6 @@ const bvgProfile = { reachableFrom: true } -module.exports = bvgProfile +export { + profile, +} diff --git a/p/bvg/products.js b/p/bvg/products.js index 6d091247..9ad80d73 100644 --- a/p/bvg/products.js +++ b/p/bvg/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'suburban', mode: 'train', @@ -58,3 +56,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/bvg/readme.md b/p/bvg/readme.md index a48516eb..b69afeb5 100644 --- a/p/bvg/readme.md +++ b/p/bvg/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const bvgProfile = require('hafas-client/p/bvg') +import {createClient} from 'hafas-client' +import {bvgProfile} from 'hafas-client/p/bvg/index.js' // create a client with BVG profile const client = createClient(bvgProfile, 'my-awesome-program') diff --git a/p/cfl/example.js b/p/cfl/example.js index 0b0aa20e..cc1a3ca5 100644 --- a/p/cfl/example.js +++ b/p/cfl/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const cflProfile = require('.') - -const client = createClient(cflProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const mersch = '9864348' const bruxellesCentral = '8800003' @@ -47,6 +46,6 @@ client.journeys(mersch, bruxellesCentral, {results: 1}) // }) .then(data => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/cfl/index.js b/p/cfl/index.js index 1276d859..e8e380b2 100644 --- a/p/cfl/index.js +++ b/p/cfl/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const cflProfile = { +const profile = { ...baseProfile, locale: 'de-LU', timezone: 'Europe/Luxembourg', @@ -18,4 +21,6 @@ const cflProfile = { remarksGetPolyline: false, } -module.exports = cflProfile; +export { + profile, +} diff --git a/p/cfl/products.js b/p/cfl/products.js index 20e2d315..8b17110b 100644 --- a/p/cfl/products.js +++ b/p/cfl/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ // todo: other bits { id: 'express-train', @@ -43,3 +41,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/cfl/readme.md b/p/cfl/readme.md index 36b6632b..d565b14e 100644 --- a/p/cfl/readme.md +++ b/p/cfl/readme.md @@ -5,8 +5,8 @@ The [*Société Nationale des Chemins de Fer Luxembourgeois (CFL)*](https://en.w ## Usage ```js -const createClient = require('hafas-client') -const cflProfile = require('hafas-client/p/cfl') +import {createClient} from 'hafas-client' +import {cflProfile} from 'hafas-client/p/cfl/index.js' // create a client with CFL profile const client = createClient(cflProfile) diff --git a/p/cmta/example.js b/p/cmta/example.js index 3277d324..5935f6bd 100644 --- a/p/cmta/example.js +++ b/p/cmta/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const cmtaProfile = require('.') - -const client = createClient(cmtaProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Broadie Oaks to Domain client.journeys('000002370', '000005919', {results: 1, polylines: true}) @@ -43,6 +42,6 @@ client.journeys('000002370', '000005919', {results: 1, polylines: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/cmta/index.js b/p/cmta/index.js index b9f697df..37947829 100644 --- a/p/cmta/index.js +++ b/p/cmta/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const cmtaProfile = { +const profile = { ...baseProfile, locale: 'en-US', timezone: 'America/Chicago', @@ -18,4 +21,6 @@ const cmtaProfile = { remarks: true, // `.svcResL[0].res.msgL[]` is missing though 🤔 } -module.exports = cmtaProfile +export { + profile, +} diff --git a/p/cmta/products.js b/p/cmta/products.js index 37cea896..a11e787d 100644 --- a/p/cmta/products.js +++ b/p/cmta/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'bus', mode: 'bus', @@ -26,3 +24,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/cmta/readme.md b/p/cmta/readme.md index a51193d5..e122254f 100644 --- a/p/cmta/readme.md +++ b/p/cmta/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const cmtaProfile = require('hafas-client/p/cmta') +import {createClient} from 'hafas-client' +import {cmtaProfile} from 'hafas-client/p/cmta/index.js' // create a client with CMTA profile const client = createClient(cmtaProfile, 'my-awesome-program') diff --git a/p/dart/example.js b/p/dart/example.js index c4abb168..2ce84967 100644 --- a/p/dart/example.js +++ b/p/dart/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const dartProfile = require('.') - -const client = createClient(dartProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const mlkJrPkwyAdamsAveDsm2055 = '100002702' const se5thStEHackleyAveDsm2294 = '100004972' @@ -49,6 +48,6 @@ client.locations('adams ave', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/dart/index.js b/p/dart/index.js index d1d9b72b..a99faccf 100644 --- a/p/dart/index.js +++ b/p/dart/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -11,7 +14,7 @@ const products = [{ default: true, }] -const dartProfile = { +const profile = { ...baseProfile, locale: 'en-US', timezone: 'America/Chicago', @@ -24,4 +27,6 @@ const dartProfile = { radar: true, } -module.exports = dartProfile +export { + profile, +} diff --git a/p/dart/readme.md b/p/dart/readme.md index c77666a1..a168a5d7 100644 --- a/p/dart/readme.md +++ b/p/dart/readme.md @@ -7,8 +7,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const dartProfile = require('hafas-client/p/dart') +import {createClient} from 'hafas-client' +import {dartProfile} from 'hafas-client/p/dart/index.js' // create a client with DART profile const client = createClient(dartProfile, 'my-awesome-program') diff --git a/p/db-busradar-nrw/example.js b/p/db-busradar-nrw/example.js index e8618f3c..50c4d122 100644 --- a/p/db-busradar-nrw/example.js +++ b/p/db-busradar-nrw/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../../') -const dbbusradarnrwProfile = require('.') - -const client = createClient(dbbusradarnrwProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Hagen Bauhaus to Schwerte Bahnhof // returns hafas error PARSE @@ -36,6 +35,6 @@ client.journeys('3307002', '3357026', {results: 1}) // }, {results: 10}) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }, console.error) diff --git a/p/db-busradar-nrw/index.js b/p/db-busradar-nrw/index.js index edbc0913..ecf3218e 100644 --- a/p/db-busradar-nrw/index.js +++ b/p/db-busradar-nrw/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -75,7 +78,7 @@ const products = [ } ] -const dbBusradarNrwProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -90,5 +93,6 @@ const dbBusradarNrwProfile = { lines: false, // `.svcResL[0].res.lineL[]` is missing 🤔 } -module.exports = dbBusradarNrwProfile - +export { + profile, +} diff --git a/p/db-busradar-nrw/readme.md b/p/db-busradar-nrw/readme.md index 4bd0cde5..b250ea59 100644 --- a/p/db-busradar-nrw/readme.md +++ b/p/db-busradar-nrw/readme.md @@ -11,8 +11,8 @@ This profile adapts `hafas-client` to the HAFAS endpoint used by the application ## Usage ```js -const createClient = require('hafas-client') -const dbbusradarnrwProfile = require('hafas-client/p/db-busradar-nrw') +import {createClient} from 'hafas-client' +import {dbbusradarnrwProfile} from 'hafas-client/p/db-busradar-nrw/index.js' // create a client with DB Busradar NRW profile const client = createClient(dbbusradarnrwProfile, 'my-awesome-program') diff --git a/p/db/ageGroup.js b/p/db/ageGroup.js index 43bf558e..40198ebf 100644 --- a/p/db/ageGroup.js +++ b/p/db/ageGroup.js @@ -1,5 +1,3 @@ -'use strict' - const ageGroup = { BABY: 'B', CHILD: 'K', @@ -30,7 +28,7 @@ const ageGroupFromAge = (age) => { throw new TypeError(`Invalid age '${age}'`) } -module.exports = { +export { ageGroup, - ageGroupFromAge + ageGroupFromAge, } diff --git a/p/db/example.js b/p/db/example.js index c1a2066c..b85dc863 100644 --- a/p/db/example.js +++ b/p/db/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../../') -const dbProfile = require('.') - -const client = createClient(dbProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Berlin Jungfernheide to München Hbf client.journeys('8011167', '8000261', {results: 1, tickets: true}) @@ -55,5 +54,5 @@ client.journeys('8011167', '8000261', {results: 1, tickets: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }, console.error) diff --git a/p/db/index.js b/p/db/index.js index dae3bbc2..1f9f639c 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -1,25 +1,28 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const trim = require('lodash/trim') -const uniqBy = require('lodash/uniqBy') -const slugg = require('slugg') -const without = require('lodash/without') -const {parseHook} = require('../../lib/profile-hooks') +import trim from 'lodash/trim.js' +import uniqBy from 'lodash/uniqBy.js' +import slugg from 'slugg' +import without from 'lodash/without.js' +import {parseHook} from '../../lib/profile-hooks.js' -const _parseJourney = require('../../parse/journey') -const _parseJourneyLeg = require('../../parse/journey-leg') -const _parseLine = require('../../parse/line') -const _parseArrival = require('../../parse/arrival') -const _parseDeparture = require('../../parse/departure') -const _parseHint = require('../../parse/hint') -const _parseLocation = require('../../parse/location') -const _formatStation = require('../../format/station') -const {bike} = require('../../format/filters') +import {parseJourney as _parseJourney} from '../../parse/journey.js' +import {parseJourneyLeg as _parseJourneyLeg} from '../../parse/journey-leg.js' +import {parseLine as _parseLine} from '../../parse/line.js' +import {parseArrival as _parseArrival} from '../../parse/arrival.js' +import {parseDeparture as _parseDeparture} from '../../parse/departure.js' +import {parseHint as _parseHint} from '../../parse/hint.js' +import {parseLocation as _parseLocation} from '../../parse/location.js' +import {formatStation as _formatStation} from '../../format/station.js' +import {bike} from '../../format/filters.js' -const products = require('./products') const baseProfile = require('./base.json') -const formatLoyaltyCard = require('./loyalty-cards').format -const {ageGroup, ageGroupFromAge} = require('./ageGroup') +import {products} from './products.js' +import {formatLoyaltyCard} from './loyalty-cards.js' +import {ageGroup, ageGroupFromAge} from './ageGroup.js' const transformReqBody = (ctx, body) => { const req = body.svcReqL[0] || {} @@ -483,7 +486,7 @@ const formatStation = (id) => { // todo: find option for absolute number of results -const dbProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -512,4 +515,6 @@ const dbProfile = { lines: false, // `.svcResL[0].res.lineL[]` is missing 🤔 } -module.exports = dbProfile +export { + profile, +} diff --git a/p/db/loyalty-cards.js b/p/db/loyalty-cards.js index ef884cee..3445c2f6 100644 --- a/p/db/loyalty-cards.js +++ b/p/db/loyalty-cards.js @@ -1,5 +1,3 @@ -'use strict' - // todo: generate from https://reiseauskunft.bahn.de/addons/fachkonfig-utf8.cfg ? const c = { NONE: Symbol('no loyalty card'), @@ -25,7 +23,7 @@ const formatLoyaltyCard = (data) => { return 0 } -module.exports = { - data: c, - format: formatLoyaltyCard +export { + c as data, + formatLoyaltyCard, } diff --git a/p/db/products.js b/p/db/products.js index 521f1e26..ff79203e 100644 --- a/p/db/products.js +++ b/p/db/products.js @@ -1,7 +1,5 @@ -'use strict' - // todo: https://gist.github.com/anonymous/d3323a5d2d6e159ed42b12afd0380434#file-haf_products-properties-L1-L95 -module.exports = [ +const products = [ { id: 'nationalExpress', mode: 'train', @@ -83,3 +81,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/db/readme.md b/p/db/readme.md index 01adad00..e8ae66c7 100644 --- a/p/db/readme.md +++ b/p/db/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const dbProfile = require('hafas-client/p/db') +import {createClient} from 'hafas-client' +import {dbProfile} from 'hafas-client/p/db/index.js' // create a client with DB profile const client = createClient(dbProfile, 'my-awesome-program') @@ -74,7 +74,7 @@ With the `db` profile, `hafas-client` will return more station information whene ## Using the `loyaltyCard` option ```js -const {data: loyaltyCards} = require('hafas-client/p/db/loyalty-cards') +import {data as loyaltyCards} from 'hafas-client/p/db/loyalty-cards.js' hafas.journeys(from, to, { loyaltyCard: {type: data.BAHNCARD, discount: 25} diff --git a/p/hvv/example.js b/p/hvv/example.js index e145e1f3..df5db1e3 100644 --- a/p/hvv/example.js +++ b/p/hvv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const hvvProfile = require('.') - -const client = createClient(hvvProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // client.journeys('116', '5900', {results: 1, polylines: true}) // client.departures('116', {duration: 1}) @@ -42,6 +41,6 @@ client.locations('dammtor', {results: 2}) // return client.refreshJourney(journey.refreshToken, {stopovers: true, remarks: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/hvv/index.js b/p/hvv/index.js index 9871c1d2..b65a008b 100644 --- a/p/hvv/index.js +++ b/p/hvv/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const hvvProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -23,4 +26,6 @@ const hvvProfile = { lines: false, // fails with `FAIL` "HCI Service: request failed" } -module.exports = hvvProfile +export { + profile, +} diff --git a/p/hvv/products.js b/p/hvv/products.js index c7dc5aa8..45992d74 100644 --- a/p/hvv/products.js +++ b/p/hvv/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ // todo: what is `512`? +const products = [ // todo: what is `512`? { id: 'subway', mode: 'train', @@ -90,3 +88,7 @@ module.exports = [ // todo: what is `512`? default: false } ] + +export { + products, +} diff --git a/p/hvv/readme.md b/p/hvv/readme.md index 566e7fa5..787aa1fc 100644 --- a/p/hvv/readme.md +++ b/p/hvv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const hvvProfile = require('hafas-client/p/hvv') +import {createClient} from 'hafas-client' +import {hvvProfile} from 'hafas-client/p/hvv/index.js' // create a client with HVV profile const client = createClient(hvvProfile, 'my-awesome-program') diff --git a/p/insa/example.js b/p/insa/example.js index 7e6b6981..3f8c316a 100644 --- a/p/insa/example.js +++ b/p/insa/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const insaProfile = require('.') - -const client = createClient(insaProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const hellestr1 = { type: 'location', @@ -41,6 +40,6 @@ client.journeys('008010226', '008013456', {results: 1}) // }) .then(data => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/insa/index.js b/p/insa/index.js index 9db1610c..882e6c6d 100644 --- a/p/insa/index.js +++ b/p/insa/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const insaProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -16,4 +19,6 @@ const insaProfile = { reachableFrom: true, } -module.exports = insaProfile; +export { + profile, +} diff --git a/p/insa/products.js b/p/insa/products.js index 6cb9f160..37edb087 100644 --- a/p/insa/products.js +++ b/p/insa/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'nationalExpress', mode: 'train', @@ -58,3 +56,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/insa/readme.md b/p/insa/readme.md index 4c0675e7..5edcf327 100644 --- a/p/insa/readme.md +++ b/p/insa/readme.md @@ -5,8 +5,8 @@ The [Nahverkehr Sachsen-Anhalt (NASA)](https://de.wikipedia.org/wiki/Nahverkehrs ## Usage ```js -const createClient = require('hafas-client') -const insaProfile = require('hafas-client/p/insa') +import {createClient} from 'hafas-client' +import {insaProfile} from 'hafas-client/p/insa/index.js' // create a client with INSA profile const client = createClient(insaProfile, 'my-awesome-program') diff --git a/p/invg/example.js b/p/invg/example.js index 2870348e..abbacb18 100644 --- a/p/invg/example.js +++ b/p/invg/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const cmtaProfile = require('.') - -const client = createClient(cmtaProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const ingolstadtHbf = '8000183' const audiParkplatz = '84999' @@ -40,6 +39,6 @@ client.journeys(ingolstadtHbf, audiParkplatz, {results: 1}) // todo: `reachableFrom` with `Ingolstadt, Tillystraße 1` 48.745769 | 11.432814 .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/invg/index.js b/p/invg/index.js index ceabf2ee..aa5c3f45 100644 --- a/p/invg/index.js +++ b/p/invg/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const invgProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -16,4 +19,6 @@ const invgProfile = { refreshJourney: true, } -module.exports = invgProfile +export { + profile, +} diff --git a/p/invg/products.js b/p/invg/products.js index e8db7b78..ba954914 100644 --- a/p/invg/products.js +++ b/p/invg/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ // https://github.com/public-transport/hafas-client/issues/93#issuecomment-437868265 { id: 'bus', @@ -67,3 +65,7 @@ module.exports = [ default: false } ] + +export { + products, +} diff --git a/p/invg/readme.md b/p/invg/readme.md index 064ab504..6f2bab39 100644 --- a/p/invg/readme.md +++ b/p/invg/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const invgProfile = require('hafas-client/p/invg') +import {createClient} from 'hafas-client' +import {invgProfile} from 'hafas-client/p/invg/index.js' // create a client with INVG profile const client = createClient(invgProfile, 'my-awesome-program') diff --git a/p/irish-rail/example.js b/p/irish-rail/example.js index cf7a4a87..c58b588d 100644 --- a/p/irish-rail/example.js +++ b/p/irish-rail/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const irishProfile = require('.') - -const client = createClient(irishProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') // from Dublin to Belfast Central client.journeys('9909002', '9990840', {results: 1}) @@ -31,6 +30,6 @@ client.journeys('9909002', '9990840', {results: 1}) // }, {results: 10}) .then(data => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/irish-rail/index.js b/p/irish-rail/index.js index 50f7acf8..e41ade1f 100644 --- a/p/irish-rail/index.js +++ b/p/irish-rail/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const irishRailProfile = { +const profile = { ...baseProfile, locale: 'en-IE', timezone: 'Europe/Dublin', @@ -18,4 +21,6 @@ const irishRailProfile = { radar: true, } -module.exports = irishRailProfile; +export { + profile, +} diff --git a/p/irish-rail/products.js b/p/irish-rail/products.js index a3e2cc43..30f41335 100644 --- a/p/irish-rail/products.js +++ b/p/irish-rail/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'national-train', mode: 'train', @@ -36,3 +34,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/irish-rail/readme.md b/p/irish-rail/readme.md index 5a629cf9..31f63437 100644 --- a/p/irish-rail/readme.md +++ b/p/irish-rail/readme.md @@ -5,8 +5,8 @@ The [*Iarnród Éireann* (Irish Rail)](https://en.wikipedia.org/wiki/Iarnród_É ## Usage ```js -const createClient = require('hafas-client') -const irishProfile = require('hafas-client/p/irish-rail') +import {createClient} from 'hafas-client' +import {irishProfile} from 'hafas-client/p/irish-rail/index.js' // create a client with Irish Rail profile const client = createClient(irishProfile, 'my-awesome-program') diff --git a/p/ivb/example.js b/p/ivb/example.js index 90ba6551..eb79acd5 100644 --- a/p/ivb/example.js +++ b/p/ivb/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const ivbProfile = require('.') - -const client = createClient(ivbProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const innsbruckGriesauweg = '476162400' const völsWest = '476431800' @@ -44,6 +43,6 @@ client.locations('griesauweg', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/ivb/index.js b/p/ivb/index.js index 1e34cb33..337a70dc 100644 --- a/p/ivb/index.js +++ b/p/ivb/index.js @@ -1,8 +1,10 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const {readFileSync} = require('fs') -const {join} = require('path') -const {Agent} = require('https') +import {readFileSync} from 'fs' +import {Agent} from 'https' const baseProfile = require('./base.json') const products = [{ @@ -80,11 +82,11 @@ const products = [{ // `fahrplan.ivb.at:443` doesn't provide the necessary CA certificate chain for // Node.js to trust the certificate, so we manually add it. // todo: fix this properly, e.g. by letting them know -const ca = readFileSync(join(__dirname, 'digicert-tls-rsa-sha256-2020-ca1.crt.pem')) +const ca = readFileSync(new URL('./digicert-tls-rsa-sha256-2020-ca1.crt.pem', import.meta.url).pathname) const agent = new Agent({ca}) const transformReq = (ctx, req) => ({...req, agent}) -const ivbProfile = { +const profile = { ...baseProfile, ver: '1.32', transformReq, @@ -99,4 +101,6 @@ const ivbProfile = { reachableFrom: true, } -module.exports = ivbProfile +export { + profile, +} diff --git a/p/ivb/readme.md b/p/ivb/readme.md index 313c6c85..d5c3ce54 100644 --- a/p/ivb/readme.md +++ b/p/ivb/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const ivbProfile = require('hafas-client/p/ivb') +import {createClient} from 'hafas-client' +import {ivbProfile} from 'hafas-client/p/ivb/index.js' // create a client with IVB profile const client = createClient(ivbProfile, 'my-awesome-program') diff --git a/p/kvb/example.js b/p/kvb/example.js index 335b43a4..e2f8f184 100644 --- a/p/kvb/example.js +++ b/p/kvb/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const kvbProfile = require('.') - -const client = createClient(kvbProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const heumarkt = '900000001' const poststr = '900000003' @@ -42,6 +41,6 @@ client.locations('heumarkt', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/kvb/index.js b/p/kvb/index.js index 6be141e5..f2103c85 100644 --- a/p/kvb/index.js +++ b/p/kvb/index.js @@ -1,8 +1,10 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const {readFileSync} = require('fs') -const {join: pathJoin} = require('path') -const {Agent} = require('https') +import {readFileSync} from 'fs' +import {Agent} from 'https' const baseProfile = require('./base.json') const products = [{ @@ -52,11 +54,11 @@ const products = [{ // `auskunft.kvb.koeln:443` doesn't provide the necessary CA certificate chain for // Node.js to trust the certificate, so we manually add it. // todo: fix this properly, e.g. by letting them know -const ca = readFileSync(pathJoin(__dirname, 'thawte-rsa-ca-2018.pem')) +const ca = readFileSync(new URL('./thawte-rsa-ca-2018.pem', import.meta.url).pathname) const agent = new Agent({ca}) const transformReq = (ctx, req) => ({...req, agent}) -const vosProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -69,4 +71,6 @@ const vosProfile = { reachableFrom: true, } -module.exports = vosProfile +export { + profile, +} diff --git a/p/kvb/readme.md b/p/kvb/readme.md index fbfd6aa2..4e2c54ad 100644 --- a/p/kvb/readme.md +++ b/p/kvb/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const kvbProfile = require('hafas-client/p/kvb') +import {createClient} from 'hafas-client' +import {kvbProfile} from 'hafas-client/p/kvb/index.js' // create a client with KVB profile const client = createClient(kvbProfile, 'my-awesome-program') diff --git a/p/mobil-nrw/example.js b/p/mobil-nrw/example.js index e5b0bee2..3ece0b20 100644 --- a/p/mobil-nrw/example.js +++ b/p/mobil-nrw/example.js @@ -1,10 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const {inspect} = require('util') -const createClient = require('../..') -const cflProfile = require('.') - -const client = createClient(cflProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const soest = '8000076' const aachenHbf = '8000001' diff --git a/p/mobil-nrw/index.js b/p/mobil-nrw/index.js index 4ae7d19e..8cf5fdb8 100644 --- a/p/mobil-nrw/index.js +++ b/p/mobil-nrw/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const cflProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -17,4 +20,6 @@ const cflProfile = { remarks: true, } -module.exports = cflProfile; +export { + profile, +} diff --git a/p/mobil-nrw/products.js b/p/mobil-nrw/products.js index e2f45788..4add9005 100644 --- a/p/mobil-nrw/products.js +++ b/p/mobil-nrw/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'regional-train', mode: 'train', @@ -76,3 +74,7 @@ module.exports = [ default: true, } ] + +export { + products, +} diff --git a/p/mobil-nrw/readme.md b/p/mobil-nrw/readme.md index a0809817..dcff0d36 100644 --- a/p/mobil-nrw/readme.md +++ b/p/mobil-nrw/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const mobilNrwProfile = require('hafas-client/p/mobil-nrw') +import {createClient} from 'hafas-client' +import {mobilNrwProfile} from 'hafas-client/p/mobil-nrw/index.js' // create a client with mobil.nrw profile const client = createClient(mobilNrwProfile) diff --git a/p/mobiliteit-lu/example.js b/p/mobiliteit-lu/example.js index 7c6f775c..306e087d 100644 --- a/p/mobiliteit-lu/example.js +++ b/p/mobiliteit-lu/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const mobiliteitProfile = require('.') - -const client = createClient(mobiliteitProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const mersch = '160904011' const luxembourgCentral = '200405060' @@ -47,6 +46,6 @@ client.journeys(mersch, luxembourgCentral, {results: 1}) // }) .then(data => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/mobiliteit-lu/index.js b/p/mobiliteit-lu/index.js index 9ef65b8b..ea1847ca 100644 --- a/p/mobiliteit-lu/index.js +++ b/p/mobiliteit-lu/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const mobiliteitLuProfile = { +const profile = { ...baseProfile, locale: 'de-LU', timezone: 'Europe/Luxembourg', @@ -17,4 +20,6 @@ const mobiliteitLuProfile = { refreshJourneyUseOutReconL: true, } -module.exports = mobiliteitLuProfile; +export { + profile, +} diff --git a/p/mobiliteit-lu/products.js b/p/mobiliteit-lu/products.js index 70bb96c6..2602bce6 100644 --- a/p/mobiliteit-lu/products.js +++ b/p/mobiliteit-lu/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'express-train', mode: 'train', @@ -42,3 +40,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/mobiliteit-lu/readme.md b/p/mobiliteit-lu/readme.md index 5b64f590..12e4de59 100644 --- a/p/mobiliteit-lu/readme.md +++ b/p/mobiliteit-lu/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const mobiliteitProfile = require('hafas-client/p/mobiliteit-lu') +import {createClient} from 'hafas-client' +import {mobiliteitProfile} from 'hafas-client/p/mobiliteit-lu/index.js' // create a client with mobiliteit.lu profile const client = createClient(mobiliteitProfile, 'my-awesome-program') diff --git a/p/nahsh/example.js b/p/nahsh/example.js index cd8b690b..d9aa8ae4 100644 --- a/p/nahsh/example.js +++ b/p/nahsh/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const nahshProfile = require('.') - -const client = createClient(nahshProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Flensburg Hbf to Kiel Hbf client.journeys('8000103', '8000199', {results: 10, tickets: true}) @@ -47,6 +46,6 @@ client.journeys('8000103', '8000199', {results: 10, tickets: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/nahsh/index.js b/p/nahsh/index.js index 9f3f1d0e..bfd528a2 100644 --- a/p/nahsh/index.js +++ b/p/nahsh/index.js @@ -1,12 +1,15 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const {parseHook} = require('../../lib/profile-hooks') +import {parseHook} from '../../lib/profile-hooks.js' -const _parseLocation = require('../../parse/location') -const _parseJourney = require('../../parse/journey') -const _parseMovement = require('../../parse/movement') +import {parseLocation as _parseLocation} from '../../parse/location.js' +import {parseJourney as _parseJourney} from '../../parse/journey.js' +import {parseMovement as _parseMovement} from '../../parse/movement.js' const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' // todo: journey prices @@ -66,7 +69,7 @@ const fixMovement = ({parsed}, m) => { return parsed } -const nahshProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -83,4 +86,6 @@ const nahshProfile = { reachableFrom: true, } -module.exports = nahshProfile +export { + profile, +} diff --git a/p/nahsh/products.js b/p/nahsh/products.js index a69be195..07d5e36c 100644 --- a/p/nahsh/products.js +++ b/p/nahsh/products.js @@ -1,6 +1,4 @@ -'use strict' - -const p = [ +const products = [ { id: 'nationalExpress', mode: 'train', @@ -83,4 +81,6 @@ const p = [ } ] -module.exports = p +export { + products, +} diff --git a/p/nahsh/readme.md b/p/nahsh/readme.md index c67a4136..c2de85b7 100644 --- a/p/nahsh/readme.md +++ b/p/nahsh/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const nahshProfile = require('hafas-client/p/nahsh') +import {createClient} from 'hafas-client' +import {nahshProfile} from 'hafas-client/p/nahsh/index.js' // create a client with NAH.SH profile const client = createClient(nahshProfile, 'my-awesome-program') diff --git a/p/nvv/example.js b/p/nvv/example.js index d95f18d1..7374ccdf 100644 --- a/p/nvv/example.js +++ b/p/nvv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const nvvProfile = require('.') - -const client = createClient(nvvProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Scheidemannplatz to Auestadion client.journeys('2200073', '2200042', {results: 1, polylines: true}) @@ -44,6 +43,6 @@ client.journeys('2200073', '2200042', {results: 1, polylines: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/nvv/index.js b/p/nvv/index.js index 0a3f5250..ea253934 100644 --- a/p/nvv/index.js +++ b/p/nvv/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const nvvProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -16,4 +19,6 @@ const nvvProfile = { reachableFrom: true, } -module.exports = nvvProfile +export { + profile, +} diff --git a/p/nvv/products.js b/p/nvv/products.js index a4907b55..0b7137d6 100644 --- a/p/nvv/products.js +++ b/p/nvv/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ // todo: what is `256`? { id: 'express', @@ -59,3 +57,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/nvv/readme.md b/p/nvv/readme.md index 60b9fae0..d06119be 100644 --- a/p/nvv/readme.md +++ b/p/nvv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const nvvProfile = require('hafas-client/p/nvv') +import {createClient} from 'hafas-client' +import {nvvProfile} from 'hafas-client/p/nvv/index.js' // create a client with NVV profile const client = createClient(nvvProfile, 'my-awesome-program') diff --git a/p/oebb/example.js b/p/oebb/example.js index 1f520897..4082a613 100644 --- a/p/oebb/example.js +++ b/p/oebb/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const oebbProfile = require('.') - -const client = createClient(oebbProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Wien Westbahnhof to Salzburg Hbf client.journeys('1291501', '8100002', {results: 1}) @@ -43,6 +42,6 @@ client.journeys('1291501', '8100002', {results: 1}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/oebb/index.js b/p/oebb/index.js index 94c0e76c..9736ada1 100644 --- a/p/oebb/index.js +++ b/p/oebb/index.js @@ -1,14 +1,17 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) // todo: https://gist.github.com/anonymous/a5fc856bc80ae7364721943243f934f4#file-haf_config_base-properties-L5 // todo: https://gist.github.com/anonymous/a5fc856bc80ae7364721943243f934f4#file-haf_config_base-properties-L47-L234 -const {parseHook} = require('../../lib/profile-hooks') +import {parseHook} from '../../lib/profile-hooks.js' -const _parseLocation = require('../../parse/location') -const _parseMovement = require('../../parse/movement') +import {parseLocation as _parseLocation} from '../../parse/location.js' +import {parseMovement as _parseMovement} from '../../parse/movement.js' const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' // ÖBB has some 'stations' **in austria** with no departures/products, // like station entrances, that are actually POIs. @@ -43,7 +46,7 @@ const fixMovement = ({parsed}, m) => { return parsed } -const oebbProfile = { +const profile = { ...baseProfile, locale: 'de-AT', timezone: 'Europe/Vienna', @@ -61,4 +64,6 @@ const oebbProfile = { // lines: false, // `.svcResL[0].res.lineL[]` is missing 🤔 } -module.exports = oebbProfile +export { + profile, +} diff --git a/p/oebb/products.js b/p/oebb/products.js index fc1badda..f2fd6708 100644 --- a/p/oebb/products.js +++ b/p/oebb/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'nationalExpress', mode: 'train', @@ -82,3 +80,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/oebb/readme.md b/p/oebb/readme.md index 58a59b18..ec8bc51b 100644 --- a/p/oebb/readme.md +++ b/p/oebb/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const oebbProfile = require('hafas-client/p/oebb') +import {createClient} from 'hafas-client' +import {oebbProfile} from 'hafas-client/p/oebb/index.js' // create a client with ÖBB profile const client = createClient(oebbProfile, 'my-awesome-program') diff --git a/p/ooevv/example.js b/p/ooevv/example.js index 89d1ac5d..ebc1a27d 100644 --- a/p/ooevv/example.js +++ b/p/ooevv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vvvProfile = require('.') - -const client = createClient(vvvProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const linzTheatergasse = '444670100' const amstettenStadtbad = '431507400' @@ -44,6 +43,6 @@ client.departures(linzTheatergasse, {duration: 12 * 60}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/ooevv/index.js b/p/ooevv/index.js index 2195b1f6..dfc8ab32 100644 --- a/p/ooevv/index.js +++ b/p/ooevv/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ default: true, }] -const vosProfile = { +const profile = { ...baseProfile, auth: { type: 'USER', @@ -95,4 +98,6 @@ const vosProfile = { reachableFrom: true, } -module.exports = vosProfile +export { + profile, +} diff --git a/p/ooevv/readme.md b/p/ooevv/readme.md index 8727bcb5..d7d53a49 100644 --- a/p/ooevv/readme.md +++ b/p/ooevv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const oövvProfile = require('hafas-client/p/ooevv') +import {createClient} from 'hafas-client' +import {oövvProfile} from 'hafas-client/p/ooevv/index.js' // create a client with OÖVV profile const client = createClient(oövvProfile, 'my-awesome-program') diff --git a/p/pkp/example.js b/p/pkp/example.js index 434ab21a..d32787e6 100644 --- a/p/pkp/example.js +++ b/p/pkp/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const pkpProfile = require('.') - -const client = createClient(pkpProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const wrocławGł = '5100069' const krakówGł = '5100028' @@ -41,6 +40,6 @@ client.journeys(krakówGł, wrocławGł, {results: 1, polylines: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/pkp/index.js b/p/pkp/index.js index b3e279d8..fdc31830 100644 --- a/p/pkp/index.js +++ b/p/pkp/index.js @@ -1,10 +1,13 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const {parseHook} = require('../../lib/profile-hooks') +import {parseHook} from '../../lib/profile-hooks.js' -const parseLocation = require('../../parse/location') +import {parseLocation} from '../../parse/location.js' const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' const trimStopName = ({parsed}, l) => { if (parsed.type === 'stop' || parsed.type === 'station' && parsed.name) { @@ -13,7 +16,7 @@ const trimStopName = ({parsed}, l) => { return parsed } -const pkpProfile = { +const profile = { ...baseProfile, locale: 'pl-PL', timezone: 'Europe/Warsaw', @@ -29,4 +32,6 @@ const pkpProfile = { remarks: false, // seems like ver >= 1.20 is required } -module.exports = pkpProfile +export { + profile, +} diff --git a/p/pkp/products.js b/p/pkp/products.js index 23e5f95b..6b109d02 100644 --- a/p/pkp/products.js +++ b/p/pkp/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'high-speed-train', mode: 'train', @@ -33,4 +31,8 @@ module.exports = [ short: 'B', default: true } -] \ No newline at end of file +] + +export { + products, +} diff --git a/p/pkp/readme.md b/p/pkp/readme.md index 3e71f9cd..7933003e 100644 --- a/p/pkp/readme.md +++ b/p/pkp/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const pkpProfile = require('hafas-client/p/pkp') +import {createClient} from 'hafas-client' +import {pkpProfile} from 'hafas-client/p/pkp/index.js' // create a client with PKP profile const client = createClient(pkpProfile, 'my-awesome-program') diff --git a/p/readme.md b/p/readme.md index f5145257..bb953b54 100644 --- a/p/readme.md +++ b/p/readme.md @@ -60,7 +60,7 @@ If you want to write a profile for an endpoint, check out the [*writing a profil Your profile must be passed into `createClient` and is expected to be in a certain structure: ```js -const createClient = require('hafas-client') +import {createClient} from 'hafas-client' const myCustomProfile = { // … diff --git a/p/rejseplanen/example.js b/p/rejseplanen/example.js index f9bd9637..a94c158f 100644 --- a/p/rejseplanen/example.js +++ b/p/rejseplanen/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const rejseplanenProfile = require('.') - -const client = createClient(rejseplanenProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // from København Central to Aalborg // client.journeys('8600626', '8600020', {results: 1}) @@ -29,6 +28,6 @@ client.departures('8600626', {duration: 5}) // }, {results: 10}) .then(data => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/rejseplanen/index.js b/p/rejseplanen/index.js index 9e25fa1c..4f46dc2c 100644 --- a/p/rejseplanen/index.js +++ b/p/rejseplanen/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const rejseplanenProfile = { +const profile = { ...baseProfile, locale: 'da-DK', timezone: 'Europe/Copenhagen', @@ -15,4 +18,6 @@ const rejseplanenProfile = { radar: true, } -module.exports = rejseplanenProfile; +export { + profile, +} diff --git a/p/rejseplanen/products.js b/p/rejseplanen/products.js index 451195e4..8c7ba9cc 100644 --- a/p/rejseplanen/products.js +++ b/p/rejseplanen/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'national-train', mode: 'train', @@ -42,3 +40,7 @@ module.exports = [ default: true, } ] + +export { + products, +} diff --git a/p/rejseplanen/readme.md b/p/rejseplanen/readme.md index 20958b3a..39c757a3 100644 --- a/p/rejseplanen/readme.md +++ b/p/rejseplanen/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const rejseplanenProfile = require('hafas-client/p/rejseplanen') +import {createClient} from 'hafas-client' +import {rejseplanenProfile} from 'hafas-client/p/rejseplanen/index.js' // create a client with Rejseplanen profile const client = createClient(rejseplanenProfile) diff --git a/p/rmv/example.js b/p/rmv/example.js index 07081bc1..43df1333 100644 --- a/p/rmv/example.js +++ b/p/rmv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const rmvProfile = require('.') - -const client = createClient(rmvProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const marburgHbf = '3010011' const mainzGonsenheim = '3011332' @@ -43,6 +42,6 @@ client.journeys(marburgHbf, mainzGonsenheim, {results: 1}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/rmv/index.js b/p/rmv/index.js index 13545164..c61455b4 100644 --- a/p/rmv/index.js +++ b/p/rmv/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const hvvProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -17,4 +20,6 @@ const hvvProfile = { reachableFrom: true, } -module.exports = hvvProfile +export { + profile, +} diff --git a/p/rmv/products.js b/p/rmv/products.js index 27486704..f9adbab2 100644 --- a/p/rmv/products.js +++ b/p/rmv/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'express-train', mode: 'train', @@ -83,3 +81,7 @@ module.exports = [ } // todo: remaining bitmask `1015` ] + +export { + products, +} diff --git a/p/rmv/readme.md b/p/rmv/readme.md index 9ebe611d..a549b157 100644 --- a/p/rmv/readme.md +++ b/p/rmv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const rmvProfile = require('hafas-client/p/rmv') +import {createClient} from 'hafas-client' +import {rmvProfile} from 'hafas-client/p/rmv/index.js' // create a client with RMV profile const client = createClient(rmvProfile, 'my-awesome-program') diff --git a/p/rsag/example.js b/p/rsag/example.js index 62a7dd39..695302cc 100644 --- a/p/rsag/example.js +++ b/p/rsag/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const rsagProfile = require('.') - -const client = createClient(rsagProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const rostockHbf = '8010304' const güstrow = '8010153' @@ -44,6 +43,6 @@ client.journeys(rostockHbf, güstrow, {results: 1}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/rsag/index.js b/p/rsag/index.js index 7cc2b711..f3eba7ad 100644 --- a/p/rsag/index.js +++ b/p/rsag/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const rsagProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -16,4 +19,6 @@ const rsagProfile = { refreshJourneyUseOutReconL: true, } -module.exports = rsagProfile +export { + profile, +} diff --git a/p/rsag/products.js b/p/rsag/products.js index 8315fbb6..ba9d431d 100644 --- a/p/rsag/products.js +++ b/p/rsag/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'ice', mode: 'train', @@ -82,3 +80,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/rsag/readme.md b/p/rsag/readme.md index a901ba82..ec11d89d 100644 --- a/p/rsag/readme.md +++ b/p/rsag/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const rsagProfile = require('hafas-client/p/rsag') +import {createClient} from 'hafas-client' +import {rsagProfile} from 'hafas-client/p/rsag/index.js' // create a client with RSAG profile const client = createClient(rsagProfile, 'my-awesome-program') diff --git a/p/saarfahrplan/example.js b/p/saarfahrplan/example.js index 58970061..680f8590 100644 --- a/p/saarfahrplan/example.js +++ b/p/saarfahrplan/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const saarfahrplanProfile = require('.') - -const client = createClient(saarfahrplanProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') client.journeys('15541', '10609', {results: 1}) // .then(({journeys}) => { @@ -40,6 +39,6 @@ client.journeys('15541', '10609', {results: 1}) // }, {maxDuration: 20}) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/saarfahrplan/index.js b/p/saarfahrplan/index.js index 6cf70318..39fdc09f 100644 --- a/p/saarfahrplan/index.js +++ b/p/saarfahrplan/index.js @@ -1,10 +1,13 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const {parseHook} = require('../../lib/profile-hooks') +import {parseHook} from '../../lib/profile-hooks.js' -const _parseMovement = require('../../parse/movement') +import {parseMovement as _parseMovement} from '../../parse/movement.js' const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' const fixMovement = ({parsed}, m) => { // filter out empty stopovers @@ -12,7 +15,7 @@ const fixMovement = ({parsed}, m) => { return parsed } -const saarfahrplanProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -29,4 +32,6 @@ const saarfahrplanProfile = { reachableFrom: true } -module.exports = saarfahrplanProfile +export { + profile, +} diff --git a/p/saarfahrplan/products.js b/p/saarfahrplan/products.js index 995cb5f3..d1bd2238 100644 --- a/p/saarfahrplan/products.js +++ b/p/saarfahrplan/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'nationalExpress', mode: 'train', @@ -91,3 +89,7 @@ module.exports = [ } // todo: `1`, `2`, `4` bitmasks? ] + +export { + products, +} diff --git a/p/saarfahrplan/readme.md b/p/saarfahrplan/readme.md index 5e195245..a64727c5 100644 --- a/p/saarfahrplan/readme.md +++ b/p/saarfahrplan/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const saarfahrplanProfile = require('hafas-client/p/saarfahrplan') +import {createClient} from 'hafas-client' +import {saarfahrplanProfile} from 'hafas-client/p/saarfahrplan/index.js' // create a client with Saarfahrplan profile const client = createClient(saarfahrplanProfile, 'my-awesome-program') diff --git a/p/salzburg/example.js b/p/salzburg/example.js index 36209911..f0fc5308 100644 --- a/p/salzburg/example.js +++ b/p/salzburg/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const salzburgProfile = require('.') - -const client = createClient(salzburgProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const salzburgGaswerkgasse = '455001300' const oberndorfKrankenhaus = '455110200' @@ -44,6 +43,6 @@ client.locations('krankenhaus', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/salzburg/index.js b/p/salzburg/index.js index 2ef98654..9d949143 100644 --- a/p/salzburg/index.js +++ b/p/salzburg/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ default: true, }] -const salzburgProfile = { +const profile = { ...baseProfile, ver: '1.32', @@ -88,4 +91,6 @@ const salzburgProfile = { reachableFrom: true, } -module.exports = salzburgProfile +export { + profile, +} diff --git a/p/salzburg/readme.md b/p/salzburg/readme.md index 364d2852..a39cc5f2 100644 --- a/p/salzburg/readme.md +++ b/p/salzburg/readme.md @@ -5,8 +5,8 @@ The government of [Salzburg](https://en.wikipedia.org/wiki/Salzburg) operates a ## Usage ```js -const createClient = require('hafas-client') -const salzburgProfile = require('hafas-client/p/salzburg') +import {createClient} from 'hafas-client' +import {salzburgProfile} from 'hafas-client/p/salzburg/index.js' // create a client with Salzburg profile const client = createClient(salzburgProfile, 'my-awesome-program') diff --git a/p/sbahn-muenchen/example.js b/p/sbahn-muenchen/example.js index 15cb4deb..b47baaf3 100644 --- a/p/sbahn-muenchen/example.js +++ b/p/sbahn-muenchen/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vbbProfile = require('.') - -const client = createClient(vbbProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Mittersendling to Charlottenburg // client.journeys('8004154', '0621790', {results: 1, polylines: true}) @@ -46,6 +45,6 @@ client.departures('8004154', {duration: 5}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/sbahn-muenchen/index.js b/p/sbahn-muenchen/index.js index 127c9318..fdbc8ee3 100644 --- a/p/sbahn-muenchen/index.js +++ b/p/sbahn-muenchen/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const sBahnMunichProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -19,4 +22,6 @@ const sBahnMunichProfile = { reachableFrom: true } -module.exports = sBahnMunichProfile +export { + profile, +} diff --git a/p/sbahn-muenchen/products.js b/p/sbahn-muenchen/products.js index 638f4642..48501a3b 100644 --- a/p/sbahn-muenchen/products.js +++ b/p/sbahn-muenchen/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'ice', mode: 'train', @@ -75,3 +73,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/sbahn-muenchen/readme.md b/p/sbahn-muenchen/readme.md index 326741a3..0acf4edb 100644 --- a/p/sbahn-muenchen/readme.md +++ b/p/sbahn-muenchen/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const sMuenchenProfile = require('hafas-client/p/sbahn-muenchen') +import {createClient} from 'hafas-client' +import {sMuenchenProfile} from 'hafas-client/p/sbahn-muenchen/index.js' // create a client with S-Bahn München profile const client = createClient(sMuenchenProfile, 'my-awesome-program') diff --git a/p/sbb/example.js b/p/sbb/example.js index 2573118b..31f495e4 100644 --- a/p/sbb/example.js +++ b/p/sbb/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../../') -const sbbProfile = require('.') - -const client = createClient(sbbProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const zurich1 = '8530813' // Zürich Kreuzplatz const zurich2 = '8503000' // Zürich HB @@ -49,6 +48,6 @@ client.journeys(baden1, baden2, { results: 1 }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/sbb/index.js b/p/sbb/index.js index 05a6ef43..69b08460 100644 --- a/p/sbb/index.js +++ b/p/sbb/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const sbbProfile = { +const profile = { ...baseProfile, locale: 'de-CH', timezone: 'Europe/Zurich', @@ -16,7 +19,9 @@ const sbbProfile = { reachableFrom: true, } -const withLocale = (locale) => ({...sbbProfile, locale}) +const withLocale = (locale) => ({...profile, locale}) -sbbProfile.withLocale = withLocale -module.exports = sbbProfile +export { + profile, + withLocale, +} diff --git a/p/sbb/products.js b/p/sbb/products.js index 4ca5b701..3f8b35b5 100644 --- a/p/sbb/products.js +++ b/p/sbb/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'express-train', mode: 'train', @@ -83,3 +81,7 @@ module.exports = [ default: true, }, ] + +export { + products, +} diff --git a/p/sbb/readme.md b/p/sbb/readme.md index 86f8697a..828c04de 100644 --- a/p/sbb/readme.md +++ b/p/sbb/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const sbbProfile = require('hafas-client/p/sbb') +import {createClient} from 'hafas-client' +import {sbbProfile} from 'hafas-client/p/sbb/index.js' // create a client with SBB profile const client = createClient(sbbProfile, 'my-awesome-program') diff --git a/p/sncb/example.js b/p/sncb/example.js index b9826857..c2b15a0c 100644 --- a/p/sncb/example.js +++ b/p/sncb/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const sncbProfile = require('.') - -const client = createClient(sncbProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const gentStPieters = '8892007' const bruxellesMidi = '8814001' @@ -36,6 +35,6 @@ client.journeys(gentStPieters, bruxellesMidi, {stopovers: true, remarks: true}) // client.reachableFrom(gentPaddenhoek) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/sncb/index.js b/p/sncb/index.js index 902707c4..a2376c27 100644 --- a/p/sncb/index.js +++ b/p/sncb/index.js @@ -1,18 +1,20 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const {readFileSync} = require('fs') -const {join} = require('path') -const {Agent} = require('https') -const {strictEqual: eql} = require('assert') -const {parseHook} = require('../../lib/profile-hooks') -const parseLine = require('../../parse/line') +import {readFileSync} from 'fs' +import {Agent} from 'https' +import {strictEqual as eql} from 'assert' +import {parseHook} from '../../lib/profile-hooks.js' +import {parseLine} from '../../parse/line.js' const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' // `www.belgianrail.be:443` doesn't provide the necessary CA certificate // chain for Node.js to trust the certificate, so we manually add it. // todo: fix this properly, e.g. by letting them know -const ca = readFileSync(join(__dirname, 'digicert-sha2-secure-server-ca.crt.pem')) +const ca = readFileSync(new URL('./digicert-sha2-secure-server-ca.crt.pem', import.meta.url).pathname) const agent = new Agent({ca}) const transformReq = (ctx, req) => ({...req, agent}) @@ -45,7 +47,7 @@ eql(lineNameWithoutFahrtNr({ parsed: {name: 'S1 123a', fahrtNr: '123'} }).name, 'S1 123a') -const sncbProfile = { +const profile = { ...baseProfile, locale: 'fr-BE', timezone: 'Europe/Brussels', @@ -62,4 +64,6 @@ const sncbProfile = { reachableFrom: false, // todo: fails with `H9240` "HAFAS Kernel: Internal error." } -module.exports = sncbProfile +export { + profile, +} diff --git a/p/sncb/products.js b/p/sncb/products.js index b82aeb31..40317fdd 100644 --- a/p/sncb/products.js +++ b/p/sncb/products.js @@ -1,7 +1,5 @@ -'use strict' - // https://www.belgiantrain.be/en/support/faq/faq-routes-schedules/faq-train-types -module.exports = [ // todo: 2, 8, 32, 128 +const products = [ // todo: 2, 8, 32, 128 { id: 'high-speed-train', mode: 'train', @@ -59,3 +57,7 @@ module.exports = [ // todo: 2, 8, 32, 128 default: true } ] + +export { + products, +} diff --git a/p/sncb/readme.md b/p/sncb/readme.md index e9503cde..867bfe57 100644 --- a/p/sncb/readme.md +++ b/p/sncb/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const sncbProfile = require('hafas-client/p/sncb') +import {createClient} from 'hafas-client' +import {sncbProfile} from 'hafas-client/p/sncb/index.js' // create a client with SNCB profile const client = createClient(sncbProfile, 'my-awesome-program') diff --git a/p/stv/example.js b/p/stv/example.js index 84d71d95..3488f12a 100644 --- a/p/stv/example.js +++ b/p/stv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const stvProfile = require('.') - -const client = createClient(stvProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const grazSonnenhang = '460413500' const grazHödlweg = '460415400' @@ -44,6 +43,6 @@ client.locations('sonnenhang', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/stv/index.js b/p/stv/index.js index b6b2e51c..4f88b03c 100644 --- a/p/stv/index.js +++ b/p/stv/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ default: true, }] -const stvProfile = { +const profile = { ...baseProfile, locale: 'at-DE', timezone: 'Europe/Vienna', @@ -86,4 +89,6 @@ const stvProfile = { reachableFrom: true, } -module.exports = stvProfile +export { + profile, +} diff --git a/p/stv/readme.md b/p/stv/readme.md index 14f0bf67..e23ae8a7 100644 --- a/p/stv/readme.md +++ b/p/stv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const stvProfile = require('hafas-client/p/stv') +import {createClient} from 'hafas-client' +import {stvProfile} from 'hafas-client/p/stv/index.js' // create a client with STV profile const client = createClient(stvProfile, 'my-awesome-program') diff --git a/p/svv/example.js b/p/svv/example.js index 7e58bf91..3c85c617 100644 --- a/p/svv/example.js +++ b/p/svv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const svvProfile = require('.') - -const client = createClient(svvProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const sam = '455086100' const volksgarten = '455082100' @@ -35,6 +34,6 @@ client.journeys(sam, volksgarten, {results: 1, polylines: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/svv/index.js b/p/svv/index.js index 0cf5e747..24d1f187 100644 --- a/p/svv/index.js +++ b/p/svv/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const svvProfile = { +const profile = { ...baseProfile, locale: 'at-DE', timezone: 'Europe/Vienna', @@ -16,4 +19,6 @@ const svvProfile = { refreshJourneyUseOutReconL: true, } -module.exports = svvProfile +export { + profile, +} diff --git a/p/svv/products.js b/p/svv/products.js index edd53e29..38588c0b 100644 --- a/p/svv/products.js +++ b/p/svv/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'bahn-s-bahn', mode: 'train', @@ -66,3 +64,7 @@ module.exports = [ default: true }, ] + +export { + products, +} diff --git a/p/svv/readme.md b/p/svv/readme.md index df5e53a4..76812a9e 100644 --- a/p/svv/readme.md +++ b/p/svv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const svvProfile = require('hafas-client/p/svv') +import {createClient} from 'hafas-client' +import {svvProfile} from 'hafas-client/p/svv/index.js' // create a client with SVV profile const client = createClient(svvProfile, 'my-awesome-program') diff --git a/p/tpg/example.js b/p/tpg/example.js index 40ea6371..eb5d21f8 100644 --- a/p/tpg/example.js +++ b/p/tpg/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const tpgProfile = require('.') - -const client = createClient(tpgProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const miremont = '100449' const moillebeau = '100451' @@ -48,6 +47,6 @@ client.journeys(miremont, moillebeau, {results: 1, stopovers: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/tpg/index.js b/p/tpg/index.js index eba6efc1..02f2eb2a 100644 --- a/p/tpg/index.js +++ b/p/tpg/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -67,7 +70,7 @@ const products = [{ default: true, }] -const tpgProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -81,4 +84,6 @@ const tpgProfile = { reachableFrom: true, } -module.exports = tpgProfile +export { + profile, +} diff --git a/p/tpg/readme.md b/p/tpg/readme.md index 4ed968d5..e2a07389 100644 --- a/p/tpg/readme.md +++ b/p/tpg/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const tpgProfile = require('hafas-client/p/TPG') +import {createClient} from 'hafas-client' +import {tpgProfile} from 'hafas-client/p/tpg/index.js' // create a client with TPG profile const client = createClient(tpgProfile, 'my-awesome-program') diff --git a/p/vbb/example.js b/p/vbb/example.js index 0d051cbb..b4d2b5a8 100644 --- a/p/vbb/example.js +++ b/p/vbb/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vbbProfile = require('.') - -const client = createClient(vbbProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') // Hauptbahnhof to Charlottenburg // client.journeys('900000003201', '900000024101', {results: 1, polylines: true}) @@ -43,6 +42,6 @@ client.locations('hansaplatz', {results: 2}) // return client.refreshJourney(journey.refreshToken, {stopovers: true, remarks: true}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vbb/index.js b/p/vbb/index.js index fabfdc3d..a1720173 100644 --- a/p/vbb/index.js +++ b/p/vbb/index.js @@ -1,15 +1,18 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const {parseHook} = require('../../lib/profile-hooks') +import {parseHook} from '../../lib/profile-hooks.js' -const parseAndAddLocationDHID = require('./parse-loc-dhid') -const _parseLine = require('../../parse/line') -const _parseLocation = require('../../parse/location') -const _parseJourney = require('../../parse/journey') -const _parseDeparture = require('../../parse/departure') +import {parseAndAddLocationDHID} from './parse-loc-dhid.js' +import {parseLine as _parseLine} from '../../parse/line.js' +import {parseLocation as _parseLocation} from '../../parse/location.js' +import {parseJourney as _parseJourney} from '../../parse/journey.js' +import {parseDeparture as _parseDeparture} from '../../parse/departure.js' const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' const parseLineWithShortName = ({parsed}, p) => { parsed.name = p.name.replace(/^(bus|tram)\s+/i, '') @@ -63,7 +66,7 @@ const parseDepartureRenameRingbahn = ({parsed}) => { return parsed } -const vbbProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -82,4 +85,6 @@ const vbbProfile = { reachableFrom: true, } -module.exports = vbbProfile +export { + profile, +} diff --git a/p/vbb/parse-loc-dhid.js b/p/vbb/parse-loc-dhid.js index 95c52a32..a787c166 100644 --- a/p/vbb/parse-loc-dhid.js +++ b/p/vbb/parse-loc-dhid.js @@ -1,5 +1,3 @@ -'use strict' - const dhidPrefix = 'A×' const parseAndAddLocationDHID = (loc, l) => { @@ -16,4 +14,6 @@ const parseAndAddLocationDHID = (loc, l) => { loc.stationDHID = dhid } -module.exports = parseAndAddLocationDHID +export { + parseAndAddLocationDHID, +} diff --git a/p/vbb/products.js b/p/vbb/products.js index 6d091247..9ad80d73 100644 --- a/p/vbb/products.js +++ b/p/vbb/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'suburban', mode: 'train', @@ -58,3 +56,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/vbb/readme.md b/p/vbb/readme.md index be184e21..93a6fa88 100644 --- a/p/vbb/readme.md +++ b/p/vbb/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vbbProfile = require('hafas-client/p/vbb') +import {createClient} from 'hafas-client' +import {vbbProfile} from 'hafas-client/p/vbb/index.js' // create a client with VBB profile const client = createClient(vbbProfile, 'my-awesome-program') diff --git a/p/vbn/example.js b/p/vbn/example.js index b30c56c8..d27b01ad 100644 --- a/p/vbn/example.js +++ b/p/vbn/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vbnProfile = require('.') - -const client = createClient(vbnProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const bremerhavenHbf = '9014418' const verden = '9093627' @@ -44,6 +43,6 @@ client.journeys(bremerhavenHbf, verden, {results: 1}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vbn/index.js b/p/vbn/index.js index a2e40ef0..5eeb5688 100644 --- a/p/vbn/index.js +++ b/p/vbn/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const insaProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -20,4 +23,6 @@ const insaProfile = { refreshJourneyUseOutReconL: true, } -module.exports = insaProfile; +export { + profile, +} diff --git a/p/vbn/products.js b/p/vbn/products.js index 16d3c106..1846d564 100644 --- a/p/vbn/products.js +++ b/p/vbn/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'express-train', mode: 'train', @@ -74,3 +72,7 @@ module.exports = [ default: true } ] + +export { + products, +} diff --git a/p/vbn/readme.md b/p/vbn/readme.md index cd73f161..92be393f 100644 --- a/p/vbn/readme.md +++ b/p/vbn/readme.md @@ -5,8 +5,8 @@ The [*Verkehrsverbund Bremen/Niedersachsen (VBN)*](https://de.wikipedia.org/wiki ## Usage ```js -const createClient = require('hafas-client') -const vbnProfile = require('hafas-client/p/vbn') +import {createClient} from 'hafas-client' +import {vbnProfile} from 'hafas-client/p/vbn/index.js' // create a client with VBN profile const client = createClient(vbnProfile, 'my-awesome-program') diff --git a/p/vkg/example.js b/p/vkg/example.js index a936eb01..f8948867 100644 --- a/p/vkg/example.js +++ b/p/vkg/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vosProfile = require('.') - -const client = createClient(vosProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const spittalMittelschule = '420512200' const klagenfurtSteingasse = '420649500' @@ -42,6 +41,6 @@ client.locations('steingasse', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vkg/index.js b/p/vkg/index.js index ebca827a..f59ef35c 100644 --- a/p/vkg/index.js +++ b/p/vkg/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ // todo: what is `8`? default: true, }] -const vosProfile = { +const profile = { ...baseProfile, locale: 'at-DE', timezone: 'Europe/Vienna', @@ -87,4 +90,6 @@ const vosProfile = { reachableFrom: true, } -module.exports = vosProfile +export { + profile, +} diff --git a/p/vkg/readme.md b/p/vkg/readme.md index 9b799614..410e59af 100644 --- a/p/vkg/readme.md +++ b/p/vkg/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vkgProfile = require('hafas-client/p/vkg') +import {createClient} from 'hafas-client' +import {vkgProfile} from 'hafas-client/p/vkg/index.js' // create a client with VKG profile const client = createClient(vkgProfile, 'my-awesome-program') diff --git a/p/vmt/example.js b/p/vmt/example.js index 6fc47dcb..aa665e93 100644 --- a/p/vmt/example.js +++ b/p/vmt/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vmtProfile = require('.') - -const client = createClient(vmtProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const jena = '190014' const gothaZOB = '167280' @@ -39,6 +38,6 @@ client.journeys(jena, gothaZOB, {results: 1}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vmt/index.js b/p/vmt/index.js index 3ad804f9..39fccee7 100644 --- a/p/vmt/index.js +++ b/p/vmt/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const hvvProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -20,4 +23,6 @@ const hvvProfile = { remarks: false, // seems like ver >= 1.20 is required } -module.exports = hvvProfile +export { + profile, +} diff --git a/p/vmt/products.js b/p/vmt/products.js index 2567dd30..cfe75279 100644 --- a/p/vmt/products.js +++ b/p/vmt/products.js @@ -1,6 +1,5 @@ -'use strict' - -module.exports = [ // todo: what is `512`? +// todo: what is `512`? +const products = [ { id: 'long-distance-train', mode: 'train', @@ -36,3 +35,7 @@ module.exports = [ // todo: what is `512`? default: true } ] + +export { + products, +} diff --git a/p/vmt/readme.md b/p/vmt/readme.md index 07ed51fb..cb6c78ce 100644 --- a/p/vmt/readme.md +++ b/p/vmt/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vmtProfile = require('hafas-client/p/vmt') +import {createClient} from 'hafas-client' +import {vmtProfile} from 'hafas-client/p/vmt/index.js' // create a client with VMT profile const client = createClient(vmtProfile, 'my-awesome-program') diff --git a/p/vor/example.js b/p/vor/example.js index ce331c69..86ea82bd 100644 --- a/p/vor/example.js +++ b/p/vor/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vorProfile = require('.') - -const client = createClient(vorProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const stPöltenLinzerTor = '431277900' const eisenstadtSchlossplatz = '415003300' @@ -44,6 +43,6 @@ client.locations('schlossplatz', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vor/index.js b/p/vor/index.js index 88daf2a6..dfc8ab32 100644 --- a/p/vor/index.js +++ b/p/vor/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ default: true, }] -const vorProfile = { +const profile = { ...baseProfile, auth: { type: 'USER', @@ -95,4 +98,6 @@ const vorProfile = { reachableFrom: true, } -module.exports = vorProfile +export { + profile, +} diff --git a/p/vor/readme.md b/p/vor/readme.md index 1387f43f..8b1cf86b 100644 --- a/p/vor/readme.md +++ b/p/vor/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vorProfile = require('hafas-client/p/vor') +import {createClient} from 'hafas-client' +import {vorProfile} from 'hafas-client/p/vor/index.js' // create a client with VOR profile const client = createClient(vorProfile, 'my-awesome-program') diff --git a/p/vos/example.js b/p/vos/example.js index f226888c..ef4c2889 100644 --- a/p/vos/example.js +++ b/p/vos/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vosProfile = require('.') - -const client = createClient(vosProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const saarplatz = '9071733' const finkenweg = '9071574' @@ -48,6 +47,6 @@ const finkenweg = '9071574' // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vos/index.js b/p/vos/index.js index 5b04a036..fbd07ba0 100644 --- a/p/vos/index.js +++ b/p/vos/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ default: true, }] -const vosProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -88,4 +91,6 @@ const vosProfile = { reachableFrom: true, } -module.exports = vosProfile +export { + profile, +} diff --git a/p/vos/readme.md b/p/vos/readme.md index 69928647..325e14db 100644 --- a/p/vos/readme.md +++ b/p/vos/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vosProfile = require('hafas-client/p/VOS') +import {createClient} from 'hafas-client' +import {vosProfile} from 'hafas-client/p/vos/index.js' // create a client with VOS profile const client = createClient(vosProfile, 'my-awesome-program') diff --git a/p/vrn/example.js b/p/vrn/example.js index 54b49529..ed977c4d 100644 --- a/p/vrn/example.js +++ b/p/vrn/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const hvvProfile = require('.') - -const client = createClient(hvvProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const ludwigshafen = '8000236' const meckesheim = '8003932' @@ -48,6 +47,6 @@ client.journeys(ludwigshafen, meckesheim, {results: 1, polylines: true}) // }, {results: 10}) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vrn/index.js b/p/vrn/index.js index 5d22f8d8..4deb4c7c 100644 --- a/p/vrn/index.js +++ b/p/vrn/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const hvvProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -17,4 +20,6 @@ const hvvProfile = { refreshJourneyUseOutReconL: true, } -module.exports = hvvProfile +export { + profile, +} diff --git a/p/vrn/products.js b/p/vrn/products.js index f7324b8a..4fc8ef5e 100644 --- a/p/vrn/products.js +++ b/p/vrn/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ // todo: what is `64`? { id: 'regional-train', @@ -59,3 +57,7 @@ module.exports = [ default: false, }, ] + +export { + products, +} diff --git a/p/vrn/readme.md b/p/vrn/readme.md index 84dae60e..fa844ee3 100644 --- a/p/vrn/readme.md +++ b/p/vrn/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vrnProfile = require('hafas-client/p/vrn') +import {createClient} from 'hafas-client' +import {vrnProfile} from 'hafas-client/p/vrn/index.js' // create a client with VRN profile const client = createClient(vrnProfile, 'my-awesome-program') diff --git a/p/vsn/example.js b/p/vsn/example.js index 9fc2ac2b..e95ba854 100644 --- a/p/vsn/example.js +++ b/p/vsn/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vsnProfile = require('.') - -const client = createClient(vsnProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') client.journeys('9033961', '9033962', {results: 1, polylines: true}) @@ -43,6 +42,6 @@ client.journeys('9033961', '9033962', {results: 1, polylines: true}) // maxDuration: 8 // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vsn/index.js b/p/vsn/index.js index 7348d2f6..efe9e618 100644 --- a/p/vsn/index.js +++ b/p/vsn/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const vsnProfile = { +const profile = { ...baseProfile, locale: 'de-DE', timezone: 'Europe/Berlin', @@ -20,4 +23,6 @@ const vsnProfile = { reachableFrom: true, } -module.exports = vsnProfile +export { + profile, +} diff --git a/p/vsn/products.js b/p/vsn/products.js index f2e064fa..49efd25e 100644 --- a/p/vsn/products.js +++ b/p/vsn/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'nationalExpress', mode: 'train', @@ -82,3 +80,7 @@ module.exports = [ default: true }, ] + +export { + products, +} diff --git a/p/vsn/readme.md b/p/vsn/readme.md index 4db240d6..e7d5a2a3 100644 --- a/p/vsn/readme.md +++ b/p/vsn/readme.md @@ -3,8 +3,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vsnProfile = require('hafas-client/p/vsn') +import {createClient} from 'hafas-client' +import {vsnProfile} from 'hafas-client/p/vsn/index.js' // create a client with VSN profile const client = createClient(vsnProfile, 'my-awesome-program') diff --git a/p/vvt/example.js b/p/vvt/example.js index 4d3e1d8c..c60f4940 100644 --- a/p/vvt/example.js +++ b/p/vvt/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vvtProfile = require('.') - -const client = createClient(vvtProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const innsbruckMitterweg = '476152300' const kufsteinListstr = '476603100' @@ -42,6 +41,6 @@ client.departures(innsbruckMitterweg, {duration: 1}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vvt/index.js b/p/vvt/index.js index d206cf64..368cdbbb 100644 --- a/p/vvt/index.js +++ b/p/vvt/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ // todo: what is `8`? default: true, }] -const vvtProfile = { +const profile = { ...baseProfile, locale: 'at-DE', timezone: 'Europe/Vienna', @@ -87,4 +90,6 @@ const vvtProfile = { reachableFrom: true, } -module.exports = vvtProfile +export { + profile, +} diff --git a/p/vvt/readme.md b/p/vvt/readme.md index 297dbad9..0628c128 100644 --- a/p/vvt/readme.md +++ b/p/vvt/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vvtProfile = require('hafas-client/p/vvt') +import {createClient} from 'hafas-client' +import {vvtProfile} from 'hafas-client/p/vvt/index.js' // create a client with VVT profile const client = createClient(vvtProfile, 'my-awesome-program') diff --git a/p/vvv/example.js b/p/vvv/example.js index 106fe945..e86da3d8 100644 --- a/p/vvv/example.js +++ b/p/vvv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const vvvProfile = require('.') - -const client = createClient(vvvProfile, 'hafas-client example') +const client = createClient(profile, 'hafas-client example') const bregenzLandeskrankenhaus = '480195700' const bludenzGymnasium = '480031300' @@ -44,6 +43,6 @@ client.locations('krankenhaus', {results: 3}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/vvv/index.js b/p/vvv/index.js index de79c505..2a8da912 100644 --- a/p/vvv/index.js +++ b/p/vvv/index.js @@ -1,4 +1,7 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') @@ -74,7 +77,7 @@ const products = [{ default: true, }] -const vosProfile = { +const profile = { ...baseProfile, auth: { aid: 'and20201hf7mcf9bv3nv8g5f', @@ -93,4 +96,6 @@ const vosProfile = { reachableFrom: true, } -module.exports = vosProfile +export { + profile, +} diff --git a/p/vvv/readme.md b/p/vvv/readme.md index 0eeaf061..2fc281ce 100644 --- a/p/vvv/readme.md +++ b/p/vvv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const vvvProfile = require('hafas-client/p/vvv') +import {createClient} from 'hafas-client' +import {vvvProfile} from 'hafas-client/p/vvv/index.js' // create a client with VVV profile const client = createClient(vvvProfile, 'my-awesome-program') diff --git a/p/zvv/example.js b/p/zvv/example.js index 04a099c9..84350e02 100644 --- a/p/zvv/example.js +++ b/p/zvv/example.js @@ -1,9 +1,8 @@ -'use strict' +import {inspect} from 'util' +import {createClient} from '../../index.js' +import {profile} from './index.js' -const createClient = require('../..') -const zvvProfile = require('.') - -const client = createClient(zvvProfile, 'hafas-client-example') +const client = createClient(profile, 'hafas-client-example') const bürkliplatz = '8591105' @@ -47,6 +46,6 @@ client.locations('bürkli', {results: 2}) // }) .then((data) => { - console.log(require('util').inspect(data, {depth: null, colors: true})) + console.log(inspect(data, {depth: null, colors: true})) }) .catch(console.error) diff --git a/p/zvv/index.js b/p/zvv/index.js index 3aa5b7e0..a59785a7 100644 --- a/p/zvv/index.js +++ b/p/zvv/index.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) const baseProfile = require('./base.json') -const products = require('./products') +import {products} from './products.js' -const zvvProfile = { +const profile = { ...baseProfile, locale: 'de-CH', timezone: 'Europe/Zurich', @@ -16,4 +19,6 @@ const zvvProfile = { reachableFrom: true, } -module.exports = zvvProfile +export { + profile, +} diff --git a/p/zvv/products.js b/p/zvv/products.js index c4e9f94b..b16e2b9a 100644 --- a/p/zvv/products.js +++ b/p/zvv/products.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const products = [ { id: 'high-speed-train', mode: 'train', @@ -58,3 +56,7 @@ module.exports = [ default: true, }, ] + +export { + products, +} diff --git a/p/zvv/readme.md b/p/zvv/readme.md index a89160a8..2808e7d7 100644 --- a/p/zvv/readme.md +++ b/p/zvv/readme.md @@ -5,8 +5,8 @@ ## Usage ```js -const createClient = require('hafas-client') -const zvvProfile = require('hafas-client/p/zvv') +import {createClient} from 'hafas-client' +import {zvvProfile} from 'hafas-client/p/zvv/index.js' // create a client with ZVV profile const client = createClient(zvvProfile, 'my-awesome-program') diff --git a/package.json b/package.json index f159b908..77e3b89f 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "hafas-client", "description": "Client for HAFAS public transport APIs.", "version": "5.26.2", + "type": "module", "main": "index.js", "files": [ "index.js", diff --git a/parse/arrival-or-departure.js b/parse/arrival-or-departure.js index 81df5cbf..0da93f2d 100644 --- a/parse/arrival-or-departure.js +++ b/parse/arrival-or-departure.js @@ -1,6 +1,4 @@ -'use strict' - -const findRemarks = require('./find-remarks') +import {findRemarks} from './find-remarks.js' const ARRIVAL = 'a' const DEPARTURE = 'd' @@ -83,4 +81,6 @@ const createParseArrOrDep = (prefix) => { return parseArrOrDep } -module.exports = createParseArrOrDep +export { + createParseArrOrDep, +} diff --git a/parse/arrival.js b/parse/arrival.js index c21e4261..dfd7e4b4 100644 --- a/parse/arrival.js +++ b/parse/arrival.js @@ -1,8 +1,8 @@ -'use strict' - -const createParseArrOrDep = require('./arrival-or-departure') +import {createParseArrOrDep} from './arrival-or-departure.js' const ARRIVAL = 'a' const parseArrival = createParseArrOrDep(ARRIVAL) -module.exports = parseArrival +export { + parseArrival, +} diff --git a/parse/common.js b/parse/common.js index 0d627d7c..f7693cf6 100644 --- a/parse/common.js +++ b/parse/common.js @@ -1,7 +1,5 @@ -'use strict' - -const omit = require('lodash/omit') -const createFindInTree = require('../lib/find-in-tree'); +import omit from 'lodash/omit.js' +import {createFindInTree} from '../lib/find-in-tree.js' const findInTree = createFindInTree([ '**.oprX', '**.icoX', '**.prodX', '**.pRefL', '**.locX', @@ -142,4 +140,6 @@ const parseCommonData = (_ctx) => { return common } -module.exports = parseCommonData +export { + parseCommonData, +} diff --git a/parse/date-time.js b/parse/date-time.js index 46639f88..2ee5fa52 100644 --- a/parse/date-time.js +++ b/parse/date-time.js @@ -1,6 +1,4 @@ -'use strict' - -const {DateTime, FixedOffsetZone, IANAZone} = require('luxon') +import {DateTime, FixedOffsetZone, IANAZone} from 'luxon' const timezones = new WeakMap() @@ -35,4 +33,6 @@ const parseDateTime = ({profile}, date, time, tzOffset = null, timestamp = false return timestamp ? dt.toMillis() : dt.toISO({suppressMilliseconds: true}) } -module.exports = parseDateTime +export { + parseDateTime, +} diff --git a/parse/departure.js b/parse/departure.js index 254a6310..b8587cab 100644 --- a/parse/departure.js +++ b/parse/departure.js @@ -1,8 +1,8 @@ -'use strict' - -const createParseArrOrDep = require('./arrival-or-departure') +import {createParseArrOrDep} from './arrival-or-departure.js' const DEPARTURE = 'd' const parseDeparture = createParseArrOrDep(DEPARTURE) -module.exports = parseDeparture +export { + parseDeparture, +} diff --git a/parse/find-remarks.js b/parse/find-remarks.js index 26b64c32..bde9f64f 100644 --- a/parse/find-remarks.js +++ b/parse/find-remarks.js @@ -1,6 +1,4 @@ -'use strict' - -const flatMap = require('lodash/flatMap') +import flatMap from 'lodash/flatMap.js' // There are two kinds of notes: "remarks" (in `remL`) and HAFAS // Information Manager (HIM) notes (in `himL`). The former describe @@ -20,4 +18,6 @@ const findRemarks = (refs) => { }) } -module.exports = findRemarks +export { + findRemarks, +} diff --git a/parse/hint.js b/parse/hint.js index dc05d3d4..7acb56a2 100644 --- a/parse/hint.js +++ b/parse/hint.js @@ -1,5 +1,3 @@ -'use strict' - const codesByIcon = Object.assign(Object.create(null), { cancel: 'cancelled', himWarn: 'disturbance', @@ -110,4 +108,6 @@ const parseHint = (ctx, h) => { return null } -module.exports = parseHint +export { + parseHint, +} diff --git a/parse/icon.js b/parse/icon.js index 14e0180d..fb190f06 100644 --- a/parse/icon.js +++ b/parse/icon.js @@ -1,5 +1,3 @@ -'use strict' - const parseIcon = (ctx, i) => { if (i.res === 'Empty') return null const res = { @@ -11,4 +9,6 @@ const parseIcon = (ctx, i) => { return res } -module.exports = parseIcon +export { + parseIcon, +} diff --git a/parse/index.js b/parse/index.js deleted file mode 100644 index e73bfb79..00000000 --- a/parse/index.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict' - -module.exports = { - dateTime: require('./date-time'), - location: require('./location'), - line: require('./line'), - hint: require('./hint'), - operator: require('./operator'), - stopover: require('./stopover'), - journeyLeg: require('./journey-leg'), - journey: require('./journey'), - nearby: require('./nearby'), - movement: require('./movement') -} diff --git a/parse/journey-leg.js b/parse/journey-leg.js index 8dfd7e54..7e6c2253 100644 --- a/parse/journey-leg.js +++ b/parse/journey-leg.js @@ -1,6 +1,4 @@ -'use strict' - -const findRemarks = require('./find-remarks') +import {findRemarks} from './find-remarks.js' const clone = obj => Object.assign({}, obj) @@ -185,4 +183,6 @@ const parseJourneyLeg = (ctx, pt, date) => { // pt = raw leg return res } -module.exports = parseJourneyLeg +export { + parseJourneyLeg, +} diff --git a/parse/journey.js b/parse/journey.js index 13292c5c..40cba50e 100644 --- a/parse/journey.js +++ b/parse/journey.js @@ -1,6 +1,4 @@ -'use strict' - -const findRemarks = require('./find-remarks') +import {findRemarks} from './find-remarks.js' // todo: c.conSubscr (e.g. `F`) // todo: c.trfRes x vbb-parse-ticket @@ -45,4 +43,6 @@ const parseJourney = (ctx, j) => { // j = raw jouney return res } -module.exports = parseJourney +export { + parseJourney, +} diff --git a/parse/line.js b/parse/line.js index e21bd5fd..c45bc911 100644 --- a/parse/line.js +++ b/parse/line.js @@ -1,6 +1,4 @@ -'use strict' - -const slugg = require('slugg') +import slugg from 'slugg' const parseLine = ({profile}, p) => { if (!p) return null // todo: handle this upstream @@ -51,4 +49,6 @@ const parseLine = ({profile}, p) => { return res } -module.exports = parseLine +export { + parseLine, +} diff --git a/parse/location.js b/parse/location.js index cccf16ff..db4a3567 100644 --- a/parse/location.js +++ b/parse/location.js @@ -1,7 +1,5 @@ -'use strict' - -const {parse} = require('qs') -const get = require('lodash/get') +import {parse} from 'qs' +import get from 'lodash/get.js' const POI = 'P' const STATION = 'S' @@ -133,4 +131,6 @@ const parseLocationWithoutCycles = (ctx, l, ...args) => { return parseLocation({...ctx, [seen]: newSeen}, l, ...args) } -module.exports = parseLocationWithoutCycles +export { + parseLocationWithoutCycles as parseLocation, +} diff --git a/parse/movement.js b/parse/movement.js index 89a82720..ac616f8c 100644 --- a/parse/movement.js +++ b/parse/movement.js @@ -1,5 +1,3 @@ -'use strict' - // todo: what is m.dirGeo? maybe the speed? // todo: what is m.stopL? // todo: what is m.proc? wut? @@ -52,4 +50,6 @@ const parseMovement = (ctx, m) => { // m = raw movement return res } -module.exports = parseMovement +export { + parseMovement, +} diff --git a/parse/nearby.js b/parse/nearby.js index 11cd6d4c..26756600 100644 --- a/parse/nearby.js +++ b/parse/nearby.js @@ -1,5 +1,3 @@ -'use strict' - // todo: remarks // todo: lines // todo: what is s.pCls? @@ -12,4 +10,6 @@ const parseNearby = (ctx, n) => { // n = raw nearby location return res } -module.exports = parseNearby +export { + parseNearby, +} diff --git a/parse/operator.js b/parse/operator.js index f8e54d49..07ac2e65 100644 --- a/parse/operator.js +++ b/parse/operator.js @@ -1,6 +1,4 @@ -'use strict' - -const slugg = require('slugg') +import slugg from 'slugg' const parseOperator = (ctx, a) => { const name = a.name && a.name.trim() @@ -12,4 +10,6 @@ const parseOperator = (ctx, a) => { } } -module.exports = parseOperator +export { + parseOperator, +} diff --git a/parse/platform.js b/parse/platform.js index 4276b3ed..3fef0dea 100644 --- a/parse/platform.js +++ b/parse/platform.js @@ -1,5 +1,3 @@ -'use strict' - const parsePlatform = (ctx, platfS, platfR, cncl = false) => { let planned = platfS || null let prognosed = platfR || null @@ -17,4 +15,6 @@ const parsePlatform = (ctx, platfS, platfR, cncl = false) => { } } -module.exports = parsePlatform +export { + parsePlatform, +} diff --git a/parse/polyline.js b/parse/polyline.js index e2aaafef..55164c78 100644 --- a/parse/polyline.js +++ b/parse/polyline.js @@ -1,7 +1,7 @@ -'use strict' +import _googlePolyline from 'google-polyline' +import distance from 'gps-distance' -const {decode} = require('google-polyline') -const distance = require('gps-distance') +const {decode} = _googlePolyline // todo: what is p.delta? // todo: what is p.type? @@ -46,4 +46,6 @@ const parsePolyline = (ctx, p) => { // p = raw polyline } } -module.exports = parsePolyline +export { + parsePolyline, +} diff --git a/parse/products-bitmask.js b/parse/products-bitmask.js index e854a116..f9f335ba 100644 --- a/parse/products-bitmask.js +++ b/parse/products-bitmask.js @@ -1,5 +1,3 @@ -'use strict' - const parseBitmask = ({profile}, bitmask) => { const res = {} for (let product of profile.products) res[product.id] = false @@ -14,4 +12,6 @@ const parseBitmask = ({profile}, bitmask) => { return res } -module.exports = parseBitmask +export { + parseBitmask, +} diff --git a/parse/prognosis-type.js b/parse/prognosis-type.js index 6b74fffd..057f044e 100644 --- a/parse/prognosis-type.js +++ b/parse/prognosis-type.js @@ -1,5 +1,3 @@ -'use strict' - const parsePrognosisType = (_, progType) => { return { 'PROGNOSED': 'prognosed', @@ -8,4 +6,6 @@ const parsePrognosisType = (_, progType) => { }[progType] || null } -module.exports = parsePrognosisType +export { + parsePrognosisType, +} diff --git a/parse/scheduled-days.js b/parse/scheduled-days.js index 2fde272d..20266f4f 100644 --- a/parse/scheduled-days.js +++ b/parse/scheduled-days.js @@ -1,6 +1,4 @@ -'use strict' - -const {DateTime} = require('luxon') +import {DateTime} from 'luxon' // todo: DRY with parse/date-time.js const parseDate = (date) => { @@ -42,4 +40,6 @@ const parseScheduledDays = (ctx, sDays) => { return res } -module.exports = parseScheduledDays +export { + parseScheduledDays, +} diff --git a/parse/stopover.js b/parse/stopover.js index 1268c5c2..a01c4fda 100644 --- a/parse/stopover.js +++ b/parse/stopover.js @@ -1,6 +1,4 @@ -'use strict' - -const findRemarks = require('./find-remarks') +import {findRemarks} from './find-remarks.js' const parseStopover = (ctx, st, date) => { // st = raw stopover const {profile, opt} = ctx @@ -46,4 +44,6 @@ const parseStopover = (ctx, st, date) => { // st = raw stopover return res } -module.exports = parseStopover +export { + parseStopover, +} diff --git a/parse/trip.js b/parse/trip.js index bf35afbd..76bfd000 100644 --- a/parse/trip.js +++ b/parse/trip.js @@ -1,8 +1,6 @@ -'use strict' - -const minBy = require('lodash/minBy') -const maxBy = require('lodash/maxBy') -const last = require('lodash/last') +import minBy from 'lodash/minBy.js' +import maxBy from 'lodash/maxBy.js' +import last from 'lodash/last.js' const parseTrip = (ctx, t) => { // t = raw trip const {profile, opt} = ctx @@ -46,4 +44,6 @@ const parseTrip = (ctx, t) => { // t = raw trip return trip } -module.exports = parseTrip +export { + parseTrip, +} diff --git a/parse/warning.js b/parse/warning.js index 8a3007d4..d478827e 100644 --- a/parse/warning.js +++ b/parse/warning.js @@ -1,7 +1,5 @@ -'use strict' - -const brToNewline = require('@derhuerst/br2nl') -const omit = require('lodash/omit') +import brToNewline from '@derhuerst/br2nl' +import omit from 'lodash/omit.js' const typesByIcon = Object.assign(Object.create(null), { HimWarn: 'status' @@ -106,4 +104,6 @@ const parseWarning = (ctx, w) => { return res } -module.exports = parseWarning +export { + parseWarning, +} diff --git a/parse/when.js b/parse/when.js index 8425813e..3d339e9c 100644 --- a/parse/when.js +++ b/parse/when.js @@ -1,5 +1,3 @@ -'use strict' - const parseWhen = (ctx, date, timeS, timeR, tzOffset, cncl = false) => { const parse = ctx.profile.parseDateTime @@ -28,4 +26,6 @@ const parseWhen = (ctx, date, timeS, timeR, tzOffset, cncl = false) => { } } -module.exports = parseWhen +export { + parseWhen, +} diff --git a/readme.md b/readme.md index 547887b6..a8a620e5 100644 --- a/readme.md +++ b/readme.md @@ -49,8 +49,8 @@ npm install hafas-client Pick the [profile](p/readme.md) for the HAFAS endpoint covering the area you want to get data for. Pass the profile and a descriptive name for your program into the `createClient` function: ```js -const createClient = require('hafas-client') -const dbProfile = require('hafas-client/p/db') +import {createClient} from 'hafas-client' +import {dbProfile} from 'hafas-client/p/db/index.js' // create a client with the Deutsche Bahn profile const client = createClient(dbProfile, 'my-awesome-program') diff --git a/retry.js b/retry.js index 89150bad..3fde5b15 100644 --- a/retry.js +++ b/retry.js @@ -1,7 +1,5 @@ -'use strict' - -const retry = require('p-retry') -const defaultProfile = require('./lib/default-profile') +import retry from 'p-retry' +import {defaultProfile} from './lib/default-profile.js' const retryDefaults = { retries: 3, @@ -36,4 +34,6 @@ const withRetrying = (profile, retryOpts = {}) => { } } -module.exports = withRetrying +export { + withRetrying, +} diff --git a/test/bvg-arrivals.js b/test/bvg-arrivals.js index 0ee83c0f..bee83e94 100644 --- a/test/bvg-arrivals.js +++ b/test/bvg-arrivals.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/bvg') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/bvg/index.js' const res = require('./fixtures/bvg-arrivals.json') -const expected = require('./fixtures/bvg-arrivals.js') +import {bvgArrivals as expected} from './fixtures/bvg-arrivals.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/bvg-journey.js b/test/bvg-journey.js index 3d2f1072..009b2bcb 100644 --- a/test/bvg-journey.js +++ b/test/bvg-journey.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/bvg') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/bvg/index.js' const res = require('./fixtures/bvg-journey.json') -const expected = require('./fixtures/bvg-journey.js') +import {bvgJourney as expected} from './fixtures/bvg-journey.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/bvg-radar.js b/test/bvg-radar.js index 371febc4..75f09435 100644 --- a/test/bvg-radar.js +++ b/test/bvg-radar.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/bvg') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/bvg/index.js' const res = require('./fixtures/bvg-radar.json') -const expected = require('./fixtures/bvg-radar.js') +import {bvgRadar as expected} from './fixtures/bvg-radar.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/bvg-trip-with-occupancy.js b/test/bvg-trip-with-occupancy.js index 011b3c02..8e4da50f 100644 --- a/test/bvg-trip-with-occupancy.js +++ b/test/bvg-trip-with-occupancy.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/bvg') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/bvg/index.js' const res = require('./fixtures/bvg-trip-with-occupancy.json') -const expected = require('./fixtures/bvg-trip-with-occupancy.js') +import {bvgTripWithOccupancy as expected} from './fixtures/bvg-trip-with-occupancy.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/db-arrivals.js b/test/db-arrivals.js index daf5e47a..a8265255 100644 --- a/test/db-arrivals.js +++ b/test/db-arrivals.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/db') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/db/index.js' const res = require('./fixtures/db-arrivals.json') -const expected = require('./fixtures/db-arrivals.js') +import {dbArrivals as expected} from './fixtures/db-arrivals.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/db-deps-with-destination.js b/test/db-deps-with-destination.js index e0728cfd..a233dd6e 100644 --- a/test/db-deps-with-destination.js +++ b/test/db-deps-with-destination.js @@ -1,11 +1,13 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/db') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/db/index.js' const res = require('./fixtures/db-deps-with-destination.json') -// const expected = require('./fixtures/db-deps-with-destination.js') const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/db-journey-2.js b/test/db-journey-2.js index e602bb53..34972b03 100644 --- a/test/db-journey-2.js +++ b/test/db-journey-2.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/db') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/db/index.js' const res = require('./fixtures/db-journey-2.json') -const expected = require('./fixtures/db-journey-2.js') +import {dbJourney as expected} from './fixtures/db-journey-2.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/db-journey-fpB-fpE-2-years.js b/test/db-journey-fpB-fpE-2-years.js index f1712849..277b000a 100644 --- a/test/db-journey-fpB-fpE-2-years.js +++ b/test/db-journey-fpB-fpE-2-years.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/db') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/db/index.js' const res = require('./fixtures/db-journey-fpB-fpE-2-years.json') const client = createClient(rawProfile, 'public-transport/hafas-client:test') diff --git a/test/db-journey-polyline.js b/test/db-journey-polyline.js index 62500f8c..32a0f302 100644 --- a/test/db-journey-polyline.js +++ b/test/db-journey-polyline.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/db') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/db/index.js' const res = require('./fixtures/db-journey-polyline.json') -const expected = require('./fixtures/db-journey-polyline.js') +import {dbJourneyPolyline as expected} from './fixtures/db-journey-polyline.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/db-journey-tzoffset-0.js b/test/db-journey-tzoffset-0.js index 2767839f..61450106 100644 --- a/test/db-journey-tzoffset-0.js +++ b/test/db-journey-tzoffset-0.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/db') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/db/index.js' const resDTZOffset0 = require('./fixtures/db-journey-dtzoffset-0.json') const resATZOffset0 = require('./fixtures/db-journey-atzoffset-0.json') diff --git a/test/db-journey.js b/test/db-journey.js index 92aab6af..8360d41c 100644 --- a/test/db-journey.js +++ b/test/db-journey.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/db') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/db/index.js' const res = require('./fixtures/db-journey.json') -const expected = require('./fixtures/db-journey.js') +import {dbJourney as expected} from './fixtures/db-journey.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/db-netz-remarks.js b/test/db-netz-remarks.js index 6a350bf3..d892a927 100644 --- a/test/db-netz-remarks.js +++ b/test/db-netz-remarks.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const parseCommon = require('../parse/common') -const defaultProfile = require('../lib/default-profile') +import {parseCommonData as parseCommon} from '../parse/common.js' +import {defaultProfile} from '../lib/default-profile.js' const res = require('./fixtures/db-netz-remarks.json') const profile = { diff --git a/test/db-stop.js b/test/db-stop.js index e3a6a2fd..ffc8f4a7 100644 --- a/test/db-stop.js +++ b/test/db-stop.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/db') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/db/index.js' const res = require('./fixtures/db-stop.json') -const expected = require('./fixtures/db-stop.js') +import {dbStop as expected} from './fixtures/db-stop.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/e2e/bls.js b/test/e2e/bls.js index 4b473e09..fb5ea9a0 100644 --- a/test/e2e/bls.js +++ b/test/e2e/bls.js @@ -1,12 +1,10 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const blsProfile = require('../../p/bls') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as blsProfile} from '../../p/bls/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(blsProfile.timezone, blsProfile.locale, T_MOCK) diff --git a/test/e2e/bvg.js b/test/e2e/bvg.js index 5c539e32..87199af4 100644 --- a/test/e2e/bvg.js +++ b/test/e2e/bvg.js @@ -1,33 +1,31 @@ -'use strict' - // todo: DRY with vbb tests -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') -const {DateTime} = require('luxon') -const flatMap = require('lodash/flatMap') +import tap from 'tap' -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const bvgProfile = require('../../p/bvg') -const products = require('../../p/bvg/products') -const createValidate = require('./lib/validate-fptf-with') -const createBvgValidators = require('./lib/vbb-bvg-validators') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testJourneysWalkingSpeed = require('./lib/journeys-walking-speed') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testLegCycleAlternatives = require('./lib/leg-cycle-alternatives') -const testRefreshJourney = require('./lib/refresh-journey') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') -const testJourneysWithDetour = require('./lib/journeys-with-detour') -const testReachableFrom = require('./lib/reachable-from') -const testRemarks = require('./lib/remarks') -const testLines = require('./lib/lines') +import isRoughlyEqual from 'is-roughly-equal' +import {DateTime} from 'luxon' +import flatMap from 'lodash/flatMap.js' + +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as bvgProfile} from '../../p/bvg/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {createVbbBvgValidators} from './lib/vbb-bvg-validators.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testJourneysWalkingSpeed} from './lib/journeys-walking-speed.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testLegCycleAlternatives} from './lib/leg-cycle-alternatives.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' +import {testReachableFrom} from './lib/reachable-from.js' +import {testRemarks} from './lib/remarks.js' +import {testLines} from './lib/lines.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(bvgProfile.timezone, bvgProfile.locale, T_MOCK) @@ -38,7 +36,7 @@ const { validateJourneyLeg, validateDeparture, validateMovement -} = createBvgValidators({ +} = createVbbBvgValidators({ when, }) @@ -123,7 +121,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: spichernstr, toId: bismarckstr, when, - products + products: bvgProfile.products, }) t.end() }) diff --git a/test/e2e/cfl.js b/test/e2e/cfl.js index 1d60070e..9919b2a3 100644 --- a/test/e2e/cfl.js +++ b/test/e2e/cfl.js @@ -1,33 +1,30 @@ -'use strict' +import tap from 'tap' +import assert from 'assert' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const assert = require('assert') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const cflProfile = require('../../p/cfl') -const products = require('../../p/cfl/products') -const { - line: createValidateLine, - journeyLeg: createValidateJourneyLeg, - movement: _validateMovement -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as cflProfile} from '../../p/cfl/index.js' +import { + createValidateLine, + createValidateJourneyLeg, + createValidateMovement, +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(cflProfile.timezone, cflProfile.locale, T_MOCK) const cfg = { when, - products, + products: cflProfile.products, minLatitude: 47.24, maxLatitude: 52.9, minLongitude: -0.63, @@ -46,6 +43,7 @@ const validateJourneyLeg = (validate, l, name) => { _validateJourneyLeg(validate, l, name) } +const _validateMovement = createValidateMovement(cfg) const validateMovement = (val, m, name = 'movement') => { // todo: fix this upstream const withFakeLocation = Object.assign({}, m) @@ -99,7 +97,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: ettelbruck, toId: luxembourgGareCentrale, when, - products + products: cflProfile.products, }) t.end() }) diff --git a/test/e2e/cmta.js b/test/e2e/cmta.js index d21f148a..b0e90c3a 100644 --- a/test/e2e/cmta.js +++ b/test/e2e/cmta.js @@ -1,22 +1,19 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const cmtaProfile = require('../../p/cmta') -const products = require('../../p/cmta/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testRefreshJourney = require('./lib/refresh-journey') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') -const testJourneysWithDetour = require('./lib/journeys-with-detour') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as cmtaProfile} from '../../p/cmta/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1652175000 * 1000 // 2022-05-10T11:30+02:00 const when = createWhen(cmtaProfile.timezone, cmtaProfile.locale, T_MOCK) @@ -24,7 +21,7 @@ const when = createWhen(cmtaProfile.timezone, cmtaProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: cmtaProfile.products, minLatitude: 26, maxLatitude: 33, minLongitude: -100, @@ -65,7 +62,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: broadieOaks, toId: domain, when, - products + products: cmtaProfile.products, }) t.end() }) diff --git a/test/e2e/common.js b/test/e2e/common.js index 0982d878..542753b2 100644 --- a/test/e2e/common.js +++ b/test/e2e/common.js @@ -1,9 +1,7 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const createClient = require('../..') -const vbbProfile = require('../../p/vbb') +import {createClient} from '../../index.js' +import {profile as vbbProfile} from '../../p/vbb/index.js' const client = createClient(vbbProfile, 'public-transport/hafas-client:test') diff --git a/test/e2e/dart.js b/test/e2e/dart.js index 8c2f3a1c..b602ac81 100644 --- a/test/e2e/dart.js +++ b/test/e2e/dart.js @@ -1,11 +1,9 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const dartProfile = require('../../p/dart') -const createValidate = require('./lib/validate-fptf-with') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as dartProfile} from '../../p/dart/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(dartProfile.timezone, dartProfile.locale, T_MOCK) diff --git a/test/e2e/db-busradar-nrw.js b/test/e2e/db-busradar-nrw.js index 36770e32..10cb7754 100644 --- a/test/e2e/db-busradar-nrw.js +++ b/test/e2e/db-busradar-nrw.js @@ -1,14 +1,12 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const dbBusradarNrwProfile = require('../../p/db-busradar-nrw') -const createValidate = require('./lib/validate-fptf-with') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as dbBusradarNrwProfile} from '../../p/db-busradar-nrw/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) diff --git a/test/e2e/db.js b/test/e2e/db.js index f076cb0f..840469b6 100644 --- a/test/e2e/db.js +++ b/test/e2e/db.js @@ -1,33 +1,30 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' +import maxBy from 'lodash/maxBy.js' +import flatMap from 'lodash/flatMap.js' +import last from 'lodash/last.js' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') -const maxBy = require('lodash/maxBy') -const flatMap = require('lodash/flatMap') -const last = require('lodash/last') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const dbProfile = require('../../p/db') -const products = require('../../p/db/products') -const { - station: createValidateStation, - trip: createValidateTrip -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testLegCycleAlternatives = require('./lib/leg-cycle-alternatives') -const testRefreshJourney = require('./lib/refresh-journey') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') -const testJourneysWithDetour = require('./lib/journeys-with-detour') -const testReachableFrom = require('./lib/reachable-from') -const testServerInfo = require('./lib/server-info') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as dbProfile} from '../../p/db/index.js' +import { + createValidateStation, + createValidateTrip +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testLegCycleAlternatives} from './lib/leg-cycle-alternatives.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' +import {testReachableFrom} from './lib/reachable-from.js' +import {testServerInfo} from './lib/server-info.js' const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) const minute = 60 * 1000 @@ -38,7 +35,7 @@ const when = createWhen(dbProfile.timezone, dbProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: dbProfile.products, minLatitude: 46.673100, maxLatitude: 55.030671, minLongitude: 6.896517, @@ -107,7 +104,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: blnSchwedterStr, toId: münchenHbf, when, - products + products: dbProfile.products, }) t.end() }) diff --git a/test/e2e/hvv.js b/test/e2e/hvv.js index 70c98a04..8d97636d 100644 --- a/test/e2e/hvv.js +++ b/test/e2e/hvv.js @@ -1,21 +1,18 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const hvvProfile = require('../../p/hvv') -const products = require('../../p/hvv/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as hvvProfile} from '../../p/hvv/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(hvvProfile.timezone, hvvProfile.locale, T_MOCK) @@ -23,7 +20,7 @@ const when = createWhen(hvvProfile.timezone, hvvProfile.locale, T_MOCK) const cfg = { when, // stationCoordsOptional: false, - products, + products: hvvProfile.products, // minLatitude: 50.7, // maxLatitude: 53.2, // minLongitude: 10.25, @@ -67,7 +64,7 @@ tap.skip('journeys – fails with no product', async (t) => { fromId: tiefstack, toId: barmbek, when, - products + products: hvvProfile.products, }) t.end() }) diff --git a/test/e2e/insa.js b/test/e2e/insa.js index 680ae591..e0720729 100644 --- a/test/e2e/insa.js +++ b/test/e2e/insa.js @@ -1,26 +1,23 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const insaProfile = require('../../p/insa') -const products = require('../../p/insa/products') -const { - movement: createValidateMovement, - journeyLeg: createValidateJourneyLeg, -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') -const testJourneysWithDetour = require('./lib/journeys-with-detour') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as insaProfile} from '../../p/insa/index.js' +import { + createValidateMovement, + createValidateJourneyLeg, +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) @@ -30,7 +27,7 @@ const when = createWhen(insaProfile.timezone, insaProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: insaProfile.products, minLatitude: 50.7, maxLatitude: 53.2, minLongitude: 9, // considering e.g. IC 245 @@ -86,7 +83,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: magdeburgHbf, toId: magdeburgBuckau, when, - products + products: insaProfile.products, }) t.end() }) diff --git a/test/e2e/invg.js b/test/e2e/invg.js index 34eeb24f..296175a2 100644 --- a/test/e2e/invg.js +++ b/test/e2e/invg.js @@ -1,30 +1,30 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const invgProfile = require('../../p/invg') -const products = require('../../p/invg/products') -const { - journeyLeg: createValidateJourneyLeg, - movement: createValidateMovement -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testRefreshJourney = require('./lib/refresh-journey') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as invgProfile} from '../../p/invg/index.js' +import { + createValidateJourneyLeg, + createValidateMovement, +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(invgProfile.timezone, invgProfile.locale, T_MOCK) -const cfg = {when, products} +const cfg = { + when, + products: invgProfile.products, +} const _validateJourneyLeg = createValidateJourneyLeg(cfg) const validateJourneyLeg = (val, leg, name = 'journeyLeg') => { @@ -85,7 +85,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: ingolstadtHbf, toId: telemannstr, when, - products + products: invgProfile.products, }) t.end() }) diff --git a/test/e2e/ivb.js b/test/e2e/ivb.js index 629a9642..c6ca1f73 100644 --- a/test/e2e/ivb.js +++ b/test/e2e/ivb.js @@ -1,11 +1,9 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const ivbProfile = require('../../p/ivb') -const createValidate = require('./lib/validate-fptf-with') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as ivbProfile} from '../../p/ivb/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(ivbProfile.timezone, ivbProfile.locale, T_MOCK) diff --git a/test/e2e/kvb.js b/test/e2e/kvb.js index ba60ae54..3a0ff97c 100644 --- a/test/e2e/kvb.js +++ b/test/e2e/kvb.js @@ -1,11 +1,9 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const kvbProfile = require('../../p/kvb') -const createValidate = require('./lib/validate-fptf-with') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as kvbProfile} from '../../p/kvb/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(kvbProfile.timezone, kvbProfile.locale, T_MOCK) diff --git a/test/e2e/lib/arrivals.js b/test/e2e/lib/arrivals.js index e8648714..1a88644a 100644 --- a/test/e2e/lib/arrivals.js +++ b/test/e2e/lib/arrivals.js @@ -1,5 +1,3 @@ -'use strict' - const testArrivals = async (cfg) => { const {test: t, res, validate} = cfg const ids = cfg.ids || (cfg.id ? [cfg.id] : []) @@ -26,4 +24,6 @@ const testArrivals = async (cfg) => { t.same(arrs, arrs.sort((a, b) => t.when > b.when), 'res.arrivals must be sorted by .when') } -module.exports = testArrivals +export { + testArrivals, +} diff --git a/test/e2e/lib/departures-in-direction.js b/test/e2e/lib/departures-in-direction.js index 8446219a..9c94f0e2 100644 --- a/test/e2e/lib/departures-in-direction.js +++ b/test/e2e/lib/departures-in-direction.js @@ -1,5 +1,3 @@ -'use strict' - const testDeparturesInDirection = async (cfg) => { const { test: t, @@ -34,4 +32,6 @@ const testDeparturesInDirection = async (cfg) => { } } -module.exports = testDeparturesInDirection +export { + testDeparturesInDirection, +} diff --git a/test/e2e/lib/departures.js b/test/e2e/lib/departures.js index 829741b1..ea0ea176 100644 --- a/test/e2e/lib/departures.js +++ b/test/e2e/lib/departures.js @@ -1,5 +1,3 @@ -'use strict' - const testDepartures = async (cfg) => { const {test: t, res, validate} = cfg const ids = cfg.ids || (cfg.id ? [cfg.id] : []) @@ -26,4 +24,6 @@ const testDepartures = async (cfg) => { t.same(deps, deps.sort((a, b) => t.when > b.when), 'res.departures must be sorted by .when') } -module.exports = testDepartures +export { + testDepartures, +} diff --git a/test/e2e/lib/earlier-later-journeys.js b/test/e2e/lib/earlier-later-journeys.js index a8fb04a3..1d7b1ccb 100644 --- a/test/e2e/lib/earlier-later-journeys.js +++ b/test/e2e/lib/earlier-later-journeys.js @@ -1,5 +1,3 @@ -'use strict' - const testEarlierLaterJourneys = async (cfg) => { const { test: t, @@ -73,4 +71,6 @@ const testEarlierLaterJourneys = async (cfg) => { } } -module.exports = testEarlierLaterJourneys +export { + testEarlierLaterJourneys, +} diff --git a/test/e2e/lib/journeys-fails-with-no-product.js b/test/e2e/lib/journeys-fails-with-no-product.js index 3a2dae85..982f5635 100644 --- a/test/e2e/lib/journeys-fails-with-no-product.js +++ b/test/e2e/lib/journeys-fails-with-no-product.js @@ -1,5 +1,3 @@ -'use strict' - const journeysFailsWithNoProduct = async (cfg) => { const { test: t, @@ -18,4 +16,6 @@ const journeysFailsWithNoProduct = async (cfg) => { }) } -module.exports = journeysFailsWithNoProduct +export { + journeysFailsWithNoProduct, +} diff --git a/test/e2e/lib/journeys-station-to-address.js b/test/e2e/lib/journeys-station-to-address.js index 35d7492c..2a7e2b1f 100644 --- a/test/e2e/lib/journeys-station-to-address.js +++ b/test/e2e/lib/journeys-station-to-address.js @@ -1,6 +1,4 @@ -'use strict' - -const isRoughlyEqual = require('is-roughly-equal') +import isRoughlyEqual from 'is-roughly-equal' const testJourneysStationToAddress = async (cfg) => { const {test: t, res, validate, fromId} = cfg @@ -27,4 +25,6 @@ const testJourneysStationToAddress = async (cfg) => { } } -module.exports = testJourneysStationToAddress +export { + testJourneysStationToAddress, +} diff --git a/test/e2e/lib/journeys-station-to-poi.js b/test/e2e/lib/journeys-station-to-poi.js index 42e299eb..86f26925 100644 --- a/test/e2e/lib/journeys-station-to-poi.js +++ b/test/e2e/lib/journeys-station-to-poi.js @@ -1,6 +1,4 @@ -'use strict' - -const isRoughlyEqual = require('is-roughly-equal') +import isRoughlyEqual from 'is-roughly-equal' const testJourneysStationToPoi = async (cfg) => { const {test: t, res, validate, fromId} = cfg @@ -36,4 +34,6 @@ const testJourneysStationToPoi = async (cfg) => { } } -module.exports = testJourneysStationToPoi +export { + testJourneysStationToPoi, +} diff --git a/test/e2e/lib/journeys-station-to-station.js b/test/e2e/lib/journeys-station-to-station.js index cc3d0db0..b85674a3 100644 --- a/test/e2e/lib/journeys-station-to-station.js +++ b/test/e2e/lib/journeys-station-to-station.js @@ -1,5 +1,3 @@ -'use strict' - const testJourneysStationToStation = async (cfg) => { const {test: t, res, validate} = cfg const fromIds = cfg.fromIds || (cfg.fromId ? [cfg.fromId] : []) @@ -28,4 +26,6 @@ const testJourneysStationToStation = async (cfg) => { } } -module.exports = testJourneysStationToStation +export { + testJourneysStationToStation, +} diff --git a/test/e2e/lib/journeys-walking-speed.js b/test/e2e/lib/journeys-walking-speed.js index 9b7210a1..96a21b70 100644 --- a/test/e2e/lib/journeys-walking-speed.js +++ b/test/e2e/lib/journeys-walking-speed.js @@ -1,6 +1,4 @@ -'use strict' - -const isRoughlyEqual = require('is-roughly-equal') +import isRoughlyEqual from 'is-roughly-equal' const testJourneysWalkingSpeed = async (cfg) => { const {test: t, journeys, validate, from, to, when, products, minTimeDifference} = cfg @@ -28,4 +26,6 @@ const testJourneysWalkingSpeed = async (cfg) => { t.end() } -module.exports = testJourneysWalkingSpeed +export { + testJourneysWalkingSpeed, +} diff --git a/test/e2e/lib/journeys-with-detour.js b/test/e2e/lib/journeys-with-detour.js index 7263d910..f9a1a689 100644 --- a/test/e2e/lib/journeys-with-detour.js +++ b/test/e2e/lib/journeys-with-detour.js @@ -1,5 +1,3 @@ -'use strict' - const testJourneysWithDetour = async (cfg) => { const {test: t, res, validate, detourIds} = cfg @@ -18,4 +16,6 @@ const testJourneysWithDetour = async (cfg) => { t.ok(leg, detourIds.join('/') + ' is not being passed') } -module.exports = testJourneysWithDetour +export { + testJourneysWithDetour, +} diff --git a/test/e2e/lib/leg-cycle-alternatives.js b/test/e2e/lib/leg-cycle-alternatives.js index af00c08c..cc0c29ea 100644 --- a/test/e2e/lib/leg-cycle-alternatives.js +++ b/test/e2e/lib/leg-cycle-alternatives.js @@ -1,8 +1,6 @@ -'use strict' +import isRoughlyEqual from 'is-roughly-equal' -const isRoughlyEqual = require('is-roughly-equal') - -const {hour} = require('./util') +import {hour} from './util.js' const testLegCycleAlternatives = async (cfg) => { const { @@ -43,4 +41,6 @@ const testLegCycleAlternatives = async (cfg) => { } } -module.exports = testLegCycleAlternatives +export { + testLegCycleAlternatives, +} diff --git a/test/e2e/lib/lines.js b/test/e2e/lib/lines.js index 2d06ccc0..75b7508f 100644 --- a/test/e2e/lib/lines.js +++ b/test/e2e/lib/lines.js @@ -1,5 +1,3 @@ -'use strict' - const testLines = async (cfg) => { const { test: t, @@ -23,4 +21,6 @@ const testLines = async (cfg) => { validate(t, realtimeDataUpdatedAt, 'realtimeDataUpdatedAt', 'res.realtimeDataUpdatedAt') } -module.exports = testLines +export { + testLines, +} diff --git a/test/e2e/lib/reachable-from.js b/test/e2e/lib/reachable-from.js index 14addb42..e05402d8 100644 --- a/test/e2e/lib/reachable-from.js +++ b/test/e2e/lib/reachable-from.js @@ -1,6 +1,4 @@ -'use strict' - -const isPlainObject = require('lodash/isPlainObject') +import isPlainObject from 'lodash/isPlainObject.js' const testReachableFrom = async (cfg) => { const { @@ -47,4 +45,6 @@ const testReachableFrom = async (cfg) => { t.same(results, sorted, 'results must be sorted by res.duration') } -module.exports = testReachableFrom +export { + testReachableFrom, +} diff --git a/test/e2e/lib/refresh-journey.js b/test/e2e/lib/refresh-journey.js index e2238041..a3a85760 100644 --- a/test/e2e/lib/refresh-journey.js +++ b/test/e2e/lib/refresh-journey.js @@ -1,5 +1,3 @@ -'use strict' - const simplify = j => j.legs.map(l => { return { origin: l.origin, @@ -41,4 +39,6 @@ const testRefreshJourney = async (cfg) => { t.same(simplify(refreshed), simplify(model)) } -module.exports = testRefreshJourney +export { + testRefreshJourney, +} diff --git a/test/e2e/lib/remarks.js b/test/e2e/lib/remarks.js index cd3c1090..004030ca 100644 --- a/test/e2e/lib/remarks.js +++ b/test/e2e/lib/remarks.js @@ -1,5 +1,3 @@ -'use strict' - const WEEK = 7 * 24 * 60 * 60 * 1000 const testRemarks = async (cfg) => { @@ -32,4 +30,6 @@ const testRemarks = async (cfg) => { } } -module.exports = testRemarks +export { + testRemarks, +} diff --git a/test/e2e/lib/server-info.js b/test/e2e/lib/server-info.js index 6edf42bb..a6a055e7 100644 --- a/test/e2e/lib/server-info.js +++ b/test/e2e/lib/server-info.js @@ -1,5 +1,3 @@ -'use strict' - const testServerInfo = async (cfg) => { const { test: t, @@ -24,4 +22,6 @@ const testServerInfo = async (cfg) => { t.ok(info.realtimeDataUpdatedAt > 0, 'invalid info.realtimeDataUpdatedAt') } -module.exports = testServerInfo +export { + testServerInfo, +} diff --git a/test/e2e/lib/util.js b/test/e2e/lib/util.js index 728f6524..fb154efa 100644 --- a/test/e2e/lib/util.js +++ b/test/e2e/lib/util.js @@ -1,10 +1,8 @@ -'use strict' - -const isRoughlyEqual = require('is-roughly-equal') -const {ok, AssertionError} = require('assert') -const {DateTime} = require('luxon') -const a = require('assert') -const {join} = require('path') +import isRoughlyEqual from 'is-roughly-equal' +import {ok, AssertionError} from 'assert' +import {DateTime} from 'luxon' +import * as a from 'assert' +import {createRequire} from 'module' const hour = 60 * 60 * 1000 const day = 24 * hour @@ -39,7 +37,9 @@ const assertValidWhen = (actual, expected, name, delta = day + 6 * hour) => { // HTTP request mocking if (process.env.VCR_MODE && !process.env.VCR_OFF) { + const require = createRequire(import.meta.url) const replayer = require('replayer') + replayer.configure({ headerWhitelist: [ // excludes User-Agent & Connection @@ -48,9 +48,9 @@ if (process.env.VCR_MODE && !process.env.VCR_OFF) { includeHeaderValues: true, touchHits: false, }) - replayer.fixtureDir(join(__dirname, '..', 'fixtures')) + replayer.fixtureDir(new URL('../fixtures', import.meta.url).pathname) } -module.exports = { +export { hour, createWhen, assertValidWhen, } diff --git a/test/e2e/lib/validate-fptf-with.js b/test/e2e/lib/validate-fptf-with.js index eb77f417..e9e3f9eb 100644 --- a/test/e2e/lib/validate-fptf-with.js +++ b/test/e2e/lib/validate-fptf-with.js @@ -1,11 +1,10 @@ -'use strict' +import validateFptf from 'validate-fptf' +const {defaultValidators} = validateFptf +import anyOf from 'validate-fptf/lib/any-of.js' -const {defaultValidators} = require('validate-fptf') -const anyOf = require('validate-fptf/lib/any-of') +import validators from './validators.js' -const validators = require('./validators') - -const create = (cfg, customValidators = {}) => { +const createValidateFptfWith = (cfg, customValidators = {}) => { const val = Object.assign({}, defaultValidators) for (let key of Object.keys(validators)) { val[key] = validators[key](cfg) @@ -23,4 +22,6 @@ const create = (cfg, customValidators = {}) => { return validateFptfWith } -module.exports = create +export { + createValidateFptfWith, +} diff --git a/test/e2e/lib/validators.js b/test/e2e/lib/validators.js index a4c5c719..b31a446b 100644 --- a/test/e2e/lib/validators.js +++ b/test/e2e/lib/validators.js @@ -1,10 +1,9 @@ -'use strict' +import * as a from 'assert' +import validateFptf from 'validate-fptf' +const {defaultValidators} = validateFptf +import anyOf from 'validate-fptf/lib/any-of.js' -const a = require('assert') -const {defaultValidators} = require('validate-fptf') -const anyOf = require('validate-fptf/lib/any-of') - -const {assertValidWhen} = require('./util') +import {assertValidWhen} from './util.js' const DAY = 24 * 60 * 60 * 1000 @@ -59,6 +58,7 @@ const validateStop = (val, s, name = 'stop') => { defaultValidators.stop(val, s, name) // todo: check if s.id has leading zeros } +const createValidateStop = () => validateStop const validatePoi = (val, poi, name = 'location') => { defaultValidators.location(val, poi, name) @@ -68,12 +68,14 @@ const validatePoi = (val, poi, name = 'location') => { a.strictEqual(typeof poi.name, 'string', name + '.name must be a string') a.ok(poi.name, name + '.name must not be empty') } +const createValidatePoi = () => validatePoi const validateAddress = (val, addr, name = 'location') => { defaultValidators.location(val, addr, name) a.strictEqual(typeof addr.address, 'string', name + '.address must be a string') a.ok(addr.address, name + '.address must not be empty') } +const createValidateAddress = () => validateAddress const validateLocation = (val, loc, name = 'location') => { a.ok(isObj(loc), name + ' must be an object') @@ -84,6 +86,7 @@ const validateLocation = (val, loc, name = 'location') => { validateAddress(val, loc, name) } else defaultValidators.location(val, loc, name) } +const createValidateLocation = () => validateLocation const validateLocations = (val, locs, name = 'locations') => { a.ok(Array.isArray(locs), name + ' must be an array') @@ -92,6 +95,7 @@ const validateLocations = (val, locs, name = 'locations') => { val.location(val, locs[i], name + `[${i}]`) } } +const createValidateLocations = () => validateLocations const createValidateLine = (cfg) => { const validLineModes = [] @@ -163,6 +167,7 @@ const validateRemark = (val, rem, name = 'remark') => { } } } +const createValidateRemark = () => validateRemark const createValidateStopover = (cfg) => { const validateStopover = (val, s, name = 'stopover') => { @@ -290,6 +295,7 @@ const validateTicket = (val, ti, name = 'ticket') => { a.ok(ti.variant, name + '.variant must not be empty') } } +const createValidateTicket = () => validateTicket const createValidateJourneyLeg = (cfg) => { const validateJourneyLeg = (val, leg, name = 'journeyLeg') => { @@ -416,6 +422,7 @@ const validateJourney = (val, j, name = 'journey') => { } } } +const createValidateJourney = () => validateJourney const validateJourneys = (val, js, name = 'journeys') => { a.ok(Array.isArray(js), name + ' must be an array') @@ -424,6 +431,7 @@ const validateJourneys = (val, js, name = 'journeys') => { val.journey(val, js[i], name + `[${i}]`) } } +const createValidateJourneys = () => validateJourneys const validateJourneysResult = (val, res, name = 'journeysResult') => { a.ok(isObj(res), name + ' must be an object') @@ -432,6 +440,7 @@ const validateJourneysResult = (val, res, name = 'journeysResult') => { val.realtimeDataUpdatedAt(val, res.realtimeDataUpdatedAt, name + '.realtimeDataUpdatedAt') } +const createValidateJourneysResult = () => validateJourneysResult const validateRefreshJourneyResult = (val, res, name = 'refreshJourneyResult') => { a.ok(isObj(res), name + ' must be an object') @@ -440,6 +449,7 @@ const validateRefreshJourneyResult = (val, res, name = 'refreshJourneyResult') = val.journey(val, res.journey, name + '.journey') } +const createValidateRefreshJourneyResult = () => validateRefreshJourneyResult const validateTrip = (val, trip, name = 'trip') => { const withFakeTripId = Object.assign({ @@ -448,6 +458,7 @@ const validateTrip = (val, trip, name = 'trip') => { delete withFakeTripId.id val.journeyLeg(val, withFakeTripId, name) } +const createValidateTrip = () => validateTrip const validateTripResult = (val, res, name = 'tripResult') => { a.ok(isObj(res), name + ' must be an object') @@ -456,6 +467,7 @@ const validateTripResult = (val, res, name = 'tripResult') => { val.realtimeDataUpdatedAt(val, res.realtimeDataUpdatedAt, name + '.realtimeDataUpdatedAt') } +const createValidateTripResult = () => validateTripResult const validateTripsByNameResult = (val, res, name = 'tripsByNameResult') => { a.ok(isObj(res), name + ' must be an object') @@ -468,6 +480,7 @@ const validateTripsByNameResult = (val, res, name = 'tripsByNameResult') => { val.realtimeDataUpdatedAt(val, res.realtimeDataUpdatedAt, name + '.realtimeDataUpdatedAt') } +const createValidateTripsByNameResult = () => validateTripsByNameResult const createValidateArrivalOrDeparture = (type, cfg) => { if (type !== 'arrival' && type !== 'departure') throw new Error('invalid type') @@ -632,6 +645,7 @@ const validateMovements = (val, ms, name = 'movements') => { val.movement(val, ms[i], name + `[${i}]`) } } +const createValidateMovements = () => validateMovements const validateRadarResult = (val, res, name = 'movementsResult') => { a.ok(isObj(res), name + ' must be an object') @@ -640,28 +654,61 @@ const validateRadarResult = (val, res, name = 'movementsResult') => { val.realtimeDataUpdatedAt(val, res.realtimeDataUpdatedAt, name + '.realtimeDataUpdatedAt') } +const createValidateRadarResult = () => validateRadarResult -module.exports = { +export { + createValidateRealtimeDataUpdatedAt, + createValidateProducts, + createValidateStation, + createValidateStop, + createValidateLocation, + createValidateLocations, + createValidatePoi, + createValidateAddress, + createValidateLine, + createValidateRemark, + createValidateStopover, + createValidateTicket, + createValidateJourneyLeg, + createValidateJourney, + createValidateJourneys, + createValidateJourneysResult, + createValidateRefreshJourneyResult, + createValidateTrip, + createValidateTripResult, + createValidateTripsByNameResult, + createValidateArrival, + createValidateDeparture, + createValidateArrivals, + createValidateDepartures, + createValidateArrivalsResponse, + createValidateDeparturesResponse, + createValidateMovement, + createValidateMovements, + createValidateRadarResult, +} + +export default { realtimeDataUpdatedAt: createValidateRealtimeDataUpdatedAt, products: createValidateProducts, station: createValidateStation, - stop: () => validateStop, - location: () => validateLocation, - locations: () => validateLocations, - poi: () => validatePoi, - address: () => validateAddress, + stop: createValidateStop, + location: createValidateLocation, + locations: createValidateLocations, + poi: createValidatePoi, + address: createValidateAddress, line: createValidateLine, - remark: () => validateRemark, + remark: createValidateRemark, stopover: createValidateStopover, - ticket: () => validateTicket, + ticket: createValidateTicket, journeyLeg: createValidateJourneyLeg, - journey: () => validateJourney, - journeys: () => validateJourneys, - journeysResult: () => validateJourneysResult, - refreshJourneyResult: () => validateRefreshJourneyResult, - trip: () => validateTrip, - tripResult: () => validateTripResult, - tripsByNameResult: () => validateTripsByNameResult, + journey: createValidateJourney, + journeys: createValidateJourneys, + journeysResult: createValidateJourneysResult, + refreshJourneyResult: createValidateRefreshJourneyResult, + trip: createValidateTrip, + tripResult: createValidateTripResult, + tripsByNameResult: createValidateTripsByNameResult, arrival: createValidateArrival, departure: createValidateDeparture, arrivals: createValidateArrivals, @@ -669,6 +716,6 @@ module.exports = { arrivalsResponse: createValidateArrivalsResponse, departuresResponse: createValidateDeparturesResponse, movement: createValidateMovement, - movements: () => validateMovements, - radarResult: () => validateRadarResult, + movements: createValidateMovements, + radarResult: createValidateRadarResult, } diff --git a/test/e2e/lib/vbb-bvg-validators.js b/test/e2e/lib/vbb-bvg-validators.js index 32962101..2288064a 100644 --- a/test/e2e/lib/vbb-bvg-validators.js +++ b/test/e2e/lib/vbb-bvg-validators.js @@ -1,16 +1,13 @@ -'use strict' +import {products} from '../../../p/bvg/products.js' -const a = require('assert') -const products = require('../../../p/bvg/products') +import { + createValidateStation, + createValidateJourneyLeg, + createValidateDeparture, + createValidateMovement, +} from './validators.js' -const { - station: createValidateStation, - journeyLeg: createValidateJourneyLeg, - departure: createValidateDeparture, - movement: createValidateMovement -} = require('./validators') - -const createValidators = ({when}) => { +const createVbbBvgValidators = ({when}) => { const cfg = { when, stationCoordsOptional: false, @@ -35,4 +32,6 @@ const createValidators = ({when}) => { } } -module.exports = createValidators +export { + createVbbBvgValidators, +} diff --git a/test/e2e/mobil-nrw.js b/test/e2e/mobil-nrw.js index f5dee942..496d9c03 100644 --- a/test/e2e/mobil-nrw.js +++ b/test/e2e/mobil-nrw.js @@ -1,20 +1,17 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const mobilNrwProfile = require('../../p/mobil-nrw') -const products = require('../../p/mobil-nrw/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testRefreshJourney = require('./lib/refresh-journey') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as mobilNrwProfile} from '../../p/mobil-nrw/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(mobilNrwProfile.timezone, mobilNrwProfile.locale, T_MOCK) @@ -22,7 +19,7 @@ const when = createWhen(mobilNrwProfile.timezone, mobilNrwProfile.locale, T_MOCK const cfg = { when, stationCoordsOptional: false, - products, + products: mobilNrwProfile.products, minLatitude: 48.089, minLongitude: 1.659, maxLatitude: 53.531, diff --git a/test/e2e/mobiliteit-lu.js b/test/e2e/mobiliteit-lu.js index c78b1e68..1aecb252 100644 --- a/test/e2e/mobiliteit-lu.js +++ b/test/e2e/mobiliteit-lu.js @@ -1,33 +1,30 @@ -'use strict' +import tap from 'tap' +import assert from 'assert' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const assert = require('assert') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const mobiliteitLuProfile = require('../../p/mobiliteit-lu') -const products = require('../../p/mobiliteit-lu/products') -const { - line: createValidateLine, - journeyLeg: createValidateJourneyLeg, - movement: _validateMovement -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as mobiliteitLuProfile} from '../../p/mobiliteit-lu/index.js' +import { + createValidateLine, + createValidateJourneyLeg, + createValidateMovement, +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' const T_MOCK = 1652175000 * 1000 // 2022-05-10T11:30+02:00 const when = createWhen(mobiliteitLuProfile.timezone, mobiliteitLuProfile.locale, T_MOCK) const cfg = { when, - products, + products: mobiliteitLuProfile.products, minLatitude: 47.24, maxLatitude: 52.9, minLongitude: -0.63, @@ -46,6 +43,7 @@ const validateJourneyLeg = (validate, l, name) => { _validateJourneyLeg(validate, l, name) } +const _validateMovement = createValidateMovement(cfg) const validateMovement = (val, m, name = 'movement') => { // todo: fix this upstream const withFakeLocation = Object.assign({}, m) @@ -103,7 +101,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: ettelbruck, toId: luxembourgGareCentrale, when, - products + products: mobiliteitLuProfile.products, }) t.end() }) diff --git a/test/e2e/nahsh.js b/test/e2e/nahsh.js index 550b212e..cc537424 100644 --- a/test/e2e/nahsh.js +++ b/test/e2e/nahsh.js @@ -1,27 +1,24 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const nahshProfile = require('../../p/nahsh') -const products = require('../../p/nahsh/products') -const { - line: createValidateLine, - station: createValidateStation -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testRefreshJourney = require('./lib/refresh-journey') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as nahshProfile} from '../../p/nahsh/index.js' +import { + createValidateLine, + createValidateStation +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(nahshProfile.timezone, nahshProfile.locale, T_MOCK) @@ -29,7 +26,7 @@ const when = createWhen(nahshProfile.timezone, nahshProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: nahshProfile.products, maxLatitude: 55.15, minLongitude: 7.5, minLatitude: 53.15, @@ -108,7 +105,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: kielHbf, toId: flensburg, when, - products + products: nahshProfile.products, }) t.end() }) @@ -340,6 +337,7 @@ tap.test('radar', async (t) => { }) // todo: cfg.stationProductsOptional option + const {products} = nahshProfile const allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {}) const validateStation = createValidateStation(cfg) const validate = createValidate(cfg, { diff --git a/test/e2e/nvv.js b/test/e2e/nvv.js index 74e862ce..3c44888c 100644 --- a/test/e2e/nvv.js +++ b/test/e2e/nvv.js @@ -1,22 +1,19 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const nvvProfile = require('../../p/nvv') -const products = require('../../p/nvv/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') -const testJourneysWithDetour = require('./lib/journeys-with-detour') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as nvvProfile} from '../../p/nvv/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) @@ -26,7 +23,7 @@ const when = createWhen(nvvProfile.timezone, nvvProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: nvvProfile.products, minLatitude: 48, minLongitude: 8, maxLatitude: 53, @@ -67,7 +64,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: scheidemannplatz, toId: auestadion, when, - products + products: nvvProfile.products, }) t.end() }) diff --git a/test/e2e/oebb.js b/test/e2e/oebb.js index 635383b2..0536df8a 100644 --- a/test/e2e/oebb.js +++ b/test/e2e/oebb.js @@ -1,27 +1,24 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' +import validateLine from 'validate-fptf/line.js' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') -const validateLine = require('validate-fptf/line') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const oebbProfile = require('../../p/oebb') -const products = require('../../p/oebb/products') -const { - station: createValidateStation, - stop: validateStop -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testRefreshJourney = require('./lib/refresh-journey') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testJourneysWithDetour = require('./lib/journeys-with-detour') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as oebbProfile} from '../../p/oebb/index.js' +import { + createValidateStation, + createValidateStop, +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(oebbProfile.timezone, oebbProfile.locale, T_MOCK) @@ -29,7 +26,7 @@ const when = createWhen(oebbProfile.timezone, oebbProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: oebbProfile.products, minLatitude: 45.992803, maxLatitude: 49.453517, minLongitude: 8.787557, @@ -40,6 +37,8 @@ const cfg = { const validate = createValidate(cfg) +const _validateStop = createValidateStop(cfg) + const assertValidPrice = (t, p) => { t.ok(p) if (p.amount !== null) { @@ -96,7 +95,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: salzburgHbf, toId: wienFickeystr, when, - products + products: oebbProfile.products, }) t.end() }) @@ -347,12 +346,13 @@ tap.test('stop', async (t) => { // todo: find a way to always get products from the API // todo: cfg.stationProductsOptional option + const {products} = oebbProfile const allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {}) const validateStation = createValidateStation(cfg) const validate = createValidate(cfg, { stop: (validate, s, name) => { const withFakeProducts = Object.assign({products: allProducts}, s) - validateStop(validate, withFakeProducts, name) + _validateStop(validate, withFakeProducts, name) }, station: (validate, s, name) => { const withFakeProducts = Object.assign({products: allProducts}, s) @@ -381,6 +381,7 @@ tap.test('radar Salzburg', async (t) => { // todo: find a way to always get products from the API // todo: cfg.stationProductsOptional option + const {products} = oebbProfile const allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {}) const validateStation = createValidateStation(cfg) const validate = createValidate(cfg, { diff --git a/test/e2e/ooevv.js b/test/e2e/ooevv.js index 5b38f467..555c8872 100644 --- a/test/e2e/ooevv.js +++ b/test/e2e/ooevv.js @@ -1,11 +1,9 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const oövvProfile = require('../../p/ooevv') -const createValidate = require('./lib/validate-fptf-with') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as oövvProfile} from '../../p/ooevv/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(oövvProfile.timezone, oövvProfile.locale, T_MOCK) diff --git a/test/e2e/pkp.js b/test/e2e/pkp.js index 4615ffc9..c849fec6 100644 --- a/test/e2e/pkp.js +++ b/test/e2e/pkp.js @@ -1,20 +1,17 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const { createWhen } = require('./lib/util') -const createClient = require('../..') -const pkpProfile = require('../../p/pkp') -const products = require('../../p/pkp/products') -const { - line: createValidateLine, - journeyLeg: createValidateJourneyLeg, - movement: _validateMovement -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testArrivals = require('./lib/arrivals') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as pkpProfile} from '../../p/pkp/index.js' +import { + createValidateLine, + createValidateJourneyLeg, + createValidateMovement, +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testArrivals} from './lib/arrivals.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(pkpProfile.timezone, pkpProfile.locale, T_MOCK) @@ -22,7 +19,7 @@ const when = createWhen(pkpProfile.timezone, pkpProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: pkpProfile.products, minLatitude: 40, maxLatitude: 65, minLongitude: 10, @@ -41,6 +38,7 @@ const validateJourneyLeg = (validate, l, name) => { _validateJourneyLeg(validate, l, name) } +const _validateMovement = createValidateMovement(cfg) const validateMovement = (val, m, name) => { if (!m.direction) m = Object.assign({}, m, { direction: 'foo' }) _validateMovement(val, m, name) diff --git a/test/e2e/rejseplanen.js b/test/e2e/rejseplanen.js index 2a142c7f..0efaede5 100644 --- a/test/e2e/rejseplanen.js +++ b/test/e2e/rejseplanen.js @@ -1,26 +1,23 @@ -'use strict' +import tap from 'tap' +import assert from 'assert' -const tap = require('tap') -const assert = require('assert') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const rejseplanenProfile = require('../../p/rejseplanen') -const products = require('../../p/rejseplanen/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as rejseplanenProfile} from '../../p/rejseplanen/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(rejseplanenProfile.timezone, rejseplanenProfile.locale, T_MOCK) const validate = createValidate({ when, - products, + products: rejseplanenProfile.products, minLatitude: 52.7, maxLatitude: 58.85, minLongitude: 5.8, @@ -59,7 +56,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: næstved, toId: aalborg, when, - products, + products: rejseplanenProfile.products, }) t.end() }) diff --git a/test/e2e/rmv.js b/test/e2e/rmv.js index 81a470b8..4616d116 100644 --- a/test/e2e/rmv.js +++ b/test/e2e/rmv.js @@ -1,15 +1,12 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const rmvProfile = require('../../p/rmv') -const products = require('../../p/rmv/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testArrivals = require('./lib/arrivals') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as rmvProfile} from '../../p/rmv/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testArrivals} from './lib/arrivals.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(rmvProfile.timezone, rmvProfile.locale, T_MOCK) @@ -17,7 +14,7 @@ const when = createWhen(rmvProfile.timezone, rmvProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: rmvProfile.products, minLatitude: 47, maxLatitude: 54, minLongitude: 6, diff --git a/test/e2e/rsag.js b/test/e2e/rsag.js index 6b0fba5e..175d2f45 100644 --- a/test/e2e/rsag.js +++ b/test/e2e/rsag.js @@ -1,17 +1,14 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const rsagProfile = require('../../p/rsag') -const products = require('../../p/rsag/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testRefreshJourney = require('./lib/refresh-journey') -const testArrivals = require('./lib/arrivals') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as rsagProfile} from '../../p/rsag/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {testArrivals} from './lib/arrivals.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(rsagProfile.timezone, rsagProfile.locale, T_MOCK) @@ -19,7 +16,7 @@ const when = createWhen(rsagProfile.timezone, rsagProfile.locale, T_MOCK) const cfg = { when, // stationCoordsOptional: false, - products, + products: rsagProfile.products, minLatitude: 52.299, maxLatitude: 54.862, minLongitude: 9.121, diff --git a/test/e2e/saarfahrplan.js b/test/e2e/saarfahrplan.js index c16e56e0..c6353990 100644 --- a/test/e2e/saarfahrplan.js +++ b/test/e2e/saarfahrplan.js @@ -1,25 +1,22 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const { createWhen } = require('./lib/util') -const createClient = require('../..') -const saarfahrplanProfile = require('../../p/saarfahrplan') -const products = require('../../p/saarfahrplan/products') -const { - station: createValidateStation, - stop: validateStop -} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testJourneysWithDetour = require('./lib/journeys-with-detour') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as saarfahrplanProfile} from '../../p/saarfahrplan/index.js' +import { + createValidateStation, + createValidateStop, +} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(saarfahrplanProfile.timezone, saarfahrplanProfile.locale, T_MOCK) @@ -27,7 +24,7 @@ const when = createWhen(saarfahrplanProfile.timezone, saarfahrplanProfile.locale const cfg = { when, // stationCoordsOptional: false, @todo - products, + products: saarfahrplanProfile.products, minLatitude: 49, maxLatitude: 49.6, minLongitude: 6.1, @@ -76,7 +73,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: saarbrueckenHbf, toId: saarlouisHbf, when, - products + products: saarfahrplanProfile.products, }) t.end() }) diff --git a/test/e2e/salzburg.js b/test/e2e/salzburg.js index 2ddc28de..9fac53e1 100644 --- a/test/e2e/salzburg.js +++ b/test/e2e/salzburg.js @@ -1,11 +1,9 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const salzburgProfile = require('../../p/salzburg') -const createValidate = require('./lib/validate-fptf-with') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as salzburgProfile} from '../../p/salzburg/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(salzburgProfile.timezone, salzburgProfile.locale, T_MOCK) diff --git a/test/e2e/sbahn-muenchen.js b/test/e2e/sbahn-muenchen.js index 504ba34f..5527ac59 100644 --- a/test/e2e/sbahn-muenchen.js +++ b/test/e2e/sbahn-muenchen.js @@ -1,23 +1,20 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const sMunichProfile = require('../../p/sbahn-muenchen') -const products = require('../../p/sbahn-muenchen/products') -const {movement: _validateMovement} = require('./lib/validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testRefreshJourney = require('./lib/refresh-journey') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') -const testJourneysWithDetour = require('./lib/journeys-with-detour') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as sMunichProfile} from '../../p/sbahn-muenchen/index.js' +import {createValidateMovement as _createValidateMovement} from './lib/validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(sMunichProfile.timezone, sMunichProfile.locale, T_MOCK) @@ -25,13 +22,14 @@ const when = createWhen(sMunichProfile.timezone, sMunichProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: sMunichProfile.products, minLatitude: 48, maxLatitude: 48.3, minLongitude: 11.3, maxLongitude: 11.8 } +const _validateMovement = _createValidateMovement(cfg) const validateMovement = (val, m, name = 'movement') => { const dummyStopA = {type: 'stop', id: '123'} const dummyStopB = {type: 'stop', id: '321'} @@ -87,7 +85,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: mittersendling, toId: karlTheodorStr, when, - products + products: sMunichProfile.products, }) t.end() }) diff --git a/test/e2e/sncb.js b/test/e2e/sncb.js index 4d9a44b9..c7d82a9b 100644 --- a/test/e2e/sncb.js +++ b/test/e2e/sncb.js @@ -1,15 +1,12 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const sncbProfile = require('../../p/sncb') -const products = require('../../p/sncb/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testArrivals = require('./lib/arrivals') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as sncbProfile} from '../../p/sncb/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testArrivals} from './lib/arrivals.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(sncbProfile.timezone, sncbProfile.locale, T_MOCK) @@ -17,7 +14,7 @@ const when = createWhen(sncbProfile.timezone, sncbProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: sncbProfile.products, minLatitude: 46.513, maxLatitude: 54.521, minLongitude: -1.423, diff --git a/test/e2e/stv.js b/test/e2e/stv.js index 435036f2..dacb55fe 100644 --- a/test/e2e/stv.js +++ b/test/e2e/stv.js @@ -1,11 +1,9 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const stvProfile = require('../../p/stv') -const createValidate = require('./lib/validate-fptf-with') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as stvProfile} from '../../p/stv/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(stvProfile.timezone, stvProfile.locale, T_MOCK) diff --git a/test/e2e/svv.js b/test/e2e/svv.js index 1706ae00..73ea1d6c 100644 --- a/test/e2e/svv.js +++ b/test/e2e/svv.js @@ -1,16 +1,13 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const svvProfile = require('../../p/svv') -const products = require('../../p/svv/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testArrivals = require('./lib/arrivals') -const testReachableFrom = require('./lib/reachable-from') -const testServerInfo = require('./lib/server-info') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as svvProfile} from '../../p/svv/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testArrivals} from './lib/arrivals.js' +import {testReachableFrom} from './lib/reachable-from.js' +import {testServerInfo} from './lib/server-info.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(svvProfile.timezone, svvProfile.locale, T_MOCK) @@ -18,7 +15,7 @@ const when = createWhen(svvProfile.timezone, svvProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: svvProfile.products, minLatitude: 45.742, maxLatitude: 49.41, minLongitude: 8.177, diff --git a/test/e2e/tpg.js b/test/e2e/tpg.js index 00ee4323..3a0ac3df 100644 --- a/test/e2e/tpg.js +++ b/test/e2e/tpg.js @@ -1,12 +1,10 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const tpgProfile = require('../../p/tpg') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as tpgProfile} from '../../p/tpg/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(tpgProfile.timezone, tpgProfile.locale, T_MOCK) diff --git a/test/e2e/vbb.js b/test/e2e/vbb.js index 5c008747..e3bc21b3 100644 --- a/test/e2e/vbb.js +++ b/test/e2e/vbb.js @@ -1,25 +1,22 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const vbbProfile = require('../../p/vbb') -const products = require('../../p/vbb/products') -const createVbbValidators = require('./lib/vbb-bvg-validators') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testJourneysWalkingSpeed = require('./lib/journeys-walking-speed') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testRefreshJourney = require('./lib/refresh-journey') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') -const testJourneysWithDetour = require('./lib/journeys-with-detour') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as vbbProfile} from '../../p/vbb/index.js' +import {createVbbBvgValidators} from './lib/vbb-bvg-validators.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testJourneysWalkingSpeed} from './lib/journeys-walking-speed.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testRefreshJourney} from './lib/refresh-journey.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' +import {testJourneysWithDetour} from './lib/journeys-with-detour.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(vbbProfile.timezone, vbbProfile.locale, T_MOCK) @@ -30,7 +27,7 @@ const { validateJourneyLeg, validateDeparture, validateMovement -} = createVbbValidators({ +} = createVbbBvgValidators({ when, }) @@ -117,7 +114,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: spichernstr, toId: bismarckstr, when, - products + products: vbbProfile.products, }) t.end() }) diff --git a/test/e2e/vbn.js b/test/e2e/vbn.js index 53b0b6a8..0c04a246 100644 --- a/test/e2e/vbn.js +++ b/test/e2e/vbn.js @@ -1,15 +1,12 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const vbnProfile = require('../../p/vbn') -const products = require('../../p/vbn/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testArrivals = require('./lib/arrivals') -const testReachableFrom = require('./lib/reachable-from') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as vbnProfile} from '../../p/vbn/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testArrivals} from './lib/arrivals.js' +import {testReachableFrom} from './lib/reachable-from.js' const T_MOCK = 1652175000 * 1000 // 2022-05-10T11:30+02:00 const when = createWhen(vbnProfile.timezone, vbnProfile.locale, T_MOCK) @@ -17,7 +14,7 @@ const when = createWhen(vbnProfile.timezone, vbnProfile.locale, T_MOCK) const cfg = { when, stationCoordsOptional: false, - products, + products: vbnProfile.products, minLatitude: 51.817, maxLatitude: 53.657, minLongitude: 5.248, diff --git a/test/e2e/vor.js b/test/e2e/vor.js index 93faf003..6c79f221 100644 --- a/test/e2e/vor.js +++ b/test/e2e/vor.js @@ -1,11 +1,9 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const vorProfile = require('../../p/vor') -const createValidate = require('./lib/validate-fptf-with') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as vorProfile} from '../../p/vor/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(vorProfile.timezone, vorProfile.locale, T_MOCK) diff --git a/test/e2e/vrn.js b/test/e2e/vrn.js index 18415cb1..0120079f 100644 --- a/test/e2e/vrn.js +++ b/test/e2e/vrn.js @@ -1,21 +1,18 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const vrnProfile = require('../../p/vrn') -const products = require('../../p/vrn/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as vrnProfile} from '../../p/vrn/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {journeysFailsWithNoProduct} from './lib/journeys-fails-with-no-product.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(vrnProfile.timezone, vrnProfile.locale, T_MOCK) @@ -23,7 +20,7 @@ const when = createWhen(vrnProfile.timezone, vrnProfile.locale, T_MOCK) const cfg = { when, // stationCoordsOptional: false, - products, + products: vrnProfile.products, minLatitude: 48.462, minLongitude: 6.163, maxLatitude: 50.440, @@ -63,7 +60,7 @@ tap.test('journeys – fails with no product', async (t) => { fromId: ludwigshafen, toId: meckesheim, when, - products + products: vrnProfile.products, }) t.end() }) diff --git a/test/e2e/vsn.js b/test/e2e/vsn.js index 73f9dc2b..5fae979f 100644 --- a/test/e2e/vsn.js +++ b/test/e2e/vsn.js @@ -1,26 +1,23 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const vsnProfile = require('../../p/vsn') -const products = require('../../p/vsn/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testDepartures = require('./lib/departures') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as vsnProfile} from '../../p/vsn/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testDepartures} from './lib/departures.js' +import {testArrivals} from './lib/arrivals.js' const T_MOCK = 1652175000 * 1000 // 2022-05-10T11:30+02:00 const when = createWhen(vsnProfile.timezone, vsnProfile.locale, T_MOCK) const cfg = { when, - products, + products: vsnProfile.products, minLatitude: 50, maxLatitude: 54.5, minLongitude: 6.5, diff --git a/test/e2e/vvv.js b/test/e2e/vvv.js index 08e77f20..9b172cd4 100644 --- a/test/e2e/vvv.js +++ b/test/e2e/vvv.js @@ -1,11 +1,9 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const vvvProfile = require('../../p/vvv') -const createValidate = require('./lib/validate-fptf-with') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as vvvProfile} from '../../p/vvv/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' const T_MOCK = 1641897000 * 1000 // 2022-01-11T11:30:00+01 const when = createWhen(vvvProfile.timezone, vvvProfile.locale, T_MOCK) diff --git a/test/e2e/zvv.js b/test/e2e/zvv.js index 5ca3c6c2..21445c20 100644 --- a/test/e2e/zvv.js +++ b/test/e2e/zvv.js @@ -1,27 +1,24 @@ -'use strict' +import tap from 'tap' +import isRoughlyEqual from 'is-roughly-equal' -const tap = require('tap') -const isRoughlyEqual = require('is-roughly-equal') - -const {createWhen} = require('./lib/util') -const createClient = require('../..') -const zvvProfile = require('../../p/zvv') -const products = require('../../p/zvv/products') -const createValidate = require('./lib/validate-fptf-with') -const testJourneysStationToStation = require('./lib/journeys-station-to-station') -const testJourneysStationToAddress = require('./lib/journeys-station-to-address') -const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') -const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') -const testDepartures = require('./lib/departures') -const testDeparturesInDirection = require('./lib/departures-in-direction') -const testArrivals = require('./lib/arrivals') +import {createWhen} from './lib/util.js' +import {createClient} from '../../index.js' +import {profile as zvvProfile} from '../../p/zvv/index.js' +import {createValidateFptfWith as createValidate} from './lib/validate-fptf-with.js' +import {testJourneysStationToStation} from './lib/journeys-station-to-station.js' +import {testJourneysStationToAddress} from './lib/journeys-station-to-address.js' +import {testJourneysStationToPoi} from './lib/journeys-station-to-poi.js' +import {testEarlierLaterJourneys} from './lib/earlier-later-journeys.js' +import {testDepartures} from './lib/departures.js' +import {testDeparturesInDirection} from './lib/departures-in-direction.js' +import {testArrivals} from './lib/arrivals.js' const T_MOCK = 1657618200 * 1000 // 2022-07-12T11:30+02:00 const when = createWhen(zvvProfile.timezone, zvvProfile.locale, T_MOCK) const validate = createValidate({ when, - products, + products: zvvProfile.products, maxLatitude: 47.75, minLongitude: 7.38, minLatitude: 46.99, diff --git a/test/fixtures/bvg-arrivals.js b/test/fixtures/bvg-arrivals.js index 28906532..8603da5d 100644 --- a/test/fixtures/bvg-arrivals.js +++ b/test/fixtures/bvg-arrivals.js @@ -1,5 +1,3 @@ -'use strict' - const sLandsbergerAllee = { type: 'stop', id: '900110004', @@ -291,7 +289,7 @@ const bauzeitverlängerung = { modified: '2021-10-27T17:00:45+02:00', } -module.exports = [ +const bvgArrivals = [ { tripId: '1|18731|26|86|28102021', stop: sLandsbergerAllee, @@ -1248,3 +1246,7 @@ module.exports = [ currentTripPosition: {type: 'location', latitude: 52.473811, longitude: 13.456142}, }, ] + +export { + bvgArrivals, +} diff --git a/test/fixtures/bvg-journey.js b/test/fixtures/bvg-journey.js index c2ae3513..3b67ca4f 100644 --- a/test/fixtures/bvg-journey.js +++ b/test/fixtures/bvg-journey.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const bvgJourney = { type: 'journey', legs: [{ origin: { @@ -1103,3 +1101,7 @@ module.exports = { '2019-12-16': false, }), } + +export { + bvgJourney, +} diff --git a/test/fixtures/bvg-radar.js b/test/fixtures/bvg-radar.js index 3a285f68..fbf850fd 100644 --- a/test/fixtures/bvg-radar.js +++ b/test/fixtures/bvg-radar.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const bvgRadar = [ { direction: 'Hohenschönhausen, Zingster Str.', tripId: '1|57637|1|86|19082019', @@ -3034,3 +3032,7 @@ module.exports = [ } } ] + +export { + bvgRadar, +} diff --git a/test/fixtures/bvg-trip-with-occupancy.js b/test/fixtures/bvg-trip-with-occupancy.js index 7cf12075..9a833d4c 100644 --- a/test/fixtures/bvg-trip-with-occupancy.js +++ b/test/fixtures/bvg-trip-with-occupancy.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const bvgTripWithOccupancy = { id: '1|6849|3|86|1112021', direction: 'S Ostbahnhof via S+U Wedding', line: { @@ -2043,3 +2041,7 @@ module.exports = { } ], } + +export { + bvgTripWithOccupancy, +} diff --git a/test/fixtures/db-arrivals.js b/test/fixtures/db-arrivals.js index 1de02693..1bb56288 100644 --- a/test/fixtures/db-arrivals.js +++ b/test/fixtures/db-arrivals.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const dbArrivals = [ { tripId: '1|1144239|52|80|9062020', stop: { @@ -267,3 +265,7 @@ module.exports = [ remarks: [] } ] + +export { + dbArrivals, +} diff --git a/test/fixtures/db-journey-2.js b/test/fixtures/db-journey-2.js index 2bfdeee9..5d6575cc 100644 --- a/test/fixtures/db-journey-2.js +++ b/test/fixtures/db-journey-2.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const dbJourney = { type: 'journey', legs: [ { @@ -1160,3 +1158,7 @@ module.exports = { cycle: {min: 1200}, price: null } + +export { + dbJourney, +} diff --git a/test/fixtures/db-journey-polyline.js b/test/fixtures/db-journey-polyline.js index 7b162eda..307036e5 100644 --- a/test/fixtures/db-journey-polyline.js +++ b/test/fixtures/db-journey-polyline.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const dbJourneyPolyline = { type: 'journey', legs: [ { @@ -3978,3 +3976,7 @@ module.exports = { 'hint': null } } + +export { + dbJourneyPolyline, +} diff --git a/test/fixtures/db-journey.js b/test/fixtures/db-journey.js index 13284cf9..5df1b48f 100644 --- a/test/fixtures/db-journey.js +++ b/test/fixtures/db-journey.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const dbJourney = { type: 'journey', legs: [ { @@ -190,3 +188,7 @@ module.exports = { refreshToken: '¶HKI¶D$A=1@O=Köln Hbf@L=8000207@a=128@$A=1@O=Köln Messe/Deutz Gl.11-12@L=8073368@a=128@$202004110517$202004110520$$$1$§T$A=1@O=Köln Messe/Deutz Gl.11-12@L=8073368@a=128@$A=1@O=Nürnberg Hbf@L=8000284@a=128@$202004110520$202004110901$ICE 523$$1$', price: {amount: 49.9, currency: 'EUR', hint: null}, } + +export { + dbJourney, +} diff --git a/test/fixtures/db-stop.js b/test/fixtures/db-stop.js index 446fd652..44c3dc2d 100644 --- a/test/fixtures/db-stop.js +++ b/test/fixtures/db-stop.js @@ -1,5 +1,3 @@ -'use strict' - const facilities = { '3SZentrale': '030/2971055', parkingLots: true, @@ -63,7 +61,7 @@ const station = { reisezentrumOpeningHours, } -module.exports = { +const dbStop = { ...station, stops: [{ type: 'stop', @@ -633,3 +631,7 @@ module.exports = { latitude: 52.522317, longitude: 13.412895 }], } + +export { + dbStop, +} diff --git a/test/fixtures/insa-stop.js b/test/fixtures/insa-stop.js index a92684a4..d46bec54 100644 --- a/test/fixtures/insa-stop.js +++ b/test/fixtures/insa-stop.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const insaStop = { type: 'stop', id: '7341', ids: { @@ -23,3 +21,7 @@ module.exports = { tourismTrain: false }, } + +export { + insaStop, +} diff --git a/test/fixtures/oebb-trip.js b/test/fixtures/oebb-trip.js index 5bc7b1f3..fb693464 100644 --- a/test/fixtures/oebb-trip.js +++ b/test/fixtures/oebb-trip.js @@ -1,5 +1,3 @@ -'use strict' - const point = (lon, lat) => ({ type: 'Point', coordinates: [lon, lat], @@ -96,7 +94,7 @@ const corona = { modified: '2020-05-28T13:48:14+02:00' } -module.exports = { +const oebbTrip = { id: '2|#VN#0#ST#1591790769#PI#0#ZI#398470#TA#0#DA#110620#1S#8100353#1T#1633#LS#8100002#LT#1948#PU#81#RT#1#CA#RJ#ZE#742#ZB#RJ 742 #', origin: wienFlughafen, destination: salzburgHbf, @@ -2024,3 +2022,7 @@ module.exports = { ] }, } + +export { + oebbTrip, +} diff --git a/test/fixtures/rejseplanen-trip.js b/test/fixtures/rejseplanen-trip.js index d11d4729..0a39cb09 100644 --- a/test/fixtures/rejseplanen-trip.js +++ b/test/fixtures/rejseplanen-trip.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const rejseplanenTrip = { id: '1|307|4|86|12102021', direction: 'CPH Lufthavn', currentLocation: { @@ -438,3 +436,7 @@ module.exports = { }, ], } + +export { + rejseplanenTrip, +} diff --git a/test/fixtures/rsag-journey.js b/test/fixtures/rsag-journey.js index d11c3d7f..a989770b 100644 --- a/test/fixtures/rsag-journey.js +++ b/test/fixtures/rsag-journey.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const rsagJourneys = { type: 'journey', legs: [ { @@ -95,3 +93,7 @@ module.exports = { refreshToken: '¶HKI¶T$A=1@O=Rostock Hbf@L=8010304@a=128@$A=1@O=Güstrow@L=8010153@a=128@$202101191414$202101191444$ S2$$1$$$$', cycle: {min: 3600}, } + +export { + rsagJourneys, +} diff --git a/test/fixtures/sbb-journeys.js b/test/fixtures/sbb-journeys.js index a2940c65..cf3d81fc 100644 --- a/test/fixtures/sbb-journeys.js +++ b/test/fixtures/sbb-journeys.js @@ -1,5 +1,3 @@ -'use strict' - const baselSBB = { type: 'stop', id: '8500010', @@ -120,7 +118,7 @@ const yverdonLesBains = { }, } -module.exports = [{ +const sbbJourneys = [{ type: 'journey', legs: [{ origin: baselSBB, @@ -682,3 +680,7 @@ module.exports = [{ {type: 'hint', code: 'cap2nd_11', text: '2.'}, ], }] + +export { + sbbJourneys, +} diff --git a/test/fixtures/vbb-departures.js b/test/fixtures/vbb-departures.js index 3b3c40d9..77200fca 100644 --- a/test/fixtures/vbb-departures.js +++ b/test/fixtures/vbb-departures.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const vbbDepartures = [ { tripId: '1|24934|5|86|12102021', stop: { @@ -2580,3 +2578,7 @@ module.exports = [ }, } ] + +export { + vbbDepartures, +} diff --git a/test/fixtures/vbb-journeys.js b/test/fixtures/vbb-journeys.js index d6a71a6b..ada4a10a 100644 --- a/test/fixtures/vbb-journeys.js +++ b/test/fixtures/vbb-journeys.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [{ +const vbbJourneys = [{ type: 'journey', legs: [{ origin: { @@ -613,3 +611,7 @@ module.exports = [{ text: 'A section of this itinerary is cancelled or unusable.' }], }] + +export { + vbbJourneys, +} diff --git a/test/fixtures/vbb-on-demand-trip.js b/test/fixtures/vbb-on-demand-trip.js index 87c94252..c88fbf80 100644 --- a/test/fixtures/vbb-on-demand-trip.js +++ b/test/fixtures/vbb-on-demand-trip.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const vbbOnDemandTrip = { id: '1|42971|454|86|24102021', direction: 'Rufbus Niederer-Fläming', line: { @@ -36,3 +34,7 @@ module.exports = { plannedArrivalPlatform: null, arrivalPrognosisType: null, } + +export { + vbbOnDemandTrip, +} diff --git a/test/fixtures/vsn-departures.js b/test/fixtures/vsn-departures.js index a8c51553..2e2964e0 100644 --- a/test/fixtures/vsn-departures.js +++ b/test/fixtures/vsn-departures.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +const vsnDepartures = { tripId: '1|265947|0|80|1082020', stop: { type: 'stop', @@ -99,3 +97,7 @@ module.exports = { }, remarks: [], } + +export { + vsnDepartures, +} diff --git a/test/fixtures/vsn-remarks.js b/test/fixtures/vsn-remarks.js index 669f8081..727a5053 100644 --- a/test/fixtures/vsn-remarks.js +++ b/test/fixtures/vsn-remarks.js @@ -1,6 +1,4 @@ -'use strict' - -module.exports = [ +const vsnRemarks = [ { id: 'HIM_FREETEXT_14164', type: 'warning', @@ -330,3 +328,7 @@ module.exports = [ modified: '2020-02-21T13:31:05+01:00' } ] + +export { + vsnRemarks, +} diff --git a/test/format/products-filter.js b/test/format/products-filter.js index d2faceac..9c0d9b7d 100644 --- a/test/format/products-filter.js +++ b/test/format/products-filter.js @@ -1,7 +1,5 @@ -'use strict' - -const tap = require('tap') -const format = require('../../format/products-filter') +import tap from 'tap' +import {formatProductsFilter as format} from '../../format/products-filter.js' const products = [ { diff --git a/test/insa-stop.js b/test/insa-stop.js index eb9763ed..4866865c 100644 --- a/test/insa-stop.js +++ b/test/insa-stop.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/insa') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/insa/index.js' const res = require('./fixtures/insa-stop.json') -const expected = require('./fixtures/insa-stop.js') +import {insaStop as expected} from './fixtures/insa-stop.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/lib/check-if-res-is-ok.js b/test/lib/check-if-res-is-ok.js deleted file mode 100644 index ca0c0184..00000000 --- a/test/lib/check-if-res-is-ok.js +++ /dev/null @@ -1,165 +0,0 @@ -'use strict' - -const tap = require('tap') -const checkIfResIsOk = require('../../lib/check-if-res-is-ok') -const { - INVALID_REQUEST, - NOT_FOUND, - HafasError, - HafasInvalidRequestError, - HafasNotFoundError, -} = require('../../lib/errors') - -const resParameter = require('../fixtures/error-parameter.json') -const resNoMatch = require('../fixtures/error-no-match.json') -const resH9360 = require('../fixtures/error-h9360.json') -const resLocation = require('../fixtures/error-location.json') - -const secret = Symbol('secret') - -tap.test('checkIfResponseIsOk properly throws HAFAS "H9360" errors', (t) => { - try { - checkIfResIsOk({ - body: resH9360, - errProps: {secret}, - }) - } catch (err) { - t.ok(err) - - t.ok(err instanceof HafasError) - t.equal(err.isHafasError, true) - t.equal(err.message.slice(0, 7), 'H9360: ') - t.ok(err.message.length > 7) - - t.ok(err instanceof HafasInvalidRequestError) - t.equal(err.isCausedByServer, false) - t.equal(err.code, INVALID_REQUEST) - t.equal(err.hafasCode, 'H9360') - - t.equal(err.hafasResponseId, resH9360.id) - t.equal(err.hafasMessage, 'HAFAS Kernel: Date outside of the timetable period.') - t.equal(err.hafasDescription, 'Fehler bei der Datumseingabe oder Datum außerhalb der Fahrplanperiode (01.05.2022 - 10.12.2022)') - t.equal(err.secret, secret) - - t.end() - } -}) - -tap.test('checkIfResponseIsOk properly throws HAFAS "LOCATION" errors', (t) => { - try { - checkIfResIsOk({ - body: resLocation, - errProps: {secret}, - }) - } catch (err) { - t.ok(err) - - t.ok(err instanceof HafasError) - t.equal(err.isHafasError, true) - t.equal(err.message.slice(0, 10), 'LOCATION: ') - t.ok(err.message.length > 10) - - t.ok(err instanceof HafasNotFoundError) - t.equal(err.isCausedByServer, false) - t.equal(err.code, NOT_FOUND) - t.equal(err.hafasCode, 'LOCATION') - - t.equal(err.hafasResponseId, resLocation.id) - t.equal(err.hafasMessage, 'HCI Service: location missing or invalid') - t.equal(err.hafasDescription, 'Während der Suche ist ein interner Fehler aufgetreten') - t.equal(err.secret, secret) - - t.end() - } -}) - -tap.test('checkIfResponseIsOk properly throws HAFAS "NO_MATCH" errors', (t) => { - try { - checkIfResIsOk({ - body: resNoMatch, - errProps: {secret}, - }) - } catch (err) { - t.ok(err) - - t.ok(err instanceof HafasError) - t.equal(err.isHafasError, true) - t.equal(err.message.slice(0, 10), 'NO_MATCH: ') - t.ok(err.message.length > 10) - - t.ok(err instanceof HafasNotFoundError) - t.equal(err.isCausedByServer, false) - t.equal(err.code, NOT_FOUND) - t.equal(err.hafasCode, 'NO_MATCH') - - t.equal(err.hafasResponseId, resNoMatch.id) - t.equal(err.hafasMessage, 'Nothing found.') - t.equal(err.hafasDescription, 'Während der Suche ist leider ein interner Fehler aufgetreten. Bitte wenden Sie sich an unsere Serviceauskunft unter Tel. 0421 596059.') - t.equal(err.secret, secret) - - t.end() - } -}) - -tap.test('checkIfResponseIsOk properly throws HAFAS "PARAMETER" errors', (t) => { - try { - checkIfResIsOk({ - body: resParameter, - errProps: {secret}, - }) - } catch (err) { - t.ok(err) - - t.ok(err instanceof HafasError) - t.equal(err.isHafasError, true) - t.equal(err.message.slice(0, 11), 'PARAMETER: ') - t.ok(err.message.length > 11) - - t.ok(err instanceof HafasInvalidRequestError) - t.equal(err.isCausedByServer, false) - t.equal(err.code, INVALID_REQUEST) - t.equal(err.hafasCode, 'PARAMETER') - - t.equal(err.hafasResponseId, resParameter.id) - t.equal(err.hafasMessage, 'HCI Service: parameter invalid') - t.equal(err.hafasDescription, 'Während der Suche ist ein interner Fehler aufgetreten') - t.equal(err.secret, secret) - - t.end() - } -}) - -tap.test('checkIfResponseIsOk properly parses an unknown HAFAS errors', (t) => { - const body = { - ver: '1.42', - id: '1234567890', - err: 'FOO', - errTxt: 'random errTxt', - errTxtOut: 'even more random errTxtOut', - svcResL: [], - } - - try { - checkIfResIsOk({ - body, - errProps: {secret}, - }) - } catch (err) { - t.ok(err) - - t.ok(err instanceof HafasError) - t.equal(err.isHafasError, true) - t.equal(err.message, `${body.err}: ${body.errTxt}`) - - t.equal(err.isCausedByServer, false) - t.equal(err.code, null) - t.equal(err.hafasCode, body.err) - - t.equal(err.hafasResponseId, body.id) - t.equal(err.hafasMessage, body.errTxt) - t.equal(err.hafasDescription, body.errTxtOut) - t.equal(err.secret, secret) - - t.end() - } -}) diff --git a/test/lib/request.js b/test/lib/request.js index 125a20f0..c6295fdc 100644 --- a/test/lib/request.js +++ b/test/lib/request.js @@ -1,12 +1,179 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') -const forEach = require('lodash/forEach') -const request = require('../../lib/request') -const formatTripReq = require('../../format/trip-req') +import tap from 'tap' +import forEach from 'lodash/forEach.js' +import { + checkIfResponseIsOk as checkIfResIsOk, + request, +} from '../../lib/request.js' +import { + INVALID_REQUEST, + NOT_FOUND, + HafasError, + HafasInvalidRequestError, + HafasNotFoundError, +} from '../../lib/errors.js' +import {formatTripReq} from '../../format/trip-req.js' + +const resParameter = require('../fixtures/error-parameter.json') +const resNoMatch = require('../fixtures/error-no-match.json') +const resH9360 = require('../fixtures/error-h9360.json') +const resLocation = require('../fixtures/error-location.json') const USER_AGENT = 'public-transport/hafas-client:test' +const secret = Symbol('secret') + +tap.test('checkIfResponseIsOk properly throws HAFAS "H9360" errors', (t) => { + try { + checkIfResIsOk({ + body: resH9360, + errProps: {secret}, + }) + } catch (err) { + t.ok(err) + + t.ok(err instanceof HafasError) + t.equal(err.isHafasError, true) + t.equal(err.message.slice(0, 7), 'H9360: ') + t.ok(err.message.length > 7) + + t.ok(err instanceof HafasInvalidRequestError) + t.equal(err.isCausedByServer, false) + t.equal(err.code, INVALID_REQUEST) + t.equal(err.hafasCode, 'H9360') + + t.equal(err.hafasResponseId, resH9360.id) + t.equal(err.hafasMessage, 'HAFAS Kernel: Date outside of the timetable period.') + t.equal(err.hafasDescription, 'Fehler bei der Datumseingabe oder Datum außerhalb der Fahrplanperiode (01.05.2022 - 10.12.2022)') + t.equal(err.secret, secret) + + t.end() + } +}) + +tap.test('checkIfResponseIsOk properly throws HAFAS "LOCATION" errors', (t) => { + try { + checkIfResIsOk({ + body: resLocation, + errProps: {secret}, + }) + } catch (err) { + t.ok(err) + + t.ok(err instanceof HafasError) + t.equal(err.isHafasError, true) + t.equal(err.message.slice(0, 10), 'LOCATION: ') + t.ok(err.message.length > 10) + + t.ok(err instanceof HafasNotFoundError) + t.equal(err.isCausedByServer, false) + t.equal(err.code, NOT_FOUND) + t.equal(err.hafasCode, 'LOCATION') + + t.equal(err.hafasResponseId, resLocation.id) + t.equal(err.hafasMessage, 'HCI Service: location missing or invalid') + t.equal(err.hafasDescription, 'Während der Suche ist ein interner Fehler aufgetreten') + t.equal(err.secret, secret) + + t.end() + } +}) + +tap.test('checkIfResponseIsOk properly throws HAFAS "NO_MATCH" errors', (t) => { + try { + checkIfResIsOk({ + body: resNoMatch, + errProps: {secret}, + }) + } catch (err) { + t.ok(err) + + t.ok(err instanceof HafasError) + t.equal(err.isHafasError, true) + t.equal(err.message.slice(0, 10), 'NO_MATCH: ') + t.ok(err.message.length > 10) + + t.ok(err instanceof HafasNotFoundError) + t.equal(err.isCausedByServer, false) + t.equal(err.code, NOT_FOUND) + t.equal(err.hafasCode, 'NO_MATCH') + + t.equal(err.hafasResponseId, resNoMatch.id) + t.equal(err.hafasMessage, 'Nothing found.') + t.equal(err.hafasDescription, 'Während der Suche ist leider ein interner Fehler aufgetreten. Bitte wenden Sie sich an unsere Serviceauskunft unter Tel. 0421 596059.') + t.equal(err.secret, secret) + + t.end() + } +}) + +tap.test('checkIfResponseIsOk properly throws HAFAS "PARAMETER" errors', (t) => { + try { + checkIfResIsOk({ + body: resParameter, + errProps: {secret}, + }) + } catch (err) { + t.ok(err) + + t.ok(err instanceof HafasError) + t.equal(err.isHafasError, true) + t.equal(err.message.slice(0, 11), 'PARAMETER: ') + t.ok(err.message.length > 11) + + t.ok(err instanceof HafasInvalidRequestError) + t.equal(err.isCausedByServer, false) + t.equal(err.code, INVALID_REQUEST) + t.equal(err.hafasCode, 'PARAMETER') + + t.equal(err.hafasResponseId, resParameter.id) + t.equal(err.hafasMessage, 'HCI Service: parameter invalid') + t.equal(err.hafasDescription, 'Während der Suche ist ein interner Fehler aufgetreten') + t.equal(err.secret, secret) + + t.end() + } +}) + +tap.test('checkIfResponseIsOk properly parses an unknown HAFAS errors', (t) => { + const body = { + ver: '1.42', + id: '1234567890', + err: 'FOO', + errTxt: 'random errTxt', + errTxtOut: 'even more random errTxtOut', + svcResL: [], + } + + try { + checkIfResIsOk({ + body, + errProps: {secret}, + }) + } catch (err) { + t.ok(err) + + t.ok(err instanceof HafasError) + t.equal(err.isHafasError, true) + t.equal(err.message, `${body.err}: ${body.errTxt}`) + + t.equal(err.isCausedByServer, false) + t.equal(err.code, null) + t.equal(err.hafasCode, body.err) + + t.equal(err.hafasResponseId, body.id) + t.equal(err.hafasMessage, body.errTxt) + t.equal(err.hafasDescription, body.errTxtOut) + t.equal(err.secret, secret) + + t.end() + } +}) + const freeze = (val) => { if ( 'object' === typeof val diff --git a/test/mobiliteit-lu-line.js b/test/mobiliteit-lu-line.js index 913d5490..8b7b3199 100644 --- a/test/mobiliteit-lu-line.js +++ b/test/mobiliteit-lu-line.js @@ -1,9 +1,7 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const createClient = require('..') -const rawProfile = require('../p/mobiliteit-lu') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/mobiliteit-lu/index.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/oebb-trip.js b/test/oebb-trip.js index 94f7b60a..214e8bce 100644 --- a/test/oebb-trip.js +++ b/test/oebb-trip.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/oebb') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/oebb/index.js' const res = require('./fixtures/oebb-trip.json') -const expected = require('./fixtures/oebb-trip.js') +import {oebbTrip as expected} from './fixtures/oebb-trip.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/parse/date-time.js b/test/parse/date-time.js index feae6c0c..cf7e95c6 100644 --- a/test/parse/date-time.js +++ b/test/parse/date-time.js @@ -1,7 +1,5 @@ -'use strict' - -const tap = require('tap') -const parse = require('../../parse/date-time') +import tap from 'tap' +import {parseDateTime as parse} from '../../parse/date-time.js' const ctx = { common: {}, diff --git a/test/parse/hint.js b/test/parse/hint.js index 6b271f4a..9dbbac14 100644 --- a/test/parse/hint.js +++ b/test/parse/hint.js @@ -1,7 +1,5 @@ -'use strict' - -const tap = require('tap') -const parse = require('../../parse/hint') +import tap from 'tap' +import {parseHint as parse} from '../../parse/hint.js' const ctx = { data: {}, diff --git a/test/parse/icon.js b/test/parse/icon.js index b8328610..1d5ec6c2 100644 --- a/test/parse/icon.js +++ b/test/parse/icon.js @@ -1,7 +1,5 @@ -'use strict' - -const tap = require('tap') -const parse = require('../../parse/icon') +import tap from 'tap' +import {parseIcon as parse} from '../../parse/icon.js' const ctx = { data: {}, diff --git a/test/parse/line.js b/test/parse/line.js index 9b63c327..fd30d279 100644 --- a/test/parse/line.js +++ b/test/parse/line.js @@ -1,8 +1,6 @@ -'use strict' - -const tap = require('tap') -const omit = require('lodash/omit') -const parse = require('../../parse/line') +import tap from 'tap' +import omit from 'lodash/omit.js' +import {parseLine as parse} from '../../parse/line.js' const profile = { products: [ diff --git a/test/parse/location.js b/test/parse/location.js index 06191c4c..0896a18a 100644 --- a/test/parse/location.js +++ b/test/parse/location.js @@ -1,8 +1,6 @@ -'use strict' - -const tap = require('tap') -const omit = require('lodash/omit') -const parse = require('../../parse/location') +import tap from 'tap' +import omit from 'lodash/omit.js' +import {parseLocation as parse} from '../../parse/location.js' const profile = { parseLocation: parse, diff --git a/test/parse/operator.js b/test/parse/operator.js index 72687dda..4036099b 100644 --- a/test/parse/operator.js +++ b/test/parse/operator.js @@ -1,7 +1,5 @@ -'use strict' - -const tap = require('tap') -const parse = require('../../parse/operator') +import tap from 'tap' +import {parseOperator as parse} from '../../parse/operator.js' const ctx = { data: {}, diff --git a/test/parse/warning.js b/test/parse/warning.js index dc813859..b1083db5 100644 --- a/test/parse/warning.js +++ b/test/parse/warning.js @@ -1,8 +1,6 @@ -'use strict' - -const tap = require('tap') -const parse = require('../../parse/warning') -const merge = require('lodash/merge') +import tap from 'tap' +import {parseWarning as parse} from '../../parse/warning.js' +import merge from 'lodash/merge.js' const profile = { parseProductsBitmask: (_, bitmask) => [bitmask], diff --git a/test/parse/when.js b/test/parse/when.js index 8c813cc5..982fca1b 100644 --- a/test/parse/when.js +++ b/test/parse/when.js @@ -1,7 +1,5 @@ -'use strict' - -const tap = require('tap') -const parse = require('../../parse/when') +import tap from 'tap' +import {parseWhen as parse} from '../../parse/when.js' const profile = { parseDateTime: ({profile}, date, time, tzOffset, timestamp = false) => { diff --git a/test/rejseplanen-trip.js b/test/rejseplanen-trip.js index 9929ed8b..00087ee8 100644 --- a/test/rejseplanen-trip.js +++ b/test/rejseplanen-trip.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/rejseplanen') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/rejseplanen/index.js' const res = require('./fixtures/rejseplanen-trip.json') -const expected = require('./fixtures/rejseplanen-trip.js') +import {rejseplanenTrip as expected} from './fixtures/rejseplanen-trip.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/retry.js b/test/retry.js index 0697b80d..0ba287a1 100644 --- a/test/retry.js +++ b/test/retry.js @@ -1,10 +1,8 @@ -'use strict' +import tap from 'tap' -const tap = require('tap') - -const createClient = require('..') -const withRetrying = require('../retry') -const vbbProfile = require('../p/vbb') +import {createClient} from '../index.js' +import {withRetrying} from '../retry.js' +import {profile as vbbProfile} from '../p/vbb/index.js' const userAgent = 'public-transport/hafas-client:test' const spichernstr = '900000042101' diff --git a/test/rsag-journey.js b/test/rsag-journey.js index 59bde8cc..b338abca 100644 --- a/test/rsag-journey.js +++ b/test/rsag-journey.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/rsag') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/rsag/index.js' const res = require('./fixtures/rsag-journey.json') -const expected = require('./fixtures/rsag-journey.js') +import {rsagJourneys as expected} from './fixtures/rsag-journey.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/sbb-journeys.js b/test/sbb-journeys.js index 9be88ff4..330e0b72 100644 --- a/test/sbb-journeys.js +++ b/test/sbb-journeys.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/sbb') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/sbb/index.js' const res = require('./fixtures/sbb-journeys.json') -const expected = require('./fixtures/sbb-journeys.js') +import {sbbJourneys as expected} from './fixtures/sbb-journeys.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/sncb-journey-with-chki.js b/test/sncb-journey-with-chki.js index aa51a71d..54a1851b 100644 --- a/test/sncb-journey-with-chki.js +++ b/test/sncb-journey-with-chki.js @@ -1,9 +1,12 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/sncb') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/sncb/index.js' const resWithChkiLeg = require('./fixtures/sncb-journey-with-chki.json') const client = createClient(rawProfile, 'public-transport/hafas-client:test') diff --git a/test/throttle.js b/test/throttle.js index 5d25c9ca..706020a0 100644 --- a/test/throttle.js +++ b/test/throttle.js @@ -1,10 +1,13 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const withThrottling = require('../throttle') -const vbbProfile = require('../p/vbb') +import {createClient} from '../index.js' +import {withThrottling} from '../throttle.js' +import {profile as vbbProfile} from '../p/vbb/index.js' const depsRes = require('./fixtures/vbb-departures.json') const ua = 'public-transport/hafas-client:test' diff --git a/test/vbb-departures.js b/test/vbb-departures.js index 0f539b4a..1d5f8072 100644 --- a/test/vbb-departures.js +++ b/test/vbb-departures.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/vbb') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/vbb/index.js' const res = require('./fixtures/vbb-departures.json') -const expected = require('./fixtures/vbb-departures.js') +import {vbbDepartures as expected} from './fixtures/vbb-departures.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/vbb-journeys.js b/test/vbb-journeys.js index 33e09479..d2e02d52 100644 --- a/test/vbb-journeys.js +++ b/test/vbb-journeys.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/vbb') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/vbb/index.js' const res = require('./fixtures/vbb-journeys.json') -const expected = require('./fixtures/vbb-journeys.js') +import {vbbJourneys as expected} from './fixtures/vbb-journeys.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/vbb-on-demand-trip.js b/test/vbb-on-demand-trip.js index d1dd2084..e8734908 100644 --- a/test/vbb-on-demand-trip.js +++ b/test/vbb-on-demand-trip.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/vbb') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/vbb/index.js' const res = require('./fixtures/vbb-on-demand-trip.json') -const expected = require('./fixtures/vbb-on-demand-trip.js') +import {vbbOnDemandTrip as expected} from './fixtures/vbb-on-demand-trip.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/vsn-departures.js b/test/vsn-departures.js index 5474e31e..bc2ca9cf 100644 --- a/test/vsn-departures.js +++ b/test/vsn-departures.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/vsn') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/vsn/index.js' const res = require('./fixtures/vsn-departures.json') -const expected = require('./fixtures/vsn-departures.js') +import {vsnDepartures as expected} from './fixtures/vsn-departures.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/test/vsn-remarks.js b/test/vsn-remarks.js index 92129cb5..fedcf519 100644 --- a/test/vsn-remarks.js +++ b/test/vsn-remarks.js @@ -1,11 +1,14 @@ -'use strict' +// todo: use import assertions once they're supported by Node.js & ESLint +// https://github.com/tc39/proposal-import-assertions +import {createRequire} from 'module' +const require = createRequire(import.meta.url) -const tap = require('tap') +import tap from 'tap' -const createClient = require('..') -const rawProfile = require('../p/vsn') +import {createClient} from '../index.js' +import {profile as rawProfile} from '../p/vsn/index.js' const res = require('./fixtures/vsn-remarks.json') -const expected = require('./fixtures/vsn-remarks.js') +import {vsnRemarks as expected} from './fixtures/vsn-remarks.js' const client = createClient(rawProfile, 'public-transport/hafas-client:test') const {profile} = client diff --git a/throttle.js b/throttle.js index ad1d2929..2d4c6dc9 100644 --- a/throttle.js +++ b/throttle.js @@ -1,7 +1,5 @@ -'use strict' - -const throttle = require('p-throttle') -const defaultProfile = require('./lib/default-profile') +import throttle from 'p-throttle' +import {defaultProfile} from './lib/default-profile.js' const withThrottling = (profile, limit = 5, interval = 1000) => { // https://github.com/public-transport/hafas-client/issues/76#issuecomment-574408717 @@ -13,4 +11,6 @@ const withThrottling = (profile, limit = 5, interval = 1000) => { } } -module.exports = withThrottling +export { + withThrottling, +} diff --git a/tools/debug-cli/cli.js b/tools/debug-cli/cli.js index 3b8b8e86..db2ac09e 100755 --- a/tools/debug-cli/cli.js +++ b/tools/debug-cli/cli.js @@ -1,9 +1,7 @@ #!/usr/bin/env node -'use strict' -const mri = require('mri') -const {join} = require('path') -const createClient = require('../..') +import mri from 'mri' +import {createClient} from '../../index.js' const showError = (err) => { console.error(err) @@ -52,19 +50,21 @@ const parseArgs = [ const argv = mri(process.argv.slice(2)) -const profile = require(join('../../p', argv._[0])) -const client = createClient(profile, 'hafas-client debug CLI') - const fnName = argv._[1] -const fn = client[fnName] const args = argv._.slice(2).map((arg, i) => { const parser = parseArgs.find(([_fnName, _i]) => _fnName === fnName && _i === i) return parser ? parser[2](arg) : arg }) -fn(...args) -.then((res) => { +;(async () => { + const {profile} = await import(`../../p/${argv._[0]}/index.js`) + + const client = createClient(profile, 'hafas-client debug CLI') + + const fn = client[fnName] + + const res = await fn(...args) process.stdout.write(JSON.stringify(res) + '\n') -}) +})() .catch(showError) diff --git a/tools/debug-cli/package.json b/tools/debug-cli/package.json index cd6a49cf..2ddf2b69 100644 --- a/tools/debug-cli/package.json +++ b/tools/debug-cli/package.json @@ -2,6 +2,7 @@ "private": true, "name": "debug-cli", "version": "1.0.0", + "type": "module", "bin": { "hafas-client-debug-cli": "./cli.js" }, diff --git a/tools/endpoint-hci-version/cli.js b/tools/endpoint-hci-version/cli.js index e34b0211..9d20882f 100755 --- a/tools/endpoint-hci-version/cli.js +++ b/tools/endpoint-hci-version/cli.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -'use strict' -const mri = require('mri') +import mri from 'mri' +import {createClient} from '../../index.js' const argv = mri(process.argv.slice(2), { boolean: [ @@ -23,18 +23,16 @@ Examples: process.exit(0) } -const {join} = require('path') -const createClient = require('../..') - -const profile = require(join(__dirname, '..', '..', 'p', argv._[0])) -const client = createClient( - profile, - 'hafas-client-endpoint-hci-version', -) - const silent = argv.silent || argv.s ;(async () => { + const {profile} = import(`../../p/${argv._[0]}/index.js`) + + const client = createClient( + profile, + 'hafas-client-endpoint-hci-version', + ) + const {hciVersion: v} = await client.serverInfo() if ('string' !== typeof v || !v) { diff --git a/tools/endpoint-hci-version/package.json b/tools/endpoint-hci-version/package.json index f2178771..a0c8494c 100644 --- a/tools/endpoint-hci-version/package.json +++ b/tools/endpoint-hci-version/package.json @@ -2,6 +2,7 @@ "private": true, "name": "endpoint-hci-version", "version": "1.0.0", + "type": "module", "bin": { "hafas-client-endpoint-hci-version": "./cli.js" },