import {z} from 'zod' export type NodePosition = {x: number, y: number}; export type SequenceNode = {id: string, type: 'startNode' | 'stopNode' | 'emailNode' | 'waitNode' | 'ifNode' | 'switchNode' | 'httpApiRequestNode' | 'linkedinProfileVisitNode' | 'linkedinConnectionNode' | 'linkedinConnectionWithMessageNode' | 'linkedinMessageNode' | 'linkedinInmailNode' | 'linkedinLikeRecentPostNode' | 'linkedinFollowNode' | 'linkedinCommentRecentPostNode' | 'linkedinWithdrawConnectionNode' | 'linkedinWaitUntilConnectedNode' | 'moveToAnotherCampaignNode' | 'taskNode' | 'noteNode', data: {[key: string]: unknown}, position: NodePosition, style?: ({width?: number | undefined, height?: number | undefined, minWidth?: number | undefined, minHeight?: number | undefined}) | undefined}; export type SequenceEdge = {id: string, source: string, sourceHandle?: string | undefined, target: string, type?: 'buttonedge' | undefined}; export type UpdateSequenceRequest = {nodes?: Array | undefined, edges?: Array | undefined}; export type PatchApiCampaignsIdSequenceArgs = {id: string, body: UpdateSequenceRequest}; export type PatchApiCampaignsIdSequenceOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const nodePosition = z.object({x: z.number(), y: z.number()}); export const sequenceNode = z.object({id: z.string(), type: z.enum(["startNode", "stopNode", "emailNode", "waitNode", "ifNode", "switchNode", "httpApiRequestNode", "linkedinProfileVisitNode", "linkedinConnectionNode", "linkedinConnectionWithMessageNode", "linkedinMessageNode", "linkedinInmailNode", "linkedinLikeRecentPostNode", "linkedinFollowNode", "linkedinCommentRecentPostNode", "linkedinWithdrawConnectionNode", "linkedinWaitUntilConnectedNode", "moveToAnotherCampaignNode", "taskNode", "noteNode"]), data: z.record(z.string(), z.unknown()), position: nodePosition, style: z.object({width: z.number().int().optional(), height: z.number().int().optional(), minWidth: z.number().int().optional(), minHeight: z.number().int().optional()}).optional()}); export const sequenceEdge = z.object({id: z.string(), source: z.string(), sourceHandle: z.string().optional(), target: z.string(), type: z.literal("buttonedge").optional()}); export const campaignSequenceResponse = z.object({success: z.boolean().optional(), request_id: z.string().optional(), data: z.object({campaign_id: z.string().optional(), nodes: z.array(sequenceNode).optional(), edges: z.array(sequenceEdge).optional()}).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 }; /** * Update Campaign Sequence * * PATCH /campaigns/{id}/sequence */ export const patchApiCampaignsIdSequence = async (args: PatchApiCampaignsIdSequenceArgs, options?: PatchApiCampaignsIdSequenceOptions) => { const {baseUrl = "https://api.supersend.io/v2", 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(/\/+$/, '')}/campaigns/${encodeURIComponent(String(args.id))}/sequence${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(campaignSequenceResponse, await res.json()) };