import {z} from 'zod' export type HomeAgainEnrollRequest = {microchipNumber?: string | undefined, ownerFirstName?: string | undefined, ownerLastName?: string | undefined, ownerAddressLine1?: (string | null) | undefined, ownerAddressLine2?: (string | null) | undefined, ownerCity?: (string | null) | undefined, ownerStateAbbr?: (string | null) | undefined, ownerPostalCode?: string | undefined, ownerTelephone?: (string | null) | undefined, ownerAltTelephone?: (string | null) | undefined, ownerMobilePhone?: (string | null) | undefined, emailAddress?: (string | null) | undefined, altOwnerFirstName?: (string | null) | undefined, altOwnerLastName?: (string | null) | undefined, altOwnerTelePhone?: (string | null) | undefined, petName?: string | undefined, petSpecies?: string | undefined, breedName?: (string | null) | undefined, additionalBreedName?: (string | null) | undefined, petGender?: (string | null) | undefined, petColorName?: (string | null) | undefined, secondaryColor?: (string | null) | undefined, petSpayNeuter?: (string | null) | undefined, petWeight?: (string | null) | undefined, petDOB?: (string | null) | undefined, petRabiesTag?: (string | null) | undefined, transactionType?: (string | null) | undefined}; export type CreateApiHomeagainEnrollArgs = {body: HomeAgainEnrollRequest}; export type CreateApiHomeagainEnrollOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const homeAgainEnrollResult = z.object({success: z.boolean().optional(), results: z.string().nullable().optional(), microchipNumber: z.string().nullable().optional(), errorMessage: z.string().nullable().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 }; /** * Enroll a single pet microchip with HomeAgain. * * POST /homeagain/enroll */ export const createApiHomeagainEnroll = async (args: CreateApiHomeagainEnrollArgs, options?: CreateApiHomeagainEnrollOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/homeagain/enroll${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(homeAgainEnrollResult, await res.json()) };