import {z} from 'zod' export type MtDestination = unknown; export type DeliveryReportType = 'none' | 'summary' | 'full' | 'per_recipient' | 'per_recipient_final'; export type UpdateBinaryMessage = {from?: string | undefined, type?: 'mt_binary' | undefined, to_add?: Array | undefined, to_remove?: Array | undefined, delivery_report?: DeliveryReportType | undefined, send_at?: string | undefined, expire_at?: string | undefined, callback_url?: string | undefined, client_reference?: string | undefined, feedback_enabled?: boolean | undefined, body?: string | undefined, udh: string, from_ton?: number | undefined, from_npi?: number | undefined}; export type MediaBody = {subject?: string | undefined, message?: string | undefined, url: string}; export type ParameterObj = {[key: string]: {[key: string]: string}}; export type UpdateMediaMessage = {from?: string | undefined, type?: 'mt_media' | undefined, to_add?: Array | undefined, to_remove?: Array | undefined, delivery_report?: DeliveryReportType | undefined, send_at?: string | undefined, expire_at?: string | undefined, callback_url?: string | undefined, client_reference?: string | undefined, feedback_enabled?: boolean | undefined, body?: MediaBody | undefined, parameters?: ParameterObj | undefined, strict_validation?: boolean | undefined}; export type UpdateTextMessage = {from?: string | undefined, type?: 'mt_text' | undefined, to_add?: Array | undefined, to_remove?: Array | undefined, delivery_report?: DeliveryReportType | undefined, send_at?: string | undefined, expire_at?: string | undefined, callback_url?: string | undefined, client_reference?: string | undefined, feedback_enabled?: boolean | undefined, parameters?: ParameterObj | undefined, body?: string | undefined, from_ton?: number | undefined, from_npi?: number | undefined, max_number_of_message_parts?: number | undefined, truncate_concat?: boolean | undefined, flash_message?: boolean | undefined}; export type CreateApiXmsV1ServicePlanIdBatchesBatchIdArgs = {body: UpdateBinaryMessage | UpdateMediaMessage | UpdateTextMessage}; export type CreateApiXmsV1ServicePlanIdBatchesBatchIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mtDestination = z.string(); export const parameterObj = z.record(z.string(), z.record(z.string(), z.string().max(1600))); export const deliveryReportType = z.enum(["none", "summary", "full", "per_recipient", "per_recipient_final"]); export const textResponse = z.object({id: z.string().optional(), to: z.array(mtDestination).optional(), from: z.string().optional(), canceled: z.boolean().optional(), parameters: parameterObj.optional(), body: z.string().max(2000).optional(), type: z.literal("mt_text"), created_at: z.string().optional(), modified_at: z.string().optional(), delivery_report: deliveryReportType.optional(), send_at: z.string().optional(), expire_at: z.string().optional(), callback_url: z.string().max(2048).optional(), client_reference: z.string().max(2048).optional(), feedback_enabled: z.boolean().optional(), flash_message: z.boolean().optional(), truncate_concat: z.boolean().optional(), max_number_of_message_parts: z.number().int().gte(1).optional(), from_ton: z.number().int().gte(0).lte(6).optional(), from_npi: z.number().int().gte(0).lte(18).optional()}); export const binaryResponse = z.object({id: z.string().optional(), to: z.array(mtDestination).optional(), from: z.string().optional(), canceled: z.boolean().optional(), body: z.string().optional(), udh: z.string().optional(), type: z.literal("mt_binary"), created_at: z.string().optional(), modified_at: z.string().optional(), delivery_report: deliveryReportType.optional(), send_at: z.string().optional(), expire_at: z.string().optional(), callback_url: z.string().max(2048).optional(), client_reference: z.string().max(2048).optional(), feedback_enabled: z.boolean().optional(), from_ton: z.number().int().gte(0).lte(6).optional(), from_npi: z.number().int().gte(0).lte(18).optional()}); export const mediaBody = z.object({subject: z.string().max(80).optional(), message: z.string().max(2000).optional(), url: z.string().max(2048)}); export const mediaResponse = z.object({id: z.string().optional(), to: z.array(mtDestination).optional(), from: z.string().optional(), canceled: z.boolean().optional(), body: mediaBody.optional(), parameters: parameterObj.optional(), type: z.literal("mt_media"), created_at: z.string().optional(), modified_at: z.string().optional(), delivery_report: deliveryReportType.optional(), send_at: z.string().optional(), expire_at: z.string().optional(), callback_url: z.string().max(2048).optional(), client_reference: z.string().max(2048).optional(), feedback_enabled: z.boolean().optional(), strict_validation: z.boolean().optional()}); export const batch = z.discriminatedUnion("type", [textResponse, binaryResponse, mediaResponse]); 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 }; /** * Update a Batch message * * POST /xms/v1/{service_plan_id}/batches/{batch_id} */ export const createApiXmsV1ServicePlanIdBatchesBatchId = async (args: CreateApiXmsV1ServicePlanIdBatchesBatchIdArgs, options?: CreateApiXmsV1ServicePlanIdBatchesBatchIdOptions) => { const {baseUrl = "https://us.sms.api.sinch.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/xms/v1/${encodeURIComponent(String(args.service_plan_id))}/batches/${encodeURIComponent(String(args.batch_id))}${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(batch, await res.json()) };