import {z} from 'zod' export type GetApiV2CampaignSummariesArgs = {version?: string | undefined, program_type?: string | undefined, tags?: Array | undefined, labels?: Array | undefined, program_label?: string | undefined, states?: (Array<'ARCHIVED' | 'ENDED' | 'LIVE' | 'NOT_LAUNCHED' | 'PAUSED' | 'STOPPED'>) | undefined, include_archived?: boolean | undefined}; export type GetApiV2CampaignSummariesOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const campaignLabelResponse = z.object({name: z.string().optional(), type: z.enum(["DATA", "PROGRAM"]).optional()}); export const campaignSummaryResponse = z.object({campaign_id: z.string(), campaign_locks: z.array(z.enum(["DUPLICATE", "EDIT"])), campaign_type: z.enum(["CONFIGURATION", "EXTENSION", "INTEGRATION", "MARKETING"]), description: z.string(), end_date: zonedDateTime, is_published: z.boolean(), labels: z.array(campaignLabelResponse), last_published_date: zonedDateTime, name: z.string(), pause_date: zonedDateTime, program_label: z.string(), program_type: z.string(), root_component_type: z.string(), start_date: zonedDateTime, state: z.enum(["ARCHIVED", "ENDED", "LIVE", "NOT_LAUNCHED", "PAUSED", "STOPPED"]), stop_date: zonedDateTime, tags: z.array(z.string()), theme_name: z.string(), updated_date: zonedDateTime, variants: z.array(z.string())}); export const getApiV2CampaignSummariesResponse = z.array(campaignSummaryResponse); 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 }; /** * List campaign summaries * * GET /v2/campaign-summaries * * Also accepts auth: QUERY, COOKIE */ export const getApiV2CampaignSummaries = async (args: GetApiV2CampaignSummariesArgs, options?: GetApiV2CampaignSummariesOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.version != null) query.append("version", String(args.version)) if (args.program_type != null) query.append("program_type", String(args.program_type)) for (const item of args.tags ?? []) query.append("tags", String(item)) for (const item of args.labels ?? []) query.append("labels", String(item)) if (args.program_label != null) query.append("program_label", String(args.program_label)) for (const item of args.states ?? []) query.append("states", String(item)) if (args.include_archived != null) query.append("include_archived", String(args.include_archived)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v2/campaign-summaries${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(getApiV2CampaignSummariesResponse, await res.json()) };