@@ -107,13 +107,25 @@ const createRestClient = (profile, token, userAgent) => {
107
107
// todo: sometimes it returns a body without any data
108
108
// e.g. `location.nearbystops` with an invalid `type`
109
109
110
- unwrapNested ( body , '**.ServiceDays[0]' , 'serviceDays' )
111
- unwrapNested ( body , '**.LegList.Leg' , 'legs' )
112
- unwrapNested ( body , '**.Notes.Note' , 'notes' )
113
- unwrapNested ( body , '**.JourneyDetailRef.ref' , 'ref' )
114
- unwrapNested ( body , '**.Stops.Stop' , 'stops' )
115
- unwrapNested ( body , '**.Names.Name' , 'products' )
116
- unwrapNested ( body , '**.Directions.Direction' , 'directions' )
110
+ const mapping = {
111
+ '**.ServiceDays[0]' : 'serviceDays' ,
112
+ '**.LegList.Leg' : 'legs' ,
113
+ '**.Notes.Note' : 'notes' ,
114
+ '**.JourneyDetailRef.ref' : 'ref' ,
115
+ '**.Stops.Stop' : 'stops' ,
116
+ '**.Names.Name' : 'products' ,
117
+ '**.Directions.Direction' : 'directions' ,
118
+ }
119
+
120
+ const allMatches = findInTree ( body , Object . keys ( mapping ) )
121
+ for ( const [ needle , matches ] of Object . entries ( allMatches ) ) {
122
+ const newKey = mapping [ needle ]
123
+
124
+ for ( const [ item , parents ] of matches ) {
125
+ const grandParent = parents [ 1 ]
126
+ grandParent [ newKey ] = item
127
+ }
128
+ }
117
129
118
130
return { profile, opt, res : body }
119
131
}
0 commit comments