import {z} from 'zod' export type CompanySearchRequestQuery = {value: string}; export type CompanySearchFilterField = 'status' | 'legal_form' | 'register_number' | 'register_court' | 'register_type' | 'city' | 'active' | 'incorporated_at' | 'zip' | 'address' | 'balance_sheet_total' | 'revenue' | 'cash' | 'employees' | 'equity' | 'real_estate' | 'materials' | 'pension_provisions' | 'salaries' | 'taxes' | 'other_taxes' | 'commission_income' | 'commission_expense' | 'liabilities' | 'capital_reserves' | 'active_accruals' | 'passive_accruals' | 'fixed_assets' | 'current_assets' | 'receivables' | 'trade_receivables' | 'inventory' | 'provisions' | 'bank_debt' | 'trade_payables' | 'tangible_assets' | 'financial_assets' | 'retained_earnings' | 'profit_carryforward' | 'other_provisions' | 'shareholder_liabilities' | 'operating_depreciation' | 'financial_depreciation' | 'other_operating_income' | 'interest_income' | 'interest_expense' | 'other_liabilities' | 'financial_debt' | 'intangible_assets' | 'other_operating_expenses' | 'affiliated_liabilities' | 'net_income' | 'parent_net_income' | 'income_before_tax' | 'income_after_tax' | 'ebit' | 'ebitda' | 'industry_codes' | 'capital_amount' | 'capital_currency' | 'number_of_owners' | 'has_sole_owner' | 'has_representative_owner' | 'is_family_owned' | 'youngest_owner_age' | 'purpose' | 'has_lei' | 'lei' | 'had_insolvency' | 'has_open_insolvency' | 'insolvency_stage' | 'insolvency_opened_at'; export type CompanySearchFilter = {value?: string | undefined, min?: string | undefined, max?: string | undefined, values?: Array | undefined, keywords?: Array | undefined, field: CompanySearchFilterField}; export type CompanySearchLocationFilter = {longitude: number, latitude: number, radius?: number | undefined}; export type SearchRequestPagination = {page?: number | undefined, per_page?: number | undefined}; export type CompanySearchRequest = {query?: CompanySearchRequestQuery | undefined, filters?: Array | undefined, location?: CompanySearchLocationFilter | undefined, pagination?: SearchRequestPagination | undefined}; export type CreateApiV1SearchCompanyArgs = {body: CompanySearchRequest}; export type CreateApiV1SearchCompanyOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const companyRegisterType = z.enum(["HRB", "HRA", "PR", "GnR", "VR"]); export const companyLegalForm = z.enum(["ag", "eg", "ek", "ev", "ewiv", "foreign", "gbr", "ggmbh", "gmbh", "kg", "kgaa", "unknown", "llp", "municipal", "ohg", "se", "ug"]); export const companySearchAddress = z.object({street: z.string().optional(), city: z.string(), postal_code: z.string().optional(), country: z.string(), extra: z.string().optional(), formatted_value: z.string()}); export const companySearchResponseItem = z.object({company_id: z.string(), name: z.string(), country: z.string().nullable(), register_number: z.string(), register_type: companyRegisterType, register_court: z.string(), active: z.boolean(), legal_form: companyLegalForm, address: companySearchAddress.nullable(), purpose: z.string().nullable()}); export const pagination = z.object({page: z.number().int(), per_page: z.number().int(), total_pages: z.number().int(), total_results: z.number().int()}); export const companySearchResponse = z.object({results: z.array(companySearchResponseItem), pagination: pagination}); 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 }; /** * Search for companies * * POST /v1/search/company */ export const createApiV1SearchCompany = async (args: CreateApiV1SearchCompanyArgs, options?: CreateApiV1SearchCompanyOptions) => { const {baseUrl = "https://api.openregister.de", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/search/company${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(companySearchResponse, await res.json()) };