import type { AxiosRequestConfig } from 'axios' ; import type { Connection , ConnectionRequest , ConnectionUpdate , UserConnectionList } from '../connection/' ; import { BackendError , BackendErrorLabel , HttpClient } from '../http/' ; import { QualifiedId } from '../user' ; import { ConnectionLegalholdMissingConsentError } from './ConnectionError' ; export class ConnectionAPI { constructor ( private readonly client ) { } public static readonly URL = { CONNECTIONS : '/connections' , } ; param userId see https public async getConnection ( userId , useFederation ) ; public async getConnection ( userId , useFederation ? ) ; public async getConnection ( userId , useFederation = false ) { const url = typeof userId !== 'string' && useFederation ? `${ ConnectionAPI . URL . CONNECTIONS }/${ userId . domain }/${ userId }` : `${ ConnectionAPI . URL . CONNECTIONS }/${ userId }` ; const config = { method : 'get' , url , } ; const response = await this . client . sendJSON < Connection > ( config ) ; return response . data ; } param limit param connectionId see https public async getConnections ( connectionId ? , limit = 100 ) { const config = { method : 'get' , params : { size : limit , start : connectionId , } , url : ConnectionAPI . URL . CONNECTIONS , } ; const response = await this . client . sendJSON < UserConnectionList > ( config ) ; return response . data ; } see https public getConnectionList ( ) { let allConnections = [ ] ; const getConnectionChunks = async ( pagingState ? ) : Promise < Connection [ ] > => { const connectionsPerRequest = 500 ; const config = { method : 'POST' , data : { size : connectionsPerRequest , paging_state : pagingState , } , url : '/list-connections' , } ; const { data } = await this . client . sendJSON < { connections : Connection [ ] ; has_more ? : boolean ; paging_state ? : string ; } > ( config ) ; const { connections , has_more , paging_state } = data ; if ( connections . length ) { allConnections = allConnections . concat ( connections ) ; } if ( has_more ) { return getConnectionChunks ( paging_state ) ; } return allConnections ; } ; return getConnectionChunks ( ) ; } deprecated public getAllConnections ( ) { let allConnections = [ ] ; const getConnectionChunks = async ( connectionId ? ) : Promise < Connection [ ] > => { const connectionsPerRequest = 500 ; const { connections , has_more } = await this . getConnections ( connectionId , connectionsPerRequest ) ; if ( connections . length ) { allConnections = allConnections . concat ( connections ) ; } if ( has_more ) { const lastConnection = connections . pop ( ) ; if ( lastConnection ) { return getConnectionChunks ( lastConnection . to ) ; } } return allConnections ; } ; return getConnectionChunks ( ) ; } public async postConnection ( data , useFederation ? ) ; public async postConnection ( data , useFederation ) ; public async postConnection ( data , useFederation = false ) { if ( useFederation ) { return this . postConnection_v2 ( data as QualifiedId ) ; } return this . postConnection_v1 ( data as ConnectionRequest ) ; } deprecated param connectionRequestData see https private async postConnection_v1 ( connectionRequestData ) { const config = { data : connectionRequestData , method : 'post' , url : ConnectionAPI . URL . CONNECTIONS , } ; try { const response = await this . client . sendJSON < Connection > ( config ) ; return response . data ; } catch ( error ) { switch ( ( error as BackendError ) . label ) { case BackendErrorLabel . LEGAL_HOLD_MISSING_CONSENT : { throw new ConnectionLegalholdMissingConsentError ( ( error as BackendError ) . message ) ; } } throw error ; } } param qualifiedUserId see https private async postConnection_v2 ( { id , domain } ) { const config = { method : 'post' , url : `${ ConnectionAPI . URL . CONNECTIONS }/${ domain }/${ id }` , } ; try { const response = await this . client . sendJSON < Connection > ( config ) ; return response . data ; } catch ( error ) { switch ( ( error as BackendError ) . label ) { case BackendErrorLabel . LEGAL_HOLD_MISSING_CONSENT : { throw new ConnectionLegalholdMissingConsentError ( ( error as BackendError ) . message ) ; } } throw error ; } } param userId param updatedConnection param useFederation see https public async putConnection ( userId , updatedConnection ) ; public async putConnection ( userId , updatedConnection , useFederation ) ; public async putConnection ( userId , updatedConnection , useFederation = false ) { const url = useFederation && typeof userId !== 'string' ? `${ ConnectionAPI . URL . CONNECTIONS }/${ userId . domain }/${ userId . id }` : `${ ConnectionAPI . URL . CONNECTIONS }/${ userId }` ; const config = { data : updatedConnection , method : 'put' , url , } ; const response = await this . client . sendJSON < Connection > ( config ) ; return response . data ; } }