Skip to content

Commit 21a0bd7

Browse files
committed
feat: add create and update note functionalities for LeetCode CN
1 parent 48184e2 commit 21a0bd7

File tree

7 files changed

+343
-1
lines changed

7 files changed

+343
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Command-line arguments take precedence over environment variables when both are
132132
| ---------------- | :----: | :-: | :-----------: | ----------------------------------------------------- |
133133
| **search_notes** |||| Searches for user notes with filtering options |
134134
| **get_note** |||| Retrieves notes for a specific problem by question ID |
135+
| **create_note** |||| Creates a new note for a specific problem |
136+
| **update_note** |||| Updates an existing note with new content |
135137

136138
### Solutions
137139

@@ -217,6 +219,16 @@ Command-line arguments take precedence over environment variables when both are
217219
- `questionId`: The question ID of the LeetCode problem (string, required)
218220
- `limit`: Maximum number of notes to return (number, optional, default: 10)
219221
- `skip`: Number of notes to skip (number, optional, default: 0)
222+
- **create_note** - Creates a new note for a specific LeetCode problem
223+
224+
- `questionId`: The question ID of the LeetCode problem (string, required)
225+
- `content`: The content of the note, supports markdown format (string, required)
226+
- `summary`: An optional short summary or title for the note (string, optional)
227+
228+
- **update_note** - Updates an existing note with new content or summary
229+
- `noteId`: The ID of the note to update (string, required)
230+
- `content`: The new content for the note, supports markdown format (string, required)
231+
- `summary`: An optional new short summary or title for the note (string, optional)
220232

221233
### Solutions
222234

README_zh-CN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ npm run build && node build/index.js --site global
132132
| ---------------- | :----: | :----: | :------: | ------------------------------ |
133133
| **search_notes** |||| 使用过滤选项搜索用户笔记 |
134134
| **get_note** |||| 通过题目 ID 获取特定题目的笔记 |
135+
| **create_note** |||| 为特定题目创建新笔记 |
136+
| **update_note** |||| 使用新内容更新现有笔记 |
135137

136138
### 题解
137139

@@ -214,10 +216,22 @@ npm run build && node build/index.js --site global
214216
- `orderBy`:返回笔记的排序顺序(枚举:"ASCENDING"、"DESCENDING",可选,默认:"DESCENDING")
215217

216218
- **get_note** - 获取特定 LeetCode 题目的用户笔记
219+
217220
- `questionId`:LeetCode 题目的题目 ID(字符串,必需)
218221
- `limit`:返回的最大笔记数量(数字,可选,默认:10)
219222
- `skip`:要跳过的笔记数量(数字,可选,默认:0)
220223

224+
- **create_note** - 为特定 LeetCode 题目创建新笔记
225+
226+
- `questionId`:LeetCode 题目的题目 ID(字符串,必需)
227+
- `content`:笔记内容,支持 markdown 格式(字符串,必需)
228+
- `summary`:可选的笔记简短摘要或标题(字符串,可选)
229+
230+
- **update_note** - 使用新内容或摘要更新现有笔记
231+
- `noteId`:要更新的笔记 ID(字符串,必需)
232+
- `content`:笔记的新内容,支持 markdown 格式(字符串,必需)
233+
- `summary`:可选的新简短摘要或标题(字符串,可选)
234+
221235
### 题解
222236

223237
- **list_problem_solutions** - 获取特定题目的社区题解文章列表

