-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathleetCodeService.ts
More file actions
336 lines (297 loc) · 11.3 KB
/
leetCodeService.ts
File metadata and controls
336 lines (297 loc) · 11.3 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import {
formatAcSubmissionData,
formatBadgesData,
formatContestData,
formatContestHistoryData,
formatDailyData,
formatLanguageStats,
formatProblemsData,
formatProgressStats,
formatQuestionData,
formatSolvedProblemsData,
formatSubmissionCalendarData,
formatSubmissionData,
formatTrendingCategoryTopicData,
formatSkillStats,
formatUserData,
formatUserProfileData,
} from '../src/FormatUtils';
import {
AcSubmissionQuery,
contestQuery,
dailyProblemQuery,
discussCommentsQuery,
discussTopicQuery,
getUserProfileQuery,
languageStatsQuery,
officialSolutionQuery,
problemListQuery,
selectProblemQuery,
submissionQuery,
trendingDiscussQuery,
userProfileCalendarQuery,
userProfileQuery,
userQuestionProgressQuery,
userContestRankingInfoQuery,
skillStatsQuery,
submissionDetailsQuery,
streakCounterQuery,
userStatusQuery,
questionNoteQuery,
updateNoteMutation,
addToFavoriteMutation,
removeFromFavoriteMutation,
favoritesListsQuery,
problemStatusQuery,
} from '../src/GQLQueries';
import type {
DailyProblemData,
ProblemSetQuestionListData,
SelectProblemData,
TrendingDiscussionObject,
UserData,
} from '../src/types';
import { executeGraphQL, requireAuth } from './serverUtils';
import {
SubmissionArgs,
CalendarArgs,
ProblemArgs,
DiscussCommentsArgs,
Variables,
SubmissionDetailArgs,
QuestionNoteArgs,
ToggleFavoriteArgs,
} from './types';
// Builds GraphQL variables by filtering out undefined, null, and NaN values.
function buildVariables(input: Record<string, unknown>): Variables {
const result: Variables = {};
for (const [key, value] of Object.entries(input)) {
if (value !== undefined && value !== null && !(typeof value === 'number' && Number.isNaN(value))) {
result[key] = value;
}
}
return result;
}
// Retrieves the formatted user profile summary.
export async function getUserProfileSummary(username: string) {
const data = await executeGraphQL(userProfileQuery, { username });
return formatUserData(data as UserData);
}
// Retrieves the formatted user badges data.
export async function getUserBadges(username: string) {
const data = await executeGraphQL(userProfileQuery, { username });
return formatBadgesData(data as UserData);
}
// Retrieves the formatted user contest data.
export async function getUserContest(username: string) {
const data = await executeGraphQL(contestQuery, { username });
return formatContestData(data as UserData);
}
// Retrieves the formatted user contest history.
export async function getUserContestHistory(username: string) {
const data = await executeGraphQL(contestQuery, { username });
return formatContestHistoryData(data as UserData);
}
// Retrieves the formatted solved problems statistics.
export async function getSolvedProblems(username: string) {
const data = await executeGraphQL(userProfileQuery, { username });
return formatSolvedProblemsData(data as UserData);
}
// Retrieves recent submissions for a user.
export async function getRecentSubmission(args: SubmissionArgs) {
const variables = buildVariables({ username: args.username, limit: args.limit });
const data = await executeGraphQL(submissionQuery, variables);
return formatSubmissionData(data as UserData);
}
// Retrieves recent accepted submissions for a user.
export async function getRecentAcSubmission(args: SubmissionArgs) {
const variables = buildVariables({ username: args.username, limit: args.limit });
const data = await executeGraphQL(AcSubmissionQuery, variables);
return formatAcSubmissionData(data as UserData);
}
// Retrieves the submission calendar for a user in a given year.
export async function getSubmissionCalendar(args: CalendarArgs) {
const variables = buildVariables({ username: args.username, year: args.year });
const data = await executeGraphQL(userProfileCalendarQuery, variables);
return formatSubmissionCalendarData(data as UserData);
}
// Retrieves the aggregated user profile data.
export async function getUserProfileAggregate(username: string) {
const data = await executeGraphQL(getUserProfileQuery, { username });
return formatUserProfileData(data);
}
// Retrieves the language statistics for a user.
export async function getLanguageStats(username: string) {
const data = await executeGraphQL(languageStatsQuery, { username });
return formatLanguageStats(data as UserData);
}
// Retrieves the skill statistics for a user.
export async function getSkillStats(username: string) {
const data = await executeGraphQL(skillStatsQuery, { username });
return formatSkillStats(data as UserData);
}
// Retrieves the daily problem.
export async function getDailyProblem() {
const data = await executeGraphQL(dailyProblemQuery, {});
return formatDailyData(data as DailyProblemData);
}
// Retrieves the raw daily problem data.
export async function getDailyProblemRaw() {
return executeGraphQL(dailyProblemQuery, {});
}
// Retrieves a selected problem by title slug.
export async function getSelectProblem(titleSlug: string) {
const data = await executeGraphQL(selectProblemQuery, { titleSlug });
return formatQuestionData(data as SelectProblemData);
}
// Retrieves the raw data for a selected problem by title slug.
export async function getSelectProblemRaw(titleSlug: string) {
return executeGraphQL(selectProblemQuery, { titleSlug });
}
// Retrieves a list of problems based on the given arguments.
export async function getProblemSet(args: ProblemArgs) {
const limit = args.skip !== undefined && args.limit === undefined ? 1 : args.limit ?? 20;
const skip = args.skip ?? 0;
const tags = args.tags ? args.tags.split(' ') : [];
const difficulty = args.difficulty ?? undefined;
const variables = buildVariables({
categorySlug: '',
limit,
skip,
filters: {
tags,
difficulty,
},
});
const data = await executeGraphQL(problemListQuery, variables);
return formatProblemsData(data as ProblemSetQuestionListData);
}
// Retrieves the official solution for a problem.
export async function getOfficialSolution(titleSlug: string) {
return executeGraphQL(officialSolutionQuery, { titleSlug });
}
// Retrieves trending discussion topics.
export async function getTrendingTopics(first: number) {
const data = await executeGraphQL(trendingDiscussQuery, { first });
return formatTrendingCategoryTopicData(data as TrendingDiscussionObject);
}
// Retrieves a discussion topic by ID.
export async function getDiscussTopic(topicId: number) {
return executeGraphQL(discussTopicQuery, { topicId });
}
// Retrieves comments for a discussion topic.
export async function getDiscussComments(args: DiscussCommentsArgs) {
const variables = buildVariables({
topicId: args.topicId,
orderBy: args.orderBy ?? 'newest_to_oldest',
pageNo: args.pageNo ?? 1,
numPerPage: args.numPerPage ?? 10,
});
return executeGraphQL(discussCommentsQuery, variables);
}
// Retrieves raw language statistics for a user.
export async function getLanguageStatsRaw(username: string) {
return executeGraphQL(languageStatsQuery, { username });
}
// Retrieves raw submission calendar data for a user.
export async function getUserProfileCalendarRaw(args: CalendarArgs) {
const variables = buildVariables({ username: args.username, year: args.year });
return executeGraphQL(userProfileCalendarQuery, variables);
}
// Retrieves raw user profile data.
export async function getUserProfileRaw(username: string) {
return executeGraphQL(getUserProfileQuery, { username });
}
// Retrieves the legacy daily problem data.
export async function getDailyProblemLegacy() {
return executeGraphQL(dailyProblemQuery, {});
}
// Retrieves raw skill statistics for a user.
export async function getSkillStatsRaw(username: string) {
return executeGraphQL(skillStatsQuery, { username });
}
// Retrieves the question progress for a user.
export async function getUserProgress(username: string) {
const data = await executeGraphQL(userQuestionProgressQuery, { username });
return formatProgressStats(data as UserData);
}
// Retrieves the contest ranking information for a user.
export async function getUserContestRankingInfo(username: string) {
return executeGraphQL(userContestRankingInfoQuery, { username });
}
// Retrieves raw question progress data for a user.
export async function getUserProgressRaw(username: string) {
return executeGraphQL(userQuestionProgressQuery, { username });
}
// ── Auth-required tools ─────────────────────────────────────────────
// Retrieves authenticated user status (username, premium, checkedIn, notifications).
export async function getUserStatus() {
requireAuth();
const data = (await executeGraphQL(userStatusQuery, {})) as { userStatus: Record<string, unknown> };
return data.userStatus;
}
// Retrieves the daily streak counter for the authenticated user.
export async function getUserStreak() {
requireAuth();
const data = (await executeGraphQL(streakCounterQuery, {})) as { streakCounter: Record<string, unknown> };
return data.streakCounter;
}
// Retrieves the authenticated user's favorite/bookmark lists with problem IDs.
export async function getUserFavorites() {
requireAuth();
const data = (await executeGraphQL(favoritesListsQuery, {})) as {
favoritesLists: { allFavorites: unknown[] };
};
return data.favoritesLists.allFavorites;
}
// Retrieves full submission details including source code.
export async function getSubmissionDetails(args: SubmissionDetailArgs) {
requireAuth();
const data = (await executeGraphQL(submissionDetailsQuery, {
submissionId: args.submissionId,
})) as { submissionDetails: Record<string, unknown> };
return data.submissionDetails;
}
// Retrieves the user's personal note on a problem.
export async function getProblemNote(titleSlug: string) {
requireAuth();
const data = (await executeGraphQL(questionNoteQuery, { titleSlug })) as {
question: { questionId: string; questionFrontendId: string; title: string; titleSlug: string; note: string | null };
};
return data.question;
}
// Creates or updates a personal note on a problem.
export async function updateProblemNote(args: QuestionNoteArgs) {
requireAuth();
const data = (await executeGraphQL(updateNoteMutation, {
titleSlug: args.titleSlug,
content: args.note ?? '',
})) as { updateNote: { question: Record<string, unknown> } };
return data.updateNote.question;
}
// Adds a problem to a favorites list.
export async function addProblemToFavorite(args: ToggleFavoriteArgs) {
requireAuth();
const data = (await executeGraphQL(addToFavoriteMutation, {
favoriteIdHash: args.favoriteIdHash,
questionId: args.questionId,
})) as { addQuestionToFavorite: { ok: boolean } };
return { ok: data.addQuestionToFavorite.ok };
}
// Removes a problem from a favorites list.
export async function removeProblemFromFavorite(args: ToggleFavoriteArgs) {
requireAuth();
const data = (await executeGraphQL(removeFromFavoriteMutation, {
favoriteIdHash: args.favoriteIdHash,
questionId: args.questionId,
})) as { removeQuestionFromFavorite: { ok: boolean } };
return { ok: data.removeQuestionFromFavorite.ok };
}
// Retrieves solve status for a specific problem (lighter than full select).
export async function getProblemStatus(titleSlug: string) {
requireAuth();
const data = (await executeGraphQL(problemStatusQuery, { titleSlug })) as {
question: Record<string, unknown>;
};
return data.question;
}