import { Client , ClientOptions , DefaultClientStack , ProviderConstructor } from '@src/core' ; import { DiscordooProviders } from '@src/constants' ; import { CreateAppOptions } from '@src/wrapper/interfaces/CreateAppOptions' ; import { is } from 'typescript-is' ; import { ValidationError } from '@src/utils/ValidationError' ; export class ClientBuilder < Stack extends DefaultClientStack = DefaultClientStack > { public token : string ; public options : ClientOptions ; private readonly customClient : any ; constructor ( token , options ? ) { if ( ! is < string > ( token ) ) throw new ValidationError ( 'ClientBuilder' , 'invalid token provided:' , token ) ; this . token = token ; this . options = { } ; if ( ! this . options . providers ) this . options . providers = [ ] ; if ( options ?. useClient ) this . customClient = options . useClient ; } rest ( options ) { if ( ! is < ClientOptions [ 'rest' ] > ( options ) ) this . throwInvalidOptionsError ( 'rest' , options ) ; this . options . rest = options ; return this ; } gateway ( options ) { if ( ! is < ClientOptions [ 'gateway' ] > ( options ) ) this . throwInvalidOptionsError ( 'gateway' , options ) ; this . options . gateway = options ; return this ; } cache ( options ) { if ( ! is < ClientOptions [ 'cache' ] > ( options ) ) this . throwInvalidOptionsError ( 'cache' , options ) ; this . options . cache = options ; return this ; } ipc ( options ) { if ( ! is < ClientOptions [ 'ipc' ] > ( options ) ) this . throwInvalidOptionsError ( 'ipc' , options ) ; this . options . ipc = options ; return this ; } extenders ( extenders ) { this . options . extenders = extenders ; return this ; } custom < T > ( options ) { this . options . custom = options ; return this ; } restProvider ( provider , ... options ) { return this . useProvider ( DiscordooProviders . REST , provider , ... options ) ; } cacheProvider ( provider , ... options ) { return this . useProvider ( DiscordooProviders . CACHE , provider , ... options ) ; } gatewayProvider ( provider , ... options ) { return this . useProvider ( DiscordooProviders . GATEWAY , provider , ... options ) ; } private useProvider ( providerSignature , provider , ... options ) { const providerIndex = this . options . providers ! . findIndex ( p => p . provide === provider ) ; if ( providerIndex > - 1 ) this . options . providers ! . splice ( providerIndex , 1 ) ; this . options . providers ! . push ( { provide : providerSignature , useClass : provider , useOptions : options } ) ; return this ; } private throwInvalidOptionsError ( from , invalid ) { throw new ValidationError ( `${ this . constructor . name }#${ from } | createApp#${ from }` , 'invalid' , from , 'options provided' ) . _setInvalidOptions ( invalid ) ; } build < T extends Client = Client < Stack > > ( ) { if ( this . customClient ) return new this . customClient ( this . token , this . options ) ; return new Client < Stack > ( this . token , this . options ) as unknown as T ; } }