import {z} from 'zod' export type GetApiApiV1CampaignListArgs = {page?: number | undefined, size?: number | undefined}; export type GetApiApiV1CampaignListOptions = {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 iCampaignListRead = 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.unknown()).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(), contact_status_counts: z.record(z.string(), z.number().int()).optional()}); export const pageBaseICampaignListRead = z.object({items: z.array(iCampaignListRead), total: z.number().int().gte(0).nullable(), page: z.number().int().gte(1).nullable(), size: z.number().int().gte(1).nullable(), pages: z.number().int().gte(0).nullable().optional(), previous_page: z.number().int().nullable().optional(), next_page: z.number().int().nullable().optional()}); export const iGetResponsePaginatedICampaignListRead = z.object({message: z.string().nullable().optional(), meta: z.record(z.string(), z.unknown()).optional(), data: pageBaseICampaignListRead}); 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 }; /** * Read Campaign List * * GET /api/v1/campaign/list * * Also accepts auth: HTTPBearer */ export const getApiApiV1CampaignList = async (args: GetApiApiV1CampaignListArgs, options: GetApiApiV1CampaignListOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/campaign/list${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(iGetResponsePaginatedICampaignListRead, await res.json()) };