Project: The Riverside League's API Client

In the last exercise, you derived the registration form’s types from Player. Those types were the final piece of the client to practice on its own. The league’s volunteers still run three old scripts, though, for the fixture emails, the league table, and the registration import. Each one starts with its own hand-typed copy of the league’s types.

In this project, you’ll replace those copies with one typed client in its own module, built in three graded steps. You’ll watch the checker find bugs that each script’s own copy hid. You’ll finish with a project where a type changes in one place and npm run check points at every script that must follow. Start with the scripts as the volunteers left them.

Add the Three Old Scripts

The scripts are small, but they’re based on the code the league really runs every week, so they’re what the client has to serve. Keep them apart from the practice files you made in the lessons by giving the project its own folder. In a terminal, in your riverside-league folder, create it:

Language: Shell
$ mkdir league

Your lesson files keep their types.ts. The league folder will get a module of its own, client.ts, and nothing in the folder imports from outside it.

The fixture email script tells each home team’s coach when their match kicks off. Create league/fixture-emails.ts:

Language: TypeScript Filename: league/fixture-emails.ts
 1type Team = {
 2  id: number;
 3  name: string;
 4  ageGroup: "U8" | "U10" | "U12";
 5  manager: { name: string; email: string };
 6};
 7
 8type Fixture = {
 9  homeTeamId: number;
10  awayTeamId: number;
11  kickoff: string;
12  pitch: string;
13};
14
15const API = "http://localhost:3000";
16
17async function getJson(path: string): Promise<any> {
18  const response = await fetch(API + path);
19  return response.json();
20}
21
22const teams: Team[] = await getJson("/teams");
23const fixtures: Fixture[] = await getJson("/fixtures");
24
25for (const fixture of fixtures) {
26  const home = teams.find((team) => team.id === fixture.homeTeamId);
27  if (home) {
28    console.log(`To ${home.manager.email}: ${fixture.kickoff}`);
29  }
30}

Every script pastes in the same getJson() helper, which returns Promise<any>. The annotation on const teams: Team[] then tells the checker to treat that any as the script’s copy of Team, without checking it. When the script runs, teams is whatever the server sent. Keep that in mind as you add the other two.

The league table script picks out the U10 teams for this season’s table. Create league/league-table.ts:

Language: TypeScript Filename: league/league-table.ts
 1type Team = {
 2  id: number;
 3  name: string;
 4  ageGroup: number;
 5  coach: { name: string; email: string; phone: string };
 6};
 7
 8const API = "http://localhost:3000";
 9
10async function getJson(path: string): Promise<any> {
11  const response = await fetch(API + path);
12  return response.json();
13}
14
15const teams: Team[] = await getJson("/teams");
16const u10 = teams.filter((team) => team.ageGroup === 10);
17
18console.log(`U10 table: ${u10.length} teams`);
19for (const team of u10) {
20  console.log(team.name);
21}

The registration import checks new sign-ups against the players already registered, so two children on one team don’t get the same shirt number. The two sign-ups are typed in, so this is a toy version. The real import reads them from the registration form’s export. Create league/registration-import.ts:

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.