feat: support cors
This commit is contained in:
parent
6ac19b5120
commit
6a9de6d059
@ -1,6 +1,9 @@
|
|||||||
import { NextResponse, NextRequest } from "next/server";
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
import { sunoApi } from "@/lib/SunoApi";
|
import { sunoApi } from "@/lib/SunoApi";
|
||||||
|
import { corsHeaders } from "@/lib/utils";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
try {
|
try {
|
||||||
@ -9,7 +12,10 @@ export async function POST(req: NextRequest) {
|
|||||||
if (!prompt || !tags || !title) {
|
if (!prompt || !tags || !title) {
|
||||||
return new NextResponse(JSON.stringify({ error: 'Prompt, tags, and title are required' }), {
|
return new NextResponse(JSON.stringify({ error: 'Prompt, tags, and title are required' }), {
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const audioInfo = await (await sunoApi).custom_generate(
|
const audioInfo = await (await sunoApi).custom_generate(
|
||||||
@ -19,25 +25,44 @@ export async function POST(req: NextRequest) {
|
|||||||
);
|
);
|
||||||
return new NextResponse(JSON.stringify(audioInfo), {
|
return new NextResponse(JSON.stringify(audioInfo), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Error generating custom audio:', error.response.data);
|
console.error('Error generating custom audio:', error.response.data);
|
||||||
if (error.response.status === 402) {
|
if (error.response.status === 402) {
|
||||||
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
|
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
|
||||||
status: 402,
|
status: 402,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
|
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return new NextResponse('Method Not Allowed', {
|
return new NextResponse('Method Not Allowed', {
|
||||||
headers: { Allow: 'POST' },
|
headers: {
|
||||||
|
Allow: 'POST',
|
||||||
|
...corsHeaders
|
||||||
|
},
|
||||||
status: 405
|
status: 405
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function OPTIONS(request: Request) {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 200,
|
||||||
|
headers: corsHeaders
|
||||||
|
});
|
||||||
}
|
}
|
@ -1,6 +1,9 @@
|
|||||||
import { NextResponse, NextRequest } from "next/server";
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
import { sunoApi } from "@/lib/SunoApi";
|
import { sunoApi } from "@/lib/SunoApi";
|
||||||
|
import { corsHeaders } from "@/lib/utils";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
try {
|
try {
|
||||||
@ -10,7 +13,10 @@ export async function POST(req: NextRequest) {
|
|||||||
if (!prompt) {
|
if (!prompt) {
|
||||||
return new NextResponse(JSON.stringify({ error: 'Prompt is required' }), {
|
return new NextResponse(JSON.stringify({ error: 'Prompt is required' }), {
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,25 +24,45 @@ export async function POST(req: NextRequest) {
|
|||||||
|
|
||||||
return new NextResponse(JSON.stringify(audioInfo), {
|
return new NextResponse(JSON.stringify(audioInfo), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Error generating custom audio:', JSON.stringify(error.response.data));
|
console.error('Error generating custom audio:', JSON.stringify(error.response.data));
|
||||||
if (error.response.status === 402) {
|
if (error.response.status === 402) {
|
||||||
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
|
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
|
||||||
status: 402,
|
status: 402,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
|
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return new NextResponse('Method Not Allowed', {
|
return new NextResponse('Method Not Allowed', {
|
||||||
headers: { Allow: 'POST' },
|
headers: {
|
||||||
|
Allow: 'POST',
|
||||||
|
...corsHeaders
|
||||||
|
},
|
||||||
status: 405
|
status: 405
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export async function OPTIONS(request: Request) {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 200,
|
||||||
|
headers: corsHeaders
|
||||||
|
});
|
||||||
}
|
}
|
@ -1,6 +1,9 @@
|
|||||||
import { NextResponse, NextRequest } from "next/server";
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
import { sunoApi } from "@/lib/SunoApi";
|
import { sunoApi } from "@/lib/SunoApi";
|
||||||
|
import { corsHeaders } from "@/lib/utils";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
try {
|
try {
|
||||||
@ -10,7 +13,10 @@ export async function POST(req: NextRequest) {
|
|||||||
if (!prompt) {
|
if (!prompt) {
|
||||||
return new NextResponse(JSON.stringify({ error: 'Prompt is required' }), {
|
return new NextResponse(JSON.stringify({ error: 'Prompt is required' }), {
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,25 +24,44 @@ export async function POST(req: NextRequest) {
|
|||||||
|
|
||||||
return new NextResponse(JSON.stringify(lyrics), {
|
return new NextResponse(JSON.stringify(lyrics), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Error generating lyrics:', JSON.stringify(error.response.data));
|
console.error('Error generating lyrics:', JSON.stringify(error.response.data));
|
||||||
if (error.response.status === 402) {
|
if (error.response.status === 402) {
|
||||||
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
|
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
|
||||||
status: 402,
|
status: 402,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
|
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return new NextResponse('Method Not Allowed', {
|
return new NextResponse('Method Not Allowed', {
|
||||||
headers: { Allow: 'POST' },
|
headers: {
|
||||||
|
Allow: 'POST',
|
||||||
|
...corsHeaders
|
||||||
|
},
|
||||||
status: 405
|
status: 405
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function OPTIONS(request: Request) {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 200,
|
||||||
|
headers: corsHeaders
|
||||||
|
});
|
||||||
}
|
}
|
@ -1,5 +1,7 @@
|
|||||||
import { NextResponse, NextRequest } from "next/server";
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
import { sunoApi } from "@/lib/SunoApi";
|
import { sunoApi } from "@/lib/SunoApi";
|
||||||
|
import { corsHeaders } from "@/lib/utils";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
@ -17,20 +19,36 @@ export async function GET(req: NextRequest) {
|
|||||||
|
|
||||||
return new NextResponse(JSON.stringify(audioInfo), {
|
return new NextResponse(JSON.stringify(audioInfo), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching audio:', error);
|
console.error('Error fetching audio:', error);
|
||||||
|
|
||||||
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
|
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return new NextResponse('Method Not Allowed', {
|
return new NextResponse('Method Not Allowed', {
|
||||||
headers: { Allow: 'GET' },
|
headers: {
|
||||||
|
Allow: 'GET',
|
||||||
|
...corsHeaders
|
||||||
|
},
|
||||||
status: 405
|
status: 405
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function OPTIONS(request: Request) {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 200,
|
||||||
|
headers: corsHeaders
|
||||||
|
});
|
||||||
}
|
}
|
@ -1,6 +1,9 @@
|
|||||||
import { NextResponse, NextRequest } from "next/server";
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
import { sunoApi } from "@/lib/SunoApi";
|
import { sunoApi } from "@/lib/SunoApi";
|
||||||
|
import { corsHeaders } from "@/lib/utils";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
try {
|
try {
|
||||||
@ -10,20 +13,36 @@ export async function GET(req: NextRequest) {
|
|||||||
|
|
||||||
return new NextResponse(JSON.stringify(limit), {
|
return new NextResponse(JSON.stringify(limit), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching limit:', error);
|
console.error('Error fetching limit:', error);
|
||||||
|
|
||||||
return new NextResponse(JSON.stringify({ error: 'Internal server error. ' + error }), {
|
return new NextResponse(JSON.stringify({ error: 'Internal server error. ' + error }), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...corsHeaders
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return new NextResponse('Method Not Allowed', {
|
return new NextResponse('Method Not Allowed', {
|
||||||
headers: { Allow: 'GET' },
|
headers: {
|
||||||
|
Allow: 'GET',
|
||||||
|
...corsHeaders
|
||||||
|
},
|
||||||
status: 405
|
status: 405
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function OPTIONS(request: Request) {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 200,
|
||||||
|
headers: corsHeaders
|
||||||
|
});
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
import { NextResponse, NextRequest } from "next/server";
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
import { sunoApi } from "@/lib/SunoApi";
|
import { sunoApi } from "@/lib/SunoApi";
|
||||||
|
import { corsHeaders } from "@/lib/utils";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
@ -25,9 +26,7 @@ export async function POST(req: NextRequest) {
|
|||||||
status: 400,
|
status: 400,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Access-Control-Allow-Origin': '*',
|
...corsHeaders
|
||||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
|
||||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -40,11 +39,7 @@ export async function POST(req: NextRequest) {
|
|||||||
|
|
||||||
return new NextResponse(data, {
|
return new NextResponse(data, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: corsHeaders
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
|
||||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Error generating audio:', JSON.stringify(error.response.data));
|
console.error('Error generating audio:', JSON.stringify(error.response.data));
|
||||||
@ -52,9 +47,7 @@ export async function POST(req: NextRequest) {
|
|||||||
status: 500,
|
status: 500,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Access-Control-Allow-Origin': '*',
|
...corsHeaders
|
||||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
|
||||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -63,10 +56,6 @@ export async function POST(req: NextRequest) {
|
|||||||
export async function OPTIONS(request: Request) {
|
export async function OPTIONS(request: Request) {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: corsHeaders
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
|
||||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -18,4 +18,11 @@ export const sleep = (x: number, y?: number): Promise<void> => {
|
|||||||
logger.info(`Sleeping for ${timeout / 1000} seconds`);
|
logger.info(`Sleeping for ${timeout / 1000} seconds`);
|
||||||
|
|
||||||
return new Promise(resolve => setTimeout(resolve, timeout));
|
return new Promise(resolve => setTimeout(resolve, timeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const corsHeaders = {
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
||||||
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user