import {z} from 'zod' export type GetApiV1WhatsappChannelIdCampaignsCampaignIdFullArgs = {channelId: string, campaignId: string}; export type GetApiV1WhatsappChannelIdCampaignsCampaignIdFullOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsAppEvent = z.object({id: z.number().int().optional(), destination: z.string().optional(), status: z.enum(["sent", "delivered", "read", "failed", "invalid", "enqueued", "pending"]).optional(), failureCode: z.string().nullable().optional(), failureReason: z.string().nullable().optional(), createdAt: z.string().optional()}); export const whatsAppCampaignFull = z.object({id: z.string().optional(), name: z.string().optional(), status: z.enum(["SCHEDULED", "SENDING", "SENT", "STOPPED", "CANCELLED"]).optional(), templateId: z.string().optional(), scheduledAt: z.string().optional(), sentCount: z.number().int().optional(), failedCount: z.number().int().optional(), events: z.array(whatsAppEvent).optional(), eventCounter: z.object({pending: z.number().int().optional(), enqueued: z.number().int().optional(), failed: z.number().int().optional(), sent: z.number().int().optional(), delivered: z.number().int().optional(), read: z.number().int().optional(), answered: z.number().int().optional(), invalid: z.number().int().optional()}).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 }; /** * Detalle completo de campaƱa * * GET /v1/whatsapp/{channelId}/campaigns/{campaignId}/full */ export const getApiV1WhatsappChannelIdCampaignsCampaignIdFull = async (args: GetApiV1WhatsappChannelIdCampaignsCampaignIdFullArgs, options?: GetApiV1WhatsappChannelIdCampaignsCampaignIdFullOptions) => { const {baseUrl = "https://connect.cliengo.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/whatsapp/${encodeURIComponent(String(args.channelId))}/campaigns/${encodeURIComponent(String(args.campaignId))}/full${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(whatsAppCampaignFull, await res.json()) };