Skip to content

Commit 4a9c66b

Browse files
committed
subscription API: basic E2E test βœ…, API docs πŸ“ [todo]
1 parent f227221 commit 4a9c66b

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

β€Ždocs/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [`remarks([opt])`](remarks.md) – get all remarks
1515
- [`lines(query, [opt])`](lines.md) – get all lines matching a name
1616
- [`serverInfo([opt])`](server-info.md) – fetch meta information from HAFAS
17+
- [subscription-related methods](subscription-api.md)
1718

1819
## Migrating from an old `hafas-client` version
1920

β€Ždocs/subscription-api.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# subscription-related methods
2+
3+
todo: explanation
4+
5+
```js
6+
const {journeys} = await client.journeys(ludwigshafen, meckesheim, {
7+
results: 1, polylines: true,
8+
})
9+
const journey = journeys[0]
10+
const channelId = 'some-channel'
11+
12+
const userId = await client.createSubscriptionsUser([channelId])
13+
const subId = await client.subscribeToJourney(userId, [channelId], journey.refreshToken)
14+
15+
const sub = await client.subscription(userId, subId, {journey: true, activeDays: true})
16+
17+
await client.unsubscribe(userId, subId)
18+
```
19+
20+
todo: API docs

β€Žindex.js

+2
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@ const createClient = (profile, userAgent, opt = {}) => {
691691
userId,
692692
channels: channelIds.map((channelId) => ({channelId})),
693693
conSubscr: {
694+
// todo: data?
695+
// seems like without it, HAFAS ignores the data inside `journeyRefreshToken`
694696
ctxRecon: journeyRefreshToken,
695697
hysteresis: {
696698
minDeviationInterval: opt.minimumDelay,

β€Žtest/e2e/vrn.js

+43
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,46 @@ test('radar', async (t) => {
227227
validate(t, vehicles, 'movements', 'vehicles')
228228
t.end()
229229
})
230+
231+
test('subscribing to a journey works', async (t) => {
232+
const {journeys} = await client.journeys(ludwigshafen, meckesheim, {
233+
departure: when, results: 1,
234+
})
235+
const journey = journeys[0]
236+
const channelId = 'some-channel'
237+
238+
const userId = await client.createSubscriptionsUser([channelId])
239+
t.equal(typeof userId, 'string')
240+
t.ok(userId)
241+
t.deepEqual(await client.subscriptions(userId), [])
242+
243+
const subId = await client.subscribeToJourney(userId, [channelId], journey.refreshToken)
244+
t.ok(subId !== undefined && subId !== null, 'subId')
245+
246+
const {
247+
subscription: sub, rtEvents, himEvents,
248+
} = await client.subscription(userId, subId, {activeDays: true})
249+
t.ok(sub)
250+
t.equal(sub.id, subId)
251+
t.deepEqual(sub.hysteresis, {minDeviationInterval: 1, notificationStart: 30})
252+
t.deepEqual(sub.monitorFlags, ['AF', 'DF', 'DV', 'FTF', 'OF', 'PF'])
253+
t.ok(Array.isArray(sub.connectionInfo))
254+
t.ok(sub.connectionInfo.length > 0)
255+
t.equal(sub.journeyRefreshToken, journey.refreshToken)
256+
t.ok(sub.activeDays)
257+
t.ok(Array.isArray(rtEvents))
258+
t.ok(Array.isArray(himEvents))
259+
260+
const subs = await client.subscriptions(userId)
261+
t.deepEqual(subs, [{
262+
id: subId,
263+
status: 'ACTIVE',
264+
channels: [{id: channelId}],
265+
journeyRefreshToken: journey.refreshToken,
266+
}])
267+
268+
await client.unsubscribe(userId, subId)
269+
t.deepEqual(await client.subscriptions(userId), [])
270+
271+
t.end()
272+
})

0 commit comments

Comments
Β (0)