-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigdbproxy.js
67 lines (58 loc) · 2.18 KB
/
igdbproxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
export default {
async fetch(request, env) {
if (request.method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Headers': 'Content-Type'
},
status: 204
});
}
try {
const { query } = await request.json();
const tokenResponse = await fetch('https://id.twitch.tv/oauth2/token', {
method: 'POST',
body: new URLSearchParams({
client_id: env.IGDB_CLIENT_ID,
client_secret: env.IGDB_CLIENT_SECRET,
grant_type: 'client_credentials'
})
});
const tokenData = await tokenResponse.json();
if (!tokenData.access_token) {
throw new Error('Failed to get access token');
}
const igdbResponse = await fetch('https://api.igdb.com/v4/games', {
method: 'POST',
headers: {
'Client-ID': env.IGDB_CLIENT_ID,
'Authorization': `Bearer ${tokenData.access_token}`,
'Content-Type': 'text/plain'
},
body: query
});
if (!igdbResponse.ok) {
const errorText = await igdbResponse.text();
throw new Error(`IGDB API error: ${errorText}`);
}
const data = await igdbResponse.json();
return new Response(JSON.stringify(data), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
});
} catch (error) {
console.error('Worker error:', error);
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
});
}
}
};