@@ -7,6 +7,7 @@ import {validateProfile} from './lib/validate-profile.js';
7
7
import { INVALID_REQUEST } from './lib/errors.js' ;
8
8
import { sliceLeg } from './lib/slice-leg.js' ;
9
9
import { HafasError } from './lib/errors.js' ;
10
+ import { profile as dbProfile } from './p/db/index.js' ;
10
11
11
12
// background info: https://github.com/public-transport/hafas-client/issues/286
12
13
const FORBIDDEN_USER_AGENTS = [
@@ -268,6 +269,88 @@ const createClient = (profile, userAgent, opt = {}) => {
268
269
} ;
269
270
} ;
270
271
272
+ const bestPrices = async ( from , to , opt = { } ) => {
273
+ from = profile . formatLocation ( profile , from , 'from' ) ;
274
+ to = profile . formatLocation ( profile , to , 'to' ) ;
275
+
276
+ opt = Object . assign ( {
277
+ via : null , // let journeys pass this station?
278
+ transfers : - 1 , // maximum nr of transfers
279
+ bike : false , // only bike-friendly journeys
280
+ tickets : false , // return tickets?
281
+ polylines : false , // return leg shapes?
282
+ subStops : false , // parse & expose sub-stops of stations?
283
+ entrances : false , // parse & expose entrances of stops/stations?
284
+ remarks : true , // parse & expose hints & warnings?
285
+ scheduledDays : false , // parse & expose dates each journey is valid on?
286
+ } , opt ) ;
287
+ if ( opt . via ) {
288
+ opt . via = profile . formatLocation ( profile , opt . via , 'opt.via' ) ;
289
+ }
290
+
291
+ let when = new Date ( ) ;
292
+ if ( opt . departure !== undefined && opt . departure !== null ) {
293
+ when = new Date ( opt . departure ) ;
294
+ if ( Number . isNaN ( Number ( when ) ) ) {
295
+ throw new TypeError ( 'opt.departure is invalid' ) ;
296
+ }
297
+ const now = new Date ( ) ;
298
+ const today = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) ) ;
299
+ if ( today > when ) {
300
+ throw new TypeError ( 'opt.departure date older than current date.' ) ;
301
+ }
302
+ }
303
+
304
+ const filters = [
305
+ profile . formatProductsFilter ( { profile} , opt . products || { } ) ,
306
+ ] ;
307
+ if (
308
+ opt . accessibility
309
+ && profile . filters
310
+ && profile . filters . accessibility
311
+ && profile . filters . accessibility [ opt . accessibility ]
312
+ ) {
313
+ filters . push ( profile . filters . accessibility [ opt . accessibility ] ) ;
314
+ }
315
+
316
+ const query = {
317
+ maxChg : opt . transfers ,
318
+ depLocL : [ from ] ,
319
+ viaLocL : opt . via ? [ { loc : opt . via } ] : [ ] ,
320
+ arrLocL : [ to ] ,
321
+ jnyFltrL : filters ,
322
+ getTariff : Boolean ( opt . tickets ) ,
323
+
324
+ getPolyline : Boolean ( opt . polylines ) ,
325
+ } ;
326
+ query . outDate = profile . formatDate ( profile , when ) ;
327
+
328
+ if ( profile . endpoint !== dbProfile . endpoint ) {
329
+ throw new Error ( 'db profile expected.' ) ;
330
+ }
331
+
332
+ const { res, common} = await profile . request ( { profile, opt} , userAgent , {
333
+ cfg : { polyEnc : 'GPA' } ,
334
+ meth : 'BestPriceSearch' ,
335
+ req : profile . transformJourneysQuery ( { profile, opt} , query ) ,
336
+ } ) ;
337
+ if ( ! Array . isArray ( res . outConL ) ) {
338
+ return { } ;
339
+ }
340
+ // todo: outConGrpL
341
+
342
+ const ctx = { profile, opt, common, res} ;
343
+ const journeys = res . outConL . map ( j => profile . parseJourney ( ctx , j ) ) ;
344
+ const bestPrices = res . outDaySegL . map ( j => profile . parseBestPrice ( ctx , j , journeys ) ) ;
345
+
346
+ return {
347
+ bestPrices,
348
+ realtimeDataUpdatedAt : res . planrtTS && res . planrtTS !== '0'
349
+ ? parseInt ( res . planrtTS )
350
+ : null ,
351
+ } ;
352
+ } ;
353
+
271
354
const refreshJourney = async ( refreshToken , opt = { } ) => {
272
355
if ( 'string' !== typeof refreshToken || ! refreshToken ) {
273
356
throw new TypeError ( 'refreshToken must be a non-empty string.' ) ;
@@ -889,6 +972,9 @@ const createClient = (profile, userAgent, opt = {}) => {
889
972
if ( profile . lines !== false ) {
890
973
client . lines = lines ;
891
974
}
975
+ if ( profile . bestPrices !== false ) {
976
+ client . bestPrices = bestPrices ;
977
+ }
892
978
Object . defineProperty ( client , 'profile' , { value : profile } ) ;
893
979
return client ;
894
980
} ;
0 commit comments