1
1
import { NextResponse } from 'next/server' ;
2
2
import axios from 'axios' ;
3
3
4
+ // Define allowed origins
5
+ const ALLOWED_ORIGINS = [
6
+ 'http://localhost:3000' ,
7
+ 'https://wallpaper-ai-navy.vercel.app'
8
+ ] ;
9
+
4
10
export async function POST ( request : Request ) {
11
+ // Check the origin of the request
12
+ const origin = request . headers . get ( 'origin' ) || '' ;
13
+ const isAllowedOrigin = ALLOWED_ORIGINS . includes ( origin ) ;
14
+
5
15
try {
6
16
const { prompt, n, size } = await request . json ( ) ;
7
17
@@ -15,6 +25,7 @@ export async function POST(request: Request) {
15
25
n,
16
26
size,
17
27
} ;
28
+
18
29
// Make the POST request to the image generation API
19
30
const response = await axios . post ( apiUrl , body , {
20
31
headers : {
@@ -23,16 +34,53 @@ export async function POST(request: Request) {
23
34
}
24
35
} ) ;
25
36
37
+ // Prepare the response with CORS headers
38
+ const responseHeaders = {
39
+ 'Access-Control-Allow-Origin' : isAllowedOrigin ? origin : '' ,
40
+ 'Access-Control-Allow-Methods' : 'POST, OPTIONS' ,
41
+ 'Access-Control-Allow-Headers' : 'Content-Type, Authorization' ,
42
+ } ;
43
+
26
44
// Return the image URL from the API response
27
- return NextResponse . json ( { images : response . data . data . map ( ( img : any ) => img . url ) } ) ;
45
+ return NextResponse . json (
46
+ { images : response . data . data . map ( ( img : any ) => img . url ) } ,
47
+ { headers : responseHeaders }
48
+ ) ;
28
49
} catch ( error ) {
29
50
console . error ( 'Error generating images:' , error ) ;
30
- return NextResponse . json ( { error : 'Failed to generate images' } , { status : 500 } ) ;
51
+
52
+ // Include CORS headers in error response as well
53
+ const responseHeaders = {
54
+ 'Access-Control-Allow-Origin' : isAllowedOrigin ? origin : '' ,
55
+ 'Access-Control-Allow-Methods' : 'POST, OPTIONS' ,
56
+ 'Access-Control-Allow-Headers' : 'Content-Type, Authorization' ,
57
+ } ;
58
+
59
+ return NextResponse . json (
60
+ { error : 'Failed to generate images' } ,
61
+ {
62
+ status : 500 ,
63
+ headers : responseHeaders
64
+ }
65
+ ) ;
31
66
}
32
67
}
33
68
69
+ // Handle OPTIONS requests for CORS preflight
70
+ export async function OPTIONS ( request : Request ) {
71
+ const origin = request . headers . get ( 'origin' ) || '' ;
72
+ const isAllowedOrigin = ALLOWED_ORIGINS . includes ( origin ) ;
73
+
74
+ // Prepare CORS headers for preflight request
75
+ const responseHeaders = {
76
+ 'Access-Control-Allow-Origin' : isAllowedOrigin ? origin : '' ,
77
+ 'Access-Control-Allow-Methods' : 'POST, OPTIONS' ,
78
+ 'Access-Control-Allow-Headers' : 'Content-Type, Authorization' ,
79
+ 'Access-Control-Max-Age' : '86400' , // Cache preflight response for 24 hours
80
+ } ;
34
81
35
- // // Validate environment variables
36
- // const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
37
- // const key = process.env.AZURE_OPENAI_KEY;
38
- // const apiVersion = '2024-02-01';
82
+ return new NextResponse ( null , {
83
+ status : 204 ,
84
+ headers : responseHeaders
85
+ } ) ;
86
+ }
0 commit comments