import {z} from 'zod' export type GetApiApiV2DebenturesListArgs = {symbols?: string | undefined, cnpjs?: string | undefined, search?: string | undefined, status?: ('active' | 'inactive') | undefined, index?: ('di' | 'ipca' | 'pre' | 'igp-m' | 'inpc' | 'tr' | 'usd' | 'eur' | 'none') | undefined, incentivized?: ('true' | 'false') | undefined, maturityFrom?: string | undefined, maturityTo?: string | undefined, sortBy?: ('symbol' | 'maturityDate' | 'issueDate') | undefined, sortOrder?: ('asc' | 'desc') | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiApiV2DebenturesListOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const debenture = z.object({symbol: z.string(), isin: z.string().nullable(), issuerCnpj: z.string(), issuerName: z.string(), issuerTicker: z.string().nullable(), status: z.enum(["active", "inactive"]), series: z.string().nullable(), issueNumber: z.string().nullable(), issueDate: z.string().nullable(), maturityDate: z.string().nullable(), removedDate: z.string().nullable(), removedReason: z.enum(["matured", "accelerated", "early-redemption", "cancelled", "converted", "other"]).nullable(), index: z.string().nullable(), indexPercent: z.number().nullable(), interestRate: z.number().nullable(), nominalValueAtIssue: z.number().nullable(), nominalValue: z.number().nullable(), nominalValueDate: z.string().nullable(), quantityIssued: z.number().nullable(), quantityOutstanding: z.number().nullable(), incentivized: z.boolean().nullable(), guarantee: z.string().nullable()}); export const debentureListResponse = z.object({results: z.array(debenture), pagination: z.object({page: z.number().int(), limit: z.number().int(), total: z.number().int(), hasMore: z.boolean()}), requestedAt: z.string(), took: z.number().int().gte(0)}); 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 }; /** * Listar debĂȘntures * * GET /api/v2/debentures/list */ export const getApiApiV2DebenturesList = async (args: GetApiApiV2DebenturesListArgs, options?: GetApiApiV2DebenturesListOptions) => { const {baseUrl = "https://brapi.dev", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbols != null) query.append("symbols", String(args.symbols)) if (args.cnpjs != null) query.append("cnpjs", String(args.cnpjs)) if (args.search != null) query.append("search", String(args.search)) if (args.status != null) query.append("status", String(args.status)) if (args.index != null) query.append("index", String(args.index)) if (args.incentivized != null) query.append("incentivized", String(args.incentivized)) if (args.maturityFrom != null) query.append("maturityFrom", String(args.maturityFrom)) if (args.maturityTo != null) query.append("maturityTo", String(args.maturityTo)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/debentures/list${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(debentureListResponse, await res.json()) };