import {z} from 'zod' export type GetApiApiV2MessagesPriceNormalizedArgs = {includeBlocked?: (0 | 1) | undefined, 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 GetApiApiV2MessagesPriceNormalizedOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getMessagePriceResponseCountriesItem = z.object({country: z.string(), countryName: z.string(), allowDedicated: z.boolean(), count: z.number(), max: z.number(), sum: z.string(), landline: z.number()}); export const getMessagePriceResponse = z.object({total: z.number(), parts: z.number().int().nullable(), countries: z.array(getMessagePriceResponseCountriesItem)}); 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 }; /** * Check message price * * GET /api/v2/messages/price/normalized */ export const getApiApiV2MessagesPriceNormalized = async (args: GetApiApiV2MessagesPriceNormalizedArgs, options?: GetApiApiV2MessagesPriceNormalizedOptions) => { 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.includeBlocked != null) query.append("includeBlocked", String(args.includeBlocked)) 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/price/normalized${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(getMessagePriceResponse, await res.json()) };