|
1 | 1 | import { Credential, LeetCodeCN } from "leetcode-query"; |
| 2 | +import { |
| 3 | + NOTE_AGGREGATE_QUERY, |
| 4 | + NOTE_BY_QUESTION_ID_QUERY |
| 5 | +} from "./graphql/cn/note-queries.js"; |
2 | 6 | import { SEARCH_PROBLEMS_QUERY } from "./graphql/cn/search-problems.js"; |
3 | 7 | import { SOLUTION_ARTICLE_DETAIL_QUERY } from "./graphql/cn/solution-article-detail.js"; |
4 | 8 | import { SOLUTION_ARTICLES_QUERY } from "./graphql/cn/solution-articles.js"; |
@@ -358,6 +362,107 @@ export class LeetCodeCNService implements LeetCodeBaseService { |
358 | 362 | } |
359 | 363 | } |
360 | 364 |
|
| 365 | + /** |
| 366 | + * Retrieves user notes from LeetCode CN with filtering and pagination options. |
| 367 | + * Available only on LeetCode CN platform. |
| 368 | + * |
| 369 | + * @param options - Query parameters for filtering notes |
| 370 | + * @param options.aggregateType - Type of notes to aggregate (e.g., "QUESTION_NOTE") |
| 371 | + * @param options.keyword - Optional search term to filter notes |
| 372 | + * @param options.orderBy - Optional sorting criteria for notes |
| 373 | + * @param options.limit - Maximum number of notes to return |
| 374 | + * @param options.skip - Number of notes to skip (for pagination) |
| 375 | + * @returns Promise resolving to the filtered notes data |
| 376 | + */ |
| 377 | + async fetchUserNotes(options: { |
| 378 | + aggregateType: string; |
| 379 | + keyword?: string; |
| 380 | + orderBy?: string; |
| 381 | + limit?: number; |
| 382 | + skip?: number; |
| 383 | + }): Promise<any> { |
| 384 | + if (!this.isAuthenticated()) { |
| 385 | + throw new Error("Authentication required to fetch user notes"); |
| 386 | + } |
| 387 | + |
| 388 | + try { |
| 389 | + const variables = { |
| 390 | + aggregateType: options.aggregateType, |
| 391 | + keyword: options.keyword, |
| 392 | + orderBy: options.orderBy || "DESCENDING", |
| 393 | + limit: options.limit || 20, |
| 394 | + skip: options.skip || 0 |
| 395 | + }; |
| 396 | + |
| 397 | + return await this.leetCodeApi |
| 398 | + .graphql({ |
| 399 | + query: NOTE_AGGREGATE_QUERY, |
| 400 | + variables |
| 401 | + }) |
| 402 | + .then((response) => { |
| 403 | + return ( |
| 404 | + response.data?.noteAggregateNote || { |
| 405 | + count: 0, |
| 406 | + userNotes: [] |
| 407 | + } |
| 408 | + ); |
| 409 | + }); |
| 410 | + } catch (error) { |
| 411 | + console.error(`Error fetching user notes:`, error); |
| 412 | + throw error; |
| 413 | + } |
| 414 | + } |
| 415 | + |
| 416 | + /** |
| 417 | + * Retrieves user notes for a specific question ID. |
| 418 | + * Available only on LeetCode CN platform. |
| 419 | + * |
| 420 | + * @param questionId - The question ID to fetch notes for |
| 421 | + * @param limit - Maximum number of notes to return (default: 20) |
| 422 | + * @param skip - Number of notes to skip (default: 0) |
| 423 | + * @returns Promise resolving to the notes data for the specified question |
| 424 | + */ |
| 425 | + async fetchNotesByQuestionId( |
| 426 | + questionId: string, |
| 427 | + limit: number = 20, |
| 428 | + skip: number = 0 |
| 429 | + ): Promise<any> { |
| 430 | + if (!this.isAuthenticated()) { |
| 431 | + throw new Error( |
| 432 | + "Authentication required to fetch notes by question ID" |
| 433 | + ); |
| 434 | + } |
| 435 | + |
| 436 | + try { |
| 437 | + const variables = { |
| 438 | + noteType: "COMMON_QUESTION", |
| 439 | + questionId: questionId, |
| 440 | + limit, |
| 441 | + skip |
| 442 | + }; |
| 443 | + |
| 444 | + return await this.leetCodeApi |
| 445 | + .graphql({ |
| 446 | + query: NOTE_BY_QUESTION_ID_QUERY, |
| 447 | + variables |
| 448 | + }) |
| 449 | + .then((response) => { |
| 450 | + return ( |
| 451 | + response.data?.noteOneTargetCommonNote || { |
| 452 | + count: 0, |
| 453 | + userNotes: [] |
| 454 | + } |
| 455 | + ); |
| 456 | + }); |
| 457 | + } catch (error) { |
| 458 | + console.error( |
| 459 | + `Error fetching notes for question ${questionId}:`, |
| 460 | + error |
| 461 | + ); |
| 462 | + throw error; |
| 463 | + } |
| 464 | + } |
| 465 | + |
361 | 466 | isAuthenticated(): boolean { |
362 | 467 | return ( |
363 | 468 | !!this.credential && |
|
0 commit comments