Project: The Millbrook Birders' Import

You’ve now built every piece the Millbrook Birders’ import needs, one slice at a time: guards for a sighting, typed lookups, a check at each boundary, and schemas that report what failed. The club’s real script hasn’t changed, though. It still reads a whole month’s export, asserts its type, and trusts every reply from the species API.

In this project, you’ll rewrite that script so last spring’s broken export can’t put “undefined” on the year list again. You’ll build it in three graded steps and then explain it to the mentor. Start by running the old import on the export that caused the trouble.

Run the Old Import

The club’s script needs its real inputs: an export file on disk and the species API. The course’s Sample Code (.zip) download has an import-project folder with the club’s old imports.ts and an exports folder holding two months of sightings. Copy imports.ts and the exports folder into your millbrook-birders folder, next to package.json.

Your project already has what the import needs. Zod is installed, @types/node gives the checker Node.js’s types, and the .env file you created while checking the API’s address holds that address. If you’ve deleted .env since, then create it again in the millbrook-birders folder:

Language: Text Filename: .env
SPECIES_API=http://localhost:3000/species

Open imports.ts. It’s the script that ran last spring, and it uses only JavaScript and TypeScript you’ve met in this course:

Language: TypeScript Filename: imports.ts
 1import { readFile } from "node:fs/promises";
 2
 3type Sighting = {
 4  species: string;
 5  count: number;
 6  date: string;
 7  site: string;
 8  observer: string;
 9};
10
11type SpeciesResponse = {
12  commonName: string;
13  scientificName: string;
14  status: string;
15};
16
17const API = process.env.SPECIES_API;
18const file = process.argv[2] ?? "exports/april.json";
19
20async function lookUp(name: string) {
21  const query = encodeURIComponent(name);
22  const response = await fetch(`${API}?name=${query}`);
23  return (await response.json()) as SpeciesResponse;
24}
25
26const text = await readFile(file, "utf8");
27const sightings = JSON.parse(text) as Sighting[];
28
29const yearList = new Set<string>();
30for (const sighting of sightings) {
31  const species = await lookUp(sighting.species);
32  yearList.add(species.commonName);
33}
34
35console.log(`Year list: ${yearList.size} species`);
36for (const name of yearList) {
37  console.log(`- ${name}`);
38}

Line 18 takes the export’s filename from the first command-line argument after the script’s name and falls back to April’s export when there isn’t one. Line 26 reads the file’s text with readFile() from Node.js’s fs module. If you’d like a refresher on reading files in Node.js, the RealJS course Timers and Files covers it.

From there, the script parses the text, looks up each sighting’s species in the API, and adds the API’s name for the bird to the year list, a Set<string>, so a species seen twice is listed once. In the club’s real script, the year list lasts all year. Here, it’s built from one export, which is enough to see what goes onto it.

The script has two assertions, as SpeciesResponse on line 23 and as Sighting[] on line 27. Check the project, and the checker accepts both:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

If npm run check reports errors in other files, look at those first. It checks every .ts file in the folder, so a file you left half-finished in an earlier lesson shows up here too. Fix it or move it out of the folder, so that the check reports only on the import.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.