import {z} from 'zod' export type GetApiV1ProvidersSearchArgs = {zipcode: string, radius?: string | undefined, services?: string | undefined, providerId?: string | undefined, employerId?: string | undefined, brandId?: string | undefined, limit?: string | undefined}; export type GetApiV1ProvidersSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ProvidersSearchResponse = z.object({status: z.string(), count: z.number(), zipcode: z.string(), radius: z.number(), providers: z.array(z.object({_id: z.string().optional(), name: z.string().optional(), slug: z.string().optional(), address: z.object({street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postalCode: z.string().optional(), county: z.string().optional(), country: z.string().optional()}).optional(), email: z.string().optional(), logoURL: z.string().optional(), phone_numbers: z.array(z.object({type: z.string().optional(), number: z.string().optional()})).optional(), loc: z.object({type: z.string().optional(), coordinates: z.array(z.number()).optional()}).optional(), distance: z.number().optional(), url: z.string().optional(), services: z.array(z.object({serviceId: z.string().optional(), name: z.string().optional(), slug: z.string().optional(), price: z.number().optional()})).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 }; /** * Search providers by zipcode * * GET /v1/providers/search */ export const getApiV1ProvidersSearch = async (args: GetApiV1ProvidersSearchArgs, options?: GetApiV1ProvidersSearchOptions) => { const {baseUrl = "https://api.bluehive.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.zipcode != null) query.append("zipcode", String(args.zipcode)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.services != null) query.append("services", String(args.services)) if (args.providerId != null) query.append("providerId", String(args.providerId)) if (args.employerId != null) query.append("employerId", String(args.employerId)) if (args.brandId != null) query.append("brandId", String(args.brandId)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/providers/search${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(getApiV1ProvidersSearchResponse, await res.json()) };