-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmiddleware.ts
45 lines (38 loc) · 1.24 KB
/
middleware.ts
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
import { GET, POST } from '@frames.js/render/next';
import { NextRequest, NextResponse } from 'next/server';
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
const { method } = req;
if (pathname === '/api/frames') {
try {
let response: Response;
if (method === 'GET') {
response = await GET(req);
} else if (method === 'POST') {
response = await POST(req);
} else {
return new NextResponse(JSON.stringify({ error: 'Method Not Allowed' }), {
status: 405,
headers: { 'Content-Type': 'application/json' },
});
}
if (response.status === 200) {
const clonedResponse = response.clone();
const data = await clonedResponse.json();
} else {
const clonedResponse = response.clone();
const errorMessage = await clonedResponse.text();
}
return response;
} catch (error) {
return new NextResponse(JSON.stringify({ error: 'Internal Server Error' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
return NextResponse.next();
}
export const config = {
unstable_allowDynamic: ['/node_modules/@protobufjs/inquire/index.js'],
};