From 116b30dff059d60e8dea13687ffb4a3d67969f0a Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Mon, 19 Sep 2022 17:25:54 +0530 Subject: [PATCH] feat: expose axios options --- src/Defaults/index.ts | 1 + src/Types/Socket.ts | 4 ++++ src/Utils/generics.ts | 22 +++++++++++++++++----- src/Utils/messages-media.ts | 12 ++++++++++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index 93334c4..d316715 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -46,6 +46,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { linkPreviewImageThumbnailWidth: 192, transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 }, generateHighQualityLinkPreview: false, + options: { }, getMessage: async() => undefined } diff --git a/src/Types/Socket.ts b/src/Types/Socket.ts index 78625d0..3a00333 100644 --- a/src/Types/Socket.ts +++ b/src/Types/Socket.ts @@ -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 /** * 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 diff --git a/src/Utils/generics.ts b/src/Utils/generics.ts index 4fad4e4..4ad9a66 100644 --- a/src/Utils/generics.ts +++ b/src/Utils/generics.ts @@ -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) => { 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) => { 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, diff --git a/src/Utils/messages-media.ts b/src/Utils/messages-media.ts index 103aa77..cbc76e7 100644 --- a/src/Utils/messages-media.ts +++ b/src/Utils/messages-media.ts @@ -376,6 +376,7 @@ const toSmallestChunkSize = (num: number) => { export type MediaDownloadOptions = { startByte?: number endByte?: number + options?: AxiosRequestConfig } 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): WAMediaUploadFunction => { +export const getWAUploadToServer = ( + { customUploadHosts, fetchAgent, logger, options }: SocketConfig, + refreshMediaConn: (force: boolean) => Promise, +): 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 },