1
- addEventListener ( 'fetch' , event => {
2
- event . respondWith ( handleRequest ( event . request ) )
3
- } )
1
+ export default {
2
+ async fetch ( request , env , ctx ) {
3
+ return await handleRequest ( request ) ;
4
+ }
5
+ } ;
4
6
5
7
async function handleRequest ( request ) {
6
- const response = await fetch ( 'https://api.github.com/repos/{owner}/{repo}/issues' )
7
- const issues = await response . json ( )
8
- const blogPosts = issues . map ( issue => ( {
9
- title : issue . title ,
10
- body : issue . body ,
11
- created_at : issue . created_at
12
- } ) )
13
- return new Response ( JSON . stringify ( blogPosts ) , {
14
- headers : { 'content-type' : 'application/json' }
15
- } )
8
+ const GITHUB_TOKEN = '' ; // 替换为你的 GitHub 个人访问令牌
9
+
10
+ try {
11
+ const response = await fetch ( 'https://api.github.com/repos/gusibi/path-meme-db/issues' , {
12
+ headers : {
13
+ 'Authorization' : `token ${ GITHUB_TOKEN } ` ,
14
+ 'Accept' : 'application/vnd.github.v3+json'
15
+ }
16
+ } ) ;
17
+
18
+ // 检查响应是否成功
19
+ if ( ! response . ok ) {
20
+ return new Response ( `GitHub API request failed with status ${ response . status } ` , {
21
+ status : response . status ,
22
+ headers : { 'content-type' : 'text/plain' }
23
+ } ) ;
24
+ }
25
+
26
+ const issues = await response . json ( ) ;
27
+ const blogPosts = issues . map ( issue => ( {
28
+ title : issue . title ,
29
+ body : issue . body ,
30
+ created_at : issue . created_at
31
+ } ) ) ;
32
+
33
+ return new Response ( JSON . stringify ( blogPosts ) , {
34
+ headers : { 'content-type' : 'application/json' }
35
+ } ) ;
36
+ } catch ( error ) {
37
+ return new Response ( `Error: ${ error . message } ` , {
38
+ status : 500 ,
39
+ headers : { 'content-type' : 'text/plain' }
40
+ } ) ;
41
+ }
16
42
}
0 commit comments