chore(core): expose init params in .params field

This commit is contained in:
alina 🌸 2023-12-16 20:00:31 +03:00
parent dfed3c2761
commit 4f7fd24dd9
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
3 changed files with 29 additions and 44 deletions

View file

@ -21,9 +21,8 @@ export async function sendCode(
const res = await client.call({ const res = await client.call({
_: 'auth.sendCode', _: 'auth.sendCode',
phoneNumber: phone, phoneNumber: phone,
apiId: client.network._initConnectionParams.apiId, apiId: client.params.apiId,
// eslint-disable-next-line dot-notation apiHash: client.params.apiHash,
apiHash: client['_apiHash'],
settings: { _: 'codeSettings' }, settings: { _: 'codeSettings' },
}) })

View file

@ -14,9 +14,8 @@ export async function signInBot(client: BaseTelegramClient, token: string): Prom
const res = await client.call({ const res = await client.call({
_: 'auth.importBotAuthorization', _: 'auth.importBotAuthorization',
flags: 0, flags: 0,
apiId: client.network._initConnectionParams.apiId, apiId: client.params.apiId,
// eslint-disable-next-line dot-notation apiHash: client.params.apiHash,
apiHash: client['_apiHash'],
botAuthToken: token, botAuthToken: token,
}) })

View file

@ -45,16 +45,6 @@ export class BaseTelegramClient extends EventEmitter {
*/ */
readonly storage: ITelegramStorage readonly storage: ITelegramStorage
/**
* API hash taken from {@link BaseTelegramClientOptions.apiHash}
*/
protected readonly _apiHash: string
/**
* "Use IPv6" taken from {@link BaseTelegramClientOptions.useIpv6}
*/
protected readonly _useIpv6: boolean
/** /**
* "Test mode" taken from {@link BaseTelegramClientOptions.testMode} * "Test mode" taken from {@link BaseTelegramClientOptions.testMode}
*/ */
@ -90,60 +80,57 @@ export class BaseTelegramClient extends EventEmitter {
readonly log = new LogManager('client') readonly log = new LogManager('client')
readonly network: NetworkManager readonly network: NetworkManager
constructor(opts: BaseTelegramClientOptions) { constructor(readonly params: BaseTelegramClientOptions) {
super() super()
if (opts.logLevel !== undefined) { if (params.logLevel !== undefined) {
this.log.level = opts.logLevel this.log.level = params.logLevel
} }
this.crypto = (opts.crypto ?? defaultCryptoProviderFactory)() this.crypto = (params.crypto ?? defaultCryptoProviderFactory)()
this.storage = params.storage
this._testMode = Boolean(params.testMode)
this.storage = opts.storage let dc = params.defaultDcs
this._apiHash = opts.apiHash
this._useIpv6 = Boolean(opts.useIpv6)
this._testMode = Boolean(opts.testMode)
let dc = opts.defaultDcs
if (!dc) { if (!dc) {
if (this._testMode) { if (params.testMode) {
dc = this._useIpv6 ? defaultTestIpv6Dc : defaultTestDc dc = params.useIpv6 ? defaultTestIpv6Dc : defaultTestDc
} else { } else {
dc = this._useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc dc = params.useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc
} }
} }
this._defaultDcs = dc this._defaultDcs = dc
this._niceStacks = opts.niceStacks ?? true this._niceStacks = params.niceStacks ?? true
this._layer = opts.overrideLayer ?? tl.LAYER this._layer = params.overrideLayer ?? tl.LAYER
this._readerMap = opts.readerMap ?? defaultReaderMap this._readerMap = params.readerMap ?? defaultReaderMap
this._writerMap = opts.writerMap ?? defaultWriterMap this._writerMap = params.writerMap ?? defaultWriterMap
this.network = new NetworkManager( this.network = new NetworkManager(
{ {
apiId: opts.apiId, apiId: params.apiId,
crypto: this.crypto, crypto: this.crypto,
disableUpdates: opts.disableUpdates ?? false, disableUpdates: params.disableUpdates ?? false,
initConnectionOptions: opts.initConnectionOptions, initConnectionOptions: params.initConnectionOptions,
layer: this._layer, layer: this._layer,
log: this.log, log: this.log,
readerMap: this._readerMap, readerMap: this._readerMap,
writerMap: this._writerMap, writerMap: this._writerMap,
reconnectionStrategy: opts.reconnectionStrategy, reconnectionStrategy: params.reconnectionStrategy,
storage: this.storage, storage: this.storage,
testMode: this._testMode, testMode: Boolean(params.testMode),
transport: opts.transport, transport: params.transport,
_emitError: this._emitError.bind(this), _emitError: this._emitError.bind(this),
floodSleepThreshold: opts.floodSleepThreshold ?? 10000, floodSleepThreshold: params.floodSleepThreshold ?? 10000,
maxRetryCount: opts.maxRetryCount ?? 5, maxRetryCount: params.maxRetryCount ?? 5,
isPremium: false, isPremium: false,
useIpv6: Boolean(opts.useIpv6), useIpv6: Boolean(params.useIpv6),
keepAliveAction: this._keepAliveAction.bind(this), keepAliveAction: this._keepAliveAction.bind(this),
enableErrorReporting: opts.enableErrorReporting ?? false, enableErrorReporting: params.enableErrorReporting ?? false,
onUsable: () => this.emit('usable'), onUsable: () => this.emit('usable'),
...(opts.network ?? {}), ...(params.network ?? {}),
}, },
this._config, this._config,
) )