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 }] }
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));
}
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);
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);
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);
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");
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" });
}
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" });
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 Tea search API. 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.