import {z} from 'zod' export type GetApiApiV1EmpresaSlugFactsArgs = {slug: string, limit?: number | undefined, 'If-None-Match'?: string | undefined}; export type GetApiApiV1EmpresaSlugFactsOptions = {baseUrl?: string | undefined, apiKey?: 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 bormeFact = z.object({fact_type: z.string().optional(), publish_date: z.string().nullable().optional(), event_date: z.string().nullable().optional(), text_value: z.string().nullable().optional(), person_name: z.string().nullable().optional(), role: z.string().nullable().optional(), source_url: z.string().nullable().optional()}).and(z.record(z.string(), jsonValue)); export const companyFactsResponse = z.object({slug: z.string(), count: z.number().int(), has_more: z.boolean(), facts: z.array(bormeFact), summary: z.object({total_facts: z.number().int().gte(0), first_seen: z.string().nullable(), last_seen: z.string().nullable(), types: z.record(z.string(), z.number().int().gte(0))}), attribution: z.string().optional(), source: z.literal("borme"), sync_date: 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 extracted BORME facts for a company * * GET /api/v1/empresa/{slug}/facts * * Also accepts auth: bearerAuth */ export const getApiApiV1EmpresaSlugFacts = async (args: GetApiApiV1EmpresaSlugFactsArgs, options?: GetApiApiV1EmpresaSlugFactsOptions) => { 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/empresa/${encodeURIComponent(String(args.slug))}/facts${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(companyFactsResponse, await res.json()) };