Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gql response interpretation #49

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 13 additions & 35 deletions lib/lc/client.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,24 @@
import type { Question } from "lc-dailies/lib/api/mod.ts";
import type {
LCClientInterface,
LCQuestion,
LCSubmission,
} from "./client_interface.ts";
import { makeQuestionURL } from "./urls.ts";
import { gql } from "./gql.ts";

/**
* LCQuestion is an alias interface for a Leetcode question.
*/
export type LCQuestion = Question;

/**
* LCSubmission is the representation of Leetcode's recent submission per user.
*/
export interface LCSubmission {
/**
* id is the id details of the submission.
*/
id: string;

/**
* name is the name of the question of the submission.
*/
name: string;

/**
* title is the title of the question of the submission.
*/
title: string;

/**
* timestamp is the time the submission was submitted.
*/
timestamp: string;
}

/**
* LCClient is the client for Leetcode.
*/
export class LCClient {
export class LCClient implements LCClientInterface {
constructor(
private readonly fetch: typeof window.fetch = window.fetch.bind(window),
) {}

/**
* verifyUser verifies the user by username.
*/
public async verifyUser(username: string): Promise<boolean> {
const response = await fetch(`https://leetcode.com/${username}/`);
const response = await this.fetch(`https://leetcode.com/${username}/`);
return response.status === 200;
}

Expand Down Expand Up @@ -142,7 +120,7 @@ export class LCClient {
*/
.then((json) =>
json.data.recentAcSubmissionList
.map((
?.map((
acSubmission: {
id: string;
title: string;
Expand All @@ -154,7 +132,7 @@ export class LCClient {
name: acSubmission.titleSlug,
title: acSubmission.title,
timestamp: acSubmission.timestamp,
}))
})) ?? []
);
}
}
Expand Down
64 changes: 64 additions & 0 deletions lib/lc/client_interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { Question } from "lc-dailies/lib/api/mod.ts";

/**
* LCQuestion is an alias interface for a Leetcode question.
*/
export type LCQuestion = Question;

/**
* LCSubmission is the representation of Leetcode's recent submission per user.
*/
export interface LCSubmission {
/**
* id is the id details of the submission.
*/
id: string;

/**
* name is the name of the question of the submission.
*/
name: string;

/**
* title is the title of the question of the submission.
*/
title: string;

/**
* timestamp is the time the submission was submitted.
*/
timestamp: string;
}

/**
* LCClientInterface is the client interface for Leetcode.
*/
export interface LCClientInterface {
/**
* verifyUser verifies the user by username.
*/
verifyUser(username: string): Promise<boolean>;

/**
* getDailyQuestion gets the daily question from Leetcode.
*/
getDailyQuestion(): Promise<LCQuestion>;

/**
* listDailyQuestions gets the last `amount` of daily questions from Leetcode since `asOfYear` and `asOfMonth`.
*/
listDailyQuestions(
limit: number,
asOfYear: number,
asOfMonth: number,
): Promise<LCQuestion[]>;

/**
* getRecentAcceptedSubmissions gets the recent accepted submissions from
* Leetcode by username.
*/
getRecentAcceptedSubmissions(
username: string,
limit?: number,
): Promise<LCSubmission[]>;
}
8 changes: 6 additions & 2 deletions lib/lc/fake_client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { LCClient, LCQuestion, LCSubmission } from "./client.ts";
import type {
LCClientInterface,
LCQuestion,
LCSubmission,
} from "./client_interface.ts";

export const FAKE_LC_USERNAME = "fake_lc_username";
export const FAKE_LC_QUESTION_NAME = "fake_lc_question_name";
Expand Down Expand Up @@ -29,7 +33,7 @@ export const FAKE_RECENT_SUBMISSIONS: LCSubmission[] = [
/**
* FakeLCClient is a fake implementation of LCClient.
*/
export class FakeLCClient implements LCClient {
export class FakeLCClient implements LCClientInterface {
public verifyUser(username: string): Promise<boolean> {
return Promise.resolve(username === FAKE_LC_USERNAME);
}
Expand Down
1 change: 1 addition & 0 deletions lib/lc/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./client_interface.ts";
export * from "./client.ts";
export * from "./urls.ts";
4 changes: 2 additions & 2 deletions lib/leaderboard/denokv/denokv_leaderboard_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DAY, ulid, WEEK } from "lc-dailies/deps.ts";
import type * as api from "lc-dailies/lib/api/mod.ts";
import type { LeaderboardClient } from "lc-dailies/lib/leaderboard/mod.ts";
import { sync } from "lc-dailies/lib/leaderboard/mod.ts";
import { LCClient } from "lc-dailies/lib/lc/mod.ts";
import type { LCClientInterface } from "lc-dailies/lib/lc/mod.ts";

/**
* DenoKvLeaderboardClient is the client for the leaderboard.
Expand All @@ -16,7 +16,7 @@ export class DenoKvLeaderboardClient implements LeaderboardClient {
/**
* lc is the Leetcode client.
*/
private readonly lc: LCClient,
private readonly lc: LCClientInterface,
/**
* restartMS is the milliseconds to restart the leaderboard.
*
Expand Down
4 changes: 2 additions & 2 deletions lib/leaderboard/sync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SECOND, WEEK } from "lc-dailies/deps.ts";
import type * as api from "lc-dailies/lib/api/mod.ts";
import type { LCClient } from "lc-dailies/lib/lc/mod.ts";
import type { LCClientInterface } from "lc-dailies/lib/lc/mod.ts";
import {
calculateScores,
makeDefaultCalculateScoresOptions,
Expand All @@ -23,7 +23,7 @@ export interface SyncOptions {
/**
* lcClient is the Leetcode client.
*/
lcClient: LCClient;
lcClient: LCClientInterface;

/**
* questionsFetchAmount is the amount of questions to fetch from Leetcode.
Expand Down