-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcodeApi.js
More file actions
26 lines (24 loc) · 800 Bytes
/
Copy pathleetcodeApi.js
File metadata and controls
26 lines (24 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Fetches title, difficulty, and tags for a LeetCode problem using the GraphQL API
async function fetchLeetCodeProblemData(slug) {
const query = {
query: `query getQuestionDetail($titleSlug: String!) { question(titleSlug: $titleSlug) { title difficulty topicTags { name slug } } }`,
variables: { titleSlug: slug },
};
const response = await fetch("https://leetcode.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(query),
credentials: "same-origin",
});
if (!response.ok) return null;
const data = await response.json();
const q = data.data?.question;
if (!q) return null;
return {
title: q.title,
difficulty: q.difficulty,
tags: q.topicTags?.map((tag) => tag.name) || [],
};
}