import {z} from 'zod' export type CreateApiOrgStorageLeadsSaveManyArgs = {'X-Phantombuster-Org'?: string | undefined, body: {leads: Array<{linkedinProfileUrl: string, linkedinCompanyUrl?: string | undefined, linkedinCompanySlug?: string | undefined, twitterProfileUrl?: string | undefined, linkedinPreviousCompanyUrl?: string | undefined, metadata?: {data: {[key: string]: unknown}} | undefined, linkedinSchoolCompanyUrl?: string | undefined, linkedinPreviousSchoolCompanyUrl?: string | undefined, salesNavigatorProfileUrl?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, location?: string | undefined, civility?: string | undefined, linkedinOpenProfile?: boolean | undefined, linkedinIsHiringBadge?: boolean | undefined, linkedinIsOpenToWorkBadge?: boolean | undefined, professionalEmails?: Array | undefined, personalEmails?: Array | undefined, phoneNumbers?: Array | undefined, websites?: Array | undefined, companyName?: string | undefined, companyWebsite?: string | undefined, linkedinCompanyId?: string | undefined, linkedinProfileId?: string | undefined, linkedinProfileUrn?: string | undefined, linkedinProfileImageUrn?: string | undefined, linkedinProfileImageUrl?: string | undefined, linkedinHeadline?: string | undefined, linkedinJobTitle?: string | undefined, linkedinJobDateRange?: string | undefined, linkedinJobLocation?: string | undefined, companyIndustry?: string | undefined, linkedinFollowersCount?: number | undefined, linkedinDescription?: string | undefined, linkedinJobDescription?: string | undefined, linkedinPreviousJobDescription?: string | undefined, linkedinSkillsLabel?: string | undefined, previousCompanyName?: string | undefined, linkedinPreviousJobTitle?: string | undefined, linkedinPreviousJobDateRange?: string | undefined, linkedinPreviousJobLocation?: string | undefined, linkedinSchoolName?: string | undefined, linkedinPreviousSchoolName?: string | undefined, linkedinSchoolDegree?: string | undefined, linkedinPreviousSchoolDegree?: string | undefined, linkedinSchoolDateRange?: string | undefined, linkedinPreviousSchoolDateRange?: string | undefined, linkedinSchoolDescription?: string | undefined, linkedinPreviousSchoolDescription?: string | undefined, linkedinAccountRelativeProperties?: (Array<{sourceUserId: string, connectionDegree?: number | undefined}>) | undefined, crmAccountRelativeProperties?: (Array<{crmContactId: string, crmAccountId: string, crmName: 'hubspot' | 'pipedrive' | 'salesforce'} | {[key: string]: string | boolean | number}>) | undefined, audienceOfLeads?: Array | undefined, aiGeneratedProperties?: {[key: string]: string} | undefined, customResultsFileName?: string | undefined, error?: string | undefined} | {[key: string]: unknown}>}}; export type CreateApiOrgStorageLeadsSaveManyOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiOrgStorageLeadsSaveManyResponse = z.array(z.union([z.object({linkedinProfileUrl: z.string(), status: z.literal("error"), error: z.string()}).and(z.record(z.string(), z.unknown())), z.object({linkedinProfileUrl: z.string(), status: z.literal("success"), isCreation: z.boolean()}).and(z.record(z.string(), z.unknown()))])); 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 }; /** * Save many leads - Beta. * * POST /org-storage/leads/save-many */ export const createApiOrgStorageLeadsSaveMany = async (args: CreateApiOrgStorageLeadsSaveManyArgs, options?: CreateApiOrgStorageLeadsSaveManyOptions) => { const {baseUrl = "https://api.phantombuster.com/api/v2", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Phantombuster-Org'] != null) headers.set("X-Phantombuster-Org", String(args['X-Phantombuster-Org'])) if (apiKeyAuth !== undefined) headers.set("X-Phantombuster-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/org-storage/leads/save-many${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(createApiOrgStorageLeadsSaveManyResponse, await res.json()) };