Skip to content

Commit 939897b

Browse files
authored
Merge pull request #48 from JacobLinCool/feat-cn-graphql-noj-go
feat: make endpoint of leatcode-cn configurable
2 parents 336335d + 9305a7e commit 939897b

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/_tests/leetcode-cn.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,24 @@ describe("LeetCode", () => {
6767
});
6868
expect(data.user.username).toBe("LeetCode");
6969
});
70+
71+
it("should be able to use graphql noj-go", async () => {
72+
const { data } = await lc.graphql(
73+
{
74+
operationName: "data",
75+
variables: { username: "LeetCode" },
76+
query: `
77+
query data($username: String!, $year: Int) {
78+
calendar: userCalendar(userSlug: $username, year: $year) {
79+
streak
80+
totalActiveDays
81+
submissionCalendar
82+
}
83+
}`,
84+
},
85+
"/graphql/noj-go/",
86+
);
87+
expect(typeof data.calendar.streak).toBe("number");
88+
});
7089
});
7190
});

src/leetcode-cn.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,19 @@ export class LeetCodeCN extends EventEmitter {
9393
/**
9494
* Use GraphQL to query LeetCodeCN API.
9595
* @param query
96+
* @param endpoint Maybe you want to use `/graphql/noj-go/` instead of `/graphql/`.
9697
* @returns
9798
*/
98-
public async graphql(query: LeetCodeGraphQLQuery): Promise<LeetCodeGraphQLResponse> {
99+
public async graphql(
100+
query: LeetCodeGraphQLQuery,
101+
endpoint = "/graphql/",
102+
): Promise<LeetCodeGraphQLResponse> {
99103
await this.initialized;
100104

101105
try {
102106
await this.limiter.lock();
103107
const BASE = BASE_URL_CN;
104-
const res = await fetch(`${BASE}/graphql`, {
108+
const res = await fetch(`${BASE}${endpoint}`, {
105109
method: "POST",
106110
headers: {
107111
"content-type": "application/json",

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface ICredential {
4444
// LeetCode GraphQL
4545
export interface LeetCodeGraphQLQuery {
4646
operationName: string;
47-
variables: { [key: string]: unknown };
47+
variables?: { [key: string]: unknown };
4848
query: string;
4949
}
5050

0 commit comments

Comments
 (0)