import {z} from 'zod' export type GetApiApiV1ContractsTopCompaniesArgs = {limit?: number | undefined}; export type GetApiApiV1ContractsTopCompaniesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 tenderSupplierResponse = z.object({schema_version: z.string(), count: z.number().int(), ranking: z.enum(["award_procedures_count_desc", "awards_count_desc"]), items: z.array(z.object({company_slug: z.string(), name: z.string(), cif: z.string(), notices_count: z.number().int().gte(0), award_procedures_count: z.number().int().gte(0), tenders_count: z.number().int(), tenders_count_status: z.literal("deprecated_alias_of_notices_count"), awards_count: z.number().int(), awards_count_status: z.literal("deprecated_alias_of_award_procedures_count"), buyers_count: z.number().int(), first_date: z.string().nullable(), last_date: z.string().nullable(), top_cpv: z.string().nullable()})), eligibility: z.string(), data_quality: z.string(), attribution: 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 }; /** * Corporate suppliers ranked by PLACSP award procedures * * GET /api/v1/contracts/top-companies */ export const getApiApiV1ContractsTopCompanies = async (args: GetApiApiV1ContractsTopCompaniesArgs, options?: GetApiApiV1ContractsTopCompaniesOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/contracts/top-companies${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(tenderSupplierResponse, await res.json()) };