import {z} from 'zod' export type Trigger = 'ig_comment' | 'ig_story' | 'ig_live' | 'ig_message' | 'fb_comment' | 'fb_live' | 'fb_message' | 'threads_reply'; export type ActivityCreate = {account_id: string, trigger: Trigger, name?: string | undefined, keywords?: Array | undefined, dm_source?: ('keywords' | 'ads') | undefined, match?: ('contains' | 'exact') | undefined, public_replies?: Array | undefined, dm?: (Array<{type: 'text' | 'button' | 'image' | 'card', text?: string | undefined, title?: string | undefined, url?: string | undefined, image_url?: string | undefined, subtitle?: string | undefined, buttons?: Array<{title: string, url: string}> | undefined}>) | undefined, post_scope?: ('all' | 'selected' | 'next_post') | undefined, post_ids?: Array | undefined, follow_gate?: ({enabled: boolean, opening_text?: string | undefined, button_title?: string | undefined, not_following_text?: string | undefined}) | undefined, start_at?: (string | null) | undefined, end_at?: (string | null) | undefined, status?: ('draft' | 'active') | undefined}; export type CreateApiActivitiesArgs = {body: ActivityCreate}; export type CreateApiActivitiesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const trigger = z.enum(["ig_comment", "ig_story", "ig_live", "ig_message", "fb_comment", "fb_live", "fb_message", "threads_reply"]); export const dmBlock = z.object({type: z.enum(["text", "button", "image", "card"]), text: z.string().optional(), title: z.string().optional(), url: z.string().optional(), image_url: z.string().optional(), subtitle: z.string().optional(), buttons: z.array(z.object({title: z.string(), url: z.string()})).optional()}); export const activity = z.object({id: z.string().optional(), name: z.string().optional(), status: z.enum(["draft", "active", "paused"]).optional(), account_id: z.string().optional(), platform: z.enum(["instagram", "facebook", "threads"]).optional(), trigger: trigger.optional(), keywords: z.array(z.string()).optional(), match: z.enum(["contains", "exact"]).optional(), public_replies: z.array(z.string()).optional(), dm: z.array(dmBlock).optional(), post_scope: z.enum(["all", "selected", "next_post"]).optional(), post_ids: z.array(z.string()).nullable().optional(), follow_gate: z.object({enabled: z.boolean(), opening_text: z.string().optional(), button_title: z.string().optional(), not_following_text: z.string().optional()}).optional(), start_at: z.string().nullable().optional(), end_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), dashboard_url: z.string().optional(), stats: z.object({public_replies_sent: z.number().int().optional(), dms_sent: z.number().int().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 }; /** * Create an activity (draft by default) * * POST /activities */ export const createApiActivities = async (args: CreateApiActivitiesArgs, options?: CreateApiActivitiesOptions) => { const {baseUrl = "https://social-automation.cresclab.com/api/v1", 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(/\/+$/, '')}/activities${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(activity, await res.json()) };