import {z} from 'zod' export type GetApiV1FilesIdArgs = {id: number, include_file_image?: boolean | undefined}; export type GetApiV1FilesIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revAppComponentsLink = z.object({rel: z.string().optional(), href: z.string().optional()}); export const revWebAPIModelsGetOrSearchOfRevAppModelsFile = z.object({ok: z.boolean().optional(), file_id: z.number().int().optional(), file_image: z.string().optional(), file_name: z.string().max(8000).optional(), file_type: z.enum(["NOT_SPECIFIED", "AGENT_ATTACHMENT", "AGENT_BATCH", "AGENT_CUSTOMER_SUMMARY", "AGENT_RECEIPT", "AGENT_REPORT_COMMISSION_SUMMARY", "AGENT_STATEMENT", "AGENT_STATEMENT_PRINT_SUMMARY", "CASH_DRAWER_CLOSING_REPORT", "CDR_EXPORT", "COLLECTION_EMAIL", "COLLECTION_LETTER", "TAX_COMPARISON_BATCH", "CUSTOM_E911_BATCH", "CUSTOMER_ACCOUNT_ATTACHMENT", "CUSTOMER_STATEMENT", "CUSTOMER_EDI_BILL", "CUSTOMER_RECEIPT", "DATA_BACKUP", "DISCONNECT_NOTICE", "DOCUMENT", "EXTERNAL_ORDER", "FAX", "FILE_MANAGER", "INVENTORY_ATTACHMENT", "LIFELINE_OTHER", "LIFELINE_PROOF_DOCUMENTATION", "LIFELINE_SELF_CERTIFICATION", "LINE_FIELD_UPDATE_TEMPLATE", "ORDER_ATTACHMENT", "REQUEST_ATTACHMENT", "REQUEST_TEMPLATE", "REQUEST_UPDATE_TEMPLATE", "SALES_PORTAL_COVER_LETTER", "STATEMENT_BATCH", "TASK_ATTACHMENT", "TICKET_ATTACHMENT", "WORKORDER_CONTRACT", "CLIENT_IMAGE", "REPORT", "EMAIL_TEMPLATE_ATTACHMENT", "VENDOR_STATEMENT"]).optional(), custom_upload_type: z.string().optional(), description: z.string().optional(), created_date: z.string().optional(), created_by: z.number().int().optional(), customer_id: z.number().int().optional(), order_id: z.number().int().optional(), request_id: z.number().int().optional(), task_id: z.number().int().optional(), ticket_id: z.number().int().optional(), show_in_customer_profile: z.boolean().optional(), show_in_customer_portal: z.boolean().optional(), show_in_agent_portal: z.boolean().optional(), links: z.array(revAppComponentsLink).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 }; /** * View file * * GET /v1/Files/{id} */ export const getApiV1FilesId = async (args: GetApiV1FilesIdArgs, options?: GetApiV1FilesIdOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_file_image != null) query.append("include_file_image", String(args.include_file_image)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Files/${encodeURIComponent(String(args.id))}${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(revWebAPIModelsGetOrSearchOfRevAppModelsFile, await res.json()) };