fix: add TypeScript types to use-multi-file-auth-state-prisma.ts functions

Added TypeScript types to functions in use-multi-file-auth-state-prisma.ts to improve type safety and code readability. This change helps catch type-related errors during development and enhances the maintainability of the codebase.
This commit is contained in:
Davidson Gomes 2024-06-17 13:42:18 -03:00
parent 85106667a2
commit d45e303d55

View File

@ -1,4 +1,4 @@
import { BufferJSON, initAuthCreds, WAProto as proto } from 'baileys'; import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys';
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; import path from 'path';
@ -7,7 +7,7 @@ import { prismaServer } from '../libs/prisma.connect';
const prisma = prismaServer; const prisma = prismaServer;
const fixFileName = (file) => { const fixFileName = (file: string): string | undefined => {
if (!file) { if (!file) {
return undefined; return undefined;
} }
@ -16,7 +16,7 @@ const fixFileName = (file) => {
return replacedColon; return replacedColon;
}; };
export async function keyExists(sessionId) { export async function keyExists(sessionId: string): Promise<any> {
try { try {
const key = await prisma.session.findUnique({ where: { sessionId: sessionId } }); const key = await prisma.session.findUnique({ where: { sessionId: sessionId } });
return !!key; return !!key;
@ -26,10 +26,10 @@ export async function keyExists(sessionId) {
} }
} }
export async function saveKey(sessionId, keyJson) { export async function saveKey(sessionId: string, keyJson: any): Promise<any> {
const jaExiste = await keyExists(sessionId); const exists = await keyExists(sessionId);
try { try {
if (!jaExiste) if (!exists)
return await prisma.session.create({ return await prisma.session.create({
data: { data: {
sessionId: sessionId, sessionId: sessionId,
@ -46,10 +46,10 @@ export async function saveKey(sessionId, keyJson) {
} }
} }
export async function getAuthKey(sessionId) { export async function getAuthKey(sessionId: string): Promise<any> {
try { try {
const registro = await keyExists(sessionId); const register = await keyExists(sessionId);
if (!registro) return null; if (!register) return null;
const auth = await prisma.session.findUnique({ where: { sessionId: sessionId } }); const auth = await prisma.session.findUnique({ where: { sessionId: sessionId } });
return JSON.parse(auth?.creds); return JSON.parse(auth?.creds);
} catch (error) { } catch (error) {
@ -58,17 +58,17 @@ export async function getAuthKey(sessionId) {
} }
} }
async function deleteAuthKey(sessionId) { async function deleteAuthKey(sessionId: string): Promise<any> {
try { try {
const registro = await keyExists(sessionId); const register = await keyExists(sessionId);
if (!registro) return; if (!register) return;
await prisma.session.delete({ where: { sessionId: sessionId } }); await prisma.session.delete({ where: { sessionId: sessionId } });
} catch (error) { } catch (error) {
console.log('2', `${error}`); console.log('2', `${error}`);
} }
} }
async function fileExists(file) { async function fileExists(file: string): Promise<any> {
try { try {
const stat = await fs.stat(file); const stat = await fs.stat(file);
if (stat.isFile()) return true; if (stat.isFile()) return true;
@ -77,12 +77,15 @@ async function fileExists(file) {
} }
} }
export default async function useMultiFileAuthStatePrisma(sessionId) { export default async function useMultiFileAuthStatePrisma(sessionId: string): Promise<{
state: AuthenticationState;
saveCreds: () => Promise<void>;
}> {
const localFolder = path.join(INSTANCE_DIR, sessionId); const localFolder = path.join(INSTANCE_DIR, sessionId);
const localFile = (key) => path.join(localFolder, fixFileName(key) + '.json'); const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
await fs.mkdir(localFolder, { recursive: true }); await fs.mkdir(localFolder, { recursive: true });
async function writeData(data, key) { async function writeData(data: any, key: string): Promise<any> {
const dataString = JSON.stringify(data, BufferJSON.replacer); const dataString = JSON.stringify(data, BufferJSON.replacer);
if (key != 'creds') { if (key != 'creds') {
@ -93,7 +96,7 @@ export default async function useMultiFileAuthStatePrisma(sessionId) {
return; return;
} }
async function readData(key) { async function readData(key: string): Promise<any> {
try { try {
let rawData; let rawData;
@ -111,7 +114,7 @@ export default async function useMultiFileAuthStatePrisma(sessionId) {
} }
} }
async function removeData(key) { async function removeData(key: string): Promise<any> {
try { try {
if (key != 'creds') { if (key != 'creds') {
await fs.unlink(localFile(key)); await fs.unlink(localFile(key));