worker.js
· 2.0 KiB · JavaScript
Raw
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run "npm run dev" in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run "npm run deploy" to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
export default {
async fetch(request, env, ctx) {
const GITHUB_TOKEN = 'github_pat_11AW00000000000000_1008DVkF0000000000AWi0031J0000000000Wm1eG700Li0000000000GgCm00000000000'; // 你的 GitHub Token
const GITHUB_API_URL = 'https://api.github.com/repos';
const url = new URL(request.url);
const repoName = url.searchParams.get('repo') || 'liushen-blog'; // 默认使用 'liushen-blog' 仓库
const dataCount = parseInt(url.searchParams.get('count')) || 10; // 默认返回 10 条数据
const commitsUrl = `${GITHUB_API_URL}/willow-god/${repoName}/commits?per_page=${dataCount}`;
try {
// 请求 GitHub API 获取提交数据
const response = await fetch(commitsUrl, {
method: 'GET',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${GITHUB_TOKEN}`,
'X-GitHub-Api-Version': '2022-11-28',
'User-Agent': 'Cloudflare Worker' // 添加 User-Agent 头部
}
});
if (!response.ok) {
const errorText = await response.text();
console.error(`GitHub API Error: ${errorText}`);
return new Response('Error fetching commits from GitHub', { status: response.status });
}
const commits = await response.json();
const result = commits.map(commit => ({
sha: commit.sha,
date: commit.commit.author.date,
message: commit.commit.message
}));
return new Response(JSON.stringify(result, null, 2), {
headers: { 'Content-Type': 'application/json' }
});
} catch (err) {
console.error('Error fetching commits:', err);
return new Response('Error fetching commits', { status: 500 });
}
}
};
1 | /** |
2 | * Welcome to Cloudflare Workers! This is your first worker. |
3 | * |
4 | * - Run "npm run dev" in your terminal to start a development server |
5 | * - Open a browser tab at http://localhost:8787/ to see your worker in action |
6 | * - Run "npm run deploy" to publish your worker |
7 | * |
8 | * Learn more at https://developers.cloudflare.com/workers/ |
9 | */ |
10 | |
11 | |
12 | export default { |
13 | async fetch(request, env, ctx) { |
14 | const GITHUB_TOKEN = 'github_pat_11AW00000000000000_1008DVkF0000000000AWi0031J0000000000Wm1eG700Li0000000000GgCm00000000000'; // 你的 GitHub Token |
15 | const GITHUB_API_URL = 'https://api.github.com/repos'; |
16 | |
17 | const url = new URL(request.url); |
18 | const repoName = url.searchParams.get('repo') || 'liushen-blog'; // 默认使用 'liushen-blog' 仓库 |
19 | const dataCount = parseInt(url.searchParams.get('count')) || 10; // 默认返回 10 条数据 |
20 | |
21 | const commitsUrl = `${GITHUB_API_URL}/willow-god/${repoName}/commits?per_page=${dataCount}`; |
22 | |
23 | try { |
24 | // 请求 GitHub API 获取提交数据 |
25 | const response = await fetch(commitsUrl, { |
26 | method: 'GET', |
27 | headers: { |
28 | 'Accept': 'application/vnd.github+json', |
29 | 'Authorization': `Bearer ${GITHUB_TOKEN}`, |
30 | 'X-GitHub-Api-Version': '2022-11-28', |
31 | 'User-Agent': 'Cloudflare Worker' // 添加 User-Agent 头部 |
32 | } |
33 | }); |
34 | |
35 | if (!response.ok) { |
36 | const errorText = await response.text(); |
37 | console.error(`GitHub API Error: ${errorText}`); |
38 | return new Response('Error fetching commits from GitHub', { status: response.status }); |
39 | } |
40 | |
41 | const commits = await response.json(); |
42 | const result = commits.map(commit => ({ |
43 | sha: commit.sha, |
44 | date: commit.commit.author.date, |
45 | message: commit.commit.message |
46 | })); |
47 | |
48 | return new Response(JSON.stringify(result, null, 2), { |
49 | headers: { 'Content-Type': 'application/json' } |
50 | }); |
51 | |
52 | } catch (err) { |
53 | console.error('Error fetching commits:', err); |
54 | return new Response('Error fetching commits', { status: 500 }); |
55 | } |
56 | } |
57 | }; |
58 | |
59 | |
60 |