import Axios from 'axios' ; import type { AxiosRequestConfig } from 'axios' ; import type { NewTeamData , TeamChunkData , TeamData } from '../' ; import { BackendError , HttpClient , RequestCancelable , SyntheticErrorLabel } from '../../http/' ; import { RequestCancellationError } from '../../user' ; import type { TeamSizeData } from './TeamSizeData' ; import type { UpdateTeamData } from './UpdateTeamData' ; import { LeadData } from './LeadData' ; export class TeamAPI { constructor ( private readonly client ) { } public static readonly URL = { SIZE : 'size' , TEAMS : '/teams' , CONSENT : '/consent' , MARKETO : 'marketo' , LEAD : 'lead' , } ; public async postTeam ( team ) { const config = { data : team , method : 'post' , url : `${ TeamAPI . URL . TEAMS }` , } ; const response = await this . client . sendJSON ( config ) ; return response . headers . location ; } public async putTeam ( teamId , teamData ) { const config = { data : teamData , method : 'put' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }` , } ; await this . client . sendJSON ( config ) ; } public async getTeams ( ) { const config = { method : 'get' , url : `${ TeamAPI . URL . TEAMS }` , } ; const response = await this . client . sendJSON < TeamChunkData > ( config ) ; return response . data ; } public async getTeam ( teamId ) { const config = { method : 'get' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }` , } ; const response = await this . client . sendJSON < TeamData > ( config ) ; return response . data ; } public async deleteTeam ( teamId , password ) { const config = { data : { password , } , method : 'delete' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }` , } ; await this . client . sendJSON ( config ) ; } public async getTeamSize ( teamId ) { const cancelSource = Axios . CancelToken . source ( ) ; const config = { cancelToken : cancelSource . token , method : 'get' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ TeamAPI . URL . SIZE }` , } ; const handleRequest = async ( ) => { try { const response = await this . client . sendJSON < TeamSizeData > ( config ) ; return response . data ; } catch ( error ) { if ( ( error as BackendError ) . message === SyntheticErrorLabel . REQUEST_CANCELLED ) { throw new RequestCancellationError ( 'Team size request got cancelled' ) ; } throw error ; } } ; return { cancel : ( ) => cancelSource . cancel ( SyntheticErrorLabel . REQUEST_CANCELLED ) , response : handleRequest ( ) , } ; } public async postLead ( data ) { const config = { data , method : 'post' , url : `${ TeamAPI . URL . CONSENT }/${ TeamAPI . URL . MARKETO }/${ TeamAPI . URL . LEAD }` , } ; const response = await this . client . sendJSON < any > ( config ) ; return response . data ; } public async putLead ( data ) { const config = { data , method : 'put' , url : `${ TeamAPI . URL . CONSENT }/${ TeamAPI . URL . MARKETO }/${ TeamAPI . URL . LEAD }` , } ; const response = await this . client . sendJSON < any > ( config ) ; return response . data ; } }