import {z} from 'zod' export type TemplateParam = {param?: string | undefined, label?: string | undefined, example?: string | undefined}; export type TemplateButton = {text?: string | undefined, type?: ('QUICK_REPLY' | 'URL' | 'PHONE_NUMBER' | 'OTP') | undefined, url?: string | undefined, phone_number?: string | undefined, example?: string | undefined}; export type TemplateExtraSettings = {addSecurityRecommendation?: boolean | undefined, codeExpirationMinutes?: number | undefined, allowChangeCategory?: boolean | undefined}; export type CreateTemplateRequest = {category: 'UTILITY' | 'MARKETING' | 'AUTHENTICATION', language: string, isVisible?: boolean | undefined, params?: Array | undefined, paramsNumber?: number | undefined, elementName: string, displayName?: string | undefined, template: string, type?: ('HSM' | 'IMAGE' | 'VIDEO' | 'DOCUMENT') | undefined, mediaUrl?: string | undefined, interactiveAction?: ('NONE' | 'CALL_TO_ACTION' | 'QUICK_REPLY' | 'OTP') | undefined, buttons?: Array | undefined, header?: string | undefined, exampleHeader?: string | undefined, headerParams?: Array> | undefined, example?: string | undefined, extraSettings?: TemplateExtraSettings | undefined}; export type CreateApiV1BotsBotIdTemplatesArgs = {botId: string, sendToAprove?: boolean | undefined, body: CreateTemplateRequest}; export type CreateApiV1BotsBotIdTemplatesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const templateResponse = z.object({message: z.array(z.string()).optional(), status: z.string().optional(), data: z.object({}).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 }; /** * Crear Plantilla * * POST /v1/bots/{botId}/templates */ export const createApiV1BotsBotIdTemplates = async (args: CreateApiV1BotsBotIdTemplatesArgs, options?: CreateApiV1BotsBotIdTemplatesOptions) => { const {baseUrl = "https://api.jelou.ai", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.sendToAprove != null) query.append("sendToAprove", String(args.sendToAprove)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/bots/${encodeURIComponent(String(args.botId))}/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()) };