import {z} from 'zod' export type CustomerBankStatementPresignedRequest = {document_type: 'bank_statements', additional_data_request_id?: string | undefined, filename: string, filetype: 'application/pdf'}; export type CustomerDocumentPresignedRequest = {document_type: 'credit_references' | 'financial_statements' | 'payment_history' | 'other', filename: string, filetype: 'application/pdf' | 'image/png' | 'image/jpeg' | 'image/jpg' | 'text/csv' | 'application/vnd.ms-excel' | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}; export type CustomerBankStatementRemoteRequest = {document_type: 'bank_statements', additional_data_request_id?: string | undefined, filename: string, filetype: 'application/pdf', download_url: string}; export type CustomerDocumentRemoteRequest = {document_type: 'credit_references' | 'financial_statements' | 'payment_history' | 'other', filename: string, filetype: 'application/pdf' | 'image/png' | 'image/jpeg' | 'image/jpg' | 'text/csv' | 'application/vnd.ms-excel' | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', download_url: string}; export type CustomerDocumentUploadRequest = CustomerBankStatementPresignedRequest | CustomerDocumentPresignedRequest | CustomerBankStatementRemoteRequest | CustomerDocumentRemoteRequest; export type CreateApiCustomersCustomerIdDocumentsArgs = {customer_id: string, body: CustomerDocumentUploadRequest}; export type CreateApiCustomersCustomerIdDocumentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const customerDocumentPresignedResponse = z.object({id: z.string(), document_id: z.string(), document_type: z.enum(["bank_statements", "credit_references", "financial_statements", "payment_history", "other"]), upload_url: z.string(), upload_expires_at: z.string(), object_key: z.string(), transfer_method: z.literal("presigned_upload"), queue_dispatch_status: z.literal("null").nullable(), upload_headers: z.record(z.string(), z.string()), validation_profile: z.literal("merchant_underwriting_document_default")}); export const customerDocumentRemoteResponse = z.object({id: z.string(), document_id: z.string(), document_type: z.enum(["bank_statements", "credit_references", "financial_statements", "payment_history", "other"]), upload_url: z.string().nullable(), upload_expires_at: z.string().nullable(), object_key: z.string(), transfer_method: z.literal("download_url"), queue_dispatch_status: z.enum(["queued", "failed"]), upload_headers: z.record(z.string(), z.string()), validation_profile: z.literal("merchant_underwriting_document_default")}); export const createApiCustomersCustomerIdDocumentsResponse = z.union([customerDocumentPresignedResponse, customerDocumentRemoteResponse]); 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 }; /** * Upload a customer supporting document * * POST /customers/{customer_id}/documents * * Also accepts auth: basicAuth */ export const createApiCustomersCustomerIdDocuments = async (args: CreateApiCustomersCustomerIdDocumentsArgs, options?: CreateApiCustomersCustomerIdDocumentsOptions) => { const {baseUrl = "https://app-sandbox.resolvepay.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customer_id))}/documents${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiCustomersCustomerIdDocumentsResponse, await res.json()) };