import type { AxiosRequestConfig } from 'axios' ; import type { PreKeyBundle } from '../auth/' ; import type { NewClient , RegisteredClient , UpdatedClient } from '../client/' ; import { ClientCapabilityRemovedError } from './ClientError' ; import { BackendError , BackendErrorLabel , HttpClient } from '../http/' ; import { ClientCapabilityData } from './ClientCapabilityData' ; export class ClientAPI { constructor ( private readonly client ) { } public static readonly URL = { CLIENTS : '/clients' , CAPABILITIES : 'capabilities' , PREKEYS : 'prekeys' , } ; public async postClient ( newClient ) { const config = { data : newClient , method : 'post' , url : ClientAPI . URL . CLIENTS , } ; const response = await this . client . sendJSON < RegisteredClient > ( config ) ; return response . data ; } public async putClient ( clientId , updatedClient ) { const config = { data : updatedClient , method : 'put' , url : `${ ClientAPI . URL . CLIENTS }/${ clientId }` , } ; try { await this . client . sendJSON ( config ) ; } catch ( error ) { switch ( ( error as BackendError ) . label ) { case BackendErrorLabel . CLIENT_CAPABILITY_REMOVED : { throw new ClientCapabilityRemovedError ( ( error as BackendError ) . message ) ; } } throw error ; } } public async getClientCapabilities ( clientId ) { const config = { method : 'get' , url : `${ ClientAPI . URL . CLIENTS }/${ clientId }/${ ClientAPI . URL . CAPABILITIES }` , } ; const response = await this . client . sendJSON < ClientCapabilityData > ( config ) ; return response . data ; } public async deleteClient ( clientId , password ? ) { const config = { data : { password , } , method : 'delete' , url : `${ ClientAPI . URL . CLIENTS }/${ clientId }` , } ; await this . client . sendJSON ( config ) ; } public async getClient ( clientId ) { const config = { method : 'get' , url : `${ ClientAPI . URL . CLIENTS }/${ clientId }` , } ; const response = await this . client . sendJSON < RegisteredClient > ( config ) ; return response . data ; } public async getClients ( ) { const config = { method : 'get' , url : ClientAPI . URL . CLIENTS , } ; const response = await this . client . sendJSON < RegisteredClient [ ] > ( config ) ; return response . data ; } public async getClientPreKeys ( clientId ) { const config = { method : 'get' , url : `${ ClientAPI . URL . CLIENTS }/${ clientId }/${ ClientAPI . URL . PREKEYS }` , } ; const response = await this . client . sendJSON < PreKeyBundle > ( config , true ) ; return response . data ; } }