import { I_END_POINT } from "../constants" ; import { extendsBuiltIn , getContinuationFromItems , YoutubeRawData } from "../common" ; import { ChannelCompact , PlaylistCompact , VideoCompact , ClientTypes , Client } from "." ; export type SearchResultType < T > = T extends "video" | VideoCompact ? VideoCompact : T extends "channel" | ChannelCompact ? ChannelCompact : T extends "playlist" | PlaylistCompact ? PlaylistCompact : VideoCompact | ChannelCompact | PlaylistCompact ; /**
 * Represents search result, usually returned from `client.search();`.
 *
 *  SearchResult is a subclass of [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
 * with  SearchResult . next method to navigate through pagination
 *
 *  example noInheritDoc @ extendsBuiltIn ( ) export default class SearchResult < T > extends Array < SearchResultType < T > > { /** The estimated search result count */ estimatedResults ! : number ; continuation ? : string ; private client ! : Client ; hidden constructor ( ) { super ( ) ; } hidden load ( client ) { this . client = client ; return this ; } param query param options hidden async init ( query , options ) { const response = await this . client . http . post ( `${ I_END_POINT }/search` , { data : { query , params : options . params || SearchResult . getSearchTypeParam ( options . type ) , } , } ) ; this . estimatedResults = + response . data . estimatedResults ; if ( this . estimatedResults > 0 ) { this . loadSearchResult ( response . data . contents . twoColumnSearchResultsRenderer . primaryContents . sectionListRenderer . contents ) ; } return this ; } example param count async next ( count = 1 ) { const newSearchResults = [ ] ; for ( let i = 0 ; i < count ; i ++ ) { if ( ! this . continuation ) break ; const response = await this . client . http . post ( `${ I_END_POINT }/search` , { data : { continuation : this . continuation } , } ) ; newSearchResults . push ( ... this . loadSearchResult ( response . data . onResponseReceivedCommands [ 0 ] . appendContinuationItemsAction . continuationItems ) ) ; } this . push ( ... newSearchResults ) ; return newSearchResults ; } /** Load videos data from youtube */ private loadSearchResult ( sectionListContents ) { const contents = sectionListContents . filter ( ( c ) => "itemSectionRenderer" in c ) . pop ( ) . itemSectionRenderer . contents ; this . continuation = getContinuationFromItems ( sectionListContents ) ; const newContent = [ ] ; for ( const content of contents ) { if ( "playlistRenderer" in content ) newContent . push ( new PlaylistCompact ( { client : this . client } ) . load ( content . playlistRenderer ) ) ; else if ( "videoRenderer" in content ) newContent . push ( new VideoCompact ( { client : this . client } ) . load ( content . videoRenderer ) ) ; else if ( "channelRenderer" in content ) newContent . push ( new ChannelCompact ( { client : this . client } ) . load ( content . channelRenderer ) ) ; } this . push ( ... ( newContent as Array < SearchResultType < T > > ) ) ; return newContent as Array < SearchResultType < T > > ; } param type hidden static getSearchTypeParam ( type ) { const searchType = { video : "EgIQAQ%3D%3D" , playlist : "EgIQAw%3D%3D" , channel : "EgIQAg%3D%3D" , all : "" , } ; return type in searchType ? searchType [ type ] : "" ; } }