import {z} from 'zod' export type CreatePatientDietRestrictionRequest = {comment?: (string | null) | undefined, name: string}; export type PostPatchPatientFlags = {patient_app_extended_logging_types?: (string | null) | undefined, patient_glucose_units_mmol_l?: (boolean | null) | undefined, patient_show_cals?: (boolean | null) | undefined, patient_show_carbs?: (boolean | null) | undefined} | {[key: string]: unknown} | null; export type CreatePatientMotivationRequest = {comment?: (string | null) | undefined, name: string}; export type CreatePatientFromClinicianRequest = {clinic_id?: (number | string) | undefined, clinic_invitation_code?: (unknown | number | string) | undefined, diet_restrictions?: (Array | null) | undefined, email: string, first_name: string, first_report_date?: (string | null) | undefined, flags_raw?: PostPatchPatientFlags | undefined, hospital_patient_custom_fields?: (Record | null) | undefined, hospital_patient_external_id?: (string | null) | undefined, last_name: string, locale?: (string | null) | undefined, motivations?: (Array | null) | undefined, patient_type?: ('research' | 'collaboration' | 'wellness' | 'obesity' | 'diabetes' | 'metabolic syndrome' | 'weight loss' | 'migraine' | 'gastroesophageal reflux' | 'gi discomfort' | 'vertigo' | 'sleep apnea' | 'rxlife' | 'dexcom' | 'menopause' | 'insurance' | null) | undefined, phone_number?: (string | null) | undefined, signup_metadata_non_sensitive?: ({[key: string]: unknown} | null) | undefined, signup_metadata_sensitive?: ({[key: string]: unknown} | null) | undefined, subscription_expiration_date?: (string | null) | undefined, subscription_name?: ('none' | 'three-day' | 'five-day' | 'seven-day' | 'ten-day' | 'one-month' | 'cgm14' | '14-day' | '21-day' | '28-day' | 'custom' | null) | undefined, subscription_type?: ('single' | 'weekly' | 'monthly' | 'bi-weekly' | '4-weeks' | 'custom' | null) | undefined, tracking_start_date?: (string | null) | undefined}; export type CreateApiWebhooksPatientSignupFormArgs = {body: CreatePatientFromClinicianRequest}; export type CreateApiWebhooksPatientSignupFormOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const patientSignupWebhookResponse = z.object({patient_id: z.number().int()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Called by Zapier when a self-service signup form on SquareSpace is submitted. * * POST /webhooks/patient-signup-form */ export const createApiWebhooksPatientSignupForm = async (args: CreateApiWebhooksPatientSignupFormArgs, options: CreateApiWebhooksPatientSignupFormOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/patient-signup-form${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(patientSignupWebhookResponse, await res.json()) };