import {z} from 'zod' export type UpdateCampaignRequest = {name?: string | undefined, track_domain?: string | undefined, nodes?: Array | undefined, edges?: Array | undefined, status?: number | undefined, is_draft?: boolean | undefined, timezone?: string | undefined, disabled?: boolean | undefined, blacklistIfBounce?: boolean | undefined, blacklistIfUnsubscribe?: boolean | undefined}; export type PatchApiCampaignsIdArgs = {id: string, body: UpdateCampaignRequest}; export type PatchApiCampaignsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const contactMetrics = z.object({total: z.number().int().optional(), not_started: z.number().int().optional(), in_progress: z.number().int().optional(), finished: z.number().int().optional(), paused: z.number().int().optional()}); export const campaign = z.object({id: z.string().optional(), name: z.string().optional(), status: z.enum(["active", "inactive"]).optional(), disabled: z.boolean().optional(), contact_count: z.number().int().optional(), contact_metrics: contactMetrics.optional(), team_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const campaignResponse = z.object({success: z.boolean().optional(), request_id: z.string().optional(), data: campaign.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 * * PATCH /campaigns/{id} */ export const patchApiCampaignsId = async (args: PatchApiCampaignsIdArgs, options?: PatchApiCampaignsIdOptions) => { 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))}${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(campaignResponse, await res.json()) };