import {z} from 'zod' export type CreateApiApiUrlV1GuestsArgs = {body: {RAW_BODY?: string | undefined}}; export type CreateApiApiUrlV1GuestsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiUrlV1GuestsResponse = z.object({id: z.string().optional(), offline_id: z.unknown(), code: z.string().optional(), center_id: z.string().optional(), preferred_service_id: z.string().optional(), is_online_booking_blocked: z.boolean().optional(), center_name: z.string().optional(), created_date: z.string().optional(), can_edit_personal_info: z.boolean().optional(), is_virtual_guest: z.boolean().optional(), is_guest_custom_form_filled: z.boolean().optional(), is_class_booking_blocked: z.boolean().optional(), is_blocked_for_no_show: z.boolean().optional(), no_show_un_block_date: z.unknown(), personal_info: z.object({user_name: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), middle_name: z.string().optional(), preferred_name: z.string().optional(), email: z.string().optional(), mobile_phone: z.object({country_code: z.number().int().optional(), phone_code: z.number().int().optional(), number: z.string().optional()}).optional(), work_phone: z.object({country_code: z.number().int().optional(), phone_code: z.number().int().optional(), number: z.string().optional()}).optional(), home_phone: z.object({country_code: z.number().int().optional(), phone_code: z.number().int().optional(), number: z.string().optional()}).optional(), gender: z.number().int().optional(), gender_name: z.string().optional(), date_of_birth: z.string().optional(), is_minor: z.boolean().optional(), nationality_id: z.number().int().optional(), language_id: z.number().int().optional(), anniversary_date: z.string().optional(), lock_guest_custom_data: z.boolean().optional(), pan: z.string().optional(), dob_incomplete_year: z.string().optional()}).optional(), address_info: z.unknown(), preferences: z.unknown(), tags: z.unknown(), referral: z.object({referral_source: z.unknown(), referred_by: z.object({id: z.string().optional(), name: z.string().optional()}).optional(), referral_code: z.string().optional()}).optional(), primary_employee: z.unknown(), guest_passes: z.unknown(), guest_milestone_details: z.unknown(), additional_details: z.unknown(), blocked_therapists: z.unknown(), validate_additional_details: z.boolean().optional(), has_statements: z.boolean().optional(), is_hac_member: z.boolean().optional()}); 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 }; /** * Create new guests using referral codes * * POST /{api_url}/v1/guests */ export const createApiApiUrlV1Guests = async (args: CreateApiApiUrlV1GuestsArgs, options?: CreateApiApiUrlV1GuestsOptions) => { const {baseUrl = "https://api.zenoti.com", 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(/\/+$/, '')}/${encodeURIComponent(String(args.api_url))}/v1/guests${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(createApiApiUrlV1GuestsResponse, await res.json()) };