import {z} from 'zod' export type CreateApiApiV1CompaniesBatchUpsertArgs = {body: {companies: Array<{name: string, websiteUrl?: (string | null) | undefined, linkedinUrl?: (string | null) | undefined, summary?: (string | null) | undefined, overview?: (string | null) | undefined, size?: ('1-10' | '11-50' | '51-200' | '201-500' | '501-1000' | '1001-5000' | '5001-10000' | '10001+' | 'null' | null) | undefined, industry?: (Array | null) | undefined, relationship?: ('client' | 'target' | 'none') | undefined, type?: ('educational' | 'government' | 'nonprofit' | 'private' | 'public' | 'null' | null) | undefined, employeeCount?: (number | null) | undefined, ticker?: (string | null) | undefined, location?: ({name?: (string | null) | undefined, locality?: (string | null) | undefined, region?: (string | null) | undefined, metro?: (string | null) | undefined, country?: (string | null) | undefined, streetAddress?: (string | null) | undefined, addressLine2?: (string | null) | undefined, postalCode?: (string | null) | undefined, raw?: (string | null) | undefined, latitude?: (number | null) | undefined, longitude?: (number | null) | undefined} | null) | undefined, customAttributes?: (Array<{customAttributeId: string, optionId?: (string | null) | undefined, value?: (string | number | null) | undefined}>) | undefined, id?: string | undefined}>}}; export type CreateApiApiV1CompaniesBatchUpsertOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1CompaniesBatchUpsertResponse = z.object({status: z.literal("ok"), data: z.array(z.object({index: z.number().int(), status: z.enum(["created", "updated", "failed"]), id: z.string().optional(), error: z.string().optional()})), summary: z.object({total: z.number().int(), created: z.number().int(), updated: z.number().int(), failed: z.number().int()})}); 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 or update companies in bulk (upsert) * * POST /api/v1/companies/batch/upsert */ export const createApiApiV1CompaniesBatchUpsert = async (args: CreateApiApiV1CompaniesBatchUpsertArgs, options: CreateApiApiV1CompaniesBatchUpsertOptions) => { const {baseUrl, 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(/\/+$/, '')}/api/v1/companies/batch/upsert${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(createApiApiV1CompaniesBatchUpsertResponse, await res.json()) };