36 lines
1.1 KiB
TypeScript
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;
|
|
}
|
|
},
|
|
};
|
|
}
|