Skip to content

Commit

Permalink
fix(route): 使用 ofetch() 替换一些 got().json() (#15780)
Browse files Browse the repository at this point in the history
* chore: some massive adoption from got().json() to ofetch()

Not sure if everything works fine (some may still have problems other than fakeGot & json issues), but this might have some help.

BTW maybe we can start "The Migration" project to update some outdated routes to new format 🤔

* fix: some other issues

* fix: query

* fix: luogu user blog & modb topic

* fix: modb topic response

* fix: query options in freebuf

---------
  • Loading branch information
Candinya authored May 31, 2024
1 parent 9b3f976 commit cce3bdb
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 102 deletions.
4 changes: 2 additions & 2 deletions lib/routes/alistapart/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

const getData = (url) => got.get(url).json();
const getData = (url) => ofetch(url);

const getList = (data) =>
data.map((value) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/chaping/newsflash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

const host = 'https://chaping.cn';
Expand Down Expand Up @@ -30,7 +30,7 @@ export const route: Route = {

async function handler() {
const newflashAPI = `${host}/api/official/information/newsflash?page=1&limit=21`;
const response = await got(newflashAPI).json();
const response = await ofetch(newflashAPI);
const data = response.data;

return {
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/codeforces/contests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);

import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import path from 'node:path';
import { art } from '@/utils/render';

Expand Down Expand Up @@ -46,7 +46,7 @@ export const route: Route = {
};

async function handler() {
const contestsData = await got.get(contestAPI).json();
const contestsData = await ofetch(contestAPI);
const contests = contestsData.result;

const items = contests
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/codeforces/recent-actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';

export const route: Route = {
Expand Down Expand Up @@ -30,7 +30,7 @@ export const route: Route = {
async function handler(ctx) {
const minRating = ctx.req.param('minrating') || 1;

const rsp = await got.get('https://codeforces.com/api/recentActions?maxCount=100').json();
const rsp = await ofetch('https://codeforces.com/api/recentActions?maxCount=100');

const actions = rsp.result.map((action) => {
const pubDate = new Date(action.timeSeconds * 1000);
Expand Down
10 changes: 4 additions & 6 deletions lib/routes/dblp/publication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route } from '@/types';
// 导入所需模组
import got from '@/utils/got'; // 自订的 got
import ofetch from '@/utils/ofetch';
// import { parseDate } from '@/utils/parse-date';

export const route: Route = {
Expand Down Expand Up @@ -35,18 +35,16 @@ async function handler(ctx) {
result: {
hits: { hit: data },
},
} = await got({
method: 'get',
url: 'https://dblp.org/search/publ/api',
searchParams: {
} = await ofetch('https://dblp.org/search/publ/api', {
query: {
q: field,
format: 'json',
h: 10,
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
}).json();
});

// console.log(data);

Expand Down
4 changes: 2 additions & 2 deletions lib/routes/dlnews/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

const baseUrl = 'https://www.dlnews.com';
const getData = async (url) => (await got.get(url).json()).content_elements;
const getData = async (url) => (await ofetch(url)).content_elements;

const getList = (data) =>
data.map((value) => {
Expand Down
21 changes: 10 additions & 11 deletions lib/routes/dushu/fuzhou/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);

import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { art } from '@/utils/render';
import path from 'node:path';

Expand Down Expand Up @@ -42,16 +42,15 @@ export const route: Route = {
};

async function handler() {
const response = await got
.post(host, {
json: {
channelTid: 'xtntzsnwsnkw511r',
pageNo: 1,
pageSize: 10,
type: 0,
},
})
.json();
const response = await ofetch(host, {
method: 'POST',
body: {
channelTid: 'xtntzsnwsnkw511r',
pageNo: 1,
pageSize: 10,
type: 0,
},
});

const data = response.data.activityListVOS;
data.map((element) => transformTime(element));
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/freebuf/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
Expand Down Expand Up @@ -40,7 +40,7 @@ async function handler(ctx) {
referer: 'https://www.freebuf.com',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
},
searchParams: {
query: {
name: type,
page: 1,
limit: 20,
Expand All @@ -50,7 +50,7 @@ async function handler(ctx) {
},
};

const response = await got.get(fapi, options).json();
const response = await ofetch(fapi, options);

const items = response.data.data_list.map((item) => ({
title: item.post_title,
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/gov/beijing/bphc/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Route } from '@/types';
import { getSubPath } from '@/utils/common-utils';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

Expand Down Expand Up @@ -36,13 +36,13 @@ async function handler(ctx) {
const obj = mapping[key];
const currentUrl = `${rootUrl}${obj.list}`;

const listResp = await got(currentUrl).json();
const listResp = await ofetch(currentUrl);
const list = listResp.data?.records ?? [];
const items = await Promise.all(
list.map((item) => {
const detail = `${rootUrl}${obj.detail}/${item.id}`;
return cache.tryGet(detail, async () => {
const detailResponse = await got(detail).json();
const detailResponse = await ofetch(detail);
const description = (detailResponse.data?.content || detailResponse.data?.introduction) ?? '';
const single = {
title: item.title || item.fullName,
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/grist/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

const getData = (url) => got.get(url).json();
const getData = (url) => ofetch(url);

const getList = (data) =>
data.map((value) => {
Expand Down
14 changes: 8 additions & 6 deletions lib/routes/hrbeu/job/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';

const rootUrl = 'http://job.hrbeu.edu.cn';
Expand Down Expand Up @@ -44,11 +44,11 @@ async function handler() {
month < 10 ? (strmMonth = '0' + month) : (strmMonth = month);
const day = date.getDate();

const response = await got('http://job.hrbeu.edu.cn/HrbeuJY/Web/Employ/QueryCalendar', {
searchParams: {
const response = await ofetch('http://job.hrbeu.edu.cn/HrbeuJY/Web/Employ/QueryCalendar', {
query: {
yearMonth: year + '-' + strmMonth,
},
}).json();
});

let link = '';
for (let i = 0, l = response.length; i < l; i++) {
Expand All @@ -58,9 +58,11 @@ async function handler() {
}
}

const todayResponse = await got(`${rootUrl}${link}`);
const todayResponse = await ofetch(`${rootUrl}${link}`, {
parseResponse: (txt) => txt,
});

const $ = load(todayResponse.data);
const $ = load(todayResponse);

const list = $('li.clearfix')
.map((_, item) => ({
Expand Down
40 changes: 19 additions & 21 deletions lib/routes/leetcode/articles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import MarkdownIt from 'markdown-it';
Expand Down Expand Up @@ -38,8 +38,8 @@ export const route: Route = {

async function handler() {
const link = new URL('/articles/', host).href;
const response = await got(link);
const $ = load(response.data);
const response = await ofetch(link, { parseResponse: (txt) => txt });
const $ = load(response);

const list = $('a.list-group-item')
.filter((i, e) => $(e).find('h4.media-heading i').length === 0)
Expand All @@ -59,37 +59,35 @@ async function handler() {
cache.tryGet(info.link, async () => {
const titleSlug = info.link.split('/')[4];

const questionContent = await got
.post(gqlEndpoint, {
json: {
operationName: 'questionContent',
variables: { titleSlug },
query: `query questionContent($titleSlug: String!) {
const questionContent = await ofetch(gqlEndpoint, {
method: 'POST',
body: {
operationName: 'questionContent',
variables: { titleSlug },
query: `query questionContent($titleSlug: String!) {
question(titleSlug: $titleSlug) {
content
mysqlSchemas
dataSchemas
}
}`,
},
})
.json();
},
});

const officialSolution = await got
.post(gqlEndpoint, {
json: {
operationName: 'officialSolution',
variables: { titleSlug },
query: `query officialSolution($titleSlug: String!) {
const officialSolution = await ofetch(gqlEndpoint, {
method: 'POST',
body: {
operationName: 'officialSolution',
variables: { titleSlug },
query: `query officialSolution($titleSlug: String!) {
question(titleSlug: $titleSlug) {
solution {
content
}
}
}`,
},
})
.json();
},
});

const solution = md.render(officialSolution.data.question.solution.content);

Expand Down
12 changes: 6 additions & 6 deletions lib/routes/luogu/user-blog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';

export const route: Route = {
Expand Down Expand Up @@ -33,8 +33,8 @@ async function handler(ctx) {

// Fetch the uid & title
const { uid: blogUid, title: blogTitle } = await cache.tryGet(blogBaseUrl, async () => {
const rsp = await got(blogBaseUrl);
const $ = load(rsp.data);
const rsp = await ofetch(blogBaseUrl);
const $ = load(rsp);
const uid = $("meta[name='blog-uid']").attr('content');
const name = $("meta[name='blog-name']").attr('content');
return {
Expand All @@ -43,7 +43,7 @@ async function handler(ctx) {
};
});

const posts = (await got(`https://www.luogu.com.cn/api/blog/lists?uid=${blogUid}`).json()).data.result.map((r) => ({
const posts = (await ofetch(`https://www.luogu.com.cn/api/blog/lists?uid=${blogUid}`)).data.result.map((r) => ({
title: r.title,
link: `${blogBaseUrl}${r.identifier}`,
pubDate: new Date(r.postTime * 1000),
Expand All @@ -53,8 +53,8 @@ async function handler(ctx) {
const item = await Promise.all(
posts.map((post) =>
cache.tryGet(post.link, async () => {
const rsp = await got(post.link);
const $ = load(rsp.data);
const rsp = await ofetch(post.link);
const $ = load(rsp);
return {
title: post.title,
link: post.link,
Expand Down
11 changes: 5 additions & 6 deletions lib/routes/modb/topic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import { load } from 'cheerio';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import logger from '@/utils/logger';
import timezone from '@/utils/timezone';
import { parseDate } from '@/utils/parse-date';
Expand All @@ -27,14 +27,13 @@ export const route: Route = {
async function handler(ctx) {
const baseUrl = 'https://www.modb.pro';
const topicId = ctx.req.param('id');
const response = await got({
url: `${baseUrl}/api/columns/getKnowledge`,
searchParams: {
const response = await ofetch(`${baseUrl}/api/columns/getKnowledge`, {
query: {
pageNum: 1,
pageSize: 20,
columnId: topicId,
},
}).json();
});
const list = response.list.map((item) => {
let doc = {};
let baseLink = {};
Expand Down Expand Up @@ -63,7 +62,7 @@ async function handler(ctx) {
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const response = await ofetch(item.link);
const $ = load(response);
item.description = $('div.editor-content-styl.article-style').first().html();
return item;
Expand Down
Loading

0 comments on commit cce3bdb

Please sign in to comment.