Skip to content

Commit

Permalink
add order()
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Apr 15, 2020
1 parent 18718c5 commit 67bc72a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
6 changes: 6 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const find = (name, stream) => {

const trip = await flix.trip(depAtAlexanderplatz.tripId)
console.error(trip)

const bookingNr = process.env.BOOKING_NR
const nameOrEmail = process.env.NAME_OR_EMAIL
if (bookingNr && nameOrEmail) {
console.log(await flix.order(bookingNr, nameOrEmail))
}
})()
.catch((err) => {
console.error(err)
Expand Down
5 changes: 2 additions & 3 deletions lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ const { fetch } = require('fetch-ponyfill')()
const { stringify } = require('query-string')
const debug = require('debug')('flix:fetch')

const endpoint = 'https://api.flixbus.com/mobile/v1/'

// keys & endpoints
// https://api.meinfernbus.de/mobile/v1: uR=s7k6m=[cCS^zY86H8CNAnkC6n
// https://api.flixbus.com/mobile/v1: k8LKgcuFoHnN5x/NdDYD6QSvjB4=
// keys seem to work for both (flix also works for mfb and vice versa)

const defaults = {
endpoint: 'https://api.flixbus.com/mobile/v1/',
apiKey: 'k8LKgcuFoHnN5x/NdDYD6QSvjB4=',
userAgent: 'FlixBus/3.3 (iPhone; iOS 9.3.4; Scale/2.00)',
}
Expand All @@ -25,7 +24,7 @@ const request = async (route, opt, headers = {}, query = {}) => {
throw new Error('`opt.userAgent` must be non-empty string')
}

const url = `${endpoint}${route}?${stringify(query)}`
const url = `${opt.endpoint}${route}?${stringify(query)}`
const fetchCfg = {
headers: {
'X-API-Authentication': opt.apiKey,
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const regions = require('./regions')
const journeys = require('./journeys')
const { departures, arrivals } = require('./timetable')
const trip = require('./trip')
const order = require('./order')

module.exports = {
stations,
regions,
journeys,
departures, arrivals,
trip,
order,
}
78 changes: 78 additions & 0 deletions lib/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict'

const {
normalizeEmpty,
parseSparseTrip,
parseStation,
parseWhen,
} = require('./util')
const fetch = require('./fetch')

const parseNote = (note) => {
return {
type: note.name,
title: note.title,
html: note.html,
}
}

const order = async (bookingNr, nameOrEmail, opt = {}) => {
opt = {
...opt,
}

if (typeof bookingNr !== 'string' || !bookingNr) {
throw new Error('bookingNr is invalid')
}
if (typeof nameOrEmail !== 'string' || !nameOrEmail) {
throw new Error('nameOrEmail is invalid')
}

let res
try {
res = await fetch(`orders/${encodeURIComponent(bookingNr)}/search.json`, {
...opt,
endpoint: 'https://api.flixbus.com/mobile/v2/',
}, {}, {
query: nameOrEmail,
})
} catch (err) {
if (err.statusCode === 404) return null
throw err
}

return {
id: res.order.order_uid,
bookingNr: res.order.id,
downloadCode: res.download_hash,
qrCodeUrl: res.order.qr_image,
bookingConfirmationUrl: res.order.reminder_link,
// @todo res.order.invoices
// @todo res.order.attachments
notes: res.order.info_blocks.map(parseNote),
legs: res.order.trips.map((trip) => ({
...parseSparseTrip(null, trip),
tripId: trip.trip_uid,
origin: parseStation(trip.departure_station),
departure: parseWhen({ planned: 'departure' }, trip, false).when,
destination: parseStation(trip.arrival_station),
arrival: parseWhen({ planned: 'arrival' }, trip, false).when,
orderStatus: trip.order_status,
passengers: trip.passengers.map((p) => ({
type: p.type,
firstName: normalizeEmpty(p.firstname),
lastName: normalizeEmpty(p.lastname),
phone: normalizeEmpty(p.phone),
})),
appleWalletUrl: trip.passbook_url,
warnings: trip.warnings,
// @todo p.push_channel_uid, p.seats_per_relation
})),
}
}

order.features = { // require by fpti
...fetch.features,
}

module.exports = order

0 comments on commit 67bc72a

Please sign in to comment.