import {z} from 'zod' export type GetApiFilingsArgs = {company?: string | undefined, jurisdiction?: Array | undefined, status?: ('pending' | 'blocked' | 'filed' | 'inapplicable') | undefined, year?: number | undefined, period?: ('annual' | 'q1' | 'q2' | 'q3' | 'q4' | 'january' | 'february' | 'march' | 'april' | 'may' | 'june' | 'july' | 'august' | 'september' | 'october' | 'november' | 'december' | 'other') | undefined, limit?: number | undefined, is_amendment?: boolean | undefined, correction?: string | undefined, id?: Array | undefined}; export type GetApiFilingsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiFilingsResponse = z.object({next: z.string().optional(), previous: z.string().optional(), results: z.array(z.object({id: z.string().optional(), company: z.string().optional(), year: z.number().int().optional(), period: z.string().optional(), label: z.string().optional(), jurisdiction: z.string().nullable().optional(), agency: z.string().nullable().optional(), due_at: z.string().optional(), filed_at: z.string().optional(), closed_at: z.string().optional(), document: z.string().optional(), filing_attempts: z.number().int().optional(), amends: z.array(z.string()).optional(), amended_by: z.array(z.string()).optional(), status: z.string().optional(), status_history: z.array(z.object({status: z.string().optional(), status_at: z.string().optional(), blocked_reasons: z.array(z.object({})).optional()})).optional()})).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 }; /** * List filings * * GET /filings */ export const getApiFilings = async (args: GetApiFilingsArgs, options?: GetApiFilingsOptions) => { const {baseUrl = "https://sandbox.checkhq.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company != null) query.append("company", String(args.company)) for (const item of args.jurisdiction ?? []) query.append("jurisdiction", String(item)) if (args.status != null) query.append("status", String(args.status)) if (args.year != null) query.append("year", String(args.year)) if (args.period != null) query.append("period", String(args.period)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.is_amendment != null) query.append("is_amendment", String(args.is_amendment)) if (args.correction != null) query.append("correction", String(args.correction)) for (const item of args.id ?? []) query.append("id", String(item)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiFilingsResponse, await res.json()) };