worker.js(файл создан)
@@ -0,0 +1,59 @@ | |||
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 | + |
Новее
Позже