Nextjs - Adding a index.html page to static export

33 Views Asked by At

I have a code for this API to Replicate AI models. The problem is that some modules a long time to get the answer from API, the longest I had was 200s. But of course the request crashes before the answer.

Where in the code I can implement a long api timeout?

Thanks for the assistance! :)

import Replicate from "replicate";
import { auth } from "@clerk/nextjs";
import { NextResponse } from "next/server";

import { incrementApiLimit, checkApiLimit } from "@/lib/api-limit";
import { checkSubscription } from "@/lib/subscription";

const replicate = new Replicate({
 auth: process.env.REPLICATE_API_TOKEN!,
});

    export async function POST(
      req: Request
   ) {
     try {
   const { userId } = auth();
  const body = await req.json();
  const { prompt  } = body;

if (!userId) {
  return new NextResponse("Unauthorized", { status: 401 });
}

if (!prompt) {
  return new NextResponse("Prompt is required", { status: 400 });
}

const freeTrial = await checkApiLimit();
const isPro = await checkSubscription();

if (!freeTrial && !isPro) {
  return new NextResponse("Free trial has expired. Please upgrade to pro.", { status: 403 });
}

const response = await replicate.run(
  "riffusion/riffusion:8cf61ea6c56afd61d8f5b9ffd14d7c216c0a93844ce2d82ac1c9ecc9c7f24e05",
  {
    input: {
      prompt_a: prompt
    }
  }
);

if (!isPro) {
  await incrementApiLimit();
}

return NextResponse.json(response);
 } catch (error) {
    console.log('[MUSIC_ERROR]', error);
  return new NextResponse("Internal Error", { status: 500 });
}
};
0

There are 0 best solutions below