Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
refactor(engine): type the lazy Baileys module (drop any + unsafe suppressions)
rmyndharis6a014f677f76
diff --git a/src/engine/adapters/baileys-message-store.service.ts b/src/engine/adapters/baileys-message-store.service.tsindex 8a76488eb..f82095f49 100644--- a/src/engine/adapters/baileys-message-store.service.ts+++ b/src/engine/adapters/baileys-message-store.service.ts@@ -1,6 +1,7 @@import { Injectable } from '@nestjs/common';import { InjectRepository } from '@nestjs/typeorm';import { Repository } from 'typeorm';+import type * as BaileysLib from '@whiskeysockets/baileys';import type { WAMessage } from '@whiskeysockets/baileys';import { BaileysStoredMessage } from './baileys-stored-message.entity';import { BaileysMessageStore } from '../types/baileys.types';@@ -13,9 +14,9 @@ function positiveIntFromEnv(name: string, fallback: number): number {@Injectable()export class BaileysMessageStoreService implements BaileysMessageStore {/** Lazily loaded @whiskeysockets/baileys module (ESM-only; loaded on first use, not at boot). */- private baileysLib: any;+ private baileysLib?: typeof BaileysLib;- private async loadLib(): Promise<any> {+ private async loadLib(): Promise<typeof BaileysLib> {return (this.baileysLib ??= await import('@whiskeysockets/baileys'));}@@ -29,9 +30,7 @@ export class BaileysMessageStoreService implements BaileysMessageStore {if (!waMessageId) {return;}- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignmentconst { BufferJSON } = await this.loadLib();- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-accessconst serializedMessage = JSON.stringify(msg, BufferJSON.replacer);// Idempotent: the same message arrives from the send return AND the messages.upsert echo.await this.repo.upsert({ sessionId, waMessageId, serializedMessage }, ['sessionId', 'waMessageId']);@@ -43,9 +42,7 @@ export class BaileysMessageStoreService implements BaileysMessageStore {if (!row) {return null;}- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignmentconst { BufferJSON } = await this.loadLib();- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-accessreturn JSON.parse(row.serializedMessage, BufferJSON.reviver) as WAMessage;}diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.tsindex 1d10a73c3..57c24e6fc 100644--- a/src/engine/adapters/baileys.adapter.ts+++ b/src/engine/adapters/baileys.adapter.ts@@ -1,4 +1,5 @@import * as path from 'path';+import type * as BaileysLib from '@whiskeysockets/baileys';import type { AnyMessageContent, MiscMessageGenerationOptions, WAMessage, WASocket } from '@whiskeysockets/baileys';import { buildIncomingMessageFromBaileys, mapBaileysStatus } from './baileys-message-mapper';import { mapBaileysGroup, mapBaileysGroupInfo } from './baileys-group-mapper';@@ -66,9 +67,9 @@ export class BaileysAdapter implements IWhatsAppEngine {private callbacks: EngineEventCallbacks = {};private intentionalClose = false;/** Lazily loaded @whiskeysockets/baileys module (ESM-only; loaded on first connect, not at boot). */- private lib: any;+ private lib?: typeof BaileysLib;- private async loadLib(): Promise<any> {+ private async loadLib(): Promise<typeof BaileysLib> {return (this.lib ??= await import('@whiskeysockets/baileys'));}@@ -94,18 +95,12 @@ export class BaileysAdapter implements IWhatsAppEngine {private async connect(): Promise<void> {this.setStatus(EngineStatus.INITIALIZING);- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignmentconst b = await this.loadLib();- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-callconst { state, saveCreds } = await b.useMultiFileAuthState(this.authPath);- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-callconst { version } = await b.fetchLatestBaileysVersion();- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-callconst sock = b.default({- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignmentauth: state,- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignmentversion,browser: BAILEYS_BROWSER,printQRInTerminal: false,@@ -113,11 +108,10 @@ export class BaileysAdapter implements IWhatsAppEngine {// the type through a deep import path that TypeScript does not auto-unify here.// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertionlogger: createSilentLogger() as unknown as ILogger,- }) as WASocket;+ });this.sock = sock;- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument- sock.ev.on('creds.update', saveCreds);+ sock.ev.on('creds.update', () => void saveCreds());sock.ev.on('connection.update', update => this.handleConnectionUpdate(update));sock.ev.on('messages.upsert', event => this.handleMessagesUpsert(event));sock.ev.on('messages.update', updates => this.handleMessagesUpdate(updates));@@ -169,7 +163,6 @@ export class BaileysAdapter implements IWhatsAppEngine {return;}- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-accessif (statusCode === this.lib?.DisconnectReason.loggedOut) {// Credentials invalidated — terminal. Re-linking requires a fresh QR/pairing.this.setStatus(EngineStatus.DISCONNECTED);@@ -617,7 +610,6 @@ export class BaileysAdapter implements IWhatsAppEngine {private mapMessage(msg: WAMessage): IncomingMessage {const content = msg.message ?? {};- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-callconst contentType = this.lib?.getContentType(msg.message ?? undefined);const body = content.conversation ?? content.extendedTextMessage?.text ?? '';return buildIncomingMessageFromBaileys({@@ -626,7 +618,6 @@ export class BaileysAdapter implements IWhatsAppEngine {fromMe: msg.key.fromMe === true,participant: msg.key.participant ?? undefined,body,- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignmentcontentType,isPtt: content.audioMessage?.ptt === true,timestamp: this.toUnixSeconds(msg.messageTimestamp),