chore(node): allow omitting input params

This commit is contained in:
alina 🌸 2023-12-13 21:43:46 +03:00
parent 38358622e7
commit b261a661d2
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
6 changed files with 48 additions and 42 deletions

View file

@ -1,6 +1,6 @@
<p align="center">
<a href="https://github.com/mtcute/mtcute/">
<img src="./.github/logo.svg" alt="mtcute logo" title="mtcute" width="480" /><br/>
<img src="https://raw.githubusercontent.com/mtcute/mtcute/master/.github/logo.svg" alt="mtcute logo" title="mtcute" width="480" /><br/>
</a><br/>
<b>modern typescript library for mtproto</b>
<br>
@ -41,12 +41,8 @@ dp.onNewMessage(filters.chat('private'), async (msg) => {
await msg.replyText('hiiii from mtcute! 🌸')
})
tg.run({
phone: () => tg.input('phone > '),
code: () => tg.input('code > '),
password: () => tg.input('password > ')
}, async (self) => {
console.log(`logged in as ${self.displayName}`)
tg.run({ /* optional params */ }, async (self) => {
console.log(`✨ logged in as ${self.displayName}`)
})
```

View file

@ -14,7 +14,7 @@ export function exec(cwd: string, ...cmd: string[]) {
if (code === 0) {
resolve()
} else {
reject(new Error(`Process exited with code ${code}`))
reject(new Error(`Process "${cmd.join(' ')}" exited with code ${code}`))
}
})
})

View file

@ -30,12 +30,6 @@ dp.onNewMessage(filters.start, async (msg) => {
tg.run(
{{#if botToken}}
{ botToken: env.BOT_TOKEN },
{{else}}
{
phone: () => tg.input('Phone > '),
code: () => tg.input('Code > '),
password: () => tg.input('2FA password > '),
},
{{/if}}
(user) => {
console.log('Logged in as', user.username)

View file

@ -30,12 +30,6 @@ dp.onNewMessage(filters.start, async (msg) => {
tg.run(
{{#if botToken}}
{ botToken: env.BOT_TOKEN },
{{else}}
{
phone: () => tg.input('Phone > '),
code: () => tg.input('Code > '),
password: () => tg.input('2FA password > '),
},
{{/if}}
(user) => {
console.log('Logged in as', user.username)

View file

@ -4,7 +4,7 @@
All-in-one package for NodeJS. Includes support for native crypto addon
(must be installed separately, `@mtcute/crypto-node`), terminal I/O via
`readline` and comes with pre-installed HTML and Markdown parsers.
`readline` and includes HTML and Markdown parsers.
## Usage
@ -16,4 +16,8 @@ const tg = new NodeTelegramClient({
apiHash: 'abcdef',
storage: 'my-account'
})
tg.run(async (user) => {
console.log(`✨ logged in as ${user.displayName}`)
})
```

View file

@ -1,7 +1,7 @@
import { createRequire } from 'module'
import { createInterface, Interface as RlInterface } from 'readline'
import { TelegramClient, TelegramClientOptions } from '@mtcute/client'
import { TelegramClient, TelegramClientOptions, User } from '@mtcute/client'
import { SqliteStorage } from '@mtcute/sqlite'
export * from '@mtcute/client'
@ -20,31 +20,15 @@ try {
nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider
} catch (e) {}
export interface NodeTelegramClientOptions extends Omit<TelegramClientOptions, 'storage'> {
/**
* Storage to use.
*
* You can pass a file name as a simple string,
* which will be passed directly to `SqliteStorage`
*
* Defaults to SQLite storage in `client.session` file in
* current working directory
*/
storage?: TelegramClientOptions['storage'] | string
}
/**
* Tiny wrapper over {@link TelegramClient} for usage inside Node JS.
*
* This automatically sets the parse modes, native
* crypto addon and defaults to SQLite session.
*
* Documentation for this class only contains the
* difference between {@link TelegramClient} and {@link NodeTelegramClient}.
* For the complete documentation, please refer to {@link TelegramClient}.
* This class automatically manages native
* crypto addon and defaults to SQLite session (unlike `TelegarmClient`,
* which defaults to a JSON file on Node).
*/
export class NodeTelegramClient extends TelegramClient {
constructor(opts: NodeTelegramClientOptions) {
constructor(opts: TelegramClientOptions) {
super({
// eslint-disable-next-line
crypto: nativeCrypto ? () => new nativeCrypto() : undefined,
@ -83,4 +67,38 @@ export class NodeTelegramClient extends TelegramClient {
return super.close()
}
start(params: Parameters<TelegramClient['start']>[0] = {}): Promise<User> {
if (!params.botToken) {
if (!params.phone) params.phone = () => this.input('phone > ')
if (!params.code) params.code = () => this.input('code > ')
if (!params.password) {
params.password = () => this.input('2fa password > ')
}
}
return super.start(params).then((user) => {
if (this._rl) {
this._rl.close()
delete this._rl
}
return user
})
}
run(
params: Parameters<TelegramClient['start']>[0] | ((user: User) => void | Promise<void>),
then?: (user: User) => void | Promise<void>,
): void {
if (typeof params === 'function') {
then = params
params = {}
}
this.start(params)
.then(then)
.catch((err) => this._emitError(err))
}
}