import {z} from 'zod' export type CampaignType = 'outbound_call'; export type CampaignStatus = 'not_started' | 'to_be_run' | 'in_progress' | 'paused' | 'cancelled' | 'completed'; export type ICampaignCreate = {name: string, widget_id?: (string | null) | undefined, type: CampaignType, attributes?: {[key: string]: unknown} | undefined, status?: CampaignStatus | undefined, execution_weekdays?: Array | undefined, time_window_start?: (string | null) | undefined, time_window_end?: (string | null) | undefined, time_zone?: string | undefined, start_time: string, interval: number, enabled?: boolean | undefined}; export type CreateApiApiV1CampaignCreateArgs = {body: ICampaignCreate}; export type CreateApiApiV1CampaignCreateOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignType = z.literal("outbound_call"); export const campaignStatus = z.enum(["not_started", "to_be_run", "in_progress", "paused", "cancelled", "completed"]); export const iCampaignRead = z.object({name: z.string(), widget_id: z.string().nullable().optional(), type: campaignType, attributes: z.record(z.string(), z.unknown()).optional(), status: campaignStatus.optional(), execution_weekdays: z.array(z.number().int()).optional(), time_window_start: z.string().nullable().optional(), time_window_end: z.string().nullable().optional(), time_zone: z.string().optional(), start_time: z.string(), interval: z.number().int(), enabled: z.boolean().optional(), id: z.string(), contacts: z.array(z.unknown()).optional(), widget_name: z.string().nullable().optional()}); export const iPostResponseBaseICampaignRead = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iCampaignRead.nullable().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 Campaign * * POST /api/v1/campaign/create * * Also accepts auth: HTTPBearer */ export const createApiApiV1CampaignCreate = async (args: CreateApiApiV1CampaignCreateArgs, options: CreateApiApiV1CampaignCreateOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/campaign/create${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(iPostResponseBaseICampaignRead, await res.json()) };