-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfetcher.ts
82 lines (76 loc) · 1.6 KB
/
fetcher.ts
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
import * as graphql from "graphql-request";
// queries
export class Queries {
// 所有题目列表 Query 语句
public static questionsQuery: string = `query allQuestions {
allQuestions {
...questionSummaryFields
}
}
fragment questionSummaryFields on QuestionNode {
title
titleSlug
translatedTitle
questionId
questionFrontendId
topicTags {
name
translatedName
}
difficulty
isPaidOnly
}`;
// 根据名称获取题目详情 Query 语句
public static questionDataQuery: string =
`query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
questionFrontendId
title
titleSlug
content
translatedTitle
translatedContent
isPaidOnly
difficulty
likes
dislikes
isLiked
codeSnippets {
lang
langSlug
code
}
}
}`;
}
/**
* fetcher
* @author yalda
* @date 2019.08
*/
export class Fetcher<T> {
private client: graphql.GraphQLClient;
constructor() {
this.client = new graphql.GraphQLClient("https://leetcode-cn.com/graphql", {
headers: {
accept: "*/*",
"content-type": "application/json",
"user-agent":
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
"x-newrelic-id": "UAQDVFVRGwEAXVlbBAg=",
},
});
}
async fetch(query: string): Promise<T> {
let data: T;
try {
data = await this.client.request(query);
} catch (e) {
// throw new Error("throw new Error()");
return Promise.reject("Fetcher#fetch");
} finally {
return Promise.resolve(data);
}
}
}