Skip to content

Commit

Permalink
feat(route/github): enhance /repos route with optional parameters for…
Browse files Browse the repository at this point in the history
… type, sort, and direction (#17827)

* feat(route/github): enhance /repos route with optional parameters for type, sort, and direction

- Updated the route to accept optional parameters: type, sort, and direction.
- Added filtering logic for repository types: all, owner, member, public, private, forks, and sources.
- Improved documentation for parameters to clarify usage.

* refactor(github): 优化仓库路由参数配置
- 移除 `direction` 参数,简化排序逻辑
- 更新路由路径以匹配简化后的参数
  • Loading branch information
CaoMeiYouRen authored Dec 10, 2024
1 parent 8434009 commit 17e2fc7
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions lib/routes/github/repos.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Route } from '@/types';
import got from '@/utils/got';
import { config } from '@/config';
import queryString from 'query-string';

export const route: Route = {
path: '/repos/:user',
path: '/repos/:user/:type?/:sort?',
categories: ['programming'],
example: '/github/repos/DIYgod',
parameters: { user: 'GitHub username' },
parameters: {
user: 'GitHub username',
type: 'Type of repository, can be `all`, `owner`, `member`, `public`, `private`, `forks`, `sources`',
sort: 'Sort by `created`, `updated`, `pushed`, `full_name`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -28,20 +31,44 @@ export const route: Route = {

async function handler(ctx) {
const user = ctx.req.param('user');

const headers = {};
const type = ctx.req.param('type') || 'all';
const sort = ctx.req.param('sort') || 'created';
let headers = {};
if (config.github && config.github.access_token) {
headers.Authorization = `token ${config.github.access_token}`;
headers = {
...headers,
Authorization: `token ${config.github.access_token}`,
};
}
const response = await got({
method: 'get',
url: `https://api.github.com/users/${user}/repos`,
searchParams: queryString.stringify({
sort: 'created',
}),
searchParams: {
type,
sort,
},
headers,
});
const data = response.data;
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;
}
});
return {
allowEmpty: true,
title: `${user}'s GitHub repositories`,
Expand Down

0 comments on commit 17e2fc7

Please sign in to comment.