import {z} from 'zod' export type CreateApiApiV1CampaignsArgs = {body: {name: string, entityType: 'project' | 'opportunity', entityId: string, ownerEmail: string, steps: Array<{position: number, type: 'email' | 'li_inmail' | 'li_request' | 'phone_call' | 'todo', dispatchType?: ('new_thread' | 'reply_to') | undefined, subject?: string | undefined, body: string, scheduledOn?: string | undefined, intervalTime?: string | undefined, intervalDays?: number | undefined, businessDays?: boolean | undefined, businessHours?: boolean | undefined, timezone?: string | undefined, fileIds?: Array | undefined}>}}; export type CreateApiApiV1CampaignsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1CampaignsResponse = z.object({status: z.literal("ok"), data: z.object({id: z.string(), name: z.string(), status: z.literal("active"), type: z.enum(["candidate_outreach", "prospect_outreach", "speculative"]), ownerId: z.string(), createdAt: z.string().nullable(), steps: z.array(z.object({position: z.number().int(), type: z.enum(["email", "li_inmail", "li_request", "phone_call", "todo"]), dispatchType: z.enum(["new_thread", "reply_to", "null"]).nullable(), subject: z.string().nullable(), body: z.string(), variables: z.array(z.object({type: z.string(), name: z.string().nullable(), value: z.string().nullable()})), aiVariables: z.array(z.object({name: z.string(), value: z.string().nullable()})), scheduledOn: z.string().nullable(), intervalTime: z.string().nullable(), intervalDays: z.number().int().nullable(), businessDays: z.boolean(), businessHours: z.boolean(), timezone: z.string().nullable(), id: z.string(), fileIds: z.array(z.string())}))})}); 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 a campaign * * POST /api/v1/campaigns */ export const createApiApiV1Campaigns = async (args: CreateApiApiV1CampaignsArgs, options: CreateApiApiV1CampaignsOptions) => { 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/campaigns${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(createApiApiV1CampaignsResponse, await res.json()) };