src/leetcode/graphql/cn/note-queries.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,56 @@ query noteOneTargetCommonNote(
6060
}
6161
}
6262
}`;
63+
64+
/**
65+
* GraphQL mutation for creating a new note on LeetCode CN
66+
*
67+
* @param content - Content of the note
68+
* @param noteType - Type of note (e.g., "COMMON_QUESTION")
69+
* @param targetId - ID of the target object (e.g., question ID)
70+
* @param summary - Optional summary of the note
71+
*/
72+
export const NOTE_CREATE_MUTATION = `
73+
mutation noteCreateCommonNote(
74+
$content: String!
75+
$noteType: NoteCommonTypeEnum!
76+
$targetId: String!
77+
$summary: String!
78+
) {
79+
noteCreateCommonNote(
80+
content: $content
81+
noteType: $noteType
82+
targetId: $targetId
83+
summary: $summary
84+
) {
85+
note {
86+
id
87+
content
88+
targetId
89+
}
90+
ok
91+
}
92+
}`;
93+
94+
/**
95+
* GraphQL mutation for updating an existing note on LeetCode CN
96+
*
97+
* @param noteId - ID of the note to update
98+
* @param content - New content for the note
99+
* @param summary - Optional new summary for the note
100+
*/
101+
export const NOTE_UPDATE_MUTATION = `
102+
mutation noteUpdateUserNote(
103+
$content: String!
104+
$noteId: ID!
105+
$summary: String!
106+
) {
107+
noteUpdateUserNote(content: $content, noteId: $noteId, summary: $summary) {
108+
note {
109+
id
110+
content
111+
targetId
112+
}
113+
ok
114+
}
115+
}`;

src/leetcode/leetcode-base-service.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,38 @@ export interface LeetCodeBaseService {
207207
limit?: number,
208208
skip?: number
209209
): Promise<any>;
210+
211+
/**
212+
* Creates a new note for a specific question on LeetCode.
213+
* Note: This feature is only available on LeetCode CN.
214+
*
215+
* @param content - The content of the note
216+
* @param noteType - The type of note (e.g., "COMMON_QUESTION")
217+
* @param targetId - The ID of the target (e.g., question ID)
218+
* @param summary - Optional summary of the note
219+
* @returns Promise resolving to the created note data
220+
* @throws Error if not implemented or feature not supported
221+
*/
222+
createUserNote(
223+
content: string,
224+
noteType: string,
225+
targetId: string,
226+
summary: string
227+
): Promise<any>;
228+
229+
/**
230+
* Updates an existing note on LeetCode.
231+
* Note: This feature is only available on LeetCode CN.
232+
*
233+
* @param noteId - The ID of the note to update
234+
* @param content - The new content of the note
235+
* @param summary - Optional new summary of the note
236+
* @returns Promise resolving to the updated note data
237+
* @throws Error if not implemented or feature not supported
238+
*/
239+
updateUserNote(
240+
noteId: string,
241+
content: string,
242+
summary: string
243+
): Promise<any>;
210244
}

src/leetcode/leetcode-cn-service.ts

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Credential, LeetCodeCN } from "leetcode-query";
22
import {
33
NOTE_AGGREGATE_QUERY,
4-
NOTE_BY_QUESTION_ID_QUERY
4+
NOTE_BY_QUESTION_ID_QUERY,
5+
NOTE_CREATE_MUTATION,
6+
NOTE_UPDATE_MUTATION
57
} from "./graphql/cn/note-queries.js";
68
import { SEARCH_PROBLEMS_QUERY } from "./graphql/cn/search-problems.js";
79
import { SOLUTION_ARTICLE_DETAIL_QUERY } from "./graphql/cn/solution-article-detail.js";
@@ -463,6 +465,97 @@ export class LeetCodeCNService implements LeetCodeBaseService {
463465
}
464466
}
465467

468+
/**
469+
* Creates a new note for a specific question on LeetCode CN.
470+
* Available only on LeetCode CN platform.
471+
*
472+
* @param content - The content of the note
473+
* @param noteType - The type of note (e.g., "COMMON_QUESTION")
474+
* @param targetId - The ID of the target (e.g., question ID)
475+
* @param summary - Optional summary of the note
476+
* @returns Promise resolving to the created note data
477+
*/
478+
async createUserNote(
479+
content: string,
480+
noteType: string,
481+
targetId: string,
482+
summary: string
483+
): Promise<any> {
484+
if (!this.isAuthenticated()) {
485+
throw new Error("Authentication required to create notes");
486+
}
487+
488+
try {
489+
const variables = {
490+
content,
491+
noteType,
492+
targetId,
493+
summary: summary || ""
494+
};
495+
496+
return await this.leetCodeApi
497+
.graphql({
498+
query: NOTE_CREATE_MUTATION,
499+
variables
500+
})
501+
.then((response) => {
502+
return (
503+
response.data?.noteCreateCommonNote || {
504+
ok: false,
505+
note: null
506+
}
507+
);
508+
});
509+
} catch (error) {
510+
console.error(`Error creating note:`, error);
511+
throw error;
512+
}
513+
}
514+
515+
/**
516+
* Updates an existing note on LeetCode CN.
517+
* Available only on LeetCode CN platform.
518+
*
519+
* @param noteId - The ID of the note to update
520+
* @param content - The new content of the note
521+
* @param summary - Optional new summary of the note
522+
* @returns Promise resolving to the updated note data
523+
*/
524+
async updateUserNote(
525+
noteId: string,
526+
content: string,
527+
summary: string
528+
): Promise<any> {
529+
if (!this.isAuthenticated()) {
530+
throw new Error("Authentication required to update notes");
531+
}
532+
533+
try {
534+
const variables = {
535+
noteId,
536+
content,
537+
summary: summary || ""
538+
};
539+
540+
return await this.leetCodeApi
541+
.graphql({
542+
query: NOTE_UPDATE_MUTATION,
543+
variables
544+
})
545+
.then((response) => {
546+
return (
547+
response.data?.noteUpdateUserNote || {
548+
ok: false,
549+
note: null
550+
}
551+
);
552+
});
553+
} catch (error) {
554+
console.error(`Error updating note:`, error);
555+
throw error;
556+
}
557+
}
558+
466559
isAuthenticated(): boolean {
467560
return (
468561
!!this.credential &&

src/leetcode/leetcode-global-service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,31 @@ export class LeetCodeGlobalService implements LeetCodeBaseService {
384384
throw new Error("Notes feature is not supported in LeetCode Global");
385385
}
386386

387+
/**
388+
* Note feature is not supported in LeetCode Global.
389+
* This method is implemented to satisfy the interface but will always throw an error.
390+
*/
391+
async createUserNote(
392+
content: string,
393+
noteType: string,
394+
targetId: string,
395+
summary: string
396+
): Promise<any> {
397+
throw new Error("Notes feature is not supported in LeetCode Global");
398+
}
399+
400+
/**
401+
* Note feature is not supported in LeetCode Global.
402+
* This method is implemented to satisfy the interface but will always throw an error.
403+
*/
404+
async updateUserNote(
405+
noteId: string,
406+
content: string,
407+
summary: string
408+
): Promise<any> {
409+
throw new Error("Notes feature is not supported in LeetCode Global");
410+
}
411+
387412
isAuthenticated(): boolean {
388413
return (
389414
!!this.credential &&

0 commit comments

Comments
 (0)