import {z} from 'zod' export type GetApiV1CompanyCompanyIdOwnersArgs = {company_id: string, realtime?: boolean | undefined, export?: boolean | undefined, best_available?: boolean | undefined}; export type GetApiV1CompanyCompanyIdOwnersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const entityType = z.enum(["natural_person", "legal_person"]); export const companyRelationType = z.enum(["shareholder", "stockholder", "limited_partner", "general_partner"]); export const companyOwnerNaturalPerson = z.object({full_name: z.string(), first_name: z.string(), last_name: z.string(), date_of_birth: z.string().nullable(), city: z.string(), country: z.string()}).nullable(); export const companyOwnerLegalPerson = z.object({name: z.string(), city: z.string().nullable(), country: z.string()}).nullable(); export const companyOwnerItem = z.object({id: z.string().nullable(), type: entityType, relation_type: companyRelationType, name: z.string(), natural_person: companyOwnerNaturalPerson, legal_person: companyOwnerLegalPerson, nominal_share: z.number(), percentage_share: z.number().nullable(), start: z.string().nullable()}); export const source = z.object({document_url: z.string()}); export const companyOwners = z.object({company_id: z.string(), owners: z.array(companyOwnerItem), sources: z.array(source), best_available: z.boolean()}); 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 company owners * * GET /v1/company/{company_id}/owners */ export const getApiV1CompanyCompanyIdOwners = async (args: GetApiV1CompanyCompanyIdOwnersArgs, options?: GetApiV1CompanyCompanyIdOwnersOptions) => { const {baseUrl = "https://api.openregister.de", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.realtime != null) query.append("realtime", String(args.realtime)) if (args.export != null) query.append("export", String(args.export)) if (args.best_available != null) query.append("best_available", String(args.best_available)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/company/${encodeURIComponent(String(args.company_id))}/owners${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(companyOwners, await res.json()) };