Skip to content

Commit e55a9ca

Browse files
author
Allan Pope
authored
feat: expose types in package
* remove unneeded file * hard code a types file and breakup types data * Fix broken types * combine into one file and improve conference types * Add team node to conference node export * Add divison node type * Add franchise node * wip refactor and split up types * reafactor roster types and remove explict types * simplify * run eslint on all files except generated ones * fix so it lints on tests * fix linting on tests * ignore lib compiled folder" * update typescript * use seperate config for build to avoid compiling the tests * use correct roster name * Fix up linking and types for position * fix up tests
1 parent e71fdc3 commit e55a9ca

32 files changed

+288
-190
lines changed

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
lib/
3+
node_modules/

index.js

-1
This file was deleted.

jest.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
preset: "ts-jest",
3-
testEnvironment: "node",
4-
}
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
};

package-lock.json

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
"name": "gatsby-source-nhl",
33
"version": "0.0.0",
44
"description": "A Gatsby source plugin to load resources from the NHL API.",
5-
"main": "index.js",
5+
"main": "gatsby-node.js",
6+
"types": "lib/types.d.ts",
67
"scripts": {
78
"test": "jest",
8-
"build": "tsc",
9+
"test:watch": "jest --watch",
10+
"build": "tsc -p ./tsconfig.build.json",
911
"watch": "tsc --watch",
10-
"lint": "tsc --noEmit && eslint src/**/*.ts"
12+
"lint": "tsc --noEmit && eslint ."
1113
},
1214
"files": [
1315
"/lib",
@@ -31,15 +33,15 @@
3133
"slugify": "^1.3.5"
3234
},
3335
"devDependencies": {
34-
"@typescript-eslint/eslint-plugin": "^2.6.1",
35-
"@typescript-eslint/parser": "^2.6.1",
36+
"@typescript-eslint/eslint-plugin": "^2.12.0",
37+
"@typescript-eslint/parser": "^2.12.0",
3638
"eslint": "^6.6.0",
3739
"eslint-config-prettier": "^6.5.0",
3840
"eslint-plugin-prettier": "^3.1.1",
3941
"gatsby": "^2.17.15",
4042
"jest": "^24.9.0",
4143
"prettier": "^1.19.1",
4244
"ts-jest": "^24.1.0",
43-
"typescript": "^3.6.3"
45+
"typescript": "^3.7.3"
4446
}
4547
}

src/createSchemaCustomization.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const createSchemaCustomization = ({ actions }: NodePluginArgs) => {
44
const { createTypes } = actions;
55

66
createTypes(`
7-
type NHLRosterItem implements Node {
7+
type NHLRoster implements Node {
88
player: NHLPlayer @link,
9-
position: NHLPosition @link(by: "name"),
9+
position: NHLPosition @link,
1010
team: NHLTeam @link
1111
}
1212
@@ -15,7 +15,7 @@ const createSchemaCustomization = ({ actions }: NodePluginArgs) => {
1515
}
1616
1717
type NHLPlayer implements Node {
18-
position: NHLPosition @link(by: "name")
18+
position: NHLPosition @link
1919
team: NHLTeam @link
2020
}
2121

src/data/nhl-api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fetch from 'node-fetch';
22

3-
import { Team } from '../types/team';
3+
import { TeamData } from '../types';
44

5-
export const getTeamsData = async (): Promise<Team[]> => {
5+
export const getTeamsData = async (): Promise<TeamData[]> => {
66
try {
77
const response = await fetch(
88
'https://statsapi.web.nhl.com/api/v1/teams?expand=team.roster',

src/nodes/build-conference-nodes.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { NodePluginArgs, NodeInput } from 'gatsby';
1+
import { NodePluginArgs } from 'gatsby';
22
import slugify from 'slugify';
33
import { groupBy } from 'lodash';
44

5-
import { Team } from '../types/team';
5+
import { TeamData, ConferenceNode } from '../types';
66

77
const buildConferenceNodes = (
8-
teams: Team[],
8+
teams: TeamData[],
99
{ createNodeId, createContentDigest }: NodePluginArgs,
10-
): Array<NodeInput> => {
10+
): ConferenceNode[] => {
1111
const teamsGroupedByConference = groupBy(teams, 'conference.name');
1212
const nodes = [];
1313

@@ -17,7 +17,7 @@ const buildConferenceNodes = (
1717
externalId: teams[0].conference.id,
1818
name: conference,
1919
slug: slugify(conference, { lower: true }),
20-
teams: teams.map((team: Team) => createNodeId(team.id)),
20+
teams: teams.map((team: TeamData) => createNodeId(team.id)),
2121
internal: {
2222
type: 'NHLConference',
2323
content: JSON.stringify(teams[0].conference),

src/nodes/build-division-nodes.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { NodePluginArgs, NodeInput } from 'gatsby';
1+
import { NodePluginArgs } from 'gatsby';
22
import slugify from 'slugify';
33
import { groupBy } from 'lodash';
44

5-
import { Team } from '../types/team';
5+
import { TeamData, DivisionNode } from '../types';
66

77
const buildDivisionNodes = (
8-
teams: Team[],
8+
teams: TeamData[],
99
{ createNodeId, createContentDigest }: NodePluginArgs,
10-
): Array<NodeInput> => {
11-
const teamsGroupedByDivisions = groupBy(
12-
teams,
13-
(team: Team) => team.division.name,
14-
);
10+
): DivisionNode[] => {
11+
const teamsGroupedByDivisions = groupBy(teams, team => team.division.name);
1512
const nodes = [];
1613

1714
for (const [division, teams] of Object.entries(teamsGroupedByDivisions)) {

src/nodes/build-franchise-nodes.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { NodePluginArgs, NodeInput } from 'gatsby';
1+
import { NodePluginArgs } from 'gatsby';
22
import slugify from 'slugify';
33

4-
import { Team } from '../types/team';
4+
import { TeamData, FranchiseNode } from '../types';
55

66
const buildFranchiseNodes = (
7-
teams: Team[],
7+
teams: TeamData[],
88
{ createNodeId, createContentDigest }: NodePluginArgs,
9-
): Array<NodeInput> => {
10-
return teams.map((team: Team) => ({
9+
): FranchiseNode[] => {
10+
return teams.map(team => ({
1111
id: createNodeId(team.franchise.franchiseId),
1212
slug: slugify(team.franchise.teamName, { lower: true }),
1313
externalId: team.franchise.franchiseId,

src/nodes/build-player-nodes.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { NodePluginArgs, NodeInput } from 'gatsby';
1+
import { NodePluginArgs } from 'gatsby';
22
import slugify from 'slugify';
33
import { flatten } from 'lodash';
44

5-
import { Team } from '../types/team';
6-
import { RosterItem } from '../types/roster';
5+
import { TeamData, PlayerNode } from '../types';
76

87
const buildPlayerNodes = (
9-
teams: Team[],
8+
teams: TeamData[],
109
{ createNodeId, createContentDigest }: NodePluginArgs,
11-
): Array<NodeInput> => {
12-
const players = teams.map((team: Team) =>
13-
team.roster.roster.map(({ person, position }: RosterItem) => ({
10+
): PlayerNode[] => {
11+
const players = teams.map(team =>
12+
team.roster.roster.map(({ person, position }) => ({
1413
id: createNodeId(person.id),
1514
externalId: person.id,
1615
fullName: person.fullName,
@@ -20,7 +19,7 @@ const buildPlayerNodes = (
2019
action: `https://nhl.bamcontent.com/images/actionshots/${person.id}@3x.jpg`,
2120
},
2221
team: createNodeId(team.id),
23-
position: position.name,
22+
position: createNodeId(position.name),
2423
internal: {
2524
type: 'NHLPlayer',
2625
content: JSON.stringify(person),

src/nodes/build-position-nodes.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
import { NodePluginArgs, NodeInput } from 'gatsby';
1+
import { NodePluginArgs } from 'gatsby';
22
import { flattenDeep } from 'lodash';
33
import slugify from 'slugify';
44

5-
import { Team } from '../types/team';
6-
import { Position } from '../types/position';
7-
import { RosterItem } from '../types/roster';
5+
import { TeamData, PositionData, PositionNode } from '../types';
86
import positions from '../data/positions';
97

108
const buildPositionNodes = (
11-
teams: Team[],
9+
teams: TeamData[],
1210
{ createNodeId, createContentDigest }: NodePluginArgs,
13-
): Array<NodeInput> => {
14-
const allRosterItems: RosterItem[] = flattenDeep(
15-
teams.map((team: Team) => team.roster.roster),
11+
): PositionNode[] => {
12+
const allRosterItems = flattenDeep(
13+
teams.map((team: TeamData) => team.roster.roster),
1614
);
1715

18-
return positions.map((position: Position) => {
19-
const filteredRosterItems = allRosterItems.filter((item: RosterItem) => {
16+
return positions.map((position: PositionData) => {
17+
const filteredRosterItems = allRosterItems.filter(item => {
2018
return item.position.name === position.name;
2119
});
2220

@@ -26,7 +24,7 @@ const buildPositionNodes = (
2624
type: position.type,
2725
abbreviation: position.abbreviation,
2826
slug: slugify(position.name, { lower: true }),
29-
players: filteredRosterItems.map((rosterItem: RosterItem) =>
27+
players: filteredRosterItems.map(rosterItem =>
3028
createNodeId(rosterItem.person.id),
3129
),
3230
internal: {

src/nodes/build-roster-nodes.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
import { NodePluginArgs, NodeInput } from 'gatsby';
1+
import { NodePluginArgs } from 'gatsby';
22
import { flatten } from 'lodash';
33

4-
import { Team } from '../types/team';
5-
import { RosterItem } from '../types/roster';
4+
import { TeamData, RosterItemNode } from '../types';
65

76
const buildRosterNodes = (
8-
teams: Team[],
7+
teams: TeamData[],
98
{ createNodeId, createContentDigest }: NodePluginArgs,
10-
): Array<NodeInput> => {
11-
const rosterItems = teams.map((team: Team) =>
12-
team.roster.roster.map((rosterItem: RosterItem) => ({
9+
): RosterItemNode[] => {
10+
const roster = teams.map(team =>
11+
team.roster.roster.map(rosterItem => ({
1312
id: createNodeId(rosterItem.person.id),
1413
externalId: rosterItem.person.id,
1514
jerseyNumber: rosterItem.jerseyNumber,
1615
player: createNodeId(rosterItem.person.id),
1716
team: createNodeId(team.id),
18-
position: rosterItem.position.name,
17+
position: createNodeId(rosterItem.position.name),
1918
internal: {
20-
type: 'NHLRosterItem',
19+
type: 'NHLRoster',
2120
content: JSON.stringify(rosterItem),
2221
contentDigest: createContentDigest(rosterItem),
2322
},
2423
})),
2524
);
26-
27-
return flatten(rosterItems);
25+
return flatten(roster);
2826
};
2927

3028
export default buildRosterNodes;

src/nodes/build-team-nodes.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { NodePluginArgs, NodeInput } from 'gatsby';
1+
import { NodePluginArgs } from 'gatsby';
22
import slugify from 'slugify';
33

4-
import { Team } from '../types/team';
4+
import { TeamData, TeamNode } from '../types';
55

66
const buildTeamNodes = (
7-
teams: Team[],
7+
teams: TeamData[],
88
{ createNodeId, createContentDigest }: NodePluginArgs,
9-
): Array<NodeInput> => {
10-
return teams.map((team: Team) => ({
9+
): TeamNode[] => {
10+
return teams.map(team => ({
1111
id: createNodeId(team.id),
1212
externalId: team.id,
1313
name: team.name,

src/nodes/build-venue-nodes.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { NodePluginArgs, NodeInput } from 'gatsby';
1+
import { NodePluginArgs } from 'gatsby';
22
import { get } from 'lodash';
33

4-
import { Team } from '../types/team';
4+
import { TeamData, VenueNode } from '../types';
55

66
const buildVenueNodes = (
7-
teams: Team[],
7+
teams: TeamData[],
88
{ createNodeId, createContentDigest }: NodePluginArgs,
9-
): Array<NodeInput> => {
10-
return teams.map((team: Team) => ({
9+
): VenueNode[] => {
10+
return teams.map(team => ({
1111
id: createNodeId(team.venue.name),
1212
externalId: get(team, 'venue.id', null), // NHL API missing ID's for some venues
1313
name: team.venue.name,

0 commit comments

Comments
 (0)