The order page in Leaf and Kettle's account area shows one order at a time, and asks for it by its number: GET /api/orders/1002. Write the GET handler in app/api/orders/[id]/route.ts.
The [id] folder makes that part of the URL a param. Next.js passes it in the handler's second argument as params, a promise of { id }, where id is always a string. The starter types that argument with RouteContext<"/api/orders/[id]">, the helper Next.js generates for every route.
An id is only digits, like 1002. For anything else, like abc, -3, 1.5, or 1e3, answer status 400 with { error: "Order id must be a whole number" }, without asking the database.
Otherwise look the order up with getOrder(id) from @/lib/orders, which takes a number. When there's no order with that id, answer status 404 with { error: "Order 999 not found" }, with the id in the message. When there is, answer the order as JSON, with status 200.
GET /api/orders/1002 → 200 { id: 1002, status: "shipped", items: [...], total: 8 }
GET /api/orders/999 → 404 { error: "Order 999 not found" }
GET /api/orders/abc → 400 { error: "Order id must be a whole number" }
lib/orders.ts, read-only. Import it as @/lib/orders.
export type Order = {
id: number;
status: "paid" | "shipped" | "delivered";
items: { tea: string; quantity: number }[];
total: number;
};
export const orders: Order[] = [
{ id: 1001, status: "delivered", items: [{ tea: "green", quantity: 2 }], total: 8 },
{ id: 1002, status: "shipped", items: [{ tea: "mint", quantity: 1 }, { tea: "oolong", quantity: 1 }], total: 8 },
{ id: 1003, status: "paid", items: [{ tea: "jasmine", quantity: 3 }], total: 15 },
];
/** Every id the database has been asked for, in order. */
export const lookups: number[] = [];
/** The order with this id, or undefined when there's none. */
export async function getOrder(id: number): Promise<Order | undefined> {
lookups.push(id);
return orders.find((order) => order.id === id);
}
const response = await GET(request("/api/orders/1002"), { params: params({ id: "1002" }) });
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toMatch("application/json");
expect(await response.json()).toEqual(orders[1]);
for (const order of orders) {
const response = await GET(request("/api/orders/" + order.id), { params: params({ id: String(order.id) }) });
expect(await response.json()).toEqual(order);
}
expect(lookups).toEqual([1001, 1002, 1003]);
const response = await GET(request("/api/orders/999"), { params: params({ id: "999" }) });
expect(response.status).toBe(404);
expect(await response.json()).toEqual({ error: "Order 999 not found" });
for (const id of ["abc", "-3", "1.5", "1e3", "12abc"]) {
const response = await GET(request("/api/orders/" + id), { params: params({ id }) });
expect(response.status).toBe(400);
expect(await response.json()).toEqual({ error: "Order id must be a whole number" });
}
await GET(request("/api/orders/1e3"), { params: params({ id: "1e3" }) });
await GET(request("/api/orders/abc"), { params: params({ id: "abc" }) });
expect(lookups).toEqual([]);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with One order by its id. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.