Cafés that stock Leaf and Kettle's teas download its price list into their spreadsheets. Write the GET handler in app/price-list.csv/route.ts: the folder's name, dot and all, makes the URL /price-list.csv.
Answer status 200 with the teas from @/lib/teas as CSV, and two headers: Content-Type: text/csv; charset=utf-8, and Content-Disposition: attachment; filename="leaf-and-kettle-prices.csv", so the browser saves the file under that name instead of showing it.
The first line is the header row, slug,name,price. Then comes one line per tea, in the shop's order, with its price written with two decimals (3.50, 6.00). Every line ends with \r\n, the last one too.
A field that holds a comma, a double quote, or a line break is wrapped in double quotes, and each double quote inside it is written twice. Any other field is written as it is.
slug,name,price
green,Green tea,4.00
mint,Mint tea,3.50
breakfast,"Breakfast blend, strong",4.25
lapsang,"Lapsang ""smoky"" souchong",6.00
chamomile,Chamomile,3.00
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.5 },
{ slug: "breakfast", name: "Breakfast blend, strong", price: 4.25 },
{ slug: "lapsang", name: 'Lapsang "smoky" souchong', price: 6 },
{ slug: "chamomile", name: "Chamomile", price: 3 },
];
const response = await GET(request("/price-list.csv"));
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("text/csv; charset=utf-8");
const response = await GET(request("/price-list.csv"));
expect(response.headers.get("content-disposition")).toBe('attachment; filename="leaf-and-kettle-prices.csv"');
const lines = (await (await GET(request("/price-list.csv"))).text()).split("\r\n");
expect(lines[0]).toBe("slug,name,price");
expect(lines.slice(1, 3)).toEqual(["green,Green tea,4.00", "mint,Mint tea,3.50"]);
expect(lines.length).toBe(teas.length + 2);
expect(lines.slice(1, -1).map((line) => line.slice(0, line.indexOf(",")))).toEqual(teas.map((tea) => tea.slug));
const lines = (await (await GET(request("/price-list.csv"))).text()).split("\r\n");
expect(lines.slice(1, -1).map((line) => line.slice(line.lastIndexOf(",") + 1))).toEqual(["4.00", "3.50", "4.25", "6.00", "3.00"]);
const lines = (await (await GET(request("/price-list.csv"))).text()).split("\r\n");
expect(lines).toContain('breakfast,"Breakfast blend, strong",4.25');
expect(lines).toContain('lapsang,"Lapsang ""smoky"" souchong",6.00');
expect(lines).toContain("chamomile,Chamomile,3.00");
const text = await (await GET(request("/price-list.csv"))).text();
expect(text.endsWith("\r\n")).toBe(true);
expect(text.replaceAll("\r\n", "")).not.toMatch(/[\r\n]/);
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 A price list to download as CSV. 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.