Skip to content

Commit 55245aa

Browse files
committed
rest-exe request(): adapt to new findInTree() 🐛
see #154
1 parent 8b708b2 commit 55245aa

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

rest-exe.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,25 @@ const createRestClient = (profile, token, userAgent) => {
107107
// todo: sometimes it returns a body without any data
108108
// e.g. `location.nearbystops` with an invalid `type`
109109

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+
}
117129

118130
return {profile, opt, res: body}
119131
}

0 commit comments

Comments
 (0)