import {z} from 'zod' export type GetApiApiV1CompanySlugAccountsArgs = {slug: string}; export type GetApiApiV1CompanySlugAccountsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const financialAccountsRecord = z.object({source: z.string().nullable(), deposit_date: z.string().nullable(), status: z.string().nullable(), ingresos_eur: z.number().int().nullable(), beneficio_eur: z.number().int().nullable(), activo_eur: z.number().int().nullable(), patrimonio_eur: z.number().int().nullable(), pdf_url: z.string().nullable()}); export const publicSourcePolicyMetadata = z.object({slug: z.string(), name: z.string(), license: z.string(), license_url: z.string().nullable().optional(), attribution_required: z.boolean(), attribution_text: z.string().nullable().optional(), official_url: z.string().nullable().optional(), reuse_conditions: z.string(), policy_effective_date: z.string(), reviewed_at: z.string(), data_updated_at: z.string().nullable().optional(), catalog_version: z.string()}); export const sourceCatalogEnvelope = z.object({catalog_version: z.string(), policy_fingerprint: z.string(), policy_effective_date: z.string().optional(), sources: z.record(z.string(), publicSourcePolicyMetadata)}); export const companyAccountsResponse = z.object({projection_status: z.literal("available"), projection: z.literal("annual_accounts_v1"), slug: z.string(), cif: z.string().nullable(), count: z.number().int().gte(0), al_dia: z.boolean(), last_year: z.number().int().nullable(), last_deposit_date: z.string().nullable(), has_financials: z.boolean(), years: z.union([z.record(z.string(), financialAccountsRecord), z.array(z.unknown())]), _source_catalog: sourceCatalogEnvelope, _data_sources_used: z.array(publicSourcePolicyMetadata), _attributions: z.record(z.string(), z.string()).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 }; /** * Get policy-gated filed accounts metadata * * GET /api/v1/company/{slug}/accounts */ export const getApiApiV1CompanySlugAccounts = async (args: GetApiApiV1CompanySlugAccountsArgs, options?: GetApiApiV1CompanySlugAccountsOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/company/${encodeURIComponent(String(args.slug))}/accounts${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(companyAccountsResponse, await res.json()) };