import { URLSearchParams } from 'url' ; import Client from '../client' ; import { Team } from '../../types' ; import { APIError , InvalidToken } from '../errors-ts' ; export interface GetTeamsV1Options { apiVersion ? : 1 ; } export interface GetTeamsV2Options { next ? : number ; limit ? : number ; apiVersion : 2 ; } export interface GetTeamsV2Response { teams : Team [ ] ; pagination : { count : number ; next : number ; prev : number ; } ; } export default function getTeams ( client , opts ? ) ; export default function getTeams ( client , opts ) ; export default async function getTeams ( client , opts = { } ) { const { apiVersion = 1 } = opts ; let query = '' ; if ( opts . apiVersion === 2 ) { const params = new URLSearchParams ( { limit : String ( typeof opts . limit === 'number' ? opts . limit : 20 ) , } ) ; if ( opts . next ) { params . set ( 'next' , String ( opts . next ) ) ; } query = `?${ params }` ; } try { const body = await client . fetch < GetTeamsV2Response > ( `/v${ apiVersion }/teams${ query }` , { useCurrentTeam : false , } ) ; if ( apiVersion === 1 ) { return body . teams || [ ] ; } return body ; } catch ( error ) { if ( error instanceof APIError && error . status === 403 ) { throw new InvalidToken ( ) ; } throw error ; } }