Skip to content

Commit

Permalink
fix(github/repos): 添加日期解析功能并优化数据处理 (#17893)
Browse files Browse the repository at this point in the history
- 引入 `parseDate` 工具函数用于解析日期
- 根据排序类型(updated 或 pushed)动态设置发布日期
- 优化数据过滤和映射逻辑,减少嵌套层级
  • Loading branch information
CaoMeiYouRen authored Dec 14, 2024
1 parent 3a8d34e commit 655f7ac
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions lib/routes/github/repos.ts
Original file line number Diff line number Diff line change
@@ -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?',
Expand Down Expand Up @@ -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,
};
}

0 comments on commit 655f7ac

Please sign in to comment.