-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
62 lines (54 loc) · 2.6 KB
/
Copy pathbasic.js
File metadata and controls
62 lines (54 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// GOAL_API_KEY=... node examples/basic.js
//
// The status section works without a key.
import { GoalApi, RateLimitError, GoalApiError } from '../src/index.js';
const apiKey = process.env.GOAL_API_KEY;
const goal = new GoalApi({ apiKey: apiKey ?? 'unset' });
// --- public status: no API key required -------------------------------------
const status = await goal.status.get();
console.log(`Platform: ${status.status}`);
for (const component of status.components ?? []) {
console.log(` ${component.name.padEnd(12)} ${component.status} 24h ${component.uptime?.['24h']}%`);
}
if (!apiKey) {
console.log('\nSet GOAL_API_KEY to run the authenticated examples.');
process.exit(0);
}
try {
// --- live matches -------------------------------------------------------
const { data: live } = await goal.fixtures.live();
console.log(`\n${live.length} match(es) in play`);
for (const m of live.slice(0, 5)) {
console.log(` ${m.homeTeam?.name ?? '?'} ${m.homeScore ?? 0}-${m.awayScore ?? 0} ${m.awayTeam?.name ?? '?'} (${m.minute ?? '?'}')`);
}
// --- pick a league and read its table ----------------------------------
const { data: leagues } = await goal.leagues.list({ isActive: true, limit: 1 });
const league = leagues[0];
if (league) {
console.log(`\nStandings: ${league.name}`);
const { data: table } = await goal.leagues.standings(league.id);
for (const row of (Array.isArray(table) ? table : []).slice(0, 5)) {
console.log(` ${String(row.position).padStart(2)}. ${(row.team?.name ?? row.teamName ?? '?').padEnd(24)} ${row.points} pts`);
}
console.log(`\nTop scorers: ${league.name}`);
const { data: scorers } = await goal.leagues.topScorers(league.id, { limit: 5 });
for (const s of Array.isArray(scorers) ? scorers : []) {
console.log(` ${(s.player?.name ?? s.name ?? '?').padEnd(24)} ${s.goals ?? 0}`);
}
}
// --- pagination across every team in a league ---------------------------
if (league) {
const teams = await goal.collect((p) => goal.leagues.teams(league.id, p));
console.log(`\n${teams.length} teams in ${league.name}`);
}
console.log('\nQuota:', goal.rateLimit);
} catch (error) {
if (error instanceof RateLimitError) {
console.error(`Rate limited (${error.rateLimitType}). Retry in ${error.retryAfter}s.`);
} else if (error instanceof GoalApiError) {
console.error(`${error.code ?? 'ERROR'}: ${error.message} [${error.correlationId ?? 'no correlation id'}]`);
} else {
throw error;
}
process.exitCode = 1;
}