fix: move onParse to root and support trailing slash in webhook route

This commit is contained in:
Your Name committed 2026-06-12 23:37:18 -04:00
1 parent ee1c7820c3
commit 32e9fd5ac6
2 files changed
+12 -9

No files matched your search

+5
View File
@@ -29,6 +29,11 @@ const CORS_ORIGIN = process.env.CORS_ORIGIN || "http://localhost:5173";
const JWT_SECRET = process.env.JWT_SECRET || "homaroma_secret_2026_scent"; const JWT_SECRET = process.env.JWT_SECRET || "homaroma_secret_2026_scent";
const app = new Elysia() const app = new Elysia()
.onParse(async ({ request }) => {
if (request.url.includes('/api/line/webhook')) {
return await request.text();
}
})
.use( .use(
cors({ cors({
origin: [ origin: [
+7 -9
View File
@@ -3,13 +3,7 @@ import * as line from '@line/bot-sdk';
import { lineConfig, lineClient } from '../lib/lineClient'; import { lineConfig, lineClient } from '../lib/lineClient';
import crypto from 'crypto'; import crypto from 'crypto';
export const lineRoutes = new Elysia({ prefix: '/line' }) const webhookHandler = async ({ request, body, set }: { request: Request, body: any, set: any }) => {
.onParse(async ({ request, contentType }) => {
if (contentType?.includes('application/json')) {
return await request.text();
}
})
.post('/webhook', async ({ request, body, set }) => {
// 1. Verify Signature // 1. Verify Signature
const signature = request.headers.get('x-line-signature'); const signature = request.headers.get('x-line-signature');
if (!signature) { if (!signature) {
@@ -17,7 +11,7 @@ export const lineRoutes = new Elysia({ prefix: '/line' })
return { error: 'Missing Signature' }; return { error: 'Missing Signature' };
} }
// In our onParse hook, we intercepted the body and returned it as a raw text string. // In our global onParse hook, we intercepted the body and returned it as a raw text string.
const rawBody = typeof body === 'string' ? body : JSON.stringify(body); const rawBody = typeof body === 'string' ? body : JSON.stringify(body);
if (!rawBody) { if (!rawBody) {
@@ -65,4 +59,8 @@ export const lineRoutes = new Elysia({ prefix: '/line' })
set.status = 500; set.status = 500;
return { error: 'Webhook processing failed' }; return { error: 'Webhook processing failed' };
} }
}); };
export const lineRoutes = new Elysia({ prefix: '/line' })
.post('/webhook', webhookHandler)
.post('/webhook/', webhookHandler);