Next.js Discord

Discord Forum

Api route pattern question

Unanswered
Champagne D’Argent posted this in #help-forum
Open in Discord
Champagne D’ArgentOP
Hi I got a question does this pattern not work anymore with the new nextjs api route changes?

export const config = {
    api: {
        bodyParser: false,
    },
};

const { R2_ACCOUNT_ID, R2_ACCESS_KEY_ID, R2_SECRET_KEY_ID, R2_BUCKET_NAME } =
    process.env;

if (!R2_ACCOUNT_ID) throw new Error("R2_ACCOUNT_ID is not set");
if (!R2_ACCESS_KEY_ID) throw new Error("R2_ACCESS_KEY_ID is not set");
if (!R2_SECRET_KEY_ID) throw new Error("R2_SECRET_KEY_ID is not set");
if (!R2_BUCKET_NAME) throw new Error("R2_BUCKET_NAME is not set");

const R2 = new S3Client({
    region: "auto",
    endpoint: `https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
    credentials: {
        accessKeyId: R2_ACCESS_KEY_ID,
        secretAccessKey: R2_SECRET_KEY_ID,
    },
});

export default async function handler(
    req: NextRequest,
    res: NextResponse
) {
    const { endpoint } = await req.json();

    console.log("handler endpoint", endpoint);

    switch (endpoint) {
        case "create-multipart-upload":
            return createMultipartUpload(req, res);
        case "prepare-upload-parts":
            return prepareUploadParts(req, res);
        case "complete-multipart-upload":
            return completeMultipartUpload(req, res);
        case "list-parts":
            return listParts(req, res);
        case "abort-multipart-upload":
            return abortMultipartUpload(req, res);
    }

    return Response.json({ status: 404 });
}

export async function createMultipartUpload(
    req: NextRequest,
    res: NextResponse
) {
...

// and for the rest of the functions



In my frontend I am calling it like this:

const fetchUploadApiEndpoint = async (endpoint: string, data: any) => {
  const res = await fetch(`/api/multipart-upload/${endpoint}`, ...

2 Replies

Champagne D’ArgentOP
Would I need to create a separate api route for each operation?
Champagne D’ArgentOP
Do I really need to do this? 😕