import {z} from 'zod' export type GetApiOrdersPrematchAllArgs = {'version-datavant'?: string | undefined}; export type GetApiOrdersPrematchAllOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const requesterType = z.enum(["attorney", "datavant", "disability", "continuing_care", "copya", "copyi", "facility", "government", "insurance", "patient", "payd", "payi", "pro", "fac"]); export const orderValidationResponse = z.object({valid: z.number().int(), failed: z.number().int(), copied: z.number().int(), in_progress: z.number().int(), validation_failures_threshold: z.number().int().optional(), tenant_name: z.string().optional()}); export const prematchOrderResponse = z.object({uuid: z.string(), id: z.string().min(1).max(100), name: z.string().optional(), type: requesterType, status: z.string(), created_at: z.string(), validation_results: orderValidationResponse.optional(), roster_id: z.string().optional(), filename: z.string().optional(), total_queries: z.number().int(), created_by: z.string().optional(), retrieval_sources: z.array(z.string()).optional(), match_reasons_documents: z.record(z.string(), z.string()).optional()}); export const getApiOrdersPrematchAllResponse = z.array(prematchOrderResponse); 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 }; /** * List Prematch Orders * * GET /orders/prematch/all */ export const getApiOrdersPrematchAll = async (args: GetApiOrdersPrematchAllArgs, options?: GetApiOrdersPrematchAllOptions) => { const {baseUrl = "https://api.datavant.io/v2", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['version-datavant'] != null) headers.set("version-datavant", String(args['version-datavant'])) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/prematch/all${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(getApiOrdersPrematchAllResponse, await res.json()) };