import {z} from 'zod' export type TemplateButton = {type: 'QUICK_REPLY' | 'URL' | 'PHONE_NUMBER' | 'SMART_LINK' | 'COPY_CODE' | 'FLOW' | 'CHARGE', text: string, url?: (string | null) | undefined, phone?: (string | null) | undefined, flowId?: (string | null) | undefined, extraConfig?: ({[key: string]: unknown} | null) | undefined}; export type CarouselCard = {mediaUrl: string, body: string, buttons?: (Array | null) | undefined}; export type CreateTemplateRequest = {name: string, category: 'MARKETING' | 'UTILITY' | 'AUTHENTICATION', body: string, language?: string | undefined, header?: (string | null) | undefined, headerType?: ('text' | 'media' | 'document' | null) | undefined, footer?: (string | null) | undefined, buttons?: (Array | null) | undefined, samples?: ({[key: string]: string} | null) | undefined, variableExamples?: (Array | null) | undefined, carouselCards?: (Array | null) | undefined}; export type CreateApiV1TemplatesArgs = {body: CreateTemplateRequest}; export type CreateApiV1TemplatesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const templateResponse = z.object({id: z.string(), name: z.string(), formattedName: z.string(), category: z.string(), originalCategory: z.string().nullable().optional(), language: z.string(), providerName: z.string(), providerTemplateId: z.string(), providerStatus: z.string(), rejectionReason: z.string().nullable().optional(), availableForSending: z.boolean(), unavailableReason: z.string().nullable().optional(), bodyPreview: z.string().nullable().optional(), structureJson: z.record(z.string(), z.unknown()), usageGuide: z.record(z.string(), z.unknown()).nullable().optional(), variablesSchema: z.record(z.string(), z.string()).nullable().optional(), createdAt: z.string(), updatedAt: 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 }; /** * Criar template * * POST /v1/templates */ export const createApiV1Templates = async (args: CreateApiV1TemplatesArgs, options?: CreateApiV1TemplatesOptions) => { const {baseUrl = "https://api.ararahq.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(/\/+$/, '')}/v1/templates${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(templateResponse, await res.json()) };