mirror of
https://dev.azure.com/globalhealthx/EMR/_git/helix-engage-server
synced 2026-05-18 20:08:19 +00:00
- Comprehensive docs: embed snippet, key management, API endpoints, chat/booking/contact flows, lead dedup, reCAPTCHA, branding, deploy checklist, troubleshooting - Widget Preact source archived in packages/widget-src/ (was only on local machine, not tracked in any repo) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
import type { WidgetConfig, Doctor, TimeSlot } from './types';
|
|
|
|
let baseUrl = '';
|
|
let widgetKey = '';
|
|
|
|
export const initApi = (url: string, key: string) => {
|
|
baseUrl = url;
|
|
widgetKey = key;
|
|
};
|
|
|
|
const headers = () => ({
|
|
'Content-Type': 'application/json',
|
|
'X-Widget-Key': widgetKey,
|
|
});
|
|
|
|
export const fetchInit = async (): Promise<WidgetConfig> => {
|
|
const res = await fetch(`${baseUrl}/api/widget/init?key=${widgetKey}`);
|
|
if (!res.ok) throw new Error('Widget init failed');
|
|
return res.json();
|
|
};
|
|
|
|
export const fetchDoctors = async (): Promise<Doctor[]> => {
|
|
const res = await fetch(`${baseUrl}/api/widget/doctors?key=${widgetKey}`);
|
|
if (!res.ok) throw new Error('Failed to load doctors');
|
|
return res.json();
|
|
};
|
|
|
|
export const fetchSlots = async (doctorId: string, date: string): Promise<TimeSlot[]> => {
|
|
const res = await fetch(`${baseUrl}/api/widget/slots?key=${widgetKey}&doctorId=${doctorId}&date=${date}`);
|
|
if (!res.ok) throw new Error('Failed to load slots');
|
|
return res.json();
|
|
};
|
|
|
|
export const submitBooking = async (data: any): Promise<{ appointmentId: string; reference: string }> => {
|
|
const res = await fetch(`${baseUrl}/api/widget/book?key=${widgetKey}`, {
|
|
method: 'POST', headers: headers(), body: JSON.stringify(data),
|
|
});
|
|
if (!res.ok) throw new Error('Booking failed');
|
|
return res.json();
|
|
};
|
|
|
|
export const submitLead = async (data: any): Promise<{ leadId: string }> => {
|
|
const res = await fetch(`${baseUrl}/api/widget/lead?key=${widgetKey}`, {
|
|
method: 'POST', headers: headers(), body: JSON.stringify(data),
|
|
});
|
|
if (!res.ok) throw new Error('Submission failed');
|
|
return res.json();
|
|
};
|
|
|
|
export const streamChat = async (messages: any[], captchaToken?: string): Promise<ReadableStream> => {
|
|
const res = await fetch(`${baseUrl}/api/widget/chat?key=${widgetKey}`, {
|
|
method: 'POST', headers: headers(),
|
|
body: JSON.stringify({ messages, captchaToken }),
|
|
});
|
|
if (!res.ok || !res.body) throw new Error('Chat failed');
|
|
return res.body;
|
|
};
|