import {z} from 'zod' export type TemplateCondition = {field: string, operator: 'equals' | 'not_equals' | 'contains' | 'is_filled' | 'is_empty', value?: (string | null) | undefined}; export type SmsNotificationTemplateIn = {sms_notification_template_id?: (string | null) | undefined, name: string, enabled?: boolean | undefined, conditions?: Array | undefined, body: string}; export type UpdateSmsNotificationTemplatesRequest = {templates: Array}; export type UpdateApiSettingsSmsNotificationTemplatesArgs = {body: UpdateSmsNotificationTemplatesRequest}; export type UpdateApiSettingsSmsNotificationTemplatesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const templateCondition = z.object({field: z.string(), operator: z.enum(["equals", "not_equals", "contains", "is_filled", "is_empty"]), value: z.string().nullable().optional()}); export const smsNotificationTemplateOut = z.object({sms_notification_template_id: z.string(), name: z.string(), enabled: z.boolean().optional(), conditions: z.array(templateCondition).optional(), body: z.string(), position: z.number().int()}); export const templateVariable = z.object({name: z.string(), description: z.string()}); export const templateOperator = z.object({operator: z.enum(["equals", "not_equals", "contains", "is_filled", "is_empty"]), label: z.string(), takes_value: z.boolean()}); export const smsNotificationTemplatesResponse = z.object({templates: z.array(smsNotificationTemplateOut), variables: z.array(templateVariable), operators: z.array(templateOperator), max_length: z.number().int()}); 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 Sms Notification Templates * * PUT /settings/sms-notification-templates */ export const updateApiSettingsSmsNotificationTemplates = async (args: UpdateApiSettingsSmsNotificationTemplatesArgs, options: UpdateApiSettingsSmsNotificationTemplatesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/settings/sms-notification-templates${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(smsNotificationTemplatesResponse, await res.json()) };