Skip to content

Commit 8d97737

Browse files
author
Mohamad Yusup S.Tr.Stat
committed
feat: enhance WAHAClient with canonical API endpoints and improved type safety
1 parent 0ee0254 commit 8d97737

4 files changed

Lines changed: 255 additions & 83 deletions

File tree

README.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,85 @@ TypeScript client library for [WAHA (WhatsApp HTTP API)](https://github.com/devl
1717
- 🚀 **Easy to Use**: Simple and intuitive API
1818
- 🛡️ **Safe Send Methods**: Built-in number verification to prevent blocking
1919

20-
> **📚 Type System**: See [TYPE_SYSTEM.md](./TYPE_SYSTEM.md) for detailed documentation on types and how they're generated from the OpenAPI schema.
21-
2220
## Installation
2321

2422
```bash
2523
npm install waha-api-client-ts
2624
```
2725

26+
## Bundles and Compatibility
27+
28+
This package ships multiple bundle formats so it works both in Node.js and in web browsers:
29+
30+
- dist/index.cjs.js — CommonJS (Node require)
31+
- dist/index.esm.js — ES Module (Node & modern bundlers)
32+
- dist/index.cjs.min.js / dist/index.esm.min.js — Minified versions
33+
- dist/index.umd.js / dist/index.umd.min.js — UMD bundle for browsers (script tag / CDN)
34+
35+
The package.json `browser` and `unpkg` entry point point to the UMD minified bundle for CDN usage.
36+
37+
## Usage
38+
39+
### Node (ESM import)
40+
41+
```js
42+
import { WAHAClient } from 'waha-api-client-ts';
43+
44+
const client = new WAHAClient({ baseURL: 'https://your-waha-instance.com' });
45+
```
46+
47+
### Node (CommonJS require)
48+
49+
```js
50+
const { WAHAClient } = require('waha-api-client-ts');
51+
const client = new WAHAClient({ baseURL: 'https://your-waha-instance.com' });
52+
```
53+
54+
### Browser (ES Module)
55+
56+
If your app supports ES modules in the browser, you can import the ESM build directly from a bundler or local server:
57+
58+
```html
59+
<script type="module">
60+
import { WAHAClient } from '/node_modules/waha-api-client-ts/dist/index.esm.js';
61+
const client = new WAHAClient({ baseURL: 'https://your-waha-instance.com' });
62+
// use client...
63+
</script>
64+
```
65+
66+
### Browser (UMD Global via CDN or local file)
67+
68+
Include the UMD bundle and access the library via the global name (package name or `WAHA`):
69+
70+
```html
71+
<script src="https://unpkg.com/waha-api-client-ts@latest/dist/index.umd.min.js"></script>
72+
<script>
73+
// If package name is not a valid JS identifier the global is also available as 'WAHA'
74+
const client = (window.WAHA || window['waha-api-client-ts']).WAHAClient
75+
? new (window.WAHA || window['waha-api-client-ts']).WAHAClient({ baseURL: 'https://...' })
76+
: null;
77+
// use client...
78+
</script>
79+
```
80+
81+
## Development & Building
82+
83+
Types are generated from the included OpenAPI specification. Regenerate types and build the library with:
84+
85+
```bash
86+
# regenerate types from OpenAPI
87+
npm run generate:types
88+
89+
# build all bundles (CJS/ESM/UMD + d.ts)
90+
npm run build
91+
92+
# or build only browser-targeted bundles
93+
npm run build:browser
94+
```
95+
96+
The build process will output files to `dist/` including `index.d.ts` for TypeScript consumers.
97+
98+
2899
## Quick Start
29100

30101
```typescript
@@ -266,6 +337,15 @@ This client implements all 147 WAHA API endpoints organized into the following c
266337
- `getMessages()` / `getMessagesAlt()` - Get messages
267338
- `checkNumberStatus()` - Check if number is on WhatsApp
268339

340+
## Migration / Compatibility notes
341+
342+
We made the client follow the OpenAPI canonical endpoints for sending messages. Concretely:
343+
344+
- The library now uses the documented POST endpoints such as `POST /api/sendText`, `POST /api/sendImage`, `POST /api/sendFile`, `POST /api/sendVoice`, and `POST /api/sendVideo` and includes the required `session` field in the request body when calling those endpoints.
345+
- Legacy/alternative helpers (for example `sendTextGet()`, `getMessagesAlt()`, `sendTextAlt()`) remain in the client for backwards compatibility but are annotated as deprecated in the source. Prefer the modern methods such as `sendText()` and `getMessages(chatId)`.
346+
347+
If you previously relied on internal session-prefixed send URLs (e.g. `/api/{session}/messages/text`), update your calls to use the canonical methods above. If you need the library to retain automatic fallback to legacy endpoints (try canonical first, fallback to legacy), tell me and I can add graceful retry/fallback behavior.
348+
269349
### 💬 Chat Management (16 methods)
270350
- `getChats()` - Get all chats
271351
- `getChatsOverview()` - Get chats overview

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@
55
"description": "Waha Api Client TS for https://github.com/devlikeapro/waha (unofficial)",
66
"main": "dist/index.cjs.js",
77
"module": "dist/index.esm.js",
8+
"browser": "dist/index.umd.min.js",
89
"types": "dist/index.d.ts",
910
"exports": {
1011
".": {
1112
"import": "./dist/index.esm.js",
1213
"require": "./dist/index.cjs.js",
14+
"browser": "./dist/index.umd.min.js",
1315
"types": "./dist/index.d.ts"
1416
}
1517
},
18+
"unpkg": "dist/index.umd.min.js",
1619
"files": [
1720
"dist"
1821
],
1922
"scripts": {
20-
"build": "rollup -c",
23+
"build": "rollup -c",
24+
"build:browser": "rollup -c --environment TARGET:browser",
2125
"build:watch": "rollup -c --watch",
2226
"test": "tsx example/index.ts",
2327
"prepare": "npm run build",

rollup.config.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { readFileSync } from 'fs';
77

88
const packageJson = JSON.parse(readFileSync('./package.json', 'utf8'));
99

10-
// Main build configuration
11-
const buildConfig = {
10+
// Node-targeted build configuration (CJS + ESM)
11+
const nodeBuildConfig = {
1212
input: 'src/index.ts',
1313
external: [], // No external dependencies - uses native fetch API
1414
output: [
@@ -37,6 +37,7 @@ const buildConfig = {
3737
],
3838
plugins: [
3939
resolve({
40+
// Node-first resolution for these builds
4041
browser: false,
4142
preferBuiltins: true,
4243
}),
@@ -50,6 +51,44 @@ const buildConfig = {
5051
],
5152
};
5253

54+
// Browser-targeted build configuration (UMD).
55+
// Produces a UMD bundle suitable for <script> inclusion and CDN usage.
56+
const browserBuildConfig = {
57+
input: 'src/index.ts',
58+
external: [],
59+
output: [
60+
{
61+
file: 'dist/index.umd.js',
62+
format: 'umd',
63+
name: packageJson.name || 'WAHA',
64+
sourcemap: true,
65+
globals: {},
66+
},
67+
{
68+
file: 'dist/index.umd.min.js',
69+
format: 'umd',
70+
name: packageJson.name || 'WAHA',
71+
sourcemap: true,
72+
plugins: [terser()],
73+
globals: {},
74+
},
75+
],
76+
plugins: [
77+
// For browser bundles prefer browser field and do not prefer builtins
78+
resolve({
79+
browser: true,
80+
preferBuiltins: false,
81+
}),
82+
commonjs(),
83+
typescript({
84+
tsconfig: './tsconfig.json',
85+
declaration: false,
86+
declarationMap: false,
87+
sourceMap: true,
88+
}),
89+
],
90+
};
91+
5392
// Type definitions build configuration
5493
const dtsConfig = {
5594
input: 'src/index.ts',
@@ -60,4 +99,4 @@ const dtsConfig = {
6099
plugins: [dts()],
61100
};
62101

63-
export default [buildConfig, dtsConfig];
102+
export default [nodeBuildConfig, browserBuildConfig, dtsConfig];

0 commit comments

Comments
 (0)