import {z} from 'zod' export type GetApiV1FaxArgs = {page?: number | undefined, pageSize?: number | undefined, id?: string | undefined, faxId?: string | undefined, status?: ('received' | 'queued' | 'processing' | 'completed' | 'error') | undefined, brandId?: string | undefined, idContains?: string | undefined, faxIdContains?: string | undefined, brandIdContains?: string | undefined, brandIdIsNull?: boolean | undefined, since?: string | undefined, until?: string | undefined}; export type GetApiV1FaxOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1FaxResponse = z.object({meta: z.object({page: z.number().int(), pageSize: z.number().int(), total: z.number().int(), returned: z.number().int()}), faxes: z.array(z.object({_id: z.string().optional(), faxId: z.string().optional(), storagePath: z.string().optional(), receivedAt: z.string().optional(), status: z.enum(["received", "queued", "processing", "completed", "error"]).optional(), attempts: z.number().int().optional(), lastError: z.string().optional(), startedAt: z.string().optional(), finishedAt: z.string().optional(), scannedAt: z.string().optional(), brandId: z.string().nullable().optional(), pageData: z.array(z.object({pageNum: z.number().int().optional(), codes: z.array(z.object({raw: z.string().optional(), data: z.record(z.string(), z.unknown()).optional(), derived: z.record(z.string(), z.unknown()).optional()})).optional(), formId: z.string().nullable().optional(), orderItemId: z.string().nullable().optional()})).optional(), splits: z.array(z.object({formId: z.string().nullable().optional(), orderItemId: z.string().nullable().optional(), storagePath: z.string().optional(), pageNums: z.array(z.number().int()).optional(), createdAt: z.string().optional(), meta: z.object({pageCount: z.number().int().optional(), fileSize: z.number().int().optional()}).optional()})).optional(), meta: z.object({pageCount: z.number().int().optional(), fileSize: z.number().int().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 Faxes * * GET /v1/fax */ export const getApiV1Fax = async (args: GetApiV1FaxArgs, options?: GetApiV1FaxOptions) => { 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.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.id != null) query.append("id", String(args.id)) if (args.faxId != null) query.append("faxId", String(args.faxId)) if (args.status != null) query.append("status", String(args.status)) if (args.brandId != null) query.append("brandId", String(args.brandId)) if (args.idContains != null) query.append("idContains", String(args.idContains)) if (args.faxIdContains != null) query.append("faxIdContains", String(args.faxIdContains)) if (args.brandIdContains != null) query.append("brandIdContains", String(args.brandIdContains)) if (args.brandIdIsNull != null) query.append("brandIdIsNull", String(args.brandIdIsNull)) 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/fax${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(getApiV1FaxResponse, await res.json()) };