-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
25 lines (22 loc) · 908 Bytes
/
Copy pathworker.js
File metadata and controls
25 lines (22 loc) · 908 Bytes
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
export default {
async fetch(request) {
const url = new URL(request.url);
const webhookMatch = url.pathname.match(/^\/api\/webhooks\/(\d+)\/([^\/]+)/);
const apiMatch = url.pathname.match(/^\/discord(\/.*)?$/);
let targetUrl;
if (webhookMatch) {
targetUrl = `https://discord.com/api/webhooks/${webhookMatch[1]}/${webhookMatch[2]}`;
} else if (apiMatch) {
targetUrl = `https://discord.com/api${apiMatch[1] || ""}${url.search}`;
} else {
return new Response("Use: /api/webhooks/ID/TOKEN or /discord/v10/...");
}
const response = await fetch(targetUrl, {
method: request.method,
headers: request.headers,
body: request.method !== "GET" && request.method !== "HEAD" ? request.body : null,
});
const body = response.status === 204 ? null : await response.text();
return new Response(body, { status: response.status });
}
}