import {z} from 'zod' export type GetApiApiBrExchangesIdArgs = {id: string, omit?: string | undefined, fields?: string | undefined}; export type GetApiApiBrExchangesIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string(); export const link = z.string().nullable(); export const createdAt = z.string(); export const collectedAt = z.string(); export const enumExchangeOperationType = z.enum(["COMPRA", "VENDA"]); export const enumExchangeSettlementMethod = z.enum(["CONTA_DEPOSITO_MOEDA_ESTRANGEIRA_PAIS", "CONTA_DEPOSITO_OU_PAGAMENTO_EXPORTADOR_INSTITUICAO_EXTERIOR", "ESPECIE_CHEQUES_VIAGEM", "CARTAO_PREPAGO", "TELETRANSMISSAO", "SEM_MOVIMENTACAO_VALORES", "DEMAIS", "CARTA_CREDITO_A_VISTA", "CARTA_CREDITO_A_PRAZO", "CONTA_DEPOSITO", "CHEQUE", "TITULOS_VALORES", "SIMBOLICA", "CONTA_DEPOSITO_EXPORTADOR_MANTIDA_NO_EXTERIOR", "CONVENIO_PAGAMENTOS_E_CREDITOS_RECIPROCOS", "OUTRO_NAO_MAPEADO_OFB", "null"]).nullable(); export const exchange = z.object({id: id, link: link, created_at: createdAt, collected_at: collectedAt, operation_identifier: z.string().max(100), operation_number: z.string().max(12).nullable().optional(), operation_type: enumExchangeOperationType, operation_requested_at: z.string(), authorized_institution_identifier: z.number().int(), authorized_institution_name: z.string().max(80), intermediary_institution_identifier: z.number().int().nullable().optional(), intermediary_institution_name: z.string().max(80).nullable().optional(), operation_due_date: z.string().min(10).max(10), local_operation_tax_amount: z.number(), local_operation_tax_currency: z.string().min(3).max(3), local_operation_value_amount: z.number(), local_operation_value_currency: z.string().min(3).max(3), foreign_operation_value_amount: z.number(), foreign_operation_value_currency: z.string().min(3).max(3), operation_outstanding_balance_amount: z.number().nullable().optional(), operation_outstanding_balance_currency: z.string().min(3).max(3).nullable().optional(), tev_amount_amount: z.number().nullable().optional(), tev_amount_currency: z.string().min(3).max(3).nullable().optional(), local_currency_advance_percentage: z.number().nullable().optional(), settlement_method: enumExchangeSettlementMethod, operation_category_code: z.string().max(5)}); 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 an exchange's details * * GET /api/br/exchanges/{id}/ */ export const getApiApiBrExchangesId = async (args: GetApiApiBrExchangesIdArgs, options?: GetApiApiBrExchangesIdOptions) => { const {baseUrl = "https://sandbox.belvo.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.omit != null) query.append("omit", String(args.omit)) if (args.fields != null) query.append("fields", String(args.fields)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/br/exchanges/${encodeURIComponent(String(args.id))}/${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(exchange, await res.json()) };