import {z} from 'zod' export type GetApiApiDocumentsV1Args = {type: string, id: string}; export type GetApiApiDocumentsV1Options = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const relationDocumentSigned = z.object({type: z.enum(["shareholders", "cases", "accounts", "identities", "onboardings", "sources_of_funds", "people", "representatives"]).optional(), id: z.string(), id_relation: z.string().nullable(), name: z.string().nullable()}); export const attachment = z.object({url: z.string(), name: z.string(), id_attachment: z.string().nullable(), metadata: z.object({size: z.number(), mimetype: z.string(), cacheControl: z.string(), eTag: z.string(), lastModified: z.string(), contentLength: z.number(), httpStatusCode: z.number()}), id_document: z.string().nullable()}); export const ocrData = z.object({document: z.lazy(() => documentSigned), payload_data: z.object({})}); export const documentSigned: z.ZodType = z.object({status: z.enum(["PENDING", "APPROVED", "REJECTED", "EXPIRED"]), ocr_status: z.enum(["CREATED", "PROCESSING", "SUCCESS", "FAILED"]), relations: z.array(relationDocumentSigned).optional(), attachments: z.array(attachment), ocr_data_responses: ocrData, created_at: z.string().nullable(), detail: z.string().nullable(), document_type: z.string().nullable(), expiration_date: z.string().nullable(), id_document: z.string().nullable(), id_ocr: z.string().nullable(), ocr_created_at: z.string().nullable(), ocr_created_by: z.string().nullable(), ocr_error: z.string().nullable(), ocr_highlighted_attachment: z.object({}).nullable(), ocr_markdown: z.string().nullable(), relation_suggestions: z.object({}).nullable(), run_ocr: z.boolean().nullable(), run_sync: z.boolean().nullable(), updated_at: z.string().nullable()}); export const getApiApiDocumentsV1Response = z.array(documentSigned); 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 }; /** * GET /api/documents/v1 */ export const getApiApiDocumentsV1 = async (args: GetApiApiDocumentsV1Args, options?: GetApiApiDocumentsV1Options) => { const {baseUrl = "https://api.complif.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.id != null) query.append("id", String(args.id)) const url = `${baseUrl.replace(/\/+$/, '')}/api/documents/v1${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(getApiApiDocumentsV1Response, await res.json()) };