Skip to content

Commit a8d2f36

Browse files
feat: add getTitleSlugQuestionNumberMapping method (#15)
1 parent 51e4e68 commit a8d2f36

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@leetnotion/leetcode-api",
3-
"version": "1.8.3",
3+
"version": "1.8.4",
44
"description": "Get user profiles, submissions, and problems on LeetCode.",
55
"type": "module",
66
"types": "lib/index.d.ts",

src/_tests/leetcode-advanced.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,22 @@ describe('LeetCode Advanced', { timeout: 60_000 * 60 }, () => {
7373
expect(problems.length).toBeGreaterThan(3000);
7474
});
7575

76-
// it('should be able to get detailed problems', async () => {
77-
// const problems = await lc.detailedProblems({
78-
// category: '',
79-
// offset: 0,
80-
// limit: 10,
81-
// });
82-
// expect(problems.length).equals(10);
83-
// expect(problems[0].questionFrontendId).toBeTruthy();
84-
// expect(problems[0].title).toBeTruthy();
85-
// expect(problems[0].difficulty).toBeTruthy();
86-
// });
76+
it('should be able to get title slug question number mapping', async () => {
77+
const mapping = await lc.getTitleSlugQuestionNumberMapping();
78+
expect(Object.keys(mapping).length).toBeGreaterThan(3000);
79+
});
80+
81+
it('should be able to get detailed problems', async () => {
82+
const problems = await lc.detailedProblems({
83+
category: '',
84+
offset: 0,
85+
limit: 10,
86+
});
87+
expect(problems.length).equals(10);
88+
expect(problems[0].questionFrontendId).toBeTruthy();
89+
expect(problems[0].title).toBeTruthy();
90+
expect(problems[0].difficulty).toBeTruthy();
91+
});
8792
});
8893

8994
describe('Authenticated', () => {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
query problemsetQuestionList(
2+
$categorySlug: String
3+
$limit: Int
4+
$skip: Int
5+
$filters: QuestionListFilterInput
6+
) {
7+
problemsetQuestionList: questionList(
8+
categorySlug: $categorySlug
9+
limit: $limit
10+
skip: $skip
11+
filters: $filters
12+
) {
13+
total: totalNum
14+
questions: data {
15+
questionFrontendId
16+
titleSlug
17+
}
18+
}
19+
}

src/leetcode-advanced.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import IS_EASTER_EGG_COLLECTED from './graphql/is-easter-egg-collected.graphql?r
99
import MINIMAL_COMPANY_TAGS from './graphql/minimal-company-tags.graphql?raw';
1010
import NO_OF_QUESTIONS from './graphql/no-of-problems.graphql?raw';
1111
import QUESTION_FRONTEND_IDS from './graphql/question-frontend-ids.graphql?raw';
12+
import TITLE_SLUG_QUESTION_NUMBER_MAPPING_QUERY from './graphql/title-slug-question-number-mapping.graphql?raw';
1213
import TOPIC_TAGS from './graphql/topic-tags.graphql?raw';
1314
import { LeetCode } from './leetcode';
1415
import {
@@ -365,6 +366,29 @@ export class LeetCodeAdvanced extends LeetCode {
365366
return problems;
366367
}
367368

369+
/**
370+
* Get title slug question number mapping for all leetcode questions
371+
* @returns Mapping
372+
*/
373+
public async getTitleSlugQuestionNumberMapping(): Promise<Record<string, string>> {
374+
await this.initialized;
375+
const { data } = await this.graphql({
376+
query: TITLE_SLUG_QUESTION_NUMBER_MAPPING_QUERY,
377+
variables: {
378+
categorySlug: '',
379+
filters: {},
380+
skip: 0,
381+
limit: 100000,
382+
},
383+
});
384+
const problems = data.problemsetQuestionList.questions as LeetcodeProblem[];
385+
const mapping: Record<string, string> = {};
386+
problems.forEach((problem) => {
387+
mapping[problem.titleSlug] = problem.questionFrontendId;
388+
});
389+
return mapping;
390+
}
391+
368392
private convertRecentSubmissionToSubmissionType(recentSubmission: RecentSubmission): Submission {
369393
return {
370394
id: parseInt(recentSubmission.id, 10),

0 commit comments

Comments
 (0)