docs: update examples

This commit is contained in:
LuanRT
2022-08-03 17:06:00 -03:00
parent af6856ced4
commit d6398296c3
8 changed files with 118 additions and 105 deletions

View File

@@ -221,26 +221,22 @@ class NavigationEndpoint extends YTNode {
*/
async callTest(actions: Actions, args: {
parse: false;
params?: { [key: string]: any; }
params?: object
}): Promise<ActionsResponse>;
async callTest(actions: Actions, args?: {
parse: true;
params?: { [key: string]: any; }
parse?: true;
params?: object
}): Promise<ParsedResponse>;
async callTest(actions: Actions, args: {
parse: boolean;
params: { [key: string]: any; }
}): Promise<ParsedResponse | ActionsResponse>;
async callTest(actions: Actions, args?: {
parse?: boolean;
params?: { [key: string]: any; }
}): Promise<ParsedResponse | ActionsResponse> {
params?: object
} = { parse: true, params: {} }): Promise<ParsedResponse | ActionsResponse> {
if (!actions)
throw new Error('An active caller must be provided');
if (!this.metadata.api_url)
throw new Error('Expected an api_url, but none was found, this is a bug.');
const response = await actions.execute(this.metadata.api_url, { ...this.payload, ...(args?.params || {}), parse: args ? args.parse : true });
const response = await actions.execute(this.metadata.api_url, { ...this.payload, ...args.params, parse: args.parse });
return response;
}

View File

@@ -1,18 +1,19 @@
import Parser, { ParsedResponse } from '..';
import Actions, { ActionsResponse } from '../../core/Actions';
import { InnertubeError } from '../../utils/Utils';
import Button from '../classes/Button';
import CommentsHeader from '../classes/comments/CommentsHeader';
import CommentSimplebox from '../classes/comments/CommentSimplebox';
import CommentThread from '../classes/comments/CommentThread';
import ContinuationItem from '../classes/ContinuationItem';
import NavigationEndpoint from '../classes/NavigationEndpoint';
import Actions from '../../core/Actions';
import { InnertubeError } from '../../utils/Utils';
import Parser, { ParsedResponse } from '../index';
class Comments {
#page: ParsedResponse;
#actions;
#continuation;
header;
#actions: Actions;
#continuation: ContinuationItem | undefined;
header: CommentsHeader | undefined;
contents;
constructor(actions: Actions, data: any, already_parsed = false) {
@@ -20,34 +21,41 @@ class Comments {
this.#actions = actions;
const contents = this.#page.on_response_received_endpoints;
this.header = contents?.[0].contents?.get({ type: 'CommentsHeader' })?.as(CommentsHeader);
// TODO: validate this
const threads: CommentThread[] = contents?.[1].contents?.getAll({ type: 'CommentThread' }) as CommentThread[];
if (!contents)
throw new InnertubeError('Comments page did not have any content.');
this.header = contents[0].contents?.get({ type: 'CommentsHeader' })?.as(CommentsHeader);
const threads: CommentThread[] = contents[1].contents?.filterType(CommentThread) || [];
this.contents = threads.map((thread) => {
thread.comment?.setActions(this.#actions);
thread.setActions(this.#actions);
return thread;
});
}) as CommentThread[];
this.#continuation = contents?.[1].contents?.get({ type: 'ContinuationItem' })?.as(ContinuationItem);
this.#continuation = contents[1].contents?.get({ type: 'ContinuationItem' })?.as(ContinuationItem);
}
/**
* Creates a top-level comment.
*/
async createComment(text: string) {
const button = this.header?.create_renderer?.as(CommentSimplebox).submit_button.item();
async createComment(text: string): Promise<ActionsResponse> {
if (!this.header)
throw new InnertubeError('Page header is missing.');
const payload = {
const button = this.header.create_renderer?.as(CommentSimplebox).submit_button.item().as(Button);
if (!button)
throw new InnertubeError('Could not find target button.');
const response = await button.endpoint.callTest(this.#actions, {
params: {
commentText: text
},
parse: false
};
const response = await button?.key('endpoint').nodeOfType(NavigationEndpoint).callTest(this.#actions, payload);
});
return response;
}
@@ -55,7 +63,7 @@ class Comments {
/**
* Retrieves next batch of comments.
*/
async getContinuation() {
async getContinuation(): Promise<Comments> {
if (!this.#continuation)
throw new InnertubeError('Continuation not found');
@@ -74,7 +82,7 @@ class Comments {
return new Comments(this.#actions, page, true);
}
get page() {
get page(): ParsedResponse {
return this.#page;
}
}