import {z} from 'zod' export type CreateApiLaasV2DebtorsArgs = {body: {person_type: 'natural_person' | 'legal_person', name: string, identification: string, email: string, phone: string, birth_date?: string | undefined, marital_status?: ('single' | 'married' | 'divorced' | 'widowed') | undefined, earnings?: number | undefined, nationality?: string | undefined, pep?: ('Sim' | 'Não') | undefined, mother_name?: string | undefined, street: string, address_number: string, address_complement?: string | undefined, neighborhood: string, city: string, state: string, zip_code: string, country?: string | undefined, account_type?: ('savings_account' | 'checking_account') | undefined, bank?: number | undefined, bank_agency?: string | undefined, bank_account_number?: string | undefined, foundation_date?: string | undefined, legal_status?: ('mei' | 'ei' | 'eireli' | 'sociedade_anonima' | 'sociedade_simples_limitada' | 'sociedade_limitada_unipessoal' | 'sociedade_empresaria_limitada' | 'inova_simples' | 'empresario_rural' | 'cooperativa' | 'sociedade_em_conta_de_participacao' | 'empresa_simples_de_credito' | 'sociedade_anonima_do_futebol') | undefined, cnae?: string | undefined, gross_revenue_last_12_months?: number | undefined, legal_representatives?: (Array<{name: string, email: string, phone: string, identification: string, birth_date: string, mother_name: string, pep: 'Sim' | 'Não', street: string, address_number: string, address_complement?: string | undefined, neighborhood: string, city: string, state: string, zip_code: string, nationality?: string | undefined, account_type: 'savings_account' | 'checking_account', bank: number, bank_agency: string, bank_account_number: string}>) | undefined}}; export type CreateApiLaasV2DebtorsOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, sec1?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiLaasV2DebtorsResponse = z.object({debtor_id: 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 }; /** * Cadastrar devedor * * POST /laas/v2/debtors */ export const createApiLaasV2Debtors = async (args: CreateApiLaasV2DebtorsArgs, options?: CreateApiLaasV2DebtorsOptions) => { const {baseUrl = "https://pagamentos.grafeno.be/api/v2", fetch: fetchFn = fetch, sec0, sec1} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) if (sec1 !== undefined) headers.set("Authorization", sec1) const url = `${baseUrl.replace(/\/+$/, '')}/laas/v2/debtors${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(createApiLaasV2DebtorsResponse, await res.json()) };