import {z} from 'zod' export type RoutingMethod = 'all' | 'single'; export type Timeouts = {provider?: (number | null) | undefined, channel?: (number | null) | undefined}; export type UTM = {source?: (string | null) | undefined, medium?: (string | null) | undefined, campaign?: (string | null) | undefined, term?: (string | null) | undefined, content?: (string | null) | undefined}; export type ChannelMetadata = {utm?: UTM | undefined}; export type Channel = {brand_id?: (string | null) | undefined, providers?: (Array | null) | undefined, routing_method?: RoutingMethod | undefined, if?: (string | null) | undefined, timeouts?: Timeouts | undefined, override?: ({[key: string]: unknown} | null) | undefined, metadata?: ChannelMetadata | undefined}; export type MessageChannels = {[key: string]: Channel}; 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 ElementalContent = {version: string, elements: Array}; export type Metadata = {utm?: UTM | undefined}; export type MessageProvidersType = {override?: ({[key: string]: unknown} | null) | undefined, if?: (string | null) | undefined, timeouts?: (number | null) | undefined, metadata?: Metadata | undefined}; export type MessageProviders = {[key: string]: MessageProvidersType}; export type MessageRoutingMethod = 'all' | 'single'; export type MessageRoutingChannel = string | MessageRouting; export type MessageRouting = {method: MessageRoutingMethod, channels: Array}; export type TenantTemplateInput = {channels?: MessageChannels | undefined, content: ElementalContent, providers?: MessageProviders | undefined, routing?: MessageRouting | undefined}; export type PutTenantTemplateRequest = {published?: boolean | undefined, template: TenantTemplateInput}; export type UpdateApiTenantsTenantIdTemplatesTemplateIdArgs = {tenant_id: string, template_id: string, body: PutTenantTemplateRequest}; export type UpdateApiTenantsTenantIdTemplatesTemplateIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const putTenantTemplateResponse = z.object({id: z.string(), version: z.string(), published_at: z.string().nullable().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 }; /** * Create or Update a Tenant Template * * PUT /tenants/{tenant_id}/templates/{template_id} */ export const updateApiTenantsTenantIdTemplatesTemplateId = async (args: UpdateApiTenantsTenantIdTemplatesTemplateIdArgs, options?: UpdateApiTenantsTenantIdTemplatesTemplateIdOptions) => { 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(/\/+$/, '')}/tenants/${encodeURIComponent(String(args.tenant_id))}/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(putTenantTemplateResponse, await res.json()) };