I am banging my head last couple of hours how to do this, I am getting some json data from api with superagent :
superagent.get(url)
.set('x-rapidapi-host','v3.football.api-sports.io')
.set('x-rapidapi-key',process.env.FOOTBALL_API_KEY)
.pipe(JSONStream.parse('response.*')).pipe(oddStream).pipe(transfStream).pipe(JSONStream.stringify()).pipe(f1)
I use JSONStream to parse it and have to transformStreams to aggregate some data and piping it back to file stream as JSON. The problem is in the oddStream I want to get the ID of the game fixture and make another http get request to get additional data about the game (returned as json) and to add it to the output json :
const oddStream = new Transform({
objectMode : true,
transform(chunk,encoding,cb){
if(allowed(chunk.league.id)){
const fixtureId = chunk.fixture.id;
//Here I want to make another get request
superagent.get(`https://v3.football.api-sports.io/odds?fixture=${fixtureId}&bookmaker=6`)
.set('x-rapidapi-host','v3.football.api-sports.io')
.set('x-rapidapi-key',process.env.FOOTBALL_API_KEY)
.end((err,res) => {
if(err){
console.log(err);
}
const odds = res.body.response[0];
//?????????? Attach the data to the chunk object
chunk.odds = odds;
//Push it no the next transform stream
this.push(chunk);
})
}
cb();
}
});
The other transform stream is working fine ,I will not even bother the show the code.The problem is in this second transform stream ,any suggestions how can I do that make http request in the transform method and add more data to the chunk object and pass it to the next transform stream ?