Skip to content

Commit 0036d41

Browse files
committed
pref:change function getQuestionList Hot 100
1 parent e738add commit 0036d41

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

common/utils/http/graphql.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import path from 'node:path'
22
import { fetch_ } from '#common/utils/http/fetch_.js'
3+
import { url_join } from '#common/utils/http/urlJoin.js'
34

45
/**
5-
* 请求
6+
* 请求 graphql
67
* @param options
78
* @param host
89
* @returns {Promise<any>}
910
*/
1011
export async function graphql(options, host = 'https://leetcode.cn/') {
11-
return await fetch_(path.join(host, 'graphql'), options)
12+
return await fetch_(url_join(host, 'graphql'), options)
1213
}

common/utils/http/submit.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import path from 'node:path'
22
import { fetch_ } from '#common/utils/http/fetch_.js'
3+
import { url_join } from '#common/utils/http/urlJoin.js'
34

45
/**
5-
* 请求
6+
* 请求 submit 提交
67
* @param options
78
* @param host
89
* @returns {Promise<any>}
910
*/
1011
export async function submit(options, host = 'https://leetcode.cn/') {
11-
return await fetch_(path.join(host, 'submit'), options)
12+
return await fetch_(url_join(host, 'submit'), options)
1213
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { graphql } from '#common/utils/http/graphql.js'
2+
import { getQuestionListHot100Json } from '#resources/headers/questionListHot100Json.js'
3+
4+
export const getQuestionListHot100 = async () => {
5+
const res = await graphql(getQuestionListHot100Json())
6+
const {
7+
data: { studyPlanV2Detail }
8+
} = res
9+
return studyPlanV2Detail
10+
}

common/utils/question-handler/getHot100QuestionList.js

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
import path from 'node:path'
22
import { createQuestionByTitleSlug } from '../create-check/createUtil.js'
3-
import { graphql } from '#common/utils/http/graphql.js'
4-
5-
const bodyString =
6-
'{"query":"\\n query studyPlanPastSolved($slug: String!) {\\n studyPlanV2Detail(planSlug: $slug) {\\n planSubGroups {\\n slug\\n questions {\\n titleSlug\\n status\\n }\\n }\\n }\\n}\\n ","variables":{"slug":"top-100-liked"},"operationName":"studyPlanPastSolved"}'
7-
const headers = {
8-
'content-type': 'application/json'
9-
}
10-
11-
const initJson = {
12-
headers,
13-
body: bodyString,
14-
method: 'POST'
15-
}
16-
// 抓hot100列表
17-
export async function getHot100QuestionList() {
18-
const res = await graphql(initJson)
19-
const {
20-
data: { studyPlanV2Detail }
21-
} = res
22-
return studyPlanV2Detail
23-
}
24-
3+
import { getQuestionListHot100 } from '#common/utils/question-getter/getQuestionListHot100.js'
254
// 获取题目列表
265
export async function getTitleSlugList() {
27-
const res = await getHot100QuestionList()
6+
const res = await getQuestionListHot100()
287
const { planSubGroups } = res
298
return planSubGroups.reduce((acc, cur) => {
309
acc.push(...cur.questions.map((res) => res.titleSlug))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* 获取hot 100 的请求header
3+
* @returns {{headers: {"content-type": string}, method: string, body: string}}
4+
*/
5+
export function getQuestionListHot100Json() {
6+
return {
7+
headers: { 'content-type': 'application/json' },
8+
body: '{"query":"\\n query studyPlanPastSolved($slug: String!) {\\n studyPlanV2Detail(planSlug: $slug) {\\n planSubGroups {\\n slug\\n questions {\\n titleSlug\\n status\\n }\\n }\\n }\\n}\\n ","variables":{"slug":"top-100-liked"},"operationName":"studyPlanPastSolved"}',
9+
method: 'POST'
10+
}
11+
}

test/getHot100List.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { describe, expect, it } from 'vitest'
2-
import { getHot100QuestionList } from '#common/utils/question-handler/getHot100QuestionList'
2+
import { getQuestionListHot100 } from '#common/utils/question-getter/getQuestionListHot100.js'
33

44
describe('hot 100获取数据测试', () => {
55
it('是否正确获取了hot 100合集', async () => {
6-
const res = await getHot100QuestionList()
6+
const res = await getQuestionListHot100()
77

88
expect(Object.keys(res)).toStrictEqual(['planSubGroups'])
99
})

0 commit comments

Comments
 (0)