import {z} from 'zod' export type CreateTemplateHeaderComponentExample = {link?: string | undefined}; export type CreateTemplateHeaderComponent = {format: 'TEXT' | 'IMAGE' | 'VIDEO' | 'DOCUMENT', text?: string | undefined, example?: CreateTemplateHeaderComponentExample | undefined}; export type CreateTemplateBodyComponent = {text: string, example?: {[key: string]: string} | undefined}; export type CreateTemplateFooterComponent = {text: string}; export type CreateTemplateButtonDto = {type: 'QUICK_REPLY' | 'URL' | 'PHONE_NUMBER', text: string, url?: string | undefined, example?: string | undefined, phoneNumber?: string | undefined}; export type CreateTemplateComponentDto = {header?: CreateTemplateHeaderComponent | undefined, body: CreateTemplateBodyComponent, footer?: CreateTemplateFooterComponent | undefined, buttons?: Array | undefined}; export type EditTemplateViaPublicAPIDto = {name: string, language: 'af' | 'sq' | 'ar' | 'az' | 'bn' | 'bg' | 'ca' | 'zh_CN' | 'zh_HK' | 'zh_TW' | 'hr' | 'cs' | 'da' | 'nl' | 'en' | 'en_GB' | 'en_US' | 'et' | 'fil' | 'fi' | 'fr' | 'ka' | 'de' | 'el' | 'gu' | 'ha' | 'he' | 'hi' | 'hu' | 'id' | 'ga' | 'it' | 'ja' | 'kn' | 'kk' | 'rw_RW' | 'ko' | 'ky_KG' | 'lo' | 'lv' | 'lt' | 'mk' | 'ms' | 'ml' | 'mr' | 'nb' | 'fa' | 'pl' | 'pt_BR' | 'pt_PT' | 'pa' | 'ro' | 'ru' | 'sr' | 'sk' | 'sl' | 'es' | 'es_AR' | 'es_ES' | 'es_MX' | 'sw' | 'sv' | 'ta' | 'te' | 'th' | 'tr' | 'uk' | 'ur' | 'uz', components: CreateTemplateComponentDto, category: 'MARKETING' | 'UTILITY', wabaNumbers?: Array | undefined, allowCategoryUpdate?: boolean | undefined}; export type PatchApiTemplateTemplateIdArgs = {templateId: string, body: EditTemplateViaPublicAPIDto}; export type PatchApiTemplateTemplateIdOptions = {baseUrl?: string | undefined, basic?: string | undefined, fetch?: typeof fetch | undefined}; export const editTemplateViaPublicAPIResponseDto = z.object({success: z.boolean()}); 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 }; /** * Edit Template * * PATCH /template/{templateId} */ export const patchApiTemplateTemplateId = async (args: PatchApiTemplateTemplateIdArgs, options?: PatchApiTemplateTemplateIdOptions) => { const {baseUrl = "https://public.doubletick.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set("Authorization", basic) const url = `${baseUrl.replace(/\/+$/, '')}/template/${encodeURIComponent(String(args.templateId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(editTemplateViaPublicAPIResponseDto, await res.json()) };