import {z} from 'zod' export type GetApiV1BulkFinancialsJsonArgs = {sector?: (string | null) | undefined, fiscal_year?: (number | null) | undefined, fields?: (string | null) | undefined}; export type GetApiV1BulkFinancialsJsonOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkFinancialOut = z.object({edinet_code: z.string(), securities_code: z.string().nullable().optional(), name_jp: z.string(), name_en: z.string().nullable().optional(), sector: z.string().nullable().optional(), fiscal_year: z.number().int(), period_end: z.string().nullable().optional(), accounting_standard: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const axioraApiSchemasMeta = z.record(z.string(), z.unknown()); export const listResponseBulkFinancialOut = z.object({data: z.array(bulkFinancialOut), meta: axioraApiSchemasMeta}); 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 }; /** * Export Financials Json * * GET /v1/bulk/financials.json */ export const getApiV1BulkFinancialsJson = async (args: GetApiV1BulkFinancialsJsonArgs, options: GetApiV1BulkFinancialsJsonOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sector != null) query.append("sector", String(args.sector)) if (args.fiscal_year != null) query.append("fiscal_year", String(args.fiscal_year)) if (args.fields != null) query.append("fields", String(args.fields)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/bulk/financials.json${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(listResponseBulkFinancialOut, await res.json()) };