Skip to content

Commit ff1517d

Browse files
committed
fix: update webhook file route
1 parent 615b9ba commit ff1517d

2 files changed

Lines changed: 142 additions & 152 deletions

File tree

src/routes/api/webhook/release.ts

Lines changed: 71 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createFileRoute } from "@tanstack/react-router";
1+
import { createServerFileRoute } from "@tanstack/react-start/server";
22
import { z } from "zod";
33
import { SendEmbed } from "@/lib/discord";
44

@@ -7,90 +7,85 @@ const envSchema = z.object({
77
DISCORD_WEBHOOK_RELEASE: z.string().url(),
88
});
99

10-
export const Route = createFileRoute("/api/webhook/release")({
11-
// @ts-expect-error TanStack Start server handlers - types provided by Vite plugin at build time
12-
server: {
13-
handlers: {
14-
POST: async ({ request }: { request: Request }) => {
15-
// Validate environment configuration
16-
const envResult = envSchema.safeParse(process.env);
17-
if (!envResult.success) {
18-
const missing = envResult.error.issues
19-
.map((i) => i.path.join("."))
20-
.join(", ");
21-
console.error(
22-
`[webhook/release] Environment configuration error: missing or invalid: ${missing}`,
23-
);
24-
return new Response("Internal server error", { status: 500 });
25-
}
26-
const env = envResult.data;
10+
export const ServerRoute = createServerFileRoute("/api/webhook/release").methods({
11+
POST: async ({ request }) => {
12+
// Validate environment configuration
13+
const envResult = envSchema.safeParse(process.env);
14+
if (!envResult.success) {
15+
const missing = envResult.error.issues
16+
.map((i) => i.path.join("."))
17+
.join(", ");
18+
console.error(
19+
`[webhook/release] Environment configuration error: missing or invalid: ${missing}`,
20+
);
21+
return new Response("Internal server error", { status: 500 });
22+
}
23+
const env = envResult.data;
2724

28-
// Authorization
29-
const { searchParams } = new URL(request.url);
30-
const code = searchParams.get("code");
25+
// Authorization
26+
const { searchParams } = new URL(request.url);
27+
const code = searchParams.get("code");
3128

32-
if (!code) {
33-
console.warn(
34-
"[webhook/release] Authorization failed: no code parameter provided",
35-
);
36-
return new Response("Unauthorized", { status: 401 });
37-
}
29+
if (!code) {
30+
console.warn(
31+
"[webhook/release] Authorization failed: no code parameter provided",
32+
);
33+
return new Response("Unauthorized", { status: 401 });
34+
}
3835

39-
if (code !== env.API_ROUTE_SECRET) {
40-
console.warn(
41-
"[webhook/release] Authorization failed: invalid code parameter",
42-
);
43-
return new Response("Unauthorized", { status: 401 });
44-
}
36+
if (code !== env.API_ROUTE_SECRET) {
37+
console.warn(
38+
"[webhook/release] Authorization failed: invalid code parameter",
39+
);
40+
return new Response("Unauthorized", { status: 401 });
41+
}
4542

46-
// Parse body
47-
const reqBody = await request.text();
48-
if (reqBody === "") {
49-
console.warn("[webhook/release] Bad request: empty body");
50-
return new Response("Bad request", { status: 400 });
51-
}
43+
// Parse body
44+
const reqBody = await request.text();
45+
if (reqBody === "") {
46+
console.warn("[webhook/release] Bad request: empty body");
47+
return new Response("Bad request", { status: 400 });
48+
}
5249

53-
let data: {
54-
title?: string;
55-
description?: string;
56-
url?: string;
57-
timestamp?: Date;
58-
color?: number;
59-
};
50+
let data: {
51+
title?: string;
52+
description?: string;
53+
url?: string;
54+
timestamp?: Date;
55+
color?: number;
56+
};
6057

61-
try {
62-
data = JSON.parse(reqBody);
63-
} catch {
64-
console.warn("[webhook/release] Bad request: invalid JSON body");
65-
return new Response("Bad request", { status: 400 });
66-
}
58+
try {
59+
data = JSON.parse(reqBody);
60+
} catch {
61+
console.warn("[webhook/release] Bad request: invalid JSON body");
62+
return new Response("Bad request", { status: 400 });
63+
}
6764

68-
console.log("[webhook/release] Processing embed:", data);
65+
console.log("[webhook/release] Processing embed:", data);
6966

70-
if (data.title) {
71-
data.title = `📦 | ${data.title}`;
72-
}
67+
if (data.title) {
68+
data.title = `📦 | ${data.title}`;
69+
}
7370

74-
// Send embed
75-
try {
76-
await SendEmbed(env.DISCORD_WEBHOOK_RELEASE, {
77-
title: "🎁 | New Release",
78-
url: "https://github.com/pyclashbot/py-clash-bot/releases/latest",
79-
color: 0x03fc49,
80-
tagId: "1128136563201671221",
81-
...data,
82-
});
83-
} catch (err) {
84-
console.error(
85-
"[webhook/release] Failed to send Discord embed:",
86-
err,
87-
);
88-
return new Response("Internal server error", { status: 500 });
89-
}
71+
// Send embed
72+
try {
73+
await SendEmbed(env.DISCORD_WEBHOOK_RELEASE, {
74+
title: "🎁 | New Release",
75+
url: "https://github.com/pyclashbot/py-clash-bot/releases/latest",
76+
color: 0x03fc49,
77+
tagId: "1128136563201671221",
78+
...data,
79+
});
80+
} catch (err) {
81+
console.error(
82+
"[webhook/release] Failed to send Discord embed:",
83+
err,
84+
);
85+
return new Response("Internal server error", { status: 500 });
86+
}
9087

91-
console.log("[webhook/release] Embed sent successfully");
92-
return new Response("OK", { status: 200 });
93-
},
94-
},
88+
console.log("[webhook/release] Embed sent successfully");
89+
return new Response("OK", { status: 200 });
9590
},
9691
});
Lines changed: 71 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createFileRoute } from "@tanstack/react-router";
1+
import { createServerFileRoute } from "@tanstack/react-start/server";
22
import { z } from "zod";
33
import { SendEmbed } from "@/lib/discord";
44

@@ -7,90 +7,85 @@ const envSchema = z.object({
77
DISCORD_WEBHOOK_PRERELEASE: z.string().url(),
88
});
99

10-
export const Route = createFileRoute("/api/webhook/release_/prerelease")({
11-
// @ts-expect-error TanStack Start server handlers - types provided by Vite plugin at build time
12-
server: {
13-
handlers: {
14-
POST: async ({ request }: { request: Request }) => {
15-
// Validate environment configuration
16-
const envResult = envSchema.safeParse(process.env);
17-
if (!envResult.success) {
18-
const missing = envResult.error.issues
19-
.map((i) => i.path.join("."))
20-
.join(", ")
21-
console.error(
22-
`[webhook/prerelease] Environment configuration error: missing or invalid: ${missing}`,
23-
)
24-
return new Response("Internal server error", { status: 500 });
25-
}
26-
const env = envResult.data;
10+
export const ServerRoute = createServerFileRoute("/api/webhook/release_/prerelease").methods({
11+
POST: async ({ request }) => {
12+
// Validate environment configuration
13+
const envResult = envSchema.safeParse(process.env);
14+
if (!envResult.success) {
15+
const missing = envResult.error.issues
16+
.map((i) => i.path.join("."))
17+
.join(", ")
18+
console.error(
19+
`[webhook/prerelease] Environment configuration error: missing or invalid: ${missing}`,
20+
)
21+
return new Response("Internal server error", { status: 500 });
22+
}
23+
const env = envResult.data;
2724

28-
// Authorization
29-
const { searchParams } = new URL(request.url);
30-
const code = searchParams.get("code");
25+
// Authorization
26+
const { searchParams } = new URL(request.url);
27+
const code = searchParams.get("code");
3128

32-
if (!code) {
33-
console.warn(
34-
"[webhook/prerelease] Authorization failed: no code parameter provided",
35-
)
36-
return new Response("Unauthorized", { status: 401 });
37-
}
29+
if (!code) {
30+
console.warn(
31+
"[webhook/prerelease] Authorization failed: no code parameter provided",
32+
)
33+
return new Response("Unauthorized", { status: 401 });
34+
}
3835

39-
if (code !== env.API_ROUTE_SECRET) {
40-
console.warn(
41-
"[webhook/prerelease] Authorization failed: invalid code parameter",
42-
)
43-
return new Response("Unauthorized", { status: 401 });
44-
}
36+
if (code !== env.API_ROUTE_SECRET) {
37+
console.warn(
38+
"[webhook/prerelease] Authorization failed: invalid code parameter",
39+
)
40+
return new Response("Unauthorized", { status: 401 });
41+
}
4542

46-
// Parse body
47-
const reqBody = await request.text();
48-
if (reqBody === "") {
49-
console.warn("[webhook/prerelease] Bad request: empty body");
50-
return new Response("Bad request", { status: 400 });
51-
}
43+
// Parse body
44+
const reqBody = await request.text();
45+
if (reqBody === "") {
46+
console.warn("[webhook/prerelease] Bad request: empty body");
47+
return new Response("Bad request", { status: 400 });
48+
}
5249

53-
let data: {
54-
title?: string
55-
description?: string;
56-
url?: string
57-
timestamp?: Date;
58-
color?: number
59-
}
50+
let data: {
51+
title?: string;
52+
description?: string;
53+
url?: string;
54+
timestamp?: Date;
55+
color?: number;
56+
}
6057

61-
try {
62-
data = JSON.parse(reqBody);
63-
} catch {
64-
console.warn("[webhook/prerelease] Bad request: invalid JSON body");
65-
return new Response("Bad request", { status: 400 });
66-
}
58+
try {
59+
data = JSON.parse(reqBody);
60+
} catch {
61+
console.warn("[webhook/prerelease] Bad request: invalid JSON body");
62+
return new Response("Bad request", { status: 400 });
63+
}
6764

68-
console.log("[webhook/prerelease] Processing embed:", data);
65+
console.log("[webhook/prerelease] Processing embed:", data);
6966

70-
if (data.title) {
71-
data.title = `📦 | ${data.title}`;
72-
}
67+
if (data.title) {
68+
data.title = `📦 | ${data.title}`;
69+
}
7370

74-
// Send embed
75-
try {
76-
await SendEmbed(env.DISCORD_WEBHOOK_PRERELEASE, {
77-
title: "📦 | New Pre-Release",
78-
url: "https://github.com/pyclashbot/py-clash-bot/releases/latest",
79-
color: 0xfca503,
80-
tagId: "1128136612715450498",
81-
...data,
82-
})
83-
} catch (err) {
84-
console.error(
85-
"[webhook/prerelease] Failed to send Discord embed:",
86-
err,
87-
)
88-
return new Response("Internal server error", { status: 500 });
89-
}
71+
// Send embed
72+
try {
73+
await SendEmbed(env.DISCORD_WEBHOOK_PRERELEASE, {
74+
title: "📦 | New Pre-Release",
75+
url: "https://github.com/pyclashbot/py-clash-bot/releases/latest",
76+
color: 0xfca503,
77+
tagId: "1128136612715450498",
78+
...data,
79+
})
80+
} catch (err) {
81+
console.error(
82+
"[webhook/prerelease] Failed to send Discord embed:",
83+
err,
84+
)
85+
return new Response("Internal server error", { status: 500 });
86+
}
9087

91-
console.log("[webhook/prerelease] Embed sent successfully");
92-
return new Response("OK", { status: 200 });
93-
},
94-
},
88+
console.log("[webhook/prerelease] Embed sent successfully");
89+
return new Response("OK", { status: 200 });
9590
},
9691
});

0 commit comments

Comments
 (0)