import { Context } from '@textile/context' ; import { ServiceError } from '@textile/hub-grpc/api/hubd/pb/hubd_pb_service' ; import { KeyInfo , UserAuth } from '@textile/security' ; import { Client } from '@textile/threads-client' ; import { ThreadID } from '@textile/threads-id' ; import { GetThreadRequest , GetThreadResponse as _GetThreadResponse , ListThreadsRequest , ListThreadsResponse as _ListThreadsResponse , } from '@textile/users-grpc/api/usersd/pb/usersd_pb' ; import { APIServiceClient } from '@textile/users-grpc/api/usersd/pb/usersd_pb_service' ; import log from 'loglevel' ; const logger = log . getLogger ( 'users' ) ; interface GetThreadResponse { id : string ; name ? : string ; } declare module '@textile/threads-client' { interface Client { getThread ( name , ctx ? ) : Promise < GetThreadResponse > ; listThreads ( ctx ? ) : Promise < Array < GetThreadResponse > > ; } } Client . prototype . getThread = async function ( name , ctx ? ) : Promise < GetThreadResponse > { logger . debug ( 'get thread request' ) ; const client = new APIServiceClient ( this . serviceHost , { transport : this . rpcOptions . transport , debug : this . rpcOptions . debug , } ) ; return new Promise < GetThreadResponse > ( ( resolve , reject ) => { const req = new GetThreadRequest ( ) ; req . setName ( name ) ; this . context . toMetadata ( ctx ) . then ( ( meta ) => { client . getThread ( req , meta , ( err , message ) => { if ( err ) reject ( err ) ; if ( message ) { const res = { name : message . getName ( ) , id : ThreadID . fromBytes ( message . getId_asU8 ( ) ) . toString ( ) , } ; resolve ( res ) ; } else { reject ( new Error ( 'No result' ) ) ; } } ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } ; const oldListDBs = Client . prototype . listDBs ; /**
 * Lists all known DBs.
 */ Client . prototype . listDBs = async function ( ctx ? ) : Promise < Array < GetThreadResponse > > { if ( this . context . withContext ( ctx ) . get ( 'x-textile-api-sig' ) ) { return this . listThreads ( ctx ) ; } return oldListDBs . bind ( this ) ( ) . catch ( ( err ) => { if ( err . message . includes ( 'Method is not accessible' ) ) { throw new Error ( `${ err . message }. If using Hub, ensure you are using a valid API signature.` ) ; } throw err ; } ) ; } ; param ctx note Client . prototype . listThreads = async function ( ctx ? ) : Promise < Array < GetThreadResponse > > { logger . debug ( 'list threads request' ) ; const client = new APIServiceClient ( this . serviceHost , { transport : this . rpcOptions . transport , debug : this . rpcOptions . debug , } ) ; return new Promise < Array < GetThreadResponse > > ( ( resolve , reject ) => { const req = new ListThreadsRequest ( ) ; this . context . toMetadata ( ctx ) . then ( ( meta ) => { client . listThreads ( req , meta , ( err , message ) => { if ( err ) return reject ( err ) ; const lst = message ?. getListList ( ) ; let results = [ ] ; if ( ! lst ) return resolve ( results ) ; results = lst . map ( ( thrd ) => { return { name : thrd . getName ( ) , id : ThreadID . fromBytes ( thrd . getId_asU8 ( ) ) . toString ( ) , } ; } ) ; return resolve ( results ) ; } ) ; } ) . catch ( ( err ) => { return reject ( err ) ; } ) ; } ) ; } ; /**
 * Clients is a web-gRPC wrapper client for communicating with the web-gRPC enabled Threads API.
 */ export { Client , GetThreadResponse , KeyInfo , UserAuth } ;