import {z} from 'zod' export type EmailTemplatesObject = {html_body: string, plaintext_body: string, subject: string}; export type SmsTemplatesObject = {alert: string}; export type OpenChannelTemplatesObject = {alert: string, media_attachment?: string | undefined, summary?: string | undefined, title?: string | undefined}; export type AppAndWebTemplatesObject = {alert: string, icon?: string | undefined, summary?: string | undefined, title?: string | undefined}; export type MessageCenterTemplatesObject = {html_body: string, title?: string | undefined}; export type FeedReferences = {defaults?: ({[key: string]: {[key: string]: string | number}}) | undefined, feeds: Array<{name: string, on_error?: ('continue' | 'cancel') | undefined, params?: ({[key: string]: string | number}) | undefined}>}; export type SnippetReferences = {snippets: Array<{name: string}>}; export type ContentTemplateObject = {content: EmailTemplatesObject | SmsTemplatesObject | OpenChannelTemplatesObject | AppAndWebTemplatesObject | MessageCenterTemplatesObject, created_at?: string | undefined, description?: string | undefined, external_id?: string | undefined, feed_references?: FeedReferences | undefined, id?: string | undefined, modified_at?: string | undefined, name: string, snippet_references?: SnippetReferences | undefined, type: 'email' | 'sms' | 'open' | 'app' | 'web' | 'message_center'}; export type UpdateApiApiContentTemplatesTemplateIdArgs = {body: ContentTemplateObject}; export type UpdateApiApiContentTemplatesTemplateIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const updateApiApiContentTemplatesTemplateIdResponse = z.object({ok: z.boolean(), operation_id: z.string().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 }; /** * Update content template * * PUT /api/content/templates/{template_id} * * Also accepts auth: oauth2Token */ export const updateApiApiContentTemplatesTemplateId = async (args: UpdateApiApiContentTemplatesTemplateIdArgs, options?: UpdateApiApiContentTemplatesTemplateIdOptions) => { const {baseUrl = "https://go.urbanairship.com", fetch: fetchFn = fetch, basicAuth} = 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 (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/content/templates/${encodeURIComponent(String(args.template_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiApiContentTemplatesTemplateIdResponse, await res.json()) };