import {z} from 'zod' export type OutboundMessageFilterType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; export type OutboundMessageFilter = {id?: number | undefined, messageId?: number | undefined, filterType?: OutboundMessageFilterType | undefined, filterColumn?: (string | null) | undefined, filterValue?: (string | null) | undefined, filterOperation?: (string | null) | undefined, filterPrefix?: (string | null) | undefined}; export type OutboundMessageType = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; export type OutboundPreviewRequestBody = {filters?: (Array | null) | undefined, type?: OutboundMessageType | undefined, settings: unknown, departmentId?: number | undefined}; export type CreateApiV1OutboundMessagesPreviewArgs = {body: OutboundPreviewRequestBody}; export type CreateApiV1OutboundMessagesPreviewOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const previewCountExcludedReasons = z.object({noEmail: z.number().int().optional(), noPhone: z.number().int().optional(), noRecentChat: z.number().int().optional(), marketingOptOut: z.number().int().optional()}); export const previewCountResult = z.object({count: z.number().int().optional(), totalMatchingFilters: z.number().int().optional(), excludedReasons: previewCountExcludedReasons.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 }; /** * Get preview count for outbound message filters * * POST /v1/outbound/messages/preview * * Also accepts auth: API Key */ export const createApiV1OutboundMessagesPreview = async (args: CreateApiV1OutboundMessagesPreviewArgs, options: CreateApiV1OutboundMessagesPreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/outbound/messages/preview${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(previewCountResult, await res.json()) };