import {z} from 'zod' export type GetApiApiContentTemplatesArgs = {page?: number | undefined, page_size?: number | undefined, sort?: ('created_at' | 'modified_at') | undefined, order?: ('asc' | 'desc') | undefined}; export type GetApiApiContentTemplatesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const emailTemplatesObject = z.object({html_body: z.string(), plaintext_body: z.string(), subject: z.string()}); export const smsTemplatesObject = z.object({alert: z.string()}); export const openChannelTemplatesObject = z.object({alert: z.string(), media_attachment: z.string().optional(), summary: z.string().optional(), title: z.string().optional()}); export const appAndWebTemplatesObject = z.object({alert: z.string(), icon: z.string().optional(), summary: z.string().optional(), title: z.string().optional()}); export const messageCenterTemplatesObject = z.object({html_body: z.string(), title: z.string().optional()}); export const feedReferences = z.object({defaults: z.record(z.string(), z.record(z.string(), z.union([z.string(), z.number()]))).optional(), feeds: z.array(z.object({name: z.string(), on_error: z.enum(["continue", "cancel"]).optional(), params: z.record(z.string(), z.union([z.string(), z.number()])).optional()}))}); export const snippetReferences = z.object({snippets: z.array(z.object({name: z.string()}))}); export const contentTemplateObject = z.object({content: z.union([emailTemplatesObject, smsTemplatesObject, openChannelTemplatesObject, appAndWebTemplatesObject, messageCenterTemplatesObject]), created_at: z.string().optional(), description: z.string().optional(), external_id: z.string().optional(), feed_references: feedReferences.optional(), id: z.string().optional(), modified_at: z.string().optional(), name: z.string(), snippet_references: snippetReferences.optional(), type: z.enum(["email", "sms", "open", "app", "web", "message_center"])}); export const getApiApiContentTemplatesResponse = z.object({content_templates: z.array(contentTemplateObject).optional(), count: z.number().int().optional(), next_page: z.string().optional(), ok: z.boolean(), prev_page: z.string().optional(), total_count: z.number().int().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 }; /** * List content templates * * GET /api/content/templates * * Also accepts auth: oauth2Token */ export const getApiApiContentTemplates = async (args: GetApiApiContentTemplatesArgs, options?: GetApiApiContentTemplatesOptions) => { const {baseUrl = "https://go.urbanairship.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.urbanairship+json; version=3") 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.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/content/templates${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(getApiApiContentTemplatesResponse, await res.json()) };