import {z} from 'zod' export type DeliveryStatus = 'Queued' | 'Dispatched' | 'Aborted' | 'Cancelled' | 'Failed' | 'Delivered' | 'Expired' | 'Rejected' | 'Deleted' | 'Unknown'; export type DeliveryReceiptStatusCode = 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418; export type GetApiXmsV1ServicePlanIdDeliveryReportsArgs = {page?: number | undefined, page_size?: number | undefined, start_date?: string | undefined, end_date?: string | undefined, status?: Array | undefined, code?: Array | undefined, client_reference?: string | undefined}; export type GetApiXmsV1ServicePlanIdDeliveryReportsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const deliveryReceiptStatusCode = z.union([z.literal(400), z.literal(401), z.literal(402), z.literal(403), z.literal(404), z.literal(405), z.literal(406), z.literal(407), z.literal(408), z.literal(410), z.literal(411), z.literal(412), z.literal(413), z.literal(414), z.literal(415), z.literal(416), z.literal(417), z.literal(418)]); export const deliveryStatus = z.enum(["Queued", "Dispatched", "Aborted", "Cancelled", "Failed", "Delivered", "Expired", "Rejected", "Deleted", "Unknown"]); export const recipientDeliveryReport = z.object({applied_originator: z.string().optional(), at: z.string(), batch_id: z.string(), client_reference: z.string().optional(), code: deliveryReceiptStatusCode, encoding: z.enum(["GSM", "UNICODE"]).optional(), number_of_message_parts: z.number().int().optional(), operator: z.string().optional(), operator_status_at: z.string().optional(), recipient: z.string(), status: deliveryStatus, type: z.enum(["recipient_delivery_report_sms", "recipient_delivery_report_mms"])}); export const deliveryReportList = z.object({count: z.number().int().optional(), page: z.number().int().optional(), page_size: z.number().int().optional(), delivery_reports: z.array(recipientDeliveryReport).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 }; /** * Retrieve a list of delivery reports * * GET /xms/v1/{service_plan_id}/delivery_reports */ export const getApiXmsV1ServicePlanIdDeliveryReports = async (args: GetApiXmsV1ServicePlanIdDeliveryReportsArgs, options?: GetApiXmsV1ServicePlanIdDeliveryReportsOptions) => { 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 (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.status != null) query.append("status", args.status.join(',')) if (args.code != null) query.append("code", args.code.join(',')) if (args.client_reference != null) query.append("client_reference", String(args.client_reference)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/xms/v1/${encodeURIComponent(String(args.service_plan_id))}/delivery_reports${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(deliveryReportList, await res.json()) };