From cce3bdb4fe49cb42670fe538db42ffac1b0aa2c8 Mon Sep 17 00:00:00 2001 From: Nya Candy Date: Sat, 1 Jun 2024 01:10:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E4=BD=BF=E7=94=A8=20ofetch()=20?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E4=B8=80=E4=BA=9B=20got().json()=20(#15780)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 :thinking: * fix: some other issues * fix: query * fix: luogu user blog & modb topic * fix: modb topic response * fix: query options in freebuf --------- --- lib/routes/alistapart/utils.ts | 4 +-- lib/routes/chaping/newsflash.ts | 4 +-- lib/routes/codeforces/contests.ts | 4 +-- lib/routes/codeforces/recent-actions.ts | 4 +-- lib/routes/dblp/publication.ts | 10 +++---- lib/routes/dlnews/utils.ts | 4 +-- lib/routes/dushu/fuzhou/index.ts | 21 +++++++------ lib/routes/freebuf/index.ts | 6 ++-- lib/routes/gov/beijing/bphc/index.ts | 6 ++-- lib/routes/grist/utils.ts | 4 +-- lib/routes/hrbeu/job/calendar.ts | 14 +++++---- lib/routes/leetcode/articles.ts | 40 ++++++++++++------------- lib/routes/luogu/user-blog.ts | 12 ++++---- lib/routes/modb/topic.ts | 11 ++++--- lib/routes/npm/package.ts | 12 ++++---- lib/routes/stockedge/utils.ts | 20 ++++++------- lib/routes/trending/all-trending.ts | 4 +-- lib/routes/zjgtjy/index.ts | 12 ++------ 18 files changed, 90 insertions(+), 102 deletions(-) diff --git a/lib/routes/alistapart/utils.ts b/lib/routes/alistapart/utils.ts index c4a4a29257af51..2d426e89b03ebf 100644 --- a/lib/routes/alistapart/utils.ts +++ b/lib/routes/alistapart/utils.ts @@ -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) => { diff --git a/lib/routes/chaping/newsflash.ts b/lib/routes/chaping/newsflash.ts index 91868481e9e5ba..8f77d108083d82 100644 --- a/lib/routes/chaping/newsflash.ts +++ b/lib/routes/chaping/newsflash.ts @@ -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 { diff --git a/lib/routes/codeforces/contests.ts b/lib/routes/codeforces/contests.ts index b78317506ce1b9..978ad8a491bc3d 100644 --- a/lib/routes/codeforces/contests.ts +++ b/lib/routes/codeforces/contests.ts @@ -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 diff --git a/lib/routes/codeforces/recent-actions.ts b/lib/routes/codeforces/recent-actions.ts index 11d5434a169928..2baaf8c823fc2f 100644 --- a/lib/routes/codeforces/recent-actions.ts +++ b/lib/routes/codeforces/recent-actions.ts @@ -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); diff --git a/lib/routes/dblp/publication.ts b/lib/routes/dblp/publication.ts index 90b1f99e0a9b57..e430c9e78794e7 100644 --- a/lib/routes/dblp/publication.ts +++ b/lib/routes/dblp/publication.ts @@ -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); diff --git a/lib/routes/dlnews/utils.ts b/lib/routes/dlnews/utils.ts index 2e9519b892b805..276cd3ca4dc056 100644 --- a/lib/routes/dlnews/utils.ts +++ b/lib/routes/dlnews/utils.ts @@ -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) => { diff --git a/lib/routes/dushu/fuzhou/index.ts b/lib/routes/dushu/fuzhou/index.ts index ab797f9d5d1335..50ab8c0e3cb5af 100644 --- a/lib/routes/dushu/fuzhou/index.ts +++ b/lib/routes/dushu/fuzhou/index.ts @@ -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)); diff --git a/lib/routes/freebuf/index.ts b/lib/routes/freebuf/index.ts index b092af41f09139..85eac4d6a5bd97 100644 --- a/lib/routes/freebuf/index.ts +++ b/lib/routes/freebuf/index.ts @@ -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, diff --git a/lib/routes/gov/beijing/bphc/index.ts b/lib/routes/gov/beijing/bphc/index.ts index e5e37f345468b5..ca1553cd084e56 100644 --- a/lib/routes/gov/beijing/bphc/index.ts +++ b/lib/routes/gov/beijing/bphc/index.ts @@ -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, diff --git a/lib/routes/grist/utils.ts b/lib/routes/grist/utils.ts index 4b4a5f59cec80b..c93f183280bd3c 100644 --- a/lib/routes/grist/utils.ts +++ b/lib/routes/grist/utils.ts @@ -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) => { diff --git a/lib/routes/hrbeu/job/calendar.ts b/lib/routes/hrbeu/job/calendar.ts index 783bb24e417a5d..8890973fce9ea8 100644 --- a/lib/routes/hrbeu/job/calendar.ts +++ b/lib/routes/hrbeu/job/calendar.ts @@ -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) => ({ diff --git a/lib/routes/leetcode/articles.ts b/lib/routes/leetcode/articles.ts index ff44afe65e4483..8bb7e6e49db625 100644 --- a/lib/routes/leetcode/articles.ts +++ b/lib/routes/leetcode/articles.ts @@ -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); diff --git a/lib/routes/luogu/user-blog.ts b/lib/routes/luogu/user-blog.ts index f38d4cc6b0fb2c..6d91e5ce8215a0 100644 --- a/lib/routes/luogu/user-blog.ts +++ b/lib/routes/luogu/user-blog.ts @@ -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, diff --git a/lib/routes/modb/topic.ts b/lib/routes/modb/topic.ts index 69666942183927..d577d71bd0bdb2 100644 --- a/lib/routes/modb/topic.ts +++ b/lib/routes/modb/topic.ts @@ -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; diff --git a/lib/routes/npm/package.ts b/lib/routes/npm/package.ts index 06191502475d66..74614cfd85bc46 100644 --- a/lib/routes/npm/package.ts +++ b/lib/routes/npm/package.ts @@ -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) diff --git a/lib/routes/stockedge/utils.ts b/lib/routes/stockedge/utils.ts index 56ccbe49c3f684..8e2958838f3ea2 100644 --- a/lib/routes/stockedge/utils.ts +++ b/lib/routes/stockedge/utils.ts @@ -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) => { diff --git a/lib/routes/trending/all-trending.ts b/lib/routes/trending/all-trending.ts index 1233cdeaf281df..a6cd7b99c01ddd 100644 --- a/lib/routes/trending/all-trending.ts +++ b/lib/routes/trending/all-trending.ts @@ -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) => ({ diff --git a/lib/routes/zjgtjy/index.ts b/lib/routes/zjgtjy/index.ts index 54f6495ec88475..47826ad0de2ec2 100644 --- a/lib/routes/zjgtjy/index.ts +++ b/lib/routes/zjgtjy/index.ts @@ -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('"', '"');