import {z} from 'zod' export type RegisterErxProviderRequestDto = {userId?: string | undefined, address1?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, phone?: string | undefined}; export type CreateApiErxProviderRegistrationsArgs = {body: RegisterErxProviderRequestDto}; export type CreateApiErxProviderRegistrationsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const erxProviderDto = z.object({userId: z.string().optional(), displayName: z.string().optional(), email: z.string().optional(), licenseNumber: z.string().nullable().optional(), licenseState: z.string().nullable().optional(), missingRequirements: z.array(z.string()).optional(), registered: z.boolean().optional(), verificationStatus: z.string().nullable().optional(), verificationUrl: z.string().nullable().optional(), registeredAt: z.string().nullable().optional(), verifiedAt: z.string().nullable().optional(), lastPolledAt: z.string().nullable().optional(), lastError: 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 }; /** * Register a vet with VetHubRx (action "A") and store the verification URL. * * POST /erx/provider-registrations */ export const createApiErxProviderRegistrations = async (args: CreateApiErxProviderRegistrationsArgs, options?: CreateApiErxProviderRegistrationsOptions) => { 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(/\/+$/, '')}/erx/provider-registrations${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(erxProviderDto, await res.json()) };