import {z} from 'zod' export type CampaignMetaSearchFields = {id?: string | undefined, channels?: (Array<'EMAIL' | 'PUSH' | 'SMS' | 'WHATSAPP' | 'FACEBOOK' | 'GOOGLE ADS' | 'CONNECTORS'>) | undefined, created_by?: Array | undefined, created_date?: {from_date: string, to_date: string} | undefined, delivery_type?: (Array<'ONE_TIME' | 'PERIODIC' | 'EVENT_TRIGGERED' | 'BUSINESS_EVENT_TRIGGERED'>) | undefined, name?: string | undefined, status?: (Array<'SCHEDULED' | 'ACTIVE' | 'PAUSED' | 'SENT' | 'STOPPED' | 'RETIRED' | 'ARCHIVED'>) | undefined, tags?: Array | undefined}; export type CampaignMetaRequest = {request_id: string, campaign_fields?: CampaignMetaSearchFields | undefined, include_child_campaigns?: boolean | undefined, limit: number, page: number}; export type CreateApiCampaignsMetaArgs = {'MOE-APPKEY': string, body: CampaignMetaRequest}; export type CreateApiCampaignsMetaOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const campaignMetaResponse = z.object({campaign_id: z.string().optional(), channel: z.enum(["EMAIL", "PUSH", "SMS", "WHATSAPP", "FACEBOOK", "GOOGLE ADS", "CONNECTORS"]).optional(), platform: z.array(z.enum(["ANDROID", "IOS", "WEB"])).optional(), created_by: z.string().optional(), campaign_delivery_type: z.enum(["ONE_TIME", "PERIODIC", "EVENT_TRIGGERED", "BUSINESS_EVENT_TRIGGERED", "DEVICE_TRIGGERED", "LOCATION_TRIGGERED", "BROADCAST_LIVE_ACTIVITY"]).optional(), campaign_name: z.string().optional(), campaign_team: z.string().optional(), campaign_tags: z.array(z.string()).optional(), campaign_status: z.enum(["SCHEDULED", "ACTIVE", "PAUSED", "SENT", "STOPPED", "RETIRED", "ARCHIVED"]).optional(), campaign_start_time: z.string().optional(), parent_campaign_id: z.string().optional(), total_child_campaigns: z.number().int().optional(), reachability_details: z.object({count: z.number().int().optional(), last_updated_time: z.string().optional()}).optional()}); export const createApiCampaignsMetaResponse = z.array(campaignMetaResponse); 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 }; /** * Get Campaign Meta (V1 — Legacy) * * POST /campaigns/meta */ export const createApiCampaignsMeta = async (args: CreateApiCampaignsMetaArgs, options?: CreateApiCampaignsMetaOptions) => { const {baseUrl = "https://api-01.moengage.com/core-services/v1", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['MOE-APPKEY'] != null) headers.set("MOE-APPKEY", String(args['MOE-APPKEY'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/campaigns/meta${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(createApiCampaignsMetaResponse, await res.json()) };