import {z} from 'zod' export type MobileOriginatedId = string; export type ShortenLinks = boolean; export type MmsOverrideObject = {fallback_text: string, shorten_links?: ShortenLinks | undefined, slides: Array<{media: {content_length?: number | undefined, content_type: 'image/jpeg' | 'image/gif' | 'image/png' | 'text/vcard' | 'audio/wav' | 'audio/3gpp' | 'audio/3gpp2' | 'audio/mp4' | 'audio/mpeg', url: string}, text?: string | undefined}>, subject?: string | undefined}; export type CreateApiApiSmsCustomResponseArgs = {'X-UA-Appkey': string, body: {mobile_originated_id: MobileOriginatedId, sms: {alert: string, shorten_links?: ShortenLinks | undefined}} | {mms: MmsOverrideObject, mobile_originated_id: MobileOriginatedId}}; export type CreateApiApiSmsCustomResponseOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const operationId = z.string(); export const pushId = z.string(); export const createApiApiSmsCustomResponseResponse = z.object({ok: z.boolean().optional(), operation_id: operationId.optional(), push_id: pushId.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 }; /** * Custom SMS response * * POST /api/sms/custom-response */ export const createApiApiSmsCustomResponse = async (args: CreateApiApiSmsCustomResponseArgs, options?: CreateApiApiSmsCustomResponseOptions) => { const {baseUrl = "https://go.urbanairship.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.urbanairship+json; version=3") if (args['X-UA-Appkey'] != null) headers.set("X-UA-Appkey", String(args['X-UA-Appkey'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/sms/custom-response${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(createApiApiSmsCustomResponseResponse, await res.json()) };