import {z} from 'zod' export type GetApiApiV2MessagesPreviewArgs = {text?: string | undefined, templateId?: number | undefined, sendingTime?: number | undefined, sendingDateTime?: string | undefined, sendingTimezone?: string | undefined, contacts?: string | undefined, lists?: string | undefined, phones?: string | undefined, cutExtra?: (0 | 1) | undefined, partsCount?: number | undefined, referenceId?: number | undefined, from?: string | undefined, rule?: string | undefined, createChat?: (0 | 1) | undefined, tts?: (0 | 1) | undefined, local?: (0 | 1) | undefined, localCountry?: string | undefined}; export type GetApiApiV2MessagesPreviewOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const messageOut = z.object({id: z.number().int(), sender: z.string().nullable().optional(), receiver: z.string().optional(), text: z.string(), status: z.enum(["q", "s", "e", "r", "a", "d", "b", "f", "u", "j", "i", "p", "h"]), rejectReason: z.enum(["a", "f", "l", "c", "u", "b", "s", "m", "n", "i", "if", "d", "t", "e", "h", "k", "r", "g", "j", "w", "v", "q", "x", "o", "p"]).optional(), contactId: z.number().int().nullable(), sessionId: z.number().int().nullable(), messageTime: z.string(), avatar: z.string().nullable(), deleted: z.boolean().optional(), deliveredAt: z.string().nullable().optional(), charset: z.string().nullable(), charsetLabel: z.string().nullable(), firstName: z.string().nullable(), lastName: z.string().nullable(), country: z.string().nullable(), phone: z.string().nullable().optional(), price: z.number().nullable().optional(), partsCount: z.number().int().nullable(), fromEmail: z.string().nullable().optional(), fromNumber: z.string().nullable().optional(), senderSource: z.object({id: z.number().int().nullable().optional(), phone: z.string().nullable().optional(), type: z.string().nullable().optional(), countryId: z.string().nullable().optional(), label: z.string().nullable().optional(), status: z.string().nullable().optional(), carrierStatus: z.string().nullable().optional()}).optional(), session: z.object({id: z.number().int().nullable().optional(), startTime: z.string().nullable().optional(), text: z.string().nullable().optional(), price: z.number().nullable().optional(), numbersCount: z.number().int().nullable().optional(), destination: z.string().nullable().optional(), source: z.string().nullable().optional(), referenceId: z.string().nullable().optional(), initiatorId: z.number().int().nullable().optional()}).optional()}); export const getMessagePreviewResponse = z.object({messages: z.array(messageOut).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 }; /** * Preview message * * GET /api/v2/messages/preview */ export const getApiApiV2MessagesPreview = async (args: GetApiApiV2MessagesPreviewArgs, options?: GetApiApiV2MessagesPreviewOptions) => { const {baseUrl = "https://rest.textmagic.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.text != null) query.append("text", String(args.text)) if (args.templateId != null) query.append("templateId", String(args.templateId)) if (args.sendingTime != null) query.append("sendingTime", String(args.sendingTime)) if (args.sendingDateTime != null) query.append("sendingDateTime", String(args.sendingDateTime)) if (args.sendingTimezone != null) query.append("sendingTimezone", String(args.sendingTimezone)) if (args.contacts != null) query.append("contacts", String(args.contacts)) if (args.lists != null) query.append("lists", String(args.lists)) if (args.phones != null) query.append("phones", String(args.phones)) if (args.cutExtra != null) query.append("cutExtra", String(args.cutExtra)) if (args.partsCount != null) query.append("partsCount", String(args.partsCount)) if (args.referenceId != null) query.append("referenceId", String(args.referenceId)) if (args.from != null) query.append("from", String(args.from)) if (args.rule != null) query.append("rule", String(args.rule)) if (args.createChat != null) query.append("createChat", String(args.createChat)) if (args.tts != null) query.append("tts", String(args.tts)) if (args.local != null) query.append("local", String(args.local)) if (args.localCountry != null) query.append("localCountry", String(args.localCountry)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/messages/preview${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(getMessagePreviewResponse, await res.json()) };