refactor: replace axios with fetch API and update related types (#1666)
This commit is contained in:
committed by
GitHub
parent
cfe7a2b2b3
commit
fb12f6d9d3
+20
-10
@@ -1,5 +1,4 @@
|
||||
import { Boom } from '@hapi/boom'
|
||||
import axios, { type AxiosRequestConfig } from 'axios'
|
||||
import { createHash, randomBytes } from 'crypto'
|
||||
import { proto } from '../../WAProto/index.js'
|
||||
const baileysVersion = [2, 3000, 1023223821]
|
||||
@@ -225,16 +224,21 @@ export const bindWaitForConnectionUpdate = (ev: BaileysEventEmitter) => bindWait
|
||||
* 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 (options: AxiosRequestConfig<{}> = {}) => {
|
||||
export const fetchLatestBaileysVersion = async (options: RequestInit = {}) => {
|
||||
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts'
|
||||
try {
|
||||
const result = await axios.get<string>(URL, {
|
||||
...options,
|
||||
responseType: 'text'
|
||||
const response = await fetch(URL, {
|
||||
dispatcher: options.dispatcher,
|
||||
method: 'GET',
|
||||
headers: options.headers
|
||||
})
|
||||
if (!response.ok) {
|
||||
throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status })
|
||||
}
|
||||
|
||||
const text = await response.text()
|
||||
// Extract version from line 7 (const version = [...])
|
||||
const lines = result.data.split('\n')
|
||||
const lines = text.split('\n')
|
||||
const versionLine = lines[6] // Line 7 (0-indexed)
|
||||
const versionMatch = versionLine!.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/)
|
||||
|
||||
@@ -261,12 +265,18 @@ export const fetchLatestBaileysVersion = async (options: AxiosRequestConfig<{}>
|
||||
* 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 (options: AxiosRequestConfig<{}>) => {
|
||||
export const fetchLatestWaWebVersion = async (options: RequestInit = {}) => {
|
||||
try {
|
||||
const { data } = await axios.get('https://web.whatsapp.com/sw.js', {
|
||||
...options,
|
||||
responseType: 'json'
|
||||
const response = await fetch('https://web.whatsapp.com/sw.js', {
|
||||
dispatcher: options.dispatcher,
|
||||
method: 'GET',
|
||||
headers: options.headers
|
||||
})
|
||||
if (!response.ok) {
|
||||
throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status })
|
||||
}
|
||||
|
||||
const data = await response.text()
|
||||
|
||||
const regex = /\\?"client_revision\\?":\s*(\d+)/
|
||||
const match = data.match(regex)
|
||||
|
||||
Reference in New Issue
Block a user