import {z} from 'zod' export type CreateApiMessagesInteractiveOrderDetailsArgs = {body: {from: string, to: string, interactive: {type?: 'order_details' | undefined, header?: {type: 'image'} | undefined, body: {text: string}, footer?: {text: string} | undefined, action: {name?: 'review_and_pay' | undefined, parameters: {reference_id: string, type: 'digital-goods' | 'physical-goods', payment_type?: 'br' | undefined, payment_settings: Array<{type: 'pix_dynamic_code' | 'payment_link' | 'boleto', pix_dynamic_code?: ({code: string, merchant_name: string, key: string, key_type: 'CPF' | 'CNPJ' | 'EMAIL' | 'PHONE' | 'EVP'}) | undefined, payment_link?: {uri: string} | undefined, boleto?: {digitable_line: string} | undefined}>, currency?: 'BRL' | undefined, total_amount: {value: number, offset?: number | undefined, description?: string | undefined}, order: {status?: 'pending' | undefined, catalog_id?: string | undefined, expiration?: {timestamp: string, description: string} | undefined, items?: (Array<{retailer_id: string, name: string, amount: {value: number, offset?: number | undefined, description?: string | undefined}, quantity?: number | undefined, sale_amount?: ({value: number, offset?: number | undefined, description?: string | undefined}) | undefined}>) | undefined, subtotal: {value: number, offset?: number | undefined, description?: string | undefined}, tax: {value: number, offset?: number | undefined, description?: string | undefined}, shipping?: ({value: number, offset?: number | undefined, description?: string | undefined}) | undefined, discount?: ({value: number, offset?: number | undefined, description?: string | undefined, discount_program_name?: string | undefined}) | undefined}}}}}}; export type CreateApiMessagesInteractiveOrderDetailsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiMessagesInteractiveOrderDetailsResponse = z.object({id: z.string()}); 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 }; /** * Send interactive order details message * * POST /messages/interactive/order_details */ export const createApiMessagesInteractiveOrderDetails = async (args: CreateApiMessagesInteractiveOrderDetailsArgs, options?: CreateApiMessagesInteractiveOrderDetailsOptions) => { const {baseUrl = "https://messenger-public-api.cogfy.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/messages/interactive/order_details${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(createApiMessagesInteractiveOrderDetailsResponse, await res.json()) };