import {z} from 'zod' export type DeleteApiXmsV1ServicePlanIdBatchesBatchIdOptions = {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 }; /** * Cancel a batch message * * DELETE /xms/v1/{service_plan_id}/batches/{batch_id} */ export const deleteApiXmsV1ServicePlanIdBatchesBatchId = async (options?: DeleteApiXmsV1ServicePlanIdBatchesBatchIdOptions) => { const {baseUrl = "https://us.sms.api.sinch.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "DELETE", headers}) 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()) };