import {z} from 'zod' export type GetApiV1StocksTickerFinancialStatementsStatementArgs = {ticker: string, statement: string, year?: number | undefined, period?: string | undefined}; export type GetApiV1StocksTickerFinancialStatementsStatementOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const financialStatementLineDto = z.object({lineItem: z.string().nullable().optional(), value: z.number().optional(), unit: z.string().nullable().optional(), splitBasis: z.string().nullable().optional(), basis: z.string().nullable().optional(), periodStart: z.string().nullable().optional(), periodEnd: z.string().optional(), form: z.string().nullable().optional(), filedDate: z.string().optional(), accessionNumber: z.string().nullable().optional(), documentId: z.string().nullable().optional()}); export const financialStatementResponse = z.object({ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), statement: z.string().nullable().optional(), fiscalYear: z.number().int().optional(), fiscalPeriod: z.string().nullable().optional(), data: z.array(financialStatementLineDto).nullable().optional()}); 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 }; /** * A company's income statement, balance sheet, or cash-flow statement for one fiscal period, sourced from structured SEC XBRL. Values use the latest restatement per line. Quarterly flow rows are discrete quarters; exact quarters derived from cumulative facts in one currency are marked derived. * * GET /v1/stocks/{ticker}/financial-statements/{statement} */ export const getApiV1StocksTickerFinancialStatementsStatement = async (args: GetApiV1StocksTickerFinancialStatementsStatementArgs, options?: GetApiV1StocksTickerFinancialStatementsStatementOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.year != null) query.append("year", String(args.year)) if (args.period != null) query.append("period", String(args.period)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stocks/${encodeURIComponent(String(args.ticker))}/financial-statements/${encodeURIComponent(String(args.statement))}${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(financialStatementResponse, await res.json()) };