|
1 | 1 | import { NextRequest, NextResponse } from 'next/server'; |
2 | 2 | import { createLink } from '@/lib/actions'; |
3 | 3 |
|
| 4 | +const CORS_HEADERS = { |
| 5 | + 'Access-Control-Allow-Origin': 'https://flexurl.link', |
| 6 | + 'Access-Control-Allow-Methods': 'POST, OPTIONS', |
| 7 | + 'Access-Control-Allow-Headers': 'Content-Type', |
| 8 | +}; |
| 9 | + |
| 10 | +export async function OPTIONS() { |
| 11 | + return new NextResponse(null, { status: 204, headers: CORS_HEADERS }); |
| 12 | +} |
| 13 | + |
4 | 14 | export async function POST(req: NextRequest) { |
5 | 15 | try { |
6 | 16 | const body = await req.json(); |
7 | 17 | const { url, customId, expiresAt } = body; |
8 | 18 |
|
9 | 19 | if (!url || typeof url !== 'string') { |
10 | | - return NextResponse.json({ error: 'URL is required' }, { status: 400 }); |
| 20 | + return NextResponse.json({ error: 'URL is required' }, { status: 400, headers: CORS_HEADERS }); |
11 | 21 | } |
12 | 22 |
|
13 | 23 | const result = await createLink(url, customId, expiresAt); |
14 | 24 |
|
15 | 25 | if (result.error) { |
16 | | - return NextResponse.json({ error: result.error }, { status: 400 }); |
| 26 | + return NextResponse.json({ error: result.error }, { status: 400, headers: CORS_HEADERS }); |
17 | 27 | } |
18 | 28 |
|
19 | | - return NextResponse.json(result); |
| 29 | + return NextResponse.json(result, { headers: CORS_HEADERS }); |
20 | 30 | } catch (error) { |
21 | 31 | console.error('Create API error:', error); |
22 | | - return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); |
| 32 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500, headers: CORS_HEADERS }); |
23 | 33 | } |
24 | 34 | } |
0 commit comments