chore: fix typing

This commit is contained in:
TonyRL 2026-07-14 00:37:46 +08:00
parent ecfefda0fe
commit 66b2c550b4
6 changed files with 25 additions and 17 deletions

View File

@ -602,6 +602,14 @@
{
"selector": "CallExpression[callee.property.name=\"map\"] > FunctionExpression",
"message": "Use an arrow function instead."
},
{
"selector": "TSTypeReference[typeName.name='ReturnType'] TSTypeQuery[exprName.name='load']",
"message": "Usage of ReturnType<typeof load> is not allowed. Please use appropriate types from cheerio instead."
},
{
"selector": "TSTypeReference[typeName.name='ReturnType'] TSTypeReference[typeName.name='CheerioAPI']",
"message": "Usage of ReturnType<CheerioAPI> is not allowed. Please use appropriate types from cheerio and domhandler instead."
}
],

View File

@ -1,13 +1,11 @@
import { load } from 'cheerio';
import { type Cheerio, type CheerioAPI, load } from 'cheerio';
import type { Element } from 'domhandler';
import type { Data, DataItem, Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
type CheerioInstance = ReturnType<typeof load>;
type CheerioSelection = ReturnType<CheerioInstance>;
interface DateContext {
currentYear: number;
prevMonth: number;
@ -36,7 +34,7 @@ function parseDateString(dateStr: string, ctx: DateContext): Date | undefined {
return timezone(parseDate(`${ctx.currentYear}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`), 8);
}
function processNewsList($: CheerioInstance, $newsList: CheerioSelection, ctx: DateContext): DataItem[] {
function processNewsList($: CheerioAPI, $newsList: Cheerio<Element>, ctx: DateContext): DataItem[] {
let currentPubDate: Date | undefined;
return $newsList

View File

@ -1,5 +1,6 @@
import type { CheerioAPI } from 'cheerio';
import type { Cheerio, CheerioAPI } from 'cheerio';
import { load } from 'cheerio';
import type { Element } from 'domhandler';
import type { BrowserContext } from 'patchright';
import type { DataItem } from '@/types';
@ -68,7 +69,7 @@ function tryParseAsDate(text: string): Date | undefined {
/**
* Extract author from article element
*/
function extractAuthor(article: ReturnType<CheerioAPI>): DataItem['author'] {
function extractAuthor(article: Cheerio<Element>): DataItem['author'] {
const container = article.find('.flex.items-center.gap-3').first();
if (container.length === 0) {
return undefined;
@ -134,7 +135,7 @@ function extractAuthor(article: ReturnType<CheerioAPI>): DataItem['author'] {
/**
* Extract categories/tags from article element
*/
function extractCategories(article: ReturnType<CheerioAPI>, $: CheerioAPI): string[] {
function extractCategories(article: Cheerio<Element>, $: CheerioAPI): string[] {
return article
.find('a[href^="/"]')
.toArray()
@ -155,7 +156,7 @@ function extractCategories(article: ReturnType<CheerioAPI>, $: CheerioAPI): stri
/**
* Extract publication date from article element
*/
function extractPubDate(article: ReturnType<CheerioAPI>): Date | undefined {
function extractPubDate(article: Cheerio<Element>): Date | undefined {
const container = article.find('.flex.items-center.gap-3').first();
if (container.length === 0) {
return undefined;
@ -208,7 +209,7 @@ function extractPubDate(article: ReturnType<CheerioAPI>): Date | undefined {
/**
* Parse a single article element into an ArticleItem
*/
function parseArticle(article: ReturnType<CheerioAPI>, $: CheerioAPI, baseUrl: string): (DataItem & ArticleItem) | null {
function parseArticle(article: Cheerio<Element>, $: CheerioAPI, baseUrl: string): (DataItem & ArticleItem) | null {
// Find the title link in h2 > a
const titleLink = article.find('h2 a[href^="http"]');
if (titleLink.length === 0) {

View File

@ -1,4 +1,4 @@
import { load } from 'cheerio';
import { type CheerioAPI, load } from 'cheerio';
import { config } from '@/config';
import type { DataItem, Route } from '@/types';
@ -61,7 +61,7 @@ Note: If you want to track a specific post's content changes (e.g., first post w
const lastPageLink = $firstPage('ul.pageNav-main li.pageNav-page:last-child a').attr('href');
const totalPages = lastPageLink ? Number(lastPageLink.match(/page-(\d+)/)?.[1] || '1') : 1;
const extractPosts = ($: ReturnType<typeof load>): DataItem[] =>
const extractPosts = ($: CheerioAPI): DataItem[] =>
$('article.message')
.toArray()
.flatMap((article) => {

View File

@ -1,4 +1,4 @@
import { load } from 'cheerio';
import { type CheerioAPI, load } from 'cheerio';
import pMap from 'p-map';
import type { DataItem, Route } from '@/types';
@ -19,12 +19,12 @@ const getRequestOptions = (referer: string) => ({
},
});
const getAbsoluteUrl = (path: string | undefined) => (path ? new URL(path, ROOT_URL).href : undefined);
const getArticleAuthor = ($: ReturnType<typeof load>) =>
const getArticleAuthor = ($: CheerioAPI) =>
$('#article .items p')
.text()
.match(/Posted by\s+(\S.*)$/)?.[1]
?.trim();
const getArticleCategories = ($: ReturnType<typeof load>) => [
const getArticleCategories = ($: CheerioAPI) => [
...new Set(
$('#article .items p a[href*="/gsc_news/en/C"]')
.toArray()

View File

@ -1,5 +1,6 @@
import type { AnyNode, Cheerio } from 'cheerio';
import type { Cheerio } from 'cheerio';
import { load } from 'cheerio';
import type { Element } from 'domhandler';
import type { Context } from 'hono';
import type { DataItem, Route } from '@/types';
@ -103,7 +104,7 @@ function checkExternal(link: string): boolean {
* @param element
* @returns A list of RSS meta node.
*/
function parseListLinkDateItem(element: Cheerio<AnyNode>, currentUrl: string) {
function parseListLinkDateItem(element: Cheerio<Element>, currentUrl: string) {
const linkElement = element.find('a').first();
const title = linkElement.text();
const href = linkElement.attr('href');