import {z} from 'zod' export type Recipient = {account_id: string, merchant_recipient_id: string, national_entity: 'INDIVIDUAL' | 'ENTITY', entity_type?: ('GOVERNMENTAL' | 'PUBLIC' | 'NON_PROFIT' | 'PRIVATE') | undefined, first_name: string, last_name: string, date_of_birth?: string | undefined, legal_name?: string | undefined, email?: string | undefined, country: string, website?: string | undefined, industry?: string | undefined, merchant_category_code?: string | undefined, document?: {document_number: string, document_type: string} | undefined, phone?: {country_code: string, number: string} | undefined, address?: ({address_line_1?: string | undefined, address_line_2?: string | undefined, city?: string | undefined, country?: string | undefined, state?: string | undefined, zip_code?: string | undefined, neighborhood?: string | undefined}) | undefined, split_configuration?: ({calculation_type: 'PERCENTAGE' | 'FIXED' | 'MIXED', percentage?: number | undefined, fixed_amount?: number | undefined, currency: string, rounding_mode?: ('STANDARD' | 'ROUND_UP' | 'ROUND_DOWN') | undefined}) | undefined, legal_representatives?: (Array<{merchant_reference?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, email?: string | undefined, date_of_birth?: string | undefined, country?: string | undefined, nationality?: string | undefined, title?: string | undefined, publicly_exposed_person?: boolean | undefined, ultimate_beneficial_owner?: boolean | undefined}>) | undefined, withdrawal_methods?: ({bank?: ({code?: string | undefined, branch?: string | undefined, branch_digit?: string | undefined, account?: string | undefined, account_digit?: string | undefined, account_type?: ('CHECKINGS' | 'SAVINGS') | undefined, routing?: string | undefined, country?: string | undefined, currency?: string | undefined, payout_schedule?: ('DAY' | 'WEEK' | 'MONTH' | 'HOLD') | undefined}) | undefined}) | undefined, documentation?: (Array<{file_name: string, content_type: 'application/pdf' | 'image/jpeg' | 'image/png', content_category: 'ACCOUNT_REQUIREMENT' | 'BANK_STATEMENT' | 'BUSINESS_ICON' | 'BUSINESS_LOGO' | 'CANCEL_POLICY' | 'CUSTOMER_INTERACTION' | 'DRIVERS_LICENCE' | 'IDENTIFICATION_DOCUMENT' | 'IDENTIFICATION_DOCUMENT_BACK' | 'IDENTIFICATION_DOCUMENT_FRONT' | 'LEGAL_AGREEMENT' | 'OTHERS' | 'PASSPORT' | 'PAYMENT_DETAIL' | 'PROOF_OF_ADDRESS' | 'PROOF_OF_AUTHENTICITY' | 'PROOF_OF_DELIVERY' | 'PROOF_OF_DIRECTOR' | 'PROOF_OF_INDUSTRY' | 'PROOF_OF_NATIONAL_ID_NUMBER' | 'PROOF_OF_OWNERSHIP' | 'PROOF_OF_REGISTRATION' | 'PROOF_OF_RELATIONSHIP' | 'PROOF_OF_SERVICE' | 'PROOF_OF_SOURCE_OF_FUNDS' | 'PROOF_OF_SOURCE_OF_WEALTH' | 'PROOF_OF_TAX' | 'PROVIDER_IDENTITY_DOCUMENT' | 'RECEIPT' | 'REFUND_POLICY' | 'SELFIE' | 'SERVICE_AGREEMENT' | 'SIGNATURE' | 'TERMS_AND_CONDITIONS' | 'VAT_DOCUMENT', content: string}>) | undefined, onboardings?: (Array<{account_id?: string | undefined, type: 'PREVIOUSLY_ONBOARDED' | 'ONBOARD_ONTO_THE_PROVIDER', workflow: 'HOSTED_BY_PROVIDER' | 'DIRECT', description?: string | undefined, callback_url?: string | undefined, provider: {id: 'PAGARME' | 'ALELO' | 'EDENRED' | 'PLUXEE' | 'VR' | 'STRIPE' | 'ADYEN', connection_id: string, recipient_id?: string | undefined, recipient_type?: ('MEAL' | 'FOOD' | 'MULTI_BENEFITS' | 'FLEET') | undefined}, documentation?: (Array<{file_name: string, content_type: 'application/pdf' | 'image/jpeg' | 'image/png', content_category: 'ACCOUNT_REQUIREMENT' | 'BANK_STATEMENT' | 'BUSINESS_ICON' | 'BUSINESS_LOGO' | 'CANCEL_POLICY' | 'CUSTOMER_INTERACTION' | 'DRIVERS_LICENCE' | 'IDENTIFICATION_DOCUMENT' | 'IDENTIFICATION_DOCUMENT_BACK' | 'IDENTIFICATION_DOCUMENT_FRONT' | 'LEGAL_AGREEMENT' | 'OTHERS' | 'PASSPORT' | 'PAYMENT_DETAIL' | 'PROOF_OF_ADDRESS' | 'PROOF_OF_AUTHENTICITY' | 'PROOF_OF_DELIVERY' | 'PROOF_OF_DIRECTOR' | 'PROOF_OF_INDUSTRY' | 'PROOF_OF_NATIONAL_ID_NUMBER' | 'PROOF_OF_OWNERSHIP' | 'PROOF_OF_REGISTRATION' | 'PROOF_OF_RELATIONSHIP' | 'PROOF_OF_SERVICE' | 'PROOF_OF_SOURCE_OF_FUNDS' | 'PROOF_OF_SOURCE_OF_WEALTH' | 'PROOF_OF_TAX' | 'PROVIDER_IDENTITY_DOCUMENT' | 'RECEIPT' | 'REFUND_POLICY' | 'SELFIE' | 'SERVICE_AGREEMENT' | 'SIGNATURE' | 'TERMS_AND_CONDITIONS' | 'VAT_DOCUMENT', content: string}>) | undefined, withdrawal_methods?: (Array<{bank?: ({code?: string | undefined, branch?: string | undefined, branch_digit?: string | undefined, account?: string | undefined, account_digit?: string | undefined, account_type?: ('CHECKINGS' | 'SAVINGS' | 'VISTA' | 'PIX_EMAIL' | 'PIX_PHONE' | 'PIX_DOCUMENT_ID' | 'PIX_BANK_ACCOUNT') | undefined, routing?: string | undefined, country?: string | undefined, currency?: string | undefined}) | undefined}>) | undefined}>) | undefined}; export type CreateApiRecipientsArgs = {body: Recipient}; export type CreateApiRecipientsOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const recipientResponse = z.object({id: z.string().optional(), account_id: z.string().optional(), merchant_recipient_id: z.string().optional(), national_entity: z.string().optional(), entity_type: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), legal_name: z.unknown(), email: z.string().optional(), date_of_birth: z.string().optional(), country: z.string().optional(), website: z.string().optional(), industry: z.string().optional(), merchant_category_code: z.string().optional(), document: z.object({document_number: z.string().optional(), document_type: z.string().optional()}).optional(), phone: z.object({country_code: z.string().optional(), number: z.string().optional()}).optional(), address: z.object({address_line_1: z.string().optional(), address_line_2: z.string().optional(), city: z.string().optional(), country: z.string().optional(), state: z.string().optional(), zip_code: z.string().optional(), neighborhood: z.string().optional()}).optional(), legal_representatives: z.array(z.object({merchant_reference: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), date_of_birth: z.string().optional(), country: z.string().optional(), nationality: z.string().optional(), title: z.string().optional(), publicly_exposed_person: z.boolean().optional(), ultimate_beneficial_owner: z.boolean().optional(), document: z.object({document_type: z.string().optional(), document_number: z.string().optional()}).optional(), phone: z.object({country_code: z.string().optional(), number: z.string().optional()}).optional(), address: z.object({address_line_1: z.string().optional(), address_line_2: z.unknown(), country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zip_code: z.string().optional()}).optional()})).optional(), withdrawal_methods: z.object({bank: z.object({code: z.string().optional(), branch: z.string().optional(), branch_digit: z.string().optional(), account: z.string().optional(), account_digit: z.string().optional(), account_type: z.string().optional(), routing: z.string().optional(), country: z.string().optional(), currency: z.string().optional()}).optional()}).optional(), documentation: z.array(z.object({file_name: z.string().optional(), content_type: z.string().optional(), content_category: z.string().optional(), content: z.string().optional()})).optional(), onboardings: z.array(z.object({id: z.string().optional(), type: z.string().optional(), workflow: z.string().optional(), status: z.string().optional(), callback_url: z.string().optional(), provider: z.object({id: z.string().optional(), connection_id: z.string().optional(), recipient_id: z.unknown(), redirect_url: z.unknown(), onboarding_url: z.unknown(), legal_entity: z.unknown(), balance_account_id: z.unknown(), legal_entity_id: z.unknown()}).optional(), documentation: z.array(z.object({file_name: z.string().optional(), content_type: z.string().optional(), content_category: z.string().optional(), content: z.string().optional()})).optional(), legal_representatives: z.array(z.object({})).optional(), requirements: z.array(z.object({field: z.string().optional(), message: z.string().optional(), status: z.string().optional()})).optional(), withdrawal_methods: z.object({bank: z.object({code: z.string().optional(), branch: z.string().optional(), branch_digit: z.unknown(), account: z.string().optional(), account_digit: z.unknown(), account_type: z.string().optional(), routing: z.string().optional(), country: z.string().optional(), currency: z.string().optional()}).optional()}).optional(), terms_of_service: z.object({acceptance: z.boolean().optional(), date: z.string().optional(), ip: z.string().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional()})).optional(), split_configuration: z.object({calculation_type: z.enum(["PERCENTAGE", "FIXED", "MIXED"]).optional(), percentage: z.number().gte(0.01).lte(100).optional(), fixed_amount: z.number().gte(0.0001).optional(), currency: z.string().optional(), rounding_mode: z.enum(["STANDARD", "ROUND_UP", "ROUND_DOWN"]).optional()}).optional(), created_at: z.string().optional(), updated_at: 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 }; /** * Create Recipient * * POST /recipients */ export const createApiRecipients = async (args: CreateApiRecipientsArgs, options?: CreateApiRecipientsOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/recipients${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(recipientResponse, await res.json()) };