import {z} from 'zod' export type GetApiV1OrdersOrderIdResultsArgs = {serviceId?: string | undefined, status?: string | undefined, page?: number | undefined, pageSize?: number | undefined, since?: string | undefined, until?: string | undefined, orderId: string}; export type GetApiV1OrdersOrderIdResultsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1OrdersOrderIdResultsResponse = z.object({meta: z.object({orderId: z.string(), orderNumber: z.string().optional(), employeeId: z.string().optional(), providerId: z.string().optional(), totalServices: z.number(), page: z.number(), pageSize: z.number(), returned: z.number()}), services: z.array(z.object({serviceId: z.string(), status: z.string(), result: z.string().optional(), altTxt: z.string().optional(), message: z.string().optional(), resultsPosted: z.string().optional(), drawn_datetime: z.string().optional(), completed_datetime: z.string().optional(), fileIds: z.array(z.string()).optional(), contacts: z.array(z.string()).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 }; /** * Get order results * * GET /v1/orders/{orderId}/results */ export const getApiV1OrdersOrderIdResults = async (args: GetApiV1OrdersOrderIdResultsArgs, options?: GetApiV1OrdersOrderIdResultsOptions) => { 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.serviceId != null) query.append("serviceId", String(args.serviceId)) if (args.status != null) query.append("status", String(args.status)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orders/${encodeURIComponent(String(args.orderId))}/results${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(getApiV1OrdersOrderIdResultsResponse, await res.json()) };