import {z} from 'zod' export type GetApiV1TemplatesTemplateIdArgs = {templateId: string}; export type GetApiV1TemplatesTemplateIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsAppCategory = z.enum(["UTILITY", "MARKETING", "AUTHENTICATION"]); export const template = z.object({id: z.string(), name: z.string(), language: z.string(), body: z.string(), smsBody: z.string().optional(), telegramBody: z.string().optional(), instagramBody: z.string().optional(), category: whatsAppCategory, status: z.enum(["draft", "pending", "approved", "rejected"]).optional(), variables: z.array(z.string()).optional(), headerType: z.string().optional(), headerContent: z.string().optional(), footer: z.string().optional(), buttons: z.array(z.object({type: z.enum(["quick_reply", "url", "phone", "otp", "request_contact_info"]).optional(), text: z.string().optional(), url: z.string().optional(), example: z.string().optional(), phoneNumber: z.string().optional(), otpType: z.enum(["COPY_CODE", "ONE_TAP"]).optional(), packageName: z.string().optional(), signatureHash: z.string().optional()})).optional(), addSecurityRecommendation: z.boolean().optional(), codeExpirationMinutes: z.number().int().gte(1).lte(90).optional(), whatsapp: z.object({templateName: z.string().optional(), namespace: z.string().optional(), status: z.string().optional()}).optional(), createdAt: z.string().optional(), updatedAt: 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 }; /** * Get template * * GET /v1/templates/{templateId} */ export const getApiV1TemplatesTemplateId = async (args: GetApiV1TemplatesTemplateIdArgs, options?: GetApiV1TemplatesTemplateIdOptions) => { const {baseUrl = "https://api.zavu.dev", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/templates/${encodeURIComponent(String(args.templateId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(template, await res.json()) };