import {z} from 'zod' export type CreateApiPeopleArgs = {body: {person: {first_name: string, last_name: string, born_at?: string | undefined, gender: string, phone_country_id?: string | undefined, telephone2?: string | undefined, email?: string | undefined, account_id: string, without_cellphone?: boolean | undefined, send_welcome_email?: boolean | undefined, address_state_id?: string | undefined, patient_group_ids?: Array | undefined, person_attributes?: ({send_reminders?: boolean | undefined, identity_number?: string | undefined}) | undefined}}}; export type CreateApiPeopleOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPeopleResponse = z.object({person: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), links: z.object({consultations: z.string().optional(), consultation_lab_test_results: z.string().optional(), consultation_schedules: z.string().optional(), vital_signs_sets: z.string().optional(), medical_history: z.string().optional(), deffers: z.string().optional(), medical_notes: z.string().optional(), insurance_coverage: z.string().optional()}).optional(), full_name: z.string().optional(), telephone: z.unknown(), email: z.unknown(), born_at: z.string().optional(), gender: z.string().optional(), avatar_url_small: z.string().optional(), avatar_url_medium: z.string().optional(), telephone2: z.string().optional(), address_state_id: z.number().int().optional(), address_city: z.unknown(), address_street: z.unknown(), address_zipcode: z.unknown(), identity_number: z.unknown(), account_id: z.number().int().optional(), avatar_url_large: z.string().optional(), notes: z.unknown(), person_attributes: z.object({}).optional(), phone_country_id: z.number().int().optional(), is_migrated: z.boolean().optional(), without_cellphone: z.boolean().optional(), nimbochat_invitation_sent: z.boolean().optional(), medical_record: z.unknown()}).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 }; /** * POST /people * * POST /people */ export const createApiPeople = async (args: CreateApiPeopleArgs, options?: CreateApiPeopleOptions) => { const {baseUrl = "https://api.example.test", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/people${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(createApiPeopleResponse, await res.json()) };