import {z} from 'zod' export type GetApiV2TransactionsArgs = {page: string, limit: string, sort: 'ASC' | 'DESC', due_date_start?: string | undefined, due_date_end?: string | undefined, include_payments?: boolean | undefined, include_history?: boolean | undefined, include_summary?: boolean | undefined, reference_id?: string | undefined, transaction_id?: string | undefined}; export type GetApiV2TransactionsOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const allowsMethod = z.object({pix: z.boolean(), bank_slip: z.boolean(), credit_card: z.boolean()}); export const edubankBuyersAddress = z.object({city: z.string(), additional_information: z.string().optional(), number: z.string(), state_code: z.string(), street: z.string(), neighborhood: z.string(), zip: z.string(), country_code: z.literal("BR")}); export const edubankBuyers = z.object({id: z.string().optional(), first_name: z.string(), last_name: z.string(), email: z.string().optional(), taxpayer_id: z.string(), birthdate: z.string().optional(), phone_number: z.string().optional(), address: edubankBuyersAddress}); export const edubankTransactionItems = z.object({item: z.string(), amount: z.string(), reference: z.string().optional(), note: z.string().optional()}); export const edubankLateFee = z.object({mode: z.string(), amount: z.string()}); export const edubankInterest = z.object({mode: z.string(), amount: z.string()}); export const edubankDiscount = z.object({mode: z.string(), date_1: z.string(), amount_1: z.number(), date_2: z.string().optional(), amount_2: z.number().optional(), date_3: z.string().optional(), amount_3: z.number().optional()}); export const edubankTransactionDetails = z.object({amount: z.number(), due_date: z.string(), payment_limit_date: z.string(), body_instructions: z.array(z.string()).optional(), late_fee: edubankLateFee.optional(), interest: edubankInterest.optional(), discount: edubankDiscount.optional()}); export const edubankSplit = z.object({recipient: z.string(), amount: z.number().optional(), percentage: z.string(), processing_fee: z.string()}); export const allTransactionReponseV2History = z.object({event_id: z.string(), created_at: z.string(), sent_with_success: z.boolean(), sent_attempts: z.number(), sent_with_success_in: z.string(), partner_response: z.array(z.string())}); export const allTransactionResponseV2 = z.object({created_at: z.string(), updated_at: z.string().optional(), sandbox: z.boolean(), status: z.enum(["PAID", "CANCELED", "SETTLED", "VOIDED", "OPEN"]), paid_at: z.string().nullable().optional(), paid_with: z.enum(["PIX", "BANK_SLIP", "CREDIT_CARD", "DEBIT_CARD", "BANK_SLIP_PIX_WITH_PIX", "BANK_SLIP_PIX_WITH_BARCODE"]).nullable(), paid_amount: z.number().optional(), transaction_id: z.string(), reference_id: z.string(), seller_id: z.string(), allows: allowsMethod, buyer: edubankBuyers.nullable().optional(), transaction_items: z.array(edubankTransactionItems).nullable().optional(), transaction_details: edubankTransactionDetails, split_rules: z.array(edubankSplit).optional(), notification_url: z.string(), history: z.array(allTransactionReponseV2History)}); 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 }; /** * Returns all transactions generated within the specified due date range * * GET /v2/transactions * * Also accepts auth: authorization, authorization */ export const getApiV2Transactions = async (args: GetApiV2TransactionsArgs, options: GetApiV2TransactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.due_date_start != null) query.append("due_date_start", String(args.due_date_start)) if (args.due_date_end != null) query.append("due_date_end", String(args.due_date_end)) if (args.include_payments != null) query.append("include_payments", String(args.include_payments)) if (args.include_history != null) query.append("include_history", String(args.include_history)) if (args.include_summary != null) query.append("include_summary", String(args.include_summary)) if (args.reference_id != null) query.append("reference_id", String(args.reference_id)) if (args.transaction_id != null) query.append("transaction_id", String(args.transaction_id)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/transactions${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(allTransactionResponseV2, await res.json()) };