Skip to the code

Tea search API

Leaf and Kettle's mobile app needs the tea list as JSON. Write app/api/teas/route.ts, whose GET handler answers GET /api/teas with { count, teas }: how many teas matched, and the teas themselves, each as { slug, name, price }, with status 200.

Three optional search params narrow and order the list, read from request.nextUrl.searchParams:

q keeps the teas whose name has the text in it, ignoring case and spaces at the ends. findTeas(text) in lib/teas.ts does exactly that; import it as @/lib/teas.

maxPrice keeps the teas that cost that much or less. It must be a positive number: anything else, like cheap, 0, or -2, is a bad request, so answer status 400 with { error: "maxPrice must be a positive number" }.

sort is name (A to Z) or price (cheapest first, and A to Z among teas with the same price). Without it, keep the shop's order. Any other value gets status 400 with { error: "sort must be name or price" }.

GET /api/teas?q=green&maxPrice=4
→ 200 { count: 1, teas: [{ slug: "green", name: "Green tea", price: 4 }] }

The app's other files

lib/teas.ts, read-only. Import it as @/lib/teas.

export type Tea = { slug: string; name: string; price: number };
 
export const teas: Tea[] = [
  { slug: "green", name: "Green tea", price: 4 },
  { slug: "mint", name: "Mint tea", price: 3 },
  { slug: "oolong", name: "Oolong tea", price: 5 },
  { slug: "jasmine", name: "Jasmine green tea", price: 5 },
  { slug: "black", name: "Black tea", price: 4 },
  { slug: "chamomile", name: "Chamomile", price: 3 },
];
 
/** The teas whose name has `text` in it, ignoring case and spaces at the ends: every tea for "". */
export function findTeas(text: string): Tea[] {
  const query = text.trim().toLowerCase();
  return teas.filter((tea) => tea.name.toLowerCase().includes(query));
}

What the tests check

  1. answers every tea as JSON, with status 200
    const response = await GET(request("/api/teas"));
    expect(response.status).toBe(200);
    expect(response.headers.get("content-type")).toMatch("application/json");
    const body = await response.json();
    expect(body.count).toBe(6);
    expect(body.teas).toEqual(teas);
    
  2. q keeps the teas whose name has the text, ignoring case
    const body = await (await GET(request("/api/teas?q=%20GREEN%20"))).json();
    expect(body.teas.map((tea) => tea.slug)).toEqual(["green", "jasmine"]);
    expect(body.count).toBe(2);
    
  3. maxPrice keeps the teas that cost that much or less, with q too
    const cheap = await (await GET(request("/api/teas?maxPrice=3"))).json();
    expect(cheap.teas.map((tea) => tea.slug)).toEqual(["mint", "chamomile"]);
    const both = await (await GET(request("/api/teas?q=tea&maxPrice=4.5"))).json();
    expect(both.teas.map((tea) => tea.slug)).toEqual(["green", "mint", "black"]);
    expect(both.count).toBe(3);
    
  4. sort orders by name, or by price and then name
    const byName = await (await GET(request("/api/teas?sort=name"))).json();
    expect(byName.teas.map((tea) => tea.name)).toEqual(["Black tea", "Chamomile", "Green tea", "Jasmine green tea", "Mint tea", "Oolong tea"]);
    const byPrice = await (await GET(request("/api/teas?sort=price&q=tea"))).json();
    expect(byPrice.teas.map((tea) => tea.slug)).toEqual(["mint", "black", "green", "jasmine", "oolong"]);
    expect(teas[0].slug).toBe("green");
    
  5. a maxPrice that isn't a positive number is a 400
    for (const value of ["cheap", "0", "-2", ""]) {
      const response = await GET(request("/api/teas?maxPrice=" + value));
      expect(response.status).toBe(400);
      expect(await response.json()).toEqual({ error: "maxPrice must be a positive number" });
    }
    
  6. an unknown sort is a 400
    const response = await GET(request("/api/teas?sort=popular"));
    expect(response.status).toBe(400);
    expect(await response.json()).toEqual({ error: "sort must be name or price" });
    

Learn more

app/api/teas/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).