Skip to content

Commit 553ad58

Browse files
committed
perf: http dir structure and functions modify
1 parent 91328cb commit 553ad58

12 files changed

+82
-7
lines changed

common/utils/http/fetch_.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* 基础请求
3+
* @param url
4+
* @param options
5+
* @returns {Promise<any>}
6+
* @private
7+
*/
8+
export const fetch_ = async (url,options)=>{
9+
return await fetch(url,options).then(res=>res.json())
10+
}

common/utils/http/graphql.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import path from "path";
2+
import {fetch_} from "#common/utils/http/fetch_.js";
3+
4+
/**
5+
* 请求
6+
* @param options
7+
* @param host
8+
* @returns {Promise<any>}
9+
*/
10+
export const graphql = async (options, host = "https://leetcode.cn/")=>{
11+
return await fetch_(path.join(host,'graphql'),options);
12+
}

common/utils/http/submit.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {fetch_} from "#common/utils/http/fetch_.js";
2+
import path from "path";
3+
4+
/**
5+
* 请求
6+
* @param options
7+
* @param host
8+
* @returns {Promise<any>}
9+
*/
10+
export const submit = async (options, host = "https://leetcode.cn/")=>{
11+
return await fetch_(path.join(host,'submit'),options);
12+
}

common/utils/question-getter/getQuestionById.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {getQuestionSearchJson} from "#resources/headers/questionSearchJson.js";
22
import {getQuestionDetail} from "../question-handler/getQuestionDetail.js";
3+
import {graphql} from "#common/utils/http/graphql.js";
34
export async function getQuestionById(id) {
4-
const base = await fetch('https://leetcode.cn/graphql/', getQuestionSearchJson(id.toString())).then((res) => res.json());
5+
const base = await graphql(getQuestionSearchJson(id.toString())).then((res) => res.json());
56
const questionContent = base.data.problemsetQuestionList.questions.find((o) => o.frontendQuestionId === id.toString());
67
if(!questionContent) {
78
return {
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {getQuestionListJson} from "#resources/headers/questionListJson.js";
2+
import {graphql} from "#common/utils/http/graphql.js";
23

34
export async function getQuestionList() {
4-
const base = await fetch('https://leetcode.cn/graphql/', getQuestionListJson()).then((res) => res.json());
5+
const base = await graphql(getQuestionListJson()).then((res) => res.json());
6+
// todo 列表
7+
const question = base.questions;
58
return question;
69
}

common/utils/question-getter/getQuestionRandom.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {getQuestionSearchJson} from "#resources/headers/questionSearchJson.js";
22
import {getQuestionDetail} from "../question-handler/getQuestionDetail.js";
33
import {getRandomId} from "#common/utils/question-handler/getRandomId.js";
4+
import {graphql} from "#common/utils/http/graphql.js";
45

56
export async function getQuestionRandom() {
67
const id = await getRandomId()
7-
const base = await fetch('https://leetcode.cn/graphql/', getQuestionSearchJson(id.toString())).then((res) => res.json());
8+
const base = await graphql(getQuestionSearchJson(id.toString())).then((res) => res.json());
89
const slug = base.data.problemsetQuestionList.questions.find((o) => o.frontendQuestionId === id.toString()).titleSlug;
910
const question = await getQuestionDetail(slug);
1011
return question;

common/utils/question-getter/getQuestionToday.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {getQuestionTodayJson} from "#resources/headers/questionTodayJson.js";
22
import {getQuestionDetail} from "../question-handler/getQuestionDetail.js";
3+
import {graphql} from "#common/utils/http/graphql.js";
34

45
export async function getQuestionToday() {
5-
const question = await fetch('https://leetcode.cn/graphql/', getQuestionTodayJson()).then(((res) => res.json()));
6+
const question = await graphql(getQuestionTodayJson()).then(((res) => res.json()));
67
const today = question.data.todayRecord[0].question;
78
const { date } = question.data.todayRecord[0];
89
const questionInfo = await getQuestionDetail(today.titleSlug, { date });

common/utils/question-getter/getQuestionTypes.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {getQuestionTypesJson} from "#resources/headers/questionTypeJson.js";
2+
import {graphql} from "#common/utils/http/graphql.js";
23

34
/**
45
* 获取问题的类型
@@ -22,7 +23,7 @@ import {getQuestionTypesJson} from "#resources/headers/questionTypeJson.js";
2223
* @returns {Promise<*>}
2324
*/
2425
export async function getQuestionTypes() {
25-
const res = await fetch('https://leetcode.cn/graphql/', getQuestionTypesJson()).then((res) => res.json());
26+
const res = await graphql(getQuestionTypesJson()).then((res) => res.json());
2627
const tags = res.data?.questionTagTypeWithTags;
2728
// console.log(JSON.stringify(tags))
2829
return tags;

common/view/check.view.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import inquirer from "inquirer";
22
import path from "path";
33
import {getQuestionFileName} from "#common/utils/question-handler/getQuestionFileName.js";
44
import {getQuestionById} from "#common/utils/question-getter/getQuestionById.js";
5-
import {getQuestionByMode} from "#common/utils/store/store-realm.js";
5+
import {getQuestionByMode} from "#common/utils/store/controller/question.js";
66
import {checkQuestion} from "#common/utils/question-handler/checkQuestion.js";
77
import {getCountBySameName} from "#common/utils/file/getCountBySameName.js";
88
import {getFileListBySameName} from "#common/utils/file/getFileListBySameName.js";

common/view/create.view.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {getQuestionToday} from "#common/utils/question-getter/getQuestionToday.j
66
import {getQuestionRandom} from "#common/utils/question-getter/getQuestionRandom.js";
77
import {createQuestion} from "#common/utils/question-handler/createQuestion.js";
88
import {createQuestionCopy} from "#common/utils/question-handler/createQuestionCopy.js";
9-
import {setQuestion} from "#common/utils/store/store-realm.js";
9+
import {setQuestion} from "#common/utils/store/controller/question.js";
1010
export const easyCreateView = async ()=>{
1111
const modeQuestion = [{
1212
type: 'list',

resources/headers/codeSubmitJson.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 提交请求的JSON数据
3+
* 地址:https://leetcode.cn/problems/reverse-integer/submit/
4+
* @param lang
5+
* @param id
6+
* @param code
7+
* @returns {{headers: {"content-type": string}, method: string, body: string}}
8+
*/
9+
export function codeSubmitJson(lang,id,code){
10+
return {
11+
headers: { "content-type": "application/json" },
12+
body: `{
13+
"lang": "${lang}",
14+
"question_id": "${id}",
15+
"typed_code": "${code}"
16+
}`,
17+
method: 'POST',
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* 获取当前语言列表
3+
* @returns {{headers: {"content-type": string}, method: string, body: string}}
4+
*/
5+
export function getQuestionLanguageListJson() {
6+
return {
7+
headers: {"content-type": "application/json"},
8+
body: `{
9+
"query": "\\n query languageList {\\n languageList {\\n id\\n name\\n }\\n}\\n ",
10+
"variables": {},
11+
"operationName": "languageList"
12+
}`,
13+
method: 'POST',
14+
}
15+
}

0 commit comments

Comments
 (0)