import {z} from 'zod' export type GetApiV1FilesArgs = {'search.file_id'?: Array | undefined, 'search.request_id'?: Array | undefined, 'search.customer_id'?: Array | undefined, 'search.created_date_start'?: string | undefined, 'search.created_date_end'?: string | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1FilesOptions = {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 revAppModelsFile = z.object({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 revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsFile = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppModelsFile).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 files * * GET /v1/Files */ export const getApiV1Files = async (args: GetApiV1FilesArgs, options?: GetApiV1FilesOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['search.file_id'] ?? []) query.append("search.file_id", String(item)) for (const item of args['search.request_id'] ?? []) query.append("search.request_id", String(item)) for (const item of args['search.customer_id'] ?? []) query.append("search.customer_id", String(item)) if (args['search.created_date_start'] != null) query.append("search.created_date_start", String(args['search.created_date_start'])) if (args['search.created_date_end'] != null) query.append("search.created_date_end", String(args['search.created_date_end'])) if (args['search.page_size'] != null) query.append("search.page_size", String(args['search.page_size'])) if (args['search.page'] != null) query.append("search.page", String(args['search.page'])) if (args['search.sort'] != null) query.append("search.sort", String(args['search.sort'])) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Files${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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsFile, await res.json()) };