import {z} from 'zod' export type LegalReportRequest = {csrf?: string | undefined}; export type CreateApiApiV1EmpresaSlugInformeLegalArgs = {slug: string, 'X-CSRF-Token': string, body: LegalReportRequest}; export type CreateApiApiV1EmpresaSlugInformeLegalOptions = {baseUrl?: string | undefined, cookieAuth?: 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 legalReportAxis = z.object({axis_id: z.string(), label: z.string(), present: z.boolean(), count: z.number().int().gte(0), first_date: z.string().nullable().optional(), last_date: z.string().nullable().optional(), norm_slug: z.string().nullable().optional(), norm_articulos: z.string().nullable().optional(), concepto_slug: z.string().nullable().optional(), anchor: z.string().nullable().optional(), official_url: z.string().nullable().optional(), official_label: z.string().nullable().optional(), is_cendoj: z.boolean().optional(), subaxes: z.array(z.record(z.string(), jsonValue)).optional(), items: z.array(jsonValue).optional()}).and(z.record(z.string(), jsonValue)); export const legalReportDocument = z.object({state: z.record(z.string(), jsonValue).nullable(), axes: z.array(legalReportAxis), totals: z.object({axes_present: z.number().int().gte(0), events_total: z.number().int().gte(0), norms_cited: z.array(z.string())})}); export const legalReportResponse = z.object({ok: z.literal(true), company: z.object({slug: z.string(), name: z.string()}), charged: z.number().int().gte(0), beta_free: z.boolean(), already_paid: z.boolean(), balance_after: z.number().int().nullable(), report: legalReportDocument}); 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 }; /** * Create the authenticated user's redacted corporate legal report * * POST /api/v1/empresa/{slug}/informe-legal */ export const createApiApiV1EmpresaSlugInformeLegal = async (args: CreateApiApiV1EmpresaSlugInformeLegalArgs, options?: CreateApiApiV1EmpresaSlugInformeLegalOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-CSRF-Token'] != null) headers.set("X-CSRF-Token", String(args['X-CSRF-Token'])) if (cookieAuth !== undefined) cookies.push("ob_sess=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/empresa/${encodeURIComponent(String(args.slug))}/informe-legal${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(legalReportResponse, await res.json()) };