import {z} from 'zod' export type GetApiApiV1GrafoPersonaSlugArgs = {slug: string, limit?: number | undefined}; export type GetApiApiV1GrafoPersonaSlugOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const personGraphCompany = z.object({slug: z.string(), name: z.string().min(1), rel_type: z.literal("OFFICER_OF"), role: z.string(), since: z.string().nullable(), until: z.string().nullable(), documentary_status: z.enum(["open_documentary_mention", "historical_documentary_mention"]), vigency_verified: z.literal(false), source_slug: z.literal("borme")}); export const personPublicProjectionMetadata = z.object({name: z.literal("person_public_v1"), schema_version: z.literal("1.0"), contract_sha256: z.string(), source_generation: z.string(), content_sha256: z.string(), projected_at: 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 personGraphResponse = z.object({schema_version: z.literal("person_graph_v1"), status: z.literal("available"), canonical_url: z.string(), center: z.object({slug: z.string(), name: z.string(), companies_count: z.number().int().gte(1), first_seen: z.string().nullable(), last_seen: z.string().nullable(), subject_type: z.literal("person_documentary_mentions"), identity_resolution: z.literal("not_performed")}), companies: z.array(personGraphCompany), coverage: z.object({borme_officer_edges: z.literal("included"), external_person_enrichment: z.literal("withheld_not_in_person_public_v1")}), projection: personPublicProjectionMetadata, as_of: z.string(), ttl_seconds: z.literal(0), subject_type: z.literal("person_documentary_mentions"), identity_resolution: z.literal("not_performed"), _legal_notice: z.string().min(1), _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 person-to-company graph * * GET /api/v1/grafo/persona/{slug} * * Also accepts auth: bearerAuth */ export const getApiApiV1GrafoPersonaSlug = async (args: GetApiApiV1GrafoPersonaSlugArgs, options?: GetApiApiV1GrafoPersonaSlugOptions) => { 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 (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/grafo/persona/${encodeURIComponent(String(args.slug))}${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(personGraphResponse, await res.json()) };