feat: add makeWASocketAutoVersion for automatic version fetching
Adds a new async function that automatically fetches the latest
WhatsApp Web version from web.whatsapp.com before connecting.
Usage:
```typescript
// Option 1: Auto-fetch version (recommended)
const sock = await makeWASocketAutoVersion({ auth: state })
// Option 2: Manual version (existing behavior)
const sock = makeWASocket({ auth: state })
```
Benefits:
- No need to update library for version changes
- Automatic fallback to bundled version if fetch fails
- Logged warnings when using fallback
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { DEFAULT_CONNECTION_CONFIG } from '../Defaults'
|
||||
import type { UserFacingSocketConfig } from '../Types'
|
||||
import { fetchLatestWaWebVersion } from '../Utils/generics'
|
||||
import { makeCommunitiesSocket } from './communities'
|
||||
|
||||
// export the last socket layer
|
||||
@@ -19,4 +20,48 @@ const makeWASocket = (config: UserFacingSocketConfig) => {
|
||||
return makeCommunitiesSocket(newConfig)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a WhatsApp socket connection with automatic version fetching.
|
||||
* Fetches the latest WhatsApp Web version from web.whatsapp.com before connecting.
|
||||
* Falls back to bundled version if fetch fails.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const sock = await makeWASocketAutoVersion({
|
||||
* auth: state
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
export const makeWASocketAutoVersion = async (config: UserFacingSocketConfig) => {
|
||||
const mergedConfig = {
|
||||
...DEFAULT_CONNECTION_CONFIG,
|
||||
...config
|
||||
}
|
||||
|
||||
const logger = mergedConfig.logger
|
||||
|
||||
// Fetch latest version
|
||||
try {
|
||||
logger?.info('Fetching latest WhatsApp Web version...')
|
||||
const result = await fetchLatestWaWebVersion()
|
||||
|
||||
if (result.isLatest) {
|
||||
logger?.info({ version: result.version }, 'Using latest WhatsApp Web version')
|
||||
mergedConfig.version = result.version
|
||||
} else {
|
||||
logger?.warn(
|
||||
{ error: result.error, fallbackVersion: mergedConfig.version },
|
||||
'Failed to fetch latest version, using bundled version'
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
logger?.warn(
|
||||
{ error, fallbackVersion: mergedConfig.version },
|
||||
'Error fetching latest version, using bundled version'
|
||||
)
|
||||
}
|
||||
|
||||
return makeWASocket(mergedConfig)
|
||||
}
|
||||
|
||||
export default makeWASocket
|
||||
|
||||
Reference in New Issue
Block a user