From bdebb9f741291d2f0640274454c90b5ccda8ea5d Mon Sep 17 00:00:00 2001 From: Luan Date: Sun, 17 Nov 2024 01:19:08 -0300 Subject: [PATCH] feat(NavigationEndpoint): Add name property Sometimes you can make assumptions based on the endpoint's object name. --- src/parser/classes/NavigationEndpoint.ts | 37 ++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/parser/classes/NavigationEndpoint.ts b/src/parser/classes/NavigationEndpoint.ts index db111791..b2064698 100644 --- a/src/parser/classes/NavigationEndpoint.ts +++ b/src/parser/classes/NavigationEndpoint.ts @@ -7,22 +7,23 @@ import CreatePlaylistDialog from './CreatePlaylistDialog.js'; import type ModalWithTitleAndButton from './ModalWithTitleAndButton.js'; import OpenPopupAction from './actions/OpenPopupAction.js'; +export type Metadata = { + url?: string; + api_url?: string; + page_type?: string; + send_post?: boolean; +}; + export default class NavigationEndpoint extends YTNode { static type = 'NavigationEndpoint'; - payload; - dialog?: CreatePlaylistDialog | YTNode | null; - modal?: ModalWithTitleAndButton | YTNode | null; - open_popup?: OpenPopupAction | null; - - next_endpoint?: NavigationEndpoint; - - metadata: { - url?: string; - api_url?: string; - page_type?: string; - send_post?: boolean; - }; + public name?: string; + public payload: any; + public dialog?: CreatePlaylistDialog | YTNode | null; + public modal?: ModalWithTitleAndButton | YTNode | null; + public open_popup?: OpenPopupAction | null; + public next_endpoint?: NavigationEndpoint; + public metadata: Metadata; constructor(data: RawNode) { super(); @@ -33,13 +34,13 @@ export default class NavigationEndpoint extends YTNode { if (Reflect.has(data || {}, 'openPopupAction')) this.open_popup = new OpenPopupAction(data.openPopupAction); - const name = Object.keys(data || {}) + this.name = Object.keys(data || {}) .find((item) => item.endsWith('Endpoint') || item.endsWith('Command') ); - this.payload = name ? Reflect.get(data, name) : {}; + this.payload = this.name ? Reflect.get(data, this.name) : {}; if (Reflect.has(this.payload, 'dialog') || Reflect.has(this.payload, 'content')) { this.dialog = Parser.parseItem(this.payload.dialog || this.payload.content); @@ -69,8 +70,8 @@ export default class NavigationEndpoint extends YTNode { if (data?.commandMetadata?.webCommandMetadata?.apiUrl) { this.metadata.api_url = data.commandMetadata.webCommandMetadata.apiUrl.replace('/youtubei/v1/', ''); - } else if (name) { - this.metadata.api_url = this.getEndpoint(name); + } else if (this.name) { + this.metadata.api_url = this.getPath(this.name); } if (data?.commandMetadata?.webCommandMetadata?.sendPost) { @@ -87,7 +88,7 @@ export default class NavigationEndpoint extends YTNode { /** * Sometimes InnerTube does not return an API url, in that case the library should set it based on the name of the payload object. */ - getEndpoint(name: string) { + getPath(name: string) { switch (name) { case 'browseEndpoint': return '/browse';