import {z} from 'zod' export type GetApiV1FilingsArgs = {date_from?: (string | null) | undefined, date_to?: (string | null) | undefined, company_code?: (string | null) | undefined, doc_type?: (string | null) | undefined, since?: (string | null) | undefined, limit?: number | undefined, offset?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1FilingsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const filingOut = z.object({doc_id: z.string(), edinet_code: z.string(), doc_type: z.string(), doc_type_label: z.string().nullable().optional(), fiscal_year: z.number().int(), period_start: z.string(), period_end: z.string(), accounting_standard: z.string(), filed_at: z.string().nullable().optional(), received_at: z.string(), doc_description: z.string().nullable().optional(), created_at: z.string()}); export const axioraApiSchemasMeta = z.record(z.string(), z.unknown()); export const listResponseFilingOut = z.object({data: z.array(filingOut), 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 }; /** * List Filings * * GET /v1/filings */ export const getApiV1Filings = async (args: GetApiV1FilingsArgs, options: GetApiV1FilingsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (args.company_code != null) query.append("company_code", String(args.company_code)) if (args.doc_type != null) query.append("doc_type", String(args.doc_type)) if (args.since != null) query.append("since", String(args.since)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/filings${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(listResponseFilingOut, await res.json()) };