import {z} from 'zod' export type GetApiApiV1CompaniesTickerArgs = {ticker: string, format?: string | undefined}; export type GetApiApiV1CompaniesTickerOptions = {baseUrl: string, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const companyDetail = z.object({cvm_code: z.string(), cnpj: z.string(), corporate_name: z.string(), trade_name: z.string().nullable(), ticker_primary: z.string().nullable().optional(), queried_ticker: z.string().nullable().optional(), tickers: z.array(z.string()).optional(), sector: z.string().nullable(), status: z.string().nullable(), registration_date: z.string().nullable(), registration_category: z.string().nullable(), market_type: z.string().nullable(), country: z.string().nullable(), state: z.string().nullable(), city: z.string().nullable(), email: z.string().nullable(), website: z.string().nullable()}); 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 Company * * GET /api/v1/companies/{ticker} */ export const getApiApiV1CompaniesTicker = async (args: GetApiApiV1CompaniesTickerArgs, options: GetApiApiV1CompaniesTickerOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.format != null) query.append("format", String(args.format)) if (ApiKeyHeader !== undefined) headers.set("X-API-Key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/companies/${encodeURIComponent(String(args.ticker))}${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(companyDetail, await res.json()) };