Skip to content

Commit d7d168b

Browse files
committed
feat: using leetcode graphql api to get problem data
1 parent 10214db commit d7d168b

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

leetcodeApi.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
async function fetchLeetCodeTags(slug) {
22
const query = {
3-
query: `\n query getQuestionDetail($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n topicTags { name slug }\n }\n }\n `,
3+
query: `query getQuestionDetail($titleSlug: String!) { question(titleSlug: $titleSlug) { topicTags { name slug } } }`,
44
variables: { titleSlug: slug },
55
};
66

@@ -17,3 +17,29 @@ async function fetchLeetCodeTags(slug) {
1717
const data = await response.json();
1818
return data.data?.question?.topicTags?.map((tag) => tag.name) || [];
1919
}
20+
21+
async function fetchLeetCodeProblemData(slug) {
22+
const query = {
23+
query: `query getQuestionDetail($titleSlug: String!) { question(titleSlug: $titleSlug) { title difficulty topicTags { name slug } } }`,
24+
variables: { titleSlug: slug },
25+
};
26+
27+
const response = await fetch("https://leetcode.com/graphql", {
28+
method: "POST",
29+
headers: {
30+
"Content-Type": "application/json",
31+
},
32+
body: JSON.stringify(query),
33+
credentials: "same-origin",
34+
});
35+
36+
if (!response.ok) return null;
37+
const data = await response.json();
38+
const q = data.data?.question;
39+
if (!q) return null;
40+
return {
41+
title: q.title,
42+
difficulty: q.difficulty,
43+
tags: q.topicTags?.map((tag) => tag.name) || [],
44+
};
45+
}

0 commit comments

Comments
 (0)