Skip to the code

A price list to download as CSV

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

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.5 },
  { slug: "breakfast", name: "Breakfast blend, strong", price: 4.25 },
  { slug: "lapsang", name: 'Lapsang "smoky" souchong', price: 6 },
  { slug: "chamomile", name: "Chamomile", price: 3 },
];

What the tests check

  1. answers status 200 as a CSV file
    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");
    
  2. asks the browser to save it as leaf-and-kettle-prices.csv
    const response = await GET(request("/price-list.csv"));
    expect(response.headers.get("content-disposition")).toBe('attachment; filename="leaf-and-kettle-prices.csv"');
    
  3. starts with the header row, then one line per tea in the shop's order
    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));
    
  4. writes every price with two decimals
    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"]);
    
  5. quotes a name with a comma or a quote, doubling the quotes inside
    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");
    
  6. ends every line with \r\n, the last one too
    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]/);
    

Learn more

app/price-list.csv/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).