sba-website/src/services/teamsService.ts

34 lines
1.1 KiB
TypeScript

import type { Team } from './apiResponseTypes'
import { CURRENT_SEASON, SITE_URL } from './utilities'
export async function fetchTeam(seasonNumber: number, teamAbbreviation: string): Promise<Team> {
const response = await fetch(`${SITE_URL}/api/v3/teams?season=${seasonNumber}&team_abbrev=${teamAbbreviation}`)
const teamResponse: {
count: number
teams: Team[]
} = await response.json()
if (teamResponse.count !== 1) {
throw new Error('teamServices.fetchTeam - Expected one team, return contained none or many')
}
return teamResponse.teams[0]
}
export async function fetchActiveTeamByOwnerId(ownerId: string): Promise<Team | undefined> {
const response = await fetch(`${SITE_URL}/api/v3/teams?season=${CURRENT_SEASON}&active_only=True&owner_id=${ownerId}`)
const teamResponse: {
count: number
teams: Team[]
} = await response.json()
if (teamResponse.count === 0) {
console.warn('teamServices.fetchActiveTeamByOwnerId - Received 0 active teams for owner id, are they active?')
return undefined
}
return teamResponse.teams[0]
}