import {z} from 'zod' export type GetApiApiV1CompanySlugTedArgs = {slug: string, limit?: number | undefined, 'If-None-Match'?: string | undefined}; export type GetApiApiV1CompanySlugTedOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const companyIntegrationProjection = z.object({name: z.literal("company_integrations_public_v1"), schema_version: z.literal("1.3"), builder_version: z.literal("1.4.0"), generation: z.string()}); export const companyIntegrationAcquisitionCoverage = z.object({scope: z.enum(["bounded_newest_first_page_prefix", "directory_snapshot_plus_relative_event_window", "publication_date_window_page_walk", "exact_identity_seed_lookup_snapshot"]), declared_scope_complete: z.literal(true), complete_history_claimed: z.literal(false), source_snapshot_claimed: z.literal(false), window_start: z.string().nullable(), window_end: z.string().nullable(), relative_window_days: z.number().int().gte(1).nullable(), source_as_of: z.string().nullable(), identity_seed_subject_count: z.number().int().gte(1), request_count: z.number().int().gte(1), partition_count: z.number().int().gte(1), source_package_schema: z.enum(["openmercantil-bdns-reingest-package/v2", "openmercantil-cnmv-reingest-package/v2", "openmercantil-ted-reingest-package/v2", "openmercantil-wikidata-reingest-package/v2"]), source_package_manifest_sha256: z.string(), source_fragment_schema: z.enum(["openmercantil-bdns-reingest-source-fragment/v2", "openmercantil-cnmv-reingest-source-fragment/v2", "openmercantil-ted-reingest-source-fragment/v2", "openmercantil-wikidata-reingest-source-fragment/v2"]), source_fragment_sha256: z.string(), source_authority_binding_sha256: z.string(), coverage_sha256: z.string()}); export const companyIntegrationSourceMetadata = z.object({slug: z.enum(["bdns", "cnmv", "ted_eu", "wikidata"]), name: z.string().min(1), license: z.string().min(1), license_url: z.string().nullable(), attribution_required: z.boolean(), attribution_text: z.string().nullable(), official_url: z.string(), reuse_conditions: z.string().min(1), policy_effective_date: z.string(), reviewed_at: z.string(), data_updated_at: z.string().nullable(), catalog_version: z.string().min(1), acquisition_coverage: companyIntegrationAcquisitionCoverage}); export const tedContractRecord = z.object({publication_number: z.string().min(1).max(100), publication_date: z.string(), buyer_name: z.string().min(1).max(300), notice_url: z.string()}); 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 companyTedResponse = z.object({projection_status: z.literal("available"), projection: companyIntegrationProjection, slug: z.string(), cif: z.string(), complete_bounded_projection: z.literal(true), coverage_status: z.enum(["available", "empty"]), source_metadata: companyIntegrationSourceMetadata, coverage_notice: z.string(), count: z.number().int().gte(0).lte(100), returned_count: z.number().int().gte(0).lte(100), items: z.array(tedContractRecord), documentary_notice: z.string(), _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 TED notice records linked to this corporate NIF * * GET /api/v1/company/{slug}/ted * * Also accepts auth: bearerAuth */ export const getApiApiV1CompanySlugTed = async (args: GetApiApiV1CompanySlugTedArgs, options?: GetApiApiV1CompanySlugTedOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args['If-None-Match'] != null) headers.set("If-None-Match", String(args['If-None-Match'])) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/company/${encodeURIComponent(String(args.slug))}/ted${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(companyTedResponse, await res.json()) };