nextjs-test/app/json-placeholder/[id]/route.ts

19 lines
441 B
TypeScript

import { NextRequest, NextResponse } from "next/server";
export async function GET(
request: NextRequest,
{ params: { id } }: { params: { id: string } },
) {
if (!id) throw new Error("Where is id...");
const res = await fetch(`https://jsonplaceholder.typicode.com/todos/${id}`);
if (!res.ok) {
return new NextResponse("{}");
}
const body = await res.json();
return new NextResponse(JSON.stringify(body, null, 2));
}