import {z} from 'zod' export type CreateApiBroadcastsArgs = {body: {type?: ('text' | 'template') | undefined, title: string, content?: string | undefined, template?: {whatsAppId: string} | undefined, phoneNumber: string, contactsQuery?: ({tags?: (Array<{id?: string | undefined, name?: string | undefined}>) | undefined}) | undefined, contacts?: (Array<{phoneNumber: string, variables?: ({[key: string]: string | number | boolean}) | undefined}>) | undefined, scheduleDate?: string | undefined, batchSize?: number | undefined, batchInterval?: number | undefined, allowedSendTimeStart?: string | undefined, allowedSendTimeEnd?: string | undefined, allowedSendTimeTimezone?: string | undefined, whatsappApi?: ('cloud_api' | 'mm_api') | undefined}}; export type CreateApiBroadcastsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiBroadcastsResponse = z.object({id: z.string(), type: z.string(), title: z.string().nullable(), status: z.string().nullable(), scheduleDate: z.string().nullable(), batchSize: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), batchInterval: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), allowedSendTimeStart: z.string().nullable(), allowedSendTimeEnd: z.string().nullable(), allowedSendTimeTimezone: z.string().nullable(), createDate: z.string(), processedContacts: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable().optional(), totalBatches: z.number().int().gte(-9007199254740991).lte(9007199254740991).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 a new broadcast * * POST /broadcasts */ export const createApiBroadcasts = async (args: CreateApiBroadcastsArgs, options?: CreateApiBroadcastsOptions) => { const {baseUrl = "https://messenger-public-api.cogfy.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/broadcasts${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(createApiBroadcastsResponse, await res.json()) };