Spaces:
Sleeping
Sleeping
File size: 3,522 Bytes
525d39f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | /**
* OpenWA JavaScript/TypeScript SDK
*
* Official client library for the OpenWA WhatsApp API Gateway.
*
* @example
* ```typescript
* import { OpenWAClient } from '@openwa/sdk';
*
* const client = new OpenWAClient({
* baseUrl: 'http://localhost:2785',
* apiKey: 'your-api-key',
* });
*
* // Send a text message
* const result = await client.messages.sendText('session-1', {
* chatId: '628123456789@c.us',
* text: 'Hello from OpenWA SDK!',
* });
* ```
*
* @packageDocumentation
*/
// ββ Client Configuration ββββββββββββββββββββββββββββββββββββββββββ
export interface OpenWAClientConfig {
/** Base URL of the OpenWA API (e.g., 'http://localhost:2785') */
baseUrl: string;
/** API key for authentication */
apiKey: string;
/** Request timeout in milliseconds (default: 30000) */
timeout?: number;
}
// ββ Response Types ββββββββββββββββββββββββββββββββββββββββββββββββ
export interface MessageResponse {
messageId: string;
timestamp: number;
}
export interface Session {
id: string;
name: string;
status: string;
phone: string | null;
pushName: string | null;
}
// ββ Client Class ββββββββββββββββββββββββββββββββββββββββββββββββββ
export class OpenWAClient {
private readonly config: Required<OpenWAClientConfig>;
constructor(config: OpenWAClientConfig) {
this.config = {
timeout: 30000,
...config,
};
}
// Placeholder β will be auto-generated from OpenAPI spec
get sessions() {
return {
list: () => this.request<Session[]>('GET', '/api/sessions'),
get: (id: string) => this.request<Session>('GET', `/api/sessions/${id}`),
create: (data: { name: string }) => this.request<Session>('POST', '/api/sessions', data),
start: (id: string) => this.request<Session>('POST', `/api/sessions/${id}/start`),
stop: (id: string) => this.request<Session>('POST', `/api/sessions/${id}/stop`),
delete: (id: string) => this.request<void>('DELETE', `/api/sessions/${id}`),
};
}
get messages() {
return {
sendText: (sessionId: string, data: { chatId: string; text: string }) =>
this.request<MessageResponse>('POST', `/api/sessions/${sessionId}/messages/text`, data),
};
}
// ββ Internal HTTP client ββββββββββββββββββββββββββββββββββββββββββ
private async request<T>(method: string, path: string, body?: unknown): Promise<T> {
const url = `${this.config.baseUrl}${path}`;
const response = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
'X-API-Key': this.config.apiKey,
},
body: body ? JSON.stringify(body) : undefined,
signal: AbortSignal.timeout(this.config.timeout),
});
if (!response.ok) {
const error = await response.json().catch(() => ({ message: response.statusText }));
throw new Error(`OpenWA API Error (${response.status}): ${(error as { message: string }).message}`);
}
// Handle empty responses (204 No Content)
if (response.status === 204) {
return undefined as T;
}
return response.json() as Promise<T>;
}
}
export default OpenWAClient;
|