Fix @xmpp typing.

This commit is contained in:
John Livingston
2021-12-08 12:29:21 +01:00
parent 0e45f9a197
commit 2c72f3bf2f
4 changed files with 202 additions and 27 deletions

View File

@ -1,8 +1,8 @@
/* eslint-disable no-void */
import { logger } from '../logger'
import { XMPP, XMPPXmlFunction, XMPPStanza, XMPPAddress } from './types'
const { component, xml } = require('@xmpp/component')
import type { XMPPStanza } from './types'
import { component, xml, Component } from '@xmpp/component'
import type { JID } from '@xmpp/jid'
interface ComponentConnectionConfig {
service: string
@ -11,22 +11,21 @@ interface ComponentConnectionConfig {
}
abstract class ComponentBot {
protected xmpp?: XMPP
protected address?: XMPPAddress
protected xmpp?: Component
protected address?: JID
protected xml = xml
constructor (
public readonly botName: string,
protected readonly connectionConfig: ComponentConnectionConfig
) {}
protected xml: XMPPXmlFunction = (...args) => xml(...args)
public async connect (): Promise<void> {
this.xmpp = component({
service: this.connectionConfig.service,
domain: this.connectionConfig.domain,
password: this.connectionConfig.password
}) as XMPP
})
this.xmpp.on('error', (err: any) => {
logger.error(err)
@ -49,14 +48,14 @@ abstract class ComponentBot {
}
})
this.xmpp.on('online', (address: XMPPAddress) => {
this.xmpp.on('online', (address) => {
logger.debug('Online with address' + address.toString())
this.address = address
void this.onOnline()
})
this.xmpp.start()
await this.xmpp.start()
}
public async stop (): Promise<any> {

View File

@ -1,21 +1,7 @@
import { EventEmitter } from 'events'
export interface XMPP extends EventEmitter {
send: (xml: any) => any
start: () => any
stop: () => Promise<any>
}
export interface XMPPAddress {
toString: () => string
}
import type { Element } from '@xmpp/xml'
export type XMPPStanzaType = 'message' | 'iq' | 'presence'
export interface XMPPStanza {
attrs: any
is: (type: XMPPStanzaType) => boolean
toString: () => string
export interface XMPPStanza extends Element {
name: XMPPStanzaType
}
export type XMPPXmlFunction = (type: string, attrs: object, content?: any) => any