feat: expose axios options

This commit is contained in:
Adhiraj Singh
2022-09-19 17:25:54 +05:30
parent 5c48cc8e61
commit 116b30dff0
4 changed files with 32 additions and 7 deletions

View File

@@ -46,6 +46,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
linkPreviewImageThumbnailWidth: 192,
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
generateHighQualityLinkPreview: false,
options: { },
getMessage: async() => undefined
}

View File

@@ -1,4 +1,5 @@
import { AxiosRequestConfig } from 'axios'
import type { Agent } from 'https'
import type NodeCache from 'node-cache'
import type { Logger } from 'pino'
@@ -68,6 +69,9 @@ export type SocketConfig = {
* entails uploading the jpegThumbnail to WA
* */
generateHighQualityLinkPreview: boolean
/** options for axios */
options: AxiosRequestConfig<any>
/**
* fetch a message from your store
* implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried

View File

@@ -1,5 +1,5 @@
import { Boom } from '@hapi/boom'
import axios from 'axios'
import axios, { AxiosRequestConfig } from 'axios'
import { randomBytes } from 'crypto'
import { platform, release } from 'os'
import { Logger } from 'pino'
@@ -218,10 +218,16 @@ export const printQRIfNecessaryListener = (ev: BaileysEventEmitter, logger: Logg
* utility that fetches latest baileys version from the master branch.
* Use to ensure your WA connection is always on the latest version
*/
export const fetchLatestBaileysVersion = async() => {
export const fetchLatestBaileysVersion = async(options: AxiosRequestConfig<any>) => {
const URL = 'https://raw.githubusercontent.com/adiwajshing/Baileys/master/src/Defaults/baileys-version.json'
try {
const result = await axios.get<{ version: WAVersion }>(URL, { responseType: 'json' })
const result = await axios.get<{ version: WAVersion }>(
URL,
{
...options,
responseType: 'json'
}
)
return {
version: result.data.version,
isLatest: true
@@ -239,9 +245,15 @@ export const fetchLatestBaileysVersion = async() => {
* A utility that fetches the latest web version of whatsapp.
* Use to ensure your WA connection is always on the latest version
*/
export const fetchLatestWaWebVersion = async() => {
export const fetchLatestWaWebVersion = async(options: AxiosRequestConfig<any>) => {
try {
const result = await axios.get('https://web.whatsapp.com/check-update?version=1&platform=web', { responseType: 'json' })
const result = await axios.get(
'https://web.whatsapp.com/check-update?version=1&platform=web',
{
...options,
responseType: 'json'
}
)
const version = result.data.currentVersion.split('.')
return {
version: [+version[0], +version[1], +version[2]] as WAVersion,

View File

@@ -376,6 +376,7 @@ const toSmallestChunkSize = (num: number) => {
export type MediaDownloadOptions = {
startByte?: number
endByte?: number
options?: AxiosRequestConfig<any>
}
export const getUrlFromDirectPath = (directPath: string) => `https://${DEF_HOST}${directPath}`
@@ -398,7 +399,7 @@ export const downloadContentFromMessage = (
export const downloadEncryptedContent = async(
downloadUrl: string,
{ cipherKey, iv }: MediaDecryptionKeyInfo,
{ startByte, endByte }: MediaDownloadOptions = { }
{ startByte, endByte, options }: MediaDownloadOptions = { }
) => {
let bytesFetched = 0
let startChunk = 0
@@ -417,6 +418,7 @@ export const downloadEncryptedContent = async(
const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined
const headers: { [_: string]: string } = {
...options?.headers || { },
Origin: DEFAULT_ORIGIN,
}
if(startChunk || endChunk) {
@@ -430,6 +432,7 @@ export const downloadEncryptedContent = async(
const fetched = await getHttpStream(
downloadUrl,
{
...options || { },
headers,
maxBodyLength: Infinity,
maxContentLength: Infinity,
@@ -514,7 +517,10 @@ export function extensionForMediaMessage(message: WAMessageContent) {
return extension
}
export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: SocketConfig, refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>): WAMediaUploadFunction => {
export const getWAUploadToServer = (
{ customUploadHosts, fetchAgent, logger, options }: SocketConfig,
refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>,
): WAMediaUploadFunction => {
return async(stream, { mediaType, fileEncSha256B64, timeoutMs }) => {
const { default: axios } = await import('axios')
// send a query JSON to obtain the url & auth token to upload our media
@@ -546,7 +552,9 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: S
url,
reqBody,
{
...options,
headers: {
...options.headers || { },
'Content-Type': 'application/octet-stream',
'Origin': DEFAULT_ORIGIN
},