import {z} from 'zod' export type GetApiApiV1CompanySlugTimelineArgs = {slug: string}; export type GetApiApiV1CompanySlugTimelineOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1CompanySlugTimelineResponse = z.object({slug: z.string(), count: z.number().int().lte(100), sources: z.array(z.enum(["borme", "placsp"])), events: z.array(z.object({date: z.string().optional(), category: z.enum(["borme", "contratos"]).optional(), type: z.string().optional(), title: z.string().optional(), amount_eur: z.number().nullable().optional(), source_slug: z.enum(["borme", "placsp"]).optional(), source_url: z.string().optional(), confidence: z.enum(["high", "medium", "low"]).optional(), amount_semantics: z.string().optional()})), coverage: z.object({borme: z.string(), placsp: z.string(), bdns: z.literal("unavailable_generation_bound_projection_missing")}), _disclaimer: 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 unified multi-source company timeline * * GET /api/v1/company/{slug}/timeline * * Also accepts auth: bearerAuth */ export const getApiApiV1CompanySlugTimeline = async (args: GetApiApiV1CompanySlugTimelineArgs, options?: GetApiApiV1CompanySlugTimelineOptions) => { 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))}/timeline${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(getApiApiV1CompanySlugTimelineResponse, await res.json()) };