import {z} from 'zod' export type GetApiApiV1CompanySlugBdeArgs = {slug: string}; export type GetApiApiV1CompanySlugBdeOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const bdeSectorMetric = z.object({value: z.number(), unit: z.string(), year: z.number().int(), source: z.literal("bde")}); 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 companyBdeResponse = z.object({projection_status: z.literal("available"), projection: z.literal("bde_sector_ratios_v1"), slug: z.string(), is_alias: z.boolean(), identity_generation: z.string(), projection_generation: z.string(), projected_at: z.string(), source_catalog_version: z.string().min(1), coverage_status: z.enum(["matched", "sector_not_projected", "cnae_unavailable"]), has_data: z.boolean(), cnae_code: z.string().optional(), sector_code: z.string().optional(), metrics: z.record(z.string(), bdeSectorMetric), _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 Banco de EspaƱa sector ratios for a company CNAE * * GET /api/v1/company/{slug}/bde * * Also accepts auth: bearerAuth */ export const getApiApiV1CompanySlugBde = async (args: GetApiApiV1CompanySlugBdeArgs, options?: GetApiApiV1CompanySlugBdeOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/company/${encodeURIComponent(String(args.slug))}/bde${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(companyBdeResponse, await res.json()) };