import {z} from 'zod' export type GetApiV1CompaniesArgs = {page?: number | undefined, limit?: number | undefined, lt?: string | undefined, sort?: string | undefined, direction?: ('asc' | 'desc') | undefined}; export type GetApiV1CompaniesOptions = {baseUrl?: string | undefined, MetalClientId?: string | undefined, MetalApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const location = z.object({city: z.string().optional(), state: z.string().optional(), country: z.string().optional()}); export const externalReference = z.object({source: z.string().optional(), id: z.string().optional()}); export const reference = z.object({id: z.string().optional(), type: z.string().optional()}); export const discoveredValue = z.object({value: z.unknown(), source: z.string().optional(), confidence: z.number().optional(), pinned: z.boolean().optional(), discoveredAt: z.string().optional()}); export const enrichmentProvenance = z.record(z.string(), z.array(discoveredValue)); export const company = z.object({id: z.string().optional(), canonicalName: z.string().optional(), alternativeNames: z.array(z.string()).optional(), website: z.string().optional(), description: z.string().optional(), shortDescription: z.string().optional(), sector: z.string().optional(), subsector: z.string().optional(), industry: z.string().optional(), location: location.optional(), companyType: z.enum(["operating", "investor", "lender", "investment_bank", "advisor", "sponsor", "service_provider"]).optional(), origin: z.enum(["user", "crm", "agent", "screening"]).optional(), externalId: z.string().optional(), externalReference: externalReference.optional(), tags: z.array(z.string()).optional(), assignees: z.array(reference).optional(), custom: z.record(z.string(), z.unknown()).optional(), employeeCount: z.number().int().optional(), ownershipStatus: z.string().optional(), lastValuation: z.number().optional(), lastRoundDate: z.string().optional(), lastAmountRaised: z.number().optional(), totalMoneyRaised: z.number().optional(), lastFinancials: z.record(z.string(), z.unknown()).optional(), enrichment: enrichmentProvenance.optional(), lastEnrichedAt: z.string().optional(), lastEnrichmentStartedAt: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const getApiV1CompaniesResponse = z.object({data: z.array(company).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 }; /** * List companies * * GET /v1/companies */ export const getApiV1Companies = async (args: GetApiV1CompaniesArgs, options?: GetApiV1CompaniesOptions) => { const {baseUrl = "https://api.metal.ai", fetch: fetchFn = fetch, MetalClientId, MetalApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.lt != null) query.append("lt", String(args.lt)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.direction != null) query.append("direction", String(args.direction)) if (MetalClientId !== undefined) headers.set("x-metal-client-id", MetalClientId) if (MetalApiKey !== undefined) headers.set("x-metal-api-key", MetalApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/companies${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(getApiV1CompaniesResponse, await res.json()) };