From 655f7ac52f12b465b0b65f17613e51278719bea8 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <40430746+CaoMeiYouRen@users.noreply.github.com> Date: Sat, 14 Dec 2024 21:02:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(github/repos):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E8=A7=A3=E6=9E=90=E5=8A=9F=E8=83=BD=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86=20(#1789?= =?UTF-8?q?3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 `parseDate` 工具函数用于解析日期 - 根据排序类型(updated 或 pushed)动态设置发布日期 - 优化数据过滤和映射逻辑,减少嵌套层级 --- lib/routes/github/repos.ts | 66 ++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/lib/routes/github/repos.ts b/lib/routes/github/repos.ts index edf19af5c40dbc..8f4ae671d0dc0b 100644 --- a/lib/routes/github/repos.ts +++ b/lib/routes/github/repos.ts @@ -1,6 +1,7 @@ import { Route } from '@/types'; import got from '@/utils/got'; import { config } from '@/config'; +import { parseDate } from '@/utils/parse-date'; export const route: Route = { path: '/repos/:user/:type?/:sort?', @@ -49,37 +50,46 @@ async function handler(ctx) { }, headers, }); - const data = response.data.filter((item) => { - switch (type) { - case 'all': - return true; - case 'owner': - return item.owner.login === user; - case 'member': - return item.owner.login !== user; - case 'public': - return item.private === false; - case 'private': - return item.private === true; - case 'forks': - return item.fork === true; - case 'sources': - return item.fork === false; - default: - return true; - } - }); + const item = response.data + .filter((item) => { + switch (type) { + case 'all': + return true; + case 'owner': + return item.owner.login === user; + case 'member': + return item.owner.login !== user; + case 'public': + return item.private === false; + case 'private': + return item.private === true; + case 'forks': + return item.fork === true; + case 'sources': + return item.fork === false; + default: + return true; + } + }) + .map((item) => { + let pubDate = parseDate(item.created_at); + if (sort === 'updated' && item.updated_at) { + pubDate = parseDate(item.updated_at); + } else if (sort === 'pushed' && item.pushed_at) { + pubDate = parseDate(item.pushed_at); + } + return { + title: item.name, + description: item.description || 'No description', + pubDate, + updated: parseDate(item.updated_at), + link: item.html_url, + }; + }); return { allowEmpty: true, title: `${user}'s GitHub repositories`, link: `https://github.com/${user}`, - item: - data && - data.map((item) => ({ - title: item.name, - description: item.description || 'No description', - pubDate: new Date(item.created_at).toUTCString(), - link: item.html_url, - })), + item, }; }