From 6aa30648fef6c9dc797b6ddc8597a66a39022b82 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Mon, 13 Jun 2022 16:23:49 -0300 Subject: [PATCH] chore: update type definitions --- .../contents/classes/NavigationEndpoint.js | 2 +- lib/parser/youtube/Search.js | 24 +++++++++--- typings/lib/parser/youtube/Search.d.ts | 39 +++++++++++++------ 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/lib/parser/contents/classes/NavigationEndpoint.js b/lib/parser/contents/classes/NavigationEndpoint.js index 4aeac269..7b14b33e 100644 --- a/lib/parser/contents/classes/NavigationEndpoint.js +++ b/lib/parser/contents/classes/NavigationEndpoint.js @@ -43,7 +43,7 @@ class NavigationEndpoint { params: data?.watchEndpoint.params || null, index: data?.watchEndpoint.index || null, supported_onesie_config: data?.watchEndpoint?.watchEndpointSupportedOnesieConfig, - music_video_type: configs.musicVideoType + music_video_type: configs?.musicVideoType || null }; } diff --git a/lib/parser/youtube/Search.js b/lib/parser/youtube/Search.js index 0dc956ac..8b0f7ede 100644 --- a/lib/parser/youtube/Search.js +++ b/lib/parser/youtube/Search.js @@ -1,7 +1,7 @@ 'use strict'; const Parser = require('../contents'); -const { InnertubeError } = require('../../utils/Utils'); +const { InnertubeError, observe } = require('../../utils/Utils'); /** @namespace */ class Search { @@ -26,6 +26,7 @@ class Search { const contents = this.#page.contents?.primary_contents.contents || this.#page.on_response_received_commands[0].continuation_items; + /** @type {object[]} */ this.results = contents.get({ type: 'itemSectionRenderer' }).contents; const shelves = this.results.findAll({ type: 'shelfRenderer' }, true); @@ -34,19 +35,26 @@ class Search { this.refinements = this.#page.refinements || []; this.estimated_results = this.#page.estimated_results; - this.sections = shelves.map((shelf) => ({ + /** @type {{ sections: { title: string; items: object[]; }[] }} */ + this.sections = observe(shelves.map((shelf) => ({ title: shelf.title.toString(), items: shelf.content.items - })); + }))); this.refinement_cards = { + /** @type {import('../contents/classes/RichListHeader')} */ header: card_list?.header || null, + /** @type {import('../contents/classes/SearchRefinementCard')} */ cards: card_list?.cards || [] }; this.#continuation = contents.get({ type: 'continuationItemRenderer' }); } + /** + * Retrieves next batch of results. + * @returns {Promise.} + */ async getContinuation() { const response = await this.#continuation.endpoint.call(this.#actions); return new Search(response, this.#actions, { is_continuation: true }); @@ -54,8 +62,8 @@ class Search { /** * Applies given refinement card and returns a new {@link Search} object. - * @param {SearchRefinementCard || string} refinement card object or query - * @returns {Search} + * @param {SearchRefinementCard | string} card - refinement card object or query + * @returns {Promise.} */ async selectRefinementCard(card) { let target_card; @@ -76,18 +84,22 @@ class Search { return new Search(page, this.#actions, { is_continuation: true }); } + /** @type {boolean} */ get has_continuation() { return !!this.#continuation; } + /** @type {string[]} */ get refinement_card_queries() { return this.refinement_cards.cards.map((card) => card.query); } + /** @type {import('../contents/classes/Video')[]} */ get videos() { return this.results.findAll({ type: 'videoRenderer' }); } - + + /** @type {import('../contents/classes/Playlist')[]} */ get playlists() { return this.results.findAll({ type: 'playlistRenderer' }); } diff --git a/typings/lib/parser/youtube/Search.d.ts b/typings/lib/parser/youtube/Search.d.ts index 4109b024..a7a4031b 100644 --- a/typings/lib/parser/youtube/Search.d.ts +++ b/typings/lib/parser/youtube/Search.d.ts @@ -11,25 +11,42 @@ declare class Search { constructor(response: object, actions: import('../../core/Actions'), args?: { is_continuation?: boolean; }); - results: any; + /** @type {object[]} */ + results: object[]; refinements: any; estimated_results: any; - sections: any; - refinement_cards: { - header: any; - cards: any; + /** @type {{ sections: { title: string; items: object[]; }[] }} */ + sections: { + sections: { + title: string; + items: object[]; + }[]; }; + refinement_cards: { + /** @type {import('../contents/classes/RichListHeader')} */ + header: import('../contents/classes/RichListHeader'); + /** @type {import('../contents/classes/SearchRefinementCard')} */ + cards: import('../contents/classes/SearchRefinementCard'); + }; + /** + * Retrieves next batch of results. + * @returns {Promise.} + */ getContinuation(): Promise; /** * Applies given refinement card and returns a new {@link Search} object. - * @param {SearchRefinementCard || string} refinement card object or query - * @returns {Search} + * @param {SearchRefinementCard | string} card - refinement card object or query + * @returns {Promise.} */ - selectRefinementCard(card: any): Search; + selectRefinementCard(card: SearchRefinementCard | string): Promise; + /** @type {boolean} */ get has_continuation(): boolean; - get refinement_card_queries(): any; - get videos(): any; - get playlists(): any; + /** @type {string[]} */ + get refinement_card_queries(): string[]; + /** @type {import('../contents/classes/Video')[]} */ + get videos(): import("../contents/classes/Video")[]; + /** @type {import('../contents/classes/Playlist')[]} */ + get playlists(): import("../contents/classes/Playlist")[]; get page(): any; #private; }