diff --git a/lib/middleware/parameter.ts b/lib/middleware/parameter.ts index 1933889128..025f6b405c 100644 --- a/lib/middleware/parameter.ts +++ b/lib/middleware/parameter.ts @@ -1,7 +1,7 @@ import * as entities from 'entities'; import { load, type CheerioAPI, type Element } from 'cheerio'; import { simplecc } from 'simplecc-wasm'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { config } from '@/config'; import { RE2JS } from 're2js'; import markdownit from 'markdown-it'; @@ -34,8 +34,9 @@ const resolveRelativeLink = ($: CheerioAPI, elem: Element, attr: string, baseUrl const summarizeArticle = async (articleText: string) => { const apiUrl = `${config.openai.endpoint}/chat/completions`; - const response = await got.post(apiUrl, { - json: { + const response = await ofetch(apiUrl, { + method: 'POST', + body: { model: config.openai.model, max_tokens: config.openai.maxTokens, messages: [ @@ -49,7 +50,6 @@ const summarizeArticle = async (articleText: string) => { }, }); - // @ts-expect-error custom field return response.data.choices[0].message.content; }; @@ -305,8 +305,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { if (link) { // if parser failed, return default description and not report error try { - // @ts-expect-error custom field - const { data: res } = await got(link); + const res = await ofetch(link); const $ = load(res); const result = await Parser.parse(link, { html: $.html(), diff --git a/lib/utils/common-config.ts b/lib/utils/common-config.ts index 65e84d2094..74d1b7c2bf 100644 --- a/lib/utils/common-config.ts +++ b/lib/utils/common-config.ts @@ -1,5 +1,5 @@ -import cheerio from 'cheerio'; -import got from '@/utils/got'; +import { load } from 'cheerio'; +import ofetch from '@/utils/ofetch'; import iconv from 'iconv-lite'; function transElemText($, prop) { @@ -37,8 +37,8 @@ function getProp(data, prop, $) { } async function buildData(data) { - const response = await got.get(data.url); - const contentType = response.headers['content-type'] || ''; + const response = await ofetch.raw(data.url); + const contentType = response.headers.get('content-type') || ''; // 若没有指定编码,则默认utf-8 let charset = 'utf-8'; for (const attr of contentType.split(';')) { @@ -47,8 +47,8 @@ async function buildData(data) { } } // @ts-expect-error custom property - const responseData = charset === 'utf-8' ? response.data : iconv.decode((await got.get({ url: data.url, responseType: 'buffer' })).data, charset); - const $ = cheerio.load(responseData); + const responseData = charset === 'utf-8' ? response._data : iconv.decode(await ofetch(data.url, { responseType: 'buffer' }), charset); + const $ = load(responseData); const $item = $(data.item.item); // 这里应该是可以通过参数注入一些代码的,不过应该无伤大雅 return { diff --git a/lib/utils/wechat-mp.ts b/lib/utils/wechat-mp.ts index ae03957b8d..0615ab1c94 100644 --- a/lib/utils/wechat-mp.ts +++ b/lib/utils/wechat-mp.ts @@ -25,7 +25,7 @@ * For more details of these functions, please refer to the jsDoc in the source code. */ -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { load, type Cheerio, type Element } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; import cache from '@/utils/cache'; @@ -196,9 +196,8 @@ const normalizeUrl = (url, bypassHostCheck = false) => { const fetchArticle = (url, bypassHostCheck = false) => { url = normalizeUrl(url, bypassHostCheck); return cache.tryGet(url, async () => { - const response = await got(url); - // @ts-expect-error custom field - const $ = load(response.data); + const data = await ofetch(url); + const $ = load(data); const title = ($('meta[property="og:title"]').attr('content') || '').replaceAll('\\r', '').replaceAll('\\n', ' '); const author = $('meta[name=author]').attr('content'); @@ -209,9 +208,8 @@ const fetchArticle = (url, bypassHostCheck = false) => { const originalUrl = detectOriginalArticleUrl($); if (originalUrl) { // try to fetch the description from the original article - const originalResponse = await got(normalizeUrl(originalUrl, bypassHostCheck)); - // @ts-expect-error custom field - const original$ = load(originalResponse.data); + const data = await ofetch(normalizeUrl(originalUrl, bypassHostCheck)); + const original$ = load(data); description += fixArticleContent(original$('#js_content')); } @@ -230,7 +228,7 @@ const fetchArticle = (url, bypassHostCheck = false) => { let mpName = $('.profile_nickname').first().text(); mpName = mpName && mpName.trim(); - return { title, author, description, summary, pubDate, mpName, link: response.url }; + return { title, author, description, summary, pubDate, mpName, link: url }; }) as Promise<{ title: string; author: string;