fix: downloadMediaMessage return type matched parameter (#757)

* fix: downloadMediaMessage return type matched parameter

* chore: keyword-spacing eslint rule warn instead of error

* chore: linting

aaaaaaaaaaaaaaaaaaaa
This commit is contained in:
arthur simas
2024-05-15 12:34:06 -03:00
committed by GitHub
parent 17ce9df557
commit 2ae664c655
2 changed files with 22 additions and 18 deletions

View File

@@ -27,6 +27,9 @@
"selector": "TSEnumDeclaration",
"message": "Don't declare enums, use literals instead"
}
],
"keyword-spacing": [
"warn"
]
}
}

View File

@@ -3,6 +3,7 @@ import axios from 'axios'
import { randomBytes } from 'crypto'
import { promises as fs } from 'fs'
import { Logger } from 'pino'
import { type Transform } from 'stream'
import { proto } from '../../WAProto'
import { MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults'
import {
@@ -868,31 +869,31 @@ const REUPLOAD_REQUIRED_STATUS = [410, 404]
/**
* Downloads the given message. Throws an error if it's not a media message
*/
export const downloadMediaMessage = async(
export const downloadMediaMessage = async<Type extends 'buffer' | 'stream'>(
message: WAMessage,
type: 'buffer' | 'stream',
type: Type,
options: MediaDownloadOptions,
ctx?: DownloadMediaMessageContext
) => {
try {
const result = await downloadMsg()
return result
} catch(error) {
if(ctx) {
if(axios.isAxiosError(error)) {
// check if the message requires a reupload
if(REUPLOAD_REQUIRED_STATUS.includes(error.response?.status!)) {
ctx.logger.info({ key: message.key }, 'sending reupload media request...')
// request reupload
message = await ctx.reuploadRequest(message)
const result = await downloadMsg()
return result
const result = await downloadMsg()
.catch(async(error) => {
if(ctx) {
if(axios.isAxiosError(error)) {
// check if the message requires a reupload
if(REUPLOAD_REQUIRED_STATUS.includes(error.response?.status!)) {
ctx.logger.info({ key: message.key }, 'sending reupload media request...')
// request reupload
message = await ctx.reuploadRequest(message)
const result = await downloadMsg()
return result
}
}
}
}
throw error
}
throw error
})
return result as Type extends 'buffer' ? Buffer : Transform
async function downloadMsg() {
const mContent = extractMessageContent(message.message)