I am expecting for the functionality using NestJS to make API call, and in case if I get token for next call then I need to make a request to the same API with that token as query params. The limitation is API returns only 50 records without token so to get next records I need to attach query params. In case if the token is empty I need to end the call. Fetching videos from youtube against particular uploaderId.
Sample I have written.
getPlaylistVideos(playlistId: string): any {
this.videos = [];
return new Promise((resolve, reject) =>
this.http
.get(this.getPlaylistURL(playlistId, ''))
.pipe(
map(response => response.data),
tap(() => console.log('\t', `Getting details by URL: this.http.get(this.getPlaylistURL(${playlistId})`)),
tap(videoResponse => {
this.videos.push(...videoResponse.items);
if (videoResponse['nextPageToken']) {
console.log('\t', 'NextToken: ', videoResponse['nextPageToken']);
return mergeMap(video => {
console.log('\t', `this.http.get(this.getPlaylistURL(${playlistId}, ${video['nextPageToken']})`);
return this.http.get(this.getPlaylistURL(playlistId, video['nextPageToken']));
});
} else {
console.log(`videoResponse:`, videoResponse['nextPageToken']);
return empty();
}
})
)
.toPromise()
.then(response => resolve(this.videos))
.catch(err => reject(err))
);
}
It fails to fetch next set of records.