nhl-api-ts 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +315 -2
- package/dist/api/edge.d.mts +180 -0
- package/dist/api/edge.mjs +184 -0
- package/dist/api/game-center.d.mts +84 -0
- package/dist/api/game-center.mjs +74 -0
- package/dist/api/helpers.d.mts +47 -0
- package/dist/api/helpers.mjs +106 -0
- package/dist/api/misc.d.mts +40 -0
- package/dist/api/misc.mjs +43 -0
- package/dist/api/players.d.mts +28 -0
- package/dist/api/players.mjs +18 -0
- package/dist/api/query/builder.d.mts +22 -0
- package/dist/api/query/builder.mjs +30 -0
- package/dist/api/query/filters.d.mts +93 -0
- package/dist/api/query/filters.mjs +118 -0
- package/dist/api/query/index.d.mts +4 -0
- package/dist/api/query/index.mjs +4 -0
- package/dist/api/query/sorting/goalie-sorts.d.mts +61 -0
- package/dist/api/query/sorting/goalie-sorts.mjs +54 -0
- package/dist/api/query/sorting/sorting-options.d.mts +136 -0
- package/dist/api/query/sorting/sorting-options.mjs +94 -0
- package/dist/api/schedule.d.mts +117 -0
- package/dist/api/schedule.mjs +131 -0
- package/dist/api/standings.d.mts +29 -0
- package/dist/api/standings.mjs +38 -0
- package/dist/api/stats.d.mts +153 -0
- package/dist/api/stats.mjs +166 -0
- package/dist/api/teams.d.mts +59 -0
- package/dist/api/teams.mjs +91 -0
- package/dist/cli/colors.d.mts +14 -0
- package/dist/cli/colors.mjs +13 -0
- package/dist/cli/commands.d.mts +27 -0
- package/dist/cli/commands.mjs +695 -0
- package/dist/cli/run.d.mts +9 -0
- package/dist/cli/run.mjs +313 -0
- package/dist/cli.d.mts +2 -0
- package/dist/cli.mjs +13 -0
- package/dist/config.d.mts +20 -0
- package/dist/config.mjs +4 -0
- package/dist/context.d.mts +6 -0
- package/dist/context.mjs +1 -0
- package/dist/errors.d.mts +35 -0
- package/dist/errors.mjs +49 -0
- package/dist/http-client.d.mts +27 -0
- package/dist/http-client.mjs +95 -0
- package/dist/index.d.mts +22 -0
- package/dist/index.mjs +15 -0
- package/dist/nhl-client.d.mts +101 -0
- package/dist/nhl-client.mjs +40 -0
- package/dist/types/common.d.mts +81 -0
- package/dist/types/common.mjs +2 -0
- package/dist/types/edge.d.mts +1024 -0
- package/dist/types/edge.mjs +1 -0
- package/dist/types/game-center.d.mts +1194 -0
- package/dist/types/game-center.mjs +1 -0
- package/dist/types/helpers.d.mts +17 -0
- package/dist/types/helpers.mjs +1 -0
- package/dist/types/index.d.mts +10 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/misc.d.mts +68 -0
- package/dist/types/misc.mjs +2 -0
- package/dist/types/players.d.mts +42 -0
- package/dist/types/players.mjs +1 -0
- package/dist/types/schedule.d.mts +456 -0
- package/dist/types/schedule.mjs +1 -0
- package/dist/types/standings.d.mts +104 -0
- package/dist/types/standings.mjs +1 -0
- package/dist/types/stats.d.mts +1086 -0
- package/dist/types/stats.mjs +1 -0
- package/dist/types/teams.d.mts +29 -0
- package/dist/types/teams.mjs +1 -0
- package/dist/utils.d.mts +4 -0
- package/dist/utils.mjs +3 -0
- package/package.json +85 -3
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Context } from '#src/context.mjs';
|
|
2
|
+
import type { Franchise, Roster, StandingsEntry } from '#src/types/index.mjs';
|
|
3
|
+
import type { Id } from '#src/utils.mjs';
|
|
4
|
+
export interface Team {
|
|
5
|
+
conference: {
|
|
6
|
+
abbr: string;
|
|
7
|
+
name: string;
|
|
8
|
+
};
|
|
9
|
+
division: {
|
|
10
|
+
abbr: string;
|
|
11
|
+
name: string;
|
|
12
|
+
};
|
|
13
|
+
name: string;
|
|
14
|
+
common_name: string;
|
|
15
|
+
abbr: string;
|
|
16
|
+
logo: string;
|
|
17
|
+
franchise_id?: number;
|
|
18
|
+
}
|
|
19
|
+
/** Converts a raw standings entry into a `Team`. */
|
|
20
|
+
export declare function toTeam(teamData: Partial<StandingsEntry>): Team;
|
|
21
|
+
/**
|
|
22
|
+
* Adds a `franchise_id` to each team that has a matching franchise. Teams
|
|
23
|
+
* without a match are returned unchanged (no `franchise_id` key).
|
|
24
|
+
*/
|
|
25
|
+
export declare function withFranchiseIds(teams: readonly Team[], franchises: readonly Pick<Franchise, 'id' | 'fullName'>[]): Team[];
|
|
26
|
+
/**
|
|
27
|
+
* Get a list of all NHL teams with conference, division, and franchise info.
|
|
28
|
+
*
|
|
29
|
+
* Team list is derived from the standings for the given date, then enriched
|
|
30
|
+
* with franchise ids.
|
|
31
|
+
*
|
|
32
|
+
* @param params.date Date in YYYY-MM-DD format. Defaults to "now".
|
|
33
|
+
* @example await client.teams.teams()
|
|
34
|
+
*/
|
|
35
|
+
export declare const teams: (ctx: Context, { date }?: {
|
|
36
|
+
date?: string;
|
|
37
|
+
}) => Promise<Team[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Get the roster for the given team and season.
|
|
40
|
+
*
|
|
41
|
+
* @param params.teamAbbr Team abbreviation, e.g. BUF, TOR
|
|
42
|
+
* @param params.season Season in YYYYYYYY format, e.g. 20232024
|
|
43
|
+
* @example https://api-web.nhle.com/v1/roster/BUF/20232024
|
|
44
|
+
*/
|
|
45
|
+
export declare const teamRoster: (ctx: Context, { teamAbbr, season }: {
|
|
46
|
+
teamAbbr: string;
|
|
47
|
+
season: Id;
|
|
48
|
+
}) => Promise<Roster>;
|
|
49
|
+
/**
|
|
50
|
+
* Get a list of all past and current NHL franchises.
|
|
51
|
+
*
|
|
52
|
+
* @example https://api.nhle.com/stats/rest/en/franchise
|
|
53
|
+
*/
|
|
54
|
+
export declare const franchises: (ctx: Context) => Promise<Franchise[]>;
|
|
55
|
+
export declare const teamsApi: {
|
|
56
|
+
readonly teams: typeof teams;
|
|
57
|
+
readonly teamRoster: typeof teamRoster;
|
|
58
|
+
readonly franchises: typeof franchises;
|
|
59
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Endpoint } from '#src/http-client.mjs';
|
|
2
|
+
async function fetchStandingsData(ctx, date) {
|
|
3
|
+
const response = await (await ctx.get(Endpoint.API_WEB_V1, `standings/${date}`)).json();
|
|
4
|
+
return response.standings ?? [];
|
|
5
|
+
}
|
|
6
|
+
/** Converts a raw standings entry into a `Team`. */
|
|
7
|
+
export function toTeam(teamData) {
|
|
8
|
+
const nestedDefault = (key) => teamData[key]?.default ?? '';
|
|
9
|
+
return {
|
|
10
|
+
conference: {
|
|
11
|
+
abbr: teamData.conferenceAbbrev ?? '',
|
|
12
|
+
name: teamData.conferenceName ?? '',
|
|
13
|
+
},
|
|
14
|
+
division: {
|
|
15
|
+
abbr: teamData.divisionAbbrev ?? '',
|
|
16
|
+
name: teamData.divisionName ?? '',
|
|
17
|
+
},
|
|
18
|
+
name: nestedDefault('teamName'),
|
|
19
|
+
common_name: nestedDefault('teamCommonName'),
|
|
20
|
+
abbr: nestedDefault('teamAbbrev'),
|
|
21
|
+
logo: teamData.teamLogo ?? '',
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Find franchise ID for a team name, with fallbacks for known name mismatches. */
|
|
25
|
+
function findFranchiseId(teamName, lookup) {
|
|
26
|
+
if (lookup.has(teamName)) {
|
|
27
|
+
return lookup.get(teamName);
|
|
28
|
+
}
|
|
29
|
+
// "Montreal Canadiens" vs "Montréal Canadiens", Utah's renames, etc.
|
|
30
|
+
for (const partial of ['Canadiens', 'Utah']) {
|
|
31
|
+
if (teamName.includes(partial)) {
|
|
32
|
+
for (const [franchiseName, franchiseId] of lookup) {
|
|
33
|
+
if (franchiseName.includes(partial)) {
|
|
34
|
+
return franchiseId;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Adds a `franchise_id` to each team that has a matching franchise. Teams
|
|
44
|
+
* without a match are returned unchanged (no `franchise_id` key).
|
|
45
|
+
*/
|
|
46
|
+
export function withFranchiseIds(teams, franchises) {
|
|
47
|
+
const lookup = new Map();
|
|
48
|
+
for (const franchise of franchises) {
|
|
49
|
+
const fullName = franchise.fullName ?? '';
|
|
50
|
+
const franchiseId = franchise.id;
|
|
51
|
+
if (fullName && franchiseId) {
|
|
52
|
+
lookup.set(fullName, franchiseId);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return teams.map((team) => {
|
|
56
|
+
const franchiseId = findFranchiseId(team.name ?? '', lookup);
|
|
57
|
+
return franchiseId ? { ...team, franchise_id: franchiseId } : { ...team };
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get a list of all NHL teams with conference, division, and franchise info.
|
|
62
|
+
*
|
|
63
|
+
* Team list is derived from the standings for the given date, then enriched
|
|
64
|
+
* with franchise ids.
|
|
65
|
+
*
|
|
66
|
+
* @param params.date Date in YYYY-MM-DD format. Defaults to "now".
|
|
67
|
+
* @example await client.teams.teams()
|
|
68
|
+
*/
|
|
69
|
+
export const teams = async (ctx, { date = 'now' } = {}) => {
|
|
70
|
+
const standingsData = await fetchStandingsData(ctx, date);
|
|
71
|
+
const franchiseData = await franchises(ctx);
|
|
72
|
+
return withFranchiseIds(standingsData.map(toTeam), franchiseData);
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Get the roster for the given team and season.
|
|
76
|
+
*
|
|
77
|
+
* @param params.teamAbbr Team abbreviation, e.g. BUF, TOR
|
|
78
|
+
* @param params.season Season in YYYYYYYY format, e.g. 20232024
|
|
79
|
+
* @example https://api-web.nhle.com/v1/roster/BUF/20232024
|
|
80
|
+
*/
|
|
81
|
+
export const teamRoster = async (ctx, { teamAbbr, season }) => (await ctx.get(Endpoint.API_WEB_V1, `roster/${teamAbbr}/${season}`)).json();
|
|
82
|
+
/**
|
|
83
|
+
* Get a list of all past and current NHL franchises.
|
|
84
|
+
*
|
|
85
|
+
* @example https://api.nhle.com/stats/rest/en/franchise
|
|
86
|
+
*/
|
|
87
|
+
export const franchises = async (ctx) => {
|
|
88
|
+
const response = await (await ctx.get(Endpoint.API_STATS, 'en/franchise')).json();
|
|
89
|
+
return response.data ?? [];
|
|
90
|
+
};
|
|
91
|
+
export const teamsApi = { teams, teamRoster, franchises };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { styleText } from 'node:util';
|
|
2
|
+
export type Style = Parameters<typeof styleText>[0];
|
|
3
|
+
export declare const toStdout: (format: Style) => (text: string) => string;
|
|
4
|
+
export declare const toStderr: (format: Style) => (text: string) => string;
|
|
5
|
+
export declare const colors: {
|
|
6
|
+
dim: (text: string) => string;
|
|
7
|
+
pass: (text: string) => string;
|
|
8
|
+
fail: (text: string) => string;
|
|
9
|
+
ok: (text: string) => string;
|
|
10
|
+
success: (text: string) => string;
|
|
11
|
+
error: (text: string) => string;
|
|
12
|
+
warn: (text: string) => string;
|
|
13
|
+
bold: (text: string) => string;
|
|
14
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { styleText } from 'node:util';
|
|
2
|
+
export const toStdout = (format) => (text) => styleText(format, text, { stream: process.stdout });
|
|
3
|
+
export const toStderr = (format) => (text) => styleText(format, text, { stream: process.stderr });
|
|
4
|
+
export const colors = {
|
|
5
|
+
dim: toStdout('dim'),
|
|
6
|
+
pass: toStdout(['bold', 'green']),
|
|
7
|
+
fail: toStdout(['bold', 'red']),
|
|
8
|
+
ok: toStdout('green'),
|
|
9
|
+
success: toStdout(['bold', 'green']),
|
|
10
|
+
error: toStderr('red'),
|
|
11
|
+
warn: toStderr('yellow'),
|
|
12
|
+
bold: toStderr('bold'),
|
|
13
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { NHLClient } from '#src/nhl-client.mjs';
|
|
2
|
+
export type ArgType = 'string' | 'int' | 'number' | 'boolean' | 'list' | 'int-list' | 'json';
|
|
3
|
+
export interface ArgSpec {
|
|
4
|
+
/** SDK parameter name (camelCase). The CLI flag is its kebab-case form. */
|
|
5
|
+
name: string;
|
|
6
|
+
type: ArgType;
|
|
7
|
+
description: string;
|
|
8
|
+
required?: boolean;
|
|
9
|
+
/** Shown in help only; the SDK applies its own defaults. */
|
|
10
|
+
default?: string;
|
|
11
|
+
choices?: readonly string[];
|
|
12
|
+
}
|
|
13
|
+
export interface CommandSpec {
|
|
14
|
+
name: string;
|
|
15
|
+
description: string;
|
|
16
|
+
args: ArgSpec[];
|
|
17
|
+
run: (client: NHLClient, values: any) => Promise<unknown>;
|
|
18
|
+
}
|
|
19
|
+
export interface ModuleSpec {
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
commands: CommandSpec[];
|
|
23
|
+
}
|
|
24
|
+
/** Thrown for invalid command-line input (exit code 2). */
|
|
25
|
+
export declare class UsageError extends Error {
|
|
26
|
+
}
|
|
27
|
+
export declare const MODULES: ModuleSpec[];
|