Skip to the code

One order by its id

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" }

The app's other files

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);
}

What the tests check

  1. answers the order as JSON, with status 200
    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]);
    
  2. finds each order by the id in its params
    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]);
    
  3. answers 404 with a message for an order that doesn't exist
    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" });
    
  4. answers 400 for an id that isn't only digits
    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" });
    }
    
  5. checks the id before asking the database
    await GET(request("/api/orders/1e3"), { params: params({ id: "1e3" }) });
    await GET(request("/api/orders/abc"), { params: params({ id: "abc" }) });
    expect(lookups).toEqual([]);
    

Learn more

app/api/orders/[id]/route.tsSaved in this browser

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).