import {z} from 'zod' export type GetApiV1ProvidersLookupArgs = {firstname?: string | undefined, lastname?: string | undefined, npi?: string | undefined, zipcode?: string | undefined}; export type GetApiV1ProvidersLookupOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ProvidersLookupResponse = z.object({providers: z.array(z.object({lastname: z.string(), firstname: z.string(), address_1: z.string(), address_2: z.string(), city: z.string(), state_province: z.string(), country: z.string(), postal_code: z.string(), work_phone: z.string(), fax_number: z.string(), npi: z.string(), distance: z.number()})), count: z.number()}); 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 }; /** * Provider Search * * GET /v1/providers/lookup */ export const getApiV1ProvidersLookup = async (args: GetApiV1ProvidersLookupArgs, options?: GetApiV1ProvidersLookupOptions) => { const {baseUrl = "https://api.bluehive.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.firstname != null) query.append("firstname", String(args.firstname)) if (args.lastname != null) query.append("lastname", String(args.lastname)) if (args.npi != null) query.append("npi", String(args.npi)) if (args.zipcode != null) query.append("zipcode", String(args.zipcode)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/providers/lookup${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1ProvidersLookupResponse, await res.json()) };