import {z} from 'zod' export type GetApiApiV2DebenturesProfileArgs = {symbols: string}; export type GetApiApiV2DebenturesProfileOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const debentureProfile = 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(), profitabilityStartDate: z.string().nullable(), cvmRegistration: z.string().nullable(), cvmRegistrationDate: z.string().nullable(), earlyRedemptionAllowed: z.boolean().nullable(), interest: z.object({every: z.number().int().nullable(), unit: z.enum(["month", "day"]).nullable(), dayCount: z.number().int().nullable()}), amortization: z.object({every: z.number().int().nullable(), unit: z.enum(["month", "day"]).nullable(), percent: z.number().nullable(), method: z.string().nullable()}), quantityInTreasury: z.number().nullable(), quantityRedeemed: z.number().nullable(), quantityCancelled: z.number().nullable(), agents: z.object({trustee: z.string().nullable(), paymentAgent: z.string().nullable(), depositary: z.string().nullable(), leadUnderwriter: z.string().nullable()})}); export const debentureProfileResponse = z.object({results: z.array(debentureProfile), 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 }; /** * Perfil da debĂȘnture * * GET /api/v2/debentures/profile */ export const getApiApiV2DebenturesProfile = async (args: GetApiApiV2DebenturesProfileArgs, options?: GetApiApiV2DebenturesProfileOptions) => { 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 (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/debentures/profile${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(debentureProfileResponse, await res.json()) };