import {z} from 'zod' export type DocumentType = 'SELF' | 'IDENTITY_FRONT' | 'IDENTITY_BACK' | 'ADDRESS_PROOF' | 'INCOME_PROOF' | 'DECLARATION_IR' | 'CORPORATE_BILLING_RELATIONSHIP' | 'PROOF_OF_COMPANY_ADDRESS' | 'SOCIAL_CONTRACT' | 'PROOF_OF_CORPORATE_INCOME'; export type MimeType = 'image/jpeg' | 'image/png' | 'application/pdf'; export type Document = {type?: DocumentType | undefined, mimeType?: MimeType | undefined, name?: string | undefined, base64?: string | undefined}; export type UpdateApiV2ProcessDocumentIdCaasArgs = {id_caas: string, body: Document}; export type UpdateApiV2ProcessDocumentIdCaasOptions = {baseUrl?: string | undefined, partnerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const documentType = z.enum(["SELF", "IDENTITY_FRONT", "IDENTITY_BACK", "ADDRESS_PROOF", "INCOME_PROOF", "DECLARATION_IR", "CORPORATE_BILLING_RELATIONSHIP", "PROOF_OF_COMPANY_ADDRESS", "SOCIAL_CONTRACT", "PROOF_OF_CORPORATE_INCOME"]); export const mimeType = z.enum(["image/jpeg", "image/png", "application/pdf"]); export const documentResponse = z.object({type: documentType.optional(), mimeType: mimeType.optional(), name: z.string().optional(), url: z.string().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 }; /** * Send document to analysis * * PUT /v2/process/document/{id_caas} */ export const updateApiV2ProcessDocumentIdCaas = async (args: UpdateApiV2ProcessDocumentIdCaasArgs, options?: UpdateApiV2ProcessDocumentIdCaasOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, partnerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (partnerAuth !== undefined) headers.set('authorization', `Bearer ${partnerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/process/document/${encodeURIComponent(String(args.id_caas))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(documentResponse, await res.json()) };