From 2535bb630ee845c5eb4a469e24dc3fc8c9fa2857 Mon Sep 17 00:00:00 2001 From: dabund24 Date: Wed, 8 Jan 2025 22:07:47 +0100 Subject: [PATCH] move check for transfers set to -1 to index.js --- index.js | 3 +++ p/db/journeys-req.js | 2 +- p/dbnav/journeys-req.js | 2 +- test/format/db-journeys-query.js | 20 -------------------- 4 files changed, 5 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 2a30d330..9a235a9c 100644 --- a/index.js +++ b/index.js @@ -146,6 +146,9 @@ const createClient = (profile, userAgent, opt = {}) => { } journeysRef = opt.laterThan; } + if (opt?.transfers === -1) { // having `transfers` set to -1 may not be allowed, see https://github.com/public-transport/db-vendo-client/issues/5 + opt.transfers = null; + } opt = Object.assign({ results: null, // number of journeys – `null` means "whatever HAFAS returns" diff --git a/p/db/journeys-req.js b/p/db/journeys-req.js index c5164a58..9b0fa122 100644 --- a/p/db/journeys-req.js +++ b/p/db/journeys-req.js @@ -7,7 +7,7 @@ const formatJourneysReq = (ctx, from, to, when, outFrwd, journeysRef) => { // TODO opt.accessibility // TODO routingMode let query = { - maxUmstiege: opt.transfers === -1 ? undefined : opt.transfers, // setting `maxUmstiege` to -1 is not allowed, see https://github.com/public-transport/db-vendo-client/issues/5 + maxUmstiege: opt.transfers, minUmstiegszeit: opt.transferTime, deutschlandTicketVorhanden: false, nurDeutschlandTicketVerbindungen: false, diff --git a/p/dbnav/journeys-req.js b/p/dbnav/journeys-req.js index 653a6c48..668f7a81 100644 --- a/p/dbnav/journeys-req.js +++ b/p/dbnav/journeys-req.js @@ -46,7 +46,7 @@ const formatJourneysReq = (ctx, from, to, when, outFrwd, journeysRef) => { ? [{locationId: profile.formatLocation(profile, opt.via, 'opt.via').lid}] : undefined, zielLocationId: to.lid, - maxUmstiege: opt.transfers === -1 ? undefined : opt.transfers, // setting `maxUmstiege` to -1 is not allowed, see https://github.com/public-transport/db-vendo-client/issues/5 + maxUmstiege: opt.transfers ?? undefined, minUmstiegsdauer: opt.transferTime || undefined, fahrradmitnahme: opt.bike, }, diff --git a/test/format/db-journeys-query.js b/test/format/db-journeys-query.js index 8255e75d..7ad0367b 100644 --- a/test/format/db-journeys-query.js +++ b/test/format/db-journeys-query.js @@ -114,23 +114,3 @@ tap.test('formats a journeys() request with BC correctly (DB)', (t) => { }); t.end(); }); - -tap.test('formats a journeys() request with unlimited transfers (DB)', (t) => { - const _opt = {...opt}; - const ctx = {profile, opt: _opt}; - - ctx.opt.transfers = 0; // no transfers - const reqZeroTransfers = profile.formatJourneysReq(ctx, '8098160', '8000284', new Date('2024-12-07T23:50:12+01:00'), true, null); - t.same(reqZeroTransfers.body.maxUmstiege, 0); - - ctx.opt.transfers = null; // unconstrained transfers implicit - const reqUnlimitedTransfersImplicit = profile.formatJourneysReq(ctx, '8098160', '8000284', new Date('2024-12-07T23:50:12+01:00'), true, null); - t.same(reqUnlimitedTransfersImplicit.body.maxUmstiege, undefined); - - ctx.opt.transfers = -1; // unconstrained transfers explicit - const reqUnlimitedTransfersExplicit = profile.formatJourneysReq(ctx, '8098160', '8000284', new Date('2024-12-07T23:50:12+01:00'), true, null); - t.same(reqUnlimitedTransfersExplicit.body.maxUmstiege, undefined); - - t.end(); - -});