import {z} from 'zod' export type GetApiApiV1EmailThreadsArgs = {page_size?: number | undefined, cursor?: string | undefined, id?: Array | undefined, queue?: Array | undefined, created_at_before?: string | undefined, created_at_after?: string | undefined, has_root_email?: boolean | undefined, has_replies?: boolean | undefined, has_new_replies?: boolean | undefined, last_email_created_at_before?: string | undefined, last_email_created_at_after?: string | undefined, recent_with_no_documents_not_replied?: boolean | undefined, ordering?: ('id' | '-id' | 'created_at' | '-created_at' | 'last_email_created_at' | '-last_email_created_at' | 'subject' | '-subject' | 'from__email' | '-from__email' | 'from__name' | '-from__name' | 'queue' | '-queue') | undefined}; export type GetApiApiV1EmailThreadsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({next: z.string().nullable().optional(), previous: z.string().nullable().optional()}); export const queueUrl = z.string(); export const emailThread = z.object({id: z.number().int(), url: z.string(), organization: z.string(), queue: queueUrl, root_email: z.string(), has_replies: z.boolean(), has_new_replies: z.boolean(), root_email_read: z.boolean(), created_at: z.string(), last_email_created_at: z.string(), subject: z.string(), from: z.object({email: z.string().optional(), name: z.string().optional()}), labels: z.array(z.string()), annotation_counts: z.object({annotations: z.number().int().optional(), annotations_processed: z.number().int().optional(), annotations_purged: z.number().int().optional(), annotations_rejected: z.number().int().optional(), annotations_unprocessed: z.number().int().optional()})}); export const getApiApiV1EmailThreadsResponse = z.object({pagination: pagination, results: z.array(emailThread)}); 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 email threads * * GET /api/v1/email_threads */ export const getApiApiV1EmailThreads = async (args: GetApiApiV1EmailThreadsArgs, options?: GetApiApiV1EmailThreadsOptions) => { const {baseUrl = "https://example.rossum.app", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.id != null) query.append("id", args.id.join(',')) if (args.queue != null) query.append("queue", args.queue.join(',')) if (args.created_at_before != null) query.append("created_at_before", String(args.created_at_before)) if (args.created_at_after != null) query.append("created_at_after", String(args.created_at_after)) if (args.has_root_email != null) query.append("has_root_email", String(args.has_root_email)) if (args.has_replies != null) query.append("has_replies", String(args.has_replies)) if (args.has_new_replies != null) query.append("has_new_replies", String(args.has_new_replies)) if (args.last_email_created_at_before != null) query.append("last_email_created_at_before", String(args.last_email_created_at_before)) if (args.last_email_created_at_after != null) query.append("last_email_created_at_after", String(args.last_email_created_at_after)) if (args.recent_with_no_documents_not_replied != null) query.append("recent_with_no_documents_not_replied", String(args.recent_with_no_documents_not_replied)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/email_threads${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(getApiApiV1EmailThreadsResponse, await res.json()) };