import {z} from 'zod' export type TextAlign = 'left' | 'center' | 'right'; export type TextStyle = 'text' | 'h1' | 'h2' | 'subtext'; export type Locale = {content: string}; export type Locales = {[key: string]: Locale} | null; export type IAlignment = 'center' | 'left' | 'right' | 'full'; export type IActionButtonStyle = 'button' | 'link'; export type ElementalNode = {type: 'text', content: string, align: TextAlign, text_style?: TextStyle | undefined, color?: (string | null) | undefined, bold?: (string | null) | undefined, italic?: (string | null) | undefined, strikethrough?: (string | null) | undefined, underline?: (string | null) | undefined, font_size?: (string | null) | undefined, line_height?: (string | null) | undefined, locales?: Locales | undefined, format?: ('markdown' | null) | undefined, channels?: (Array | null) | undefined, ref?: (string | null) | undefined, if?: (string | null) | undefined, loop?: (string | null) | undefined} | {type: 'meta', title?: (string | null) | undefined, channels?: (Array | null) | undefined, ref?: (string | null) | undefined, if?: (string | null) | undefined, loop?: (string | null) | undefined} | {type: 'channel', channel: string, padding?: (string | null) | undefined, font_size?: (string | null) | undefined, line_height?: (string | null) | undefined, raw?: ({[key: string]: unknown} | null) | undefined, channels?: (Array | null) | undefined, ref?: (string | null) | undefined, if?: (string | null) | undefined, loop?: (string | null) | undefined} | {type: 'image', src: string, href?: (string | null) | undefined, align?: IAlignment | undefined, altText?: (string | null) | undefined, width?: (string | null) | undefined, border_color?: (string | null) | undefined, border_size?: (string | null) | undefined, padding?: (string | null) | undefined, channels?: (Array | null) | undefined, ref?: (string | null) | undefined, if?: (string | null) | undefined, loop?: (string | null) | undefined} | {type: 'action', content: string, href: string, action_id?: (string | null) | undefined, align?: (IAlignment | null) | undefined, background_color?: (string | null) | undefined, border_radius?: (string | null) | undefined, border_size?: (string | null) | undefined, font_size?: (string | null) | undefined, padding?: (string | null) | undefined, disable_tracking?: (boolean | null) | undefined, style?: (IActionButtonStyle | null) | undefined, locales: Locales, channels?: (Array | null) | undefined, ref?: (string | null) | undefined, if?: (string | null) | undefined, loop?: (string | null) | undefined} | {type: 'divider', color?: (string | null) | undefined, channels?: (Array | null) | undefined, ref?: (string | null) | undefined, if?: (string | null) | undefined, loop?: (string | null) | undefined} | {type: 'quote', content: string, align?: IAlignment | undefined, borderColor?: (string | null) | undefined, text_style: TextStyle, font_size?: (string | null) | undefined, line_height?: (string | null) | undefined, locales: Locales, channels?: (Array | null) | undefined, ref?: (string | null) | undefined, if?: (string | null) | undefined, loop?: (string | null) | undefined} | {type: 'html', content: string, locales?: Locales | undefined, channels?: (Array | null) | undefined, ref?: (string | null) | undefined, if?: (string | null) | undefined, loop?: (string | null) | undefined}; export type NotificationTemplateState = 'DRAFT' | 'PUBLISHED'; export type NotificationContentPutRequest = {content: {version?: string | undefined, elements: Array}, state?: NotificationTemplateState | undefined}; export type UpdateApiJourneysTemplateIdTemplatesNotificationIdContentArgs = {templateId: string, notificationId: string, body: NotificationContentPutRequest}; export type UpdateApiJourneysTemplateIdTemplatesNotificationIdContentOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationTemplateState = z.enum(["DRAFT", "PUBLISHED"]); export const notificationContentMutationResponse = z.object({id: z.string(), version: z.string(), elements: z.array(z.object({id: z.string(), checksum: z.string()})), state: notificationTemplateState}); 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 }; /** * Replace the content of a journey-scoped notification template * * PUT /journeys/{templateId}/templates/{notificationId}/content */ export const updateApiJourneysTemplateIdTemplatesNotificationIdContent = async (args: UpdateApiJourneysTemplateIdTemplatesNotificationIdContentArgs, options?: UpdateApiJourneysTemplateIdTemplatesNotificationIdContentOptions) => { const {baseUrl = "https://api.courier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/journeys/${encodeURIComponent(String(args.templateId))}/templates/${encodeURIComponent(String(args.notificationId))}/content${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(notificationContentMutationResponse, await res.json()) };