import {z} from 'zod' export type GetApiApiV1TendersArgs = {q?: string | undefined, cpv?: string | undefined, phase?: ('PUB' | 'PRE' | 'EV' | 'ADJ' | 'RES' | 'ANUL') | undefined, province?: string | undefined, buyer_nif?: string | undefined, supplier_cif?: string | undefined, open_only?: boolean | undefined, amount_kind?: ('budget' | 'estimated' | 'award') | undefined, min_amount_eur?: number | undefined, max_amount_eur?: number | undefined, published_from?: string | undefined, published_to?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiApiV1TendersOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tenderParty = z.object({name: z.string().nullable().optional(), cif: z.string().nullable().optional(), nif: z.string().nullable().optional(), dir3: z.string().nullable().optional(), company_slug: z.string().nullable().optional(), withheld: z.boolean().optional()}); export const tenderMoney = z.object({value: z.number().gte(0).lte(10000000000).nullable(), currency: z.string().nullable()}); export const tenderNotice = z.object({key: z.string(), folder_id: z.string().nullable(), object: z.string(), buyer: tenderParty, supplier: tenderParty.nullable(), supplier_withheld: z.boolean(), cpv: z.string().nullable(), cpv_codes: z.array(z.string()), contract_type: z.string().nullable(), procedure: z.string().nullable(), amounts: z.object({budget_total: tenderMoney, budget_tax_exclusive: tenderMoney, estimated_total: tenderMoney, award_payable_total: tenderMoney}), reported_amount_eur: z.number().nullable(), reported_tax_exclusive_amount_eur: z.number().nullable(), amount_quality: z.string(), amount_semantics: z.string(), phase: z.enum(["PUB", "PRE", "EV", "ADJ", "RES", "ANUL", "null"]).nullable(), phase_description: z.string().nullable(), published_at: z.string().nullable(), awarded_at: z.string().nullable(), formalised_at: z.string().nullable(), submission_deadline: z.string().nullable(), is_open: z.boolean(), province: z.string().nullable(), province_slug: z.string().nullable(), document_count: z.number().int().gte(0), lot_count: z.number().int().gte(0), result_count: z.number().int().gte(0), supplier_count: z.number().int().gte(0), official_url: z.string().nullable(), parser_version: z.string().nullable(), source: z.literal("PLACSP")}); export const tenderSearchFilters = z.object({q: z.string().nullable(), cpv: z.string().nullable(), phase: z.string().nullable(), buyer_nif: z.string().nullable(), supplier_cif: z.string().nullable(), province: z.string().nullable(), published_from: z.string().nullable(), published_to: z.string().nullable(), amount_kind: z.string().nullable(), min_amount_eur: z.number().nullable(), max_amount_eur: z.number().nullable(), open_only: z.boolean()}); export const tenderCoverage = z.object({projection: z.string().nullable().optional(), lots: z.string().nullable().optional(), multiple_cpv: z.string().nullable().optional(), supplier_edges: z.string().nullable().optional(), documents: z.string().nullable().optional(), cpv_prefix_filter: z.string().nullable().optional(), projection_generation: z.string().optional(), company_public_generation: z.string().optional()}); 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 tenderSearchResponse = z.object({schema_version: z.string(), count: z.number().int(), items: z.array(tenderNotice), page: z.object({limit: z.number().int(), has_more: z.boolean(), next_cursor: z.string().nullable()}), ordering: z.enum(["published_desc", "ingestion_desc", "cpv_prefix_published_desc", "deadline_asc", "budget_amount_desc", "estimated_amount_desc", "award_amount_desc"]), filters: tenderSearchFilters, coverage: tenderCoverage, attribution: z.string(), disclaimer: 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 }; /** * Search public procurement notices * * GET /api/v1/tenders * * Also accepts auth: bearerAuth */ export const getApiApiV1Tenders = async (args: GetApiApiV1TendersArgs, options?: GetApiApiV1TendersOptions) => { 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.q != null) query.append("q", String(args.q)) if (args.cpv != null) query.append("cpv", String(args.cpv)) if (args.phase != null) query.append("phase", String(args.phase)) if (args.province != null) query.append("province", String(args.province)) if (args.buyer_nif != null) query.append("buyer_nif", String(args.buyer_nif)) if (args.supplier_cif != null) query.append("supplier_cif", String(args.supplier_cif)) if (args.open_only != null) query.append("open_only", String(args.open_only)) if (args.amount_kind != null) query.append("amount_kind", String(args.amount_kind)) if (args.min_amount_eur != null) query.append("min_amount_eur", String(args.min_amount_eur)) if (args.max_amount_eur != null) query.append("max_amount_eur", String(args.max_amount_eur)) if (args.published_from != null) query.append("published_from", String(args.published_from)) if (args.published_to != null) query.append("published_to", String(args.published_to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/tenders${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(tenderSearchResponse, await res.json()) };