import {z} from 'zod' export type GetApiApiV1CompanySlugEnrichmentArgs = {slug: string, 'If-None-Match'?: string | undefined}; export type GetApiApiV1CompanySlugEnrichmentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const jsonValue: z.ZodType = z.union([z.string(), z.number(), z.boolean(), z.array(z.lazy(() => jsonValue)), z.record(z.string(), z.lazy(() => jsonValue))]).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 companyEnrichmentResponse = z.object({slug: z.string().optional(), sources: z.record(z.string(), jsonValue).optional(), _source_catalog: sourceCatalogEnvelope.optional(), _data_sources_used: z.array(publicSourcePolicyMetadata).optional()}).and(z.record(z.string(), jsonValue)); 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 }; /** * Fail-closed public enrichment payload * * GET /api/v1/company/{slug}/enrichment */ export const getApiApiV1CompanySlugEnrichment = async (args: GetApiApiV1CompanySlugEnrichmentArgs, options?: GetApiApiV1CompanySlugEnrichmentOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['If-None-Match'] != null) headers.set("If-None-Match", String(args['If-None-Match'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/company/${encodeURIComponent(String(args.slug))}/enrichment${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(companyEnrichmentResponse, await res.json()) };