srs-api/src/whatsapp/bridge.ts
Aswin Biju 6851d66b38 feat: SRS backend: orders intake, results pipeline, user management
Fastify + TypeScript + SQLite backend for a single Kerala lottery agency.
2026-11-03 10:09:46 +05:30

36 lines
1.1 KiB
TypeScript

export interface Bridge {
sendText(chatId: string, message: string): Promise<{ messageIds: string[] }>;
health(): Promise<{ status: string; queueLength?: number } | null>;
}
export function createBridge(baseUrl: string, fetchImpl: typeof fetch = fetch): Bridge {
const base = baseUrl.replace(/\/+$/, '');
return {
async sendText(chatId, message) {
const res = await fetchImpl(`${base}/send`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ chatId, message }),
signal: AbortSignal.timeout(15_000),
});
if (!res.ok) {
throw new Error(`bridge /send failed with HTTP ${res.status}`);
}
const body = (await res.json()) as { messageIds?: string[] };
return { messageIds: body.messageIds ?? [] };
},
async health() {
try {
const res = await fetchImpl(`${base}/health`, { signal: AbortSignal.timeout(2_000) });
if (!res.ok) return null;
return (await res.json()) as { status: string; queueLength?: number };
} catch {
return null;
}
},
};
}