import Axios , { AxiosRequestConfig } from 'axios' ; import { BackendError , HttpClient , RequestCancelable , SyntheticErrorLabel } from '../../http' ; import { RequestCancellationError } from '../../user' ; import { TeamAPI } from '../team' ; import { TeamSearchOptions } from './TeamSearchOptions' ; import { TeamSearchResult } from './TeamSearchResult' ; export class TeamSearchAPI { constructor ( private readonly client ) { } public static readonly URL = { SEARCH : 'search' , } ; param query param options public async getSearchMembers ( teamId , query , options = { } ) { const cancelSource = Axios . CancelToken . source ( ) ; const config = { cancelToken : cancelSource . token , method : 'get' , params : { q : query , ... options , frole : options . frole ?. join ( ',' ) , } , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ TeamSearchAPI . URL . SEARCH }` , } ; const handleRequest = async ( ) => { try { const response = await this . client . sendJSON < TeamSearchResult > ( config ) ; return response . data ; } catch ( error ) { if ( ( error as BackendError ) . message === SyntheticErrorLabel . REQUEST_CANCELLED ) { throw new RequestCancellationError ( 'Search request got cancelled' ) ; } throw error ; } } ; return { cancel : ( ) => cancelSource . cancel ( SyntheticErrorLabel . REQUEST_CANCELLED ) , response : handleRequest ( ) , } ; } }