fix(route): 使用 ofetch() 替换一些 got().json() (#15780)
* 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
---------
This commit is contained in:
parent
9b3f9766dc
commit
cce3bdb4fe
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
@ -35,10 +35,8 @@ 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,
|
||||
|
|
@ -46,7 +44,7 @@ async function handler(ctx) {
|
|||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
||||
},
|
||||
}).json();
|
||||
});
|
||||
|
||||
// console.log(data);
|
||||
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
||||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
@ -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,
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
@ -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++) {
|
||||
|
|
@ -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) => ({
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
@ -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)
|
||||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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),
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
@ -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 = {};
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@ 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';
|
||||
|
||||
export const route: Route = {
|
||||
path: 'package/:name{(@[a-z0-9-~][a-z0-9-._~]*/)?[a-z0-9-~][a-z0-9-._~]*}',
|
||||
path: '/package/:name{(@[a-z0-9-~][a-z0-9-._~]*/)?[a-z0-9-~][a-z0-9-._~]*}',
|
||||
name: 'Unknown',
|
||||
maintainers: [],
|
||||
handler,
|
||||
|
|
@ -20,10 +20,10 @@ async function handler(ctx) {
|
|||
const packageDownloadLastDayAPI = `https://api.npmjs.org/downloads/point/last-day/${name}`; // 按天统计
|
||||
const packageVersionAPI = `https://registry.npmjs.org/${name}`; // 包基本信息
|
||||
|
||||
const downloadCountLastMonthRes = await got(packageDownloadLastMonthAPI).json();
|
||||
const downloadCountLastWeekRes = await got(packageDownloadLastWeekAPI).json();
|
||||
const downloadCountLastDayRes = await got(packageDownloadLastDayAPI).json();
|
||||
const packageVersionRes = await got(packageVersionAPI).json();
|
||||
const downloadCountLastMonthRes = await ofetch(packageDownloadLastMonthAPI);
|
||||
const downloadCountLastWeekRes = await ofetch(packageDownloadLastWeekAPI);
|
||||
const downloadCountLastDayRes = await ofetch(packageDownloadLastDayAPI);
|
||||
const packageVersionRes = await ofetch(packageVersionAPI);
|
||||
|
||||
const packageVersion = packageVersionRes.time;
|
||||
const packageVersionList = Object.keys(packageVersion)
|
||||
|
|
|
|||
|
|
@ -1,18 +1,16 @@
|
|||
import got from '@/utils/got';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
const baseUrl = 'https://web.stockedge.com/share/';
|
||||
const getData = (url) =>
|
||||
got
|
||||
.get(url, {
|
||||
headers: {
|
||||
Host: 'api.stockedge.com',
|
||||
Origin: 'https://web.stockedge.com',
|
||||
Referer: 'https://web.stockedge.com/',
|
||||
accept: 'application/json, text/plain, */*',
|
||||
},
|
||||
})
|
||||
.json();
|
||||
ofetch(url, {
|
||||
headers: {
|
||||
Host: 'api.stockedge.com',
|
||||
Origin: 'https://web.stockedge.com',
|
||||
Referer: 'https://web.stockedge.com/',
|
||||
accept: 'application/json, text/plain, */*',
|
||||
},
|
||||
});
|
||||
|
||||
const getList = (data) =>
|
||||
data.map((value) => {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import utc from 'dayjs/plugin/utc';
|
|||
import timezone from 'dayjs/plugin/timezone';
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
import got from '@/utils/got';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { art } from '@/utils/render';
|
||||
import path from 'node:path';
|
||||
import { config } from '@/config';
|
||||
|
|
@ -75,7 +75,7 @@ const filterKeyword = (keywordList) => (res) => res.filter(({ title }) => hasKey
|
|||
// Data Fetcher
|
||||
// TODO: support channel selection
|
||||
const fetchAllData = async (keywordList = [], dateList = [], cache) => {
|
||||
const cachedGetData = (url) => cache.tryGet(url, () => got(url).json(), config.cache.contentExpire, false);
|
||||
const cachedGetData = (url) => cache.tryGet(url, () => ofetch(url), config.cache.contentExpire, false);
|
||||
|
||||
let data = await Promise.all(
|
||||
dateList.map(async (dateTime) => ({
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { Route } from '@/types';
|
||||
import cache from '@/utils/cache';
|
||||
import got from '@/utils/got';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/:type?',
|
||||
|
|
@ -13,10 +13,7 @@ async function handler(ctx) {
|
|||
const type = ctx.req.param('type') === 'all' ? '' : ctx.req.param('type').toUpperCase();
|
||||
const host = `https://td.zjgtjy.cn:8553/devops/noticeInfo/queryNoticeInfoList?pageSize=10&pageNumber=1¬iceType=${type}&sort=DESC`;
|
||||
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: host,
|
||||
}).json();
|
||||
const response = await ofetch(host);
|
||||
const data = response.data;
|
||||
|
||||
const items = await Promise.all(
|
||||
|
|
@ -25,10 +22,7 @@ async function handler(ctx) {
|
|||
const pageLink = `https://td.zjgtjy.cn/view/trade/announcement/detail?id=${item.GGID}&category=${item.ZYLB}&type=${item.JYFS}`;
|
||||
|
||||
const desc = await cache.tryGet(pageUrl, async () => {
|
||||
let desc = await got({
|
||||
method: 'get',
|
||||
url: pageUrl,
|
||||
}).json();
|
||||
let desc = await ofetch(pageUrl);
|
||||
desc = desc.queryNoticeContent.GGNR;
|
||||
|
||||
desc = desc.replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"');
|
||||
|
|
|
|||
Loading…
Reference in New Issue