import {z} from 'zod' export type ChatHomepageWidget = {component?: (string | null) | undefined, props?: (string | null) | undefined}; export type ChatHomePage = {id?: number | undefined, enabled?: boolean | undefined, logo?: (string | null) | undefined, title?: (string | null) | undefined, subtitle?: (string | null) | undefined, chatStarterTitle?: (string | null) | undefined, chatStarterSubtitle?: (string | null) | undefined, desk?: (string | null) | undefined, widgets?: (Array | null) | undefined}; export type ChatDesk = {_id?: (string | null) | undefined, domain?: (string | null) | undefined, slug?: (string | null) | undefined}; export type Table = 0 | 1 | 2 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 50 | 51 | 52 | 53 | 55 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 72; export type SurveyType = 1 | 2 | 3; export type SurveyInputType = 1 | 2; export type SurveyLink = {id?: number | undefined, linkedId?: number | undefined, linkedTable?: Table | undefined}; export type Survey = {id?: number | undefined, accountId?: number | undefined, departmentId?: number | undefined, name?: (string | null) | undefined, message?: (string | null) | undefined, linkedId?: number | undefined, linkedTable?: Table | undefined, type?: SurveyType | undefined, inputType?: SurveyInputType | undefined, options?: (string | null) | undefined, sipServerId?: number | undefined, createdDate?: string | undefined, links?: (Array | null) | undefined}; export type ChatWidgetModel = {id?: number | undefined, accountId?: number | undefined, departmentId?: number | undefined, name: string, domain: string, workingHourType?: number | undefined, offlineMsg?: (string | null) | undefined, onlineMsg?: (string | null) | undefined, chatTitle?: (string | null) | undefined, chatSubtitle?: (string | null) | undefined, chatTitleOffline?: (string | null) | undefined, chatSubtitleOffline?: (string | null) | undefined, botLogo?: (string | null) | undefined, language?: (string | null) | undefined, status?: boolean | undefined, contactInfo?: (string | null) | undefined, messages?: (string | null) | undefined, showEmojiButton?: boolean | undefined, allowVisitorCloseChat?: boolean | undefined, showAttachmentButton?: boolean | undefined, showChatHistory?: boolean | undefined, otpEnabled?: boolean | undefined, otpEmailRawHtml?: boolean | undefined, mailId?: number | undefined, removeBranding?: boolean | undefined, apiKey?: (string | null) | undefined, identityVerification?: boolean | undefined, privateKey?: (string | null) | undefined, primaryColor?: (string | null) | undefined, contactInfoEnabled?: boolean | undefined, androidKey?: (string | null) | undefined, iosKey?: (string | null) | undefined, integrations?: (string | null) | undefined, chatHomePage?: ChatHomePage | undefined, desk?: ChatDesk | undefined, survey?: Survey | undefined}; export type CreateApiV1ChatwidgetsArgs = {body: ChatWidgetModel}; export type CreateApiV1ChatwidgetsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const chatHomepageWidget = z.object({component: z.string().nullable().optional(), props: z.string().nullable().optional()}); export const chatHomePage = z.object({id: z.number().int().optional(), enabled: z.boolean().optional(), logo: z.string().nullable().optional(), title: z.string().nullable().optional(), subtitle: z.string().nullable().optional(), chatStarterTitle: z.string().nullable().optional(), chatStarterSubtitle: z.string().nullable().optional(), desk: z.string().nullable().optional(), widgets: z.array(chatHomepageWidget).nullable().optional()}); export const chatDesk = z.object({_id: z.string().nullable().optional(), domain: z.string().nullable().optional(), slug: z.string().nullable().optional()}); export const table = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(5), z.literal(6), z.literal(7), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(19), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(25), z.literal(26), z.literal(27), z.literal(31), z.literal(32), z.literal(34), z.literal(35), z.literal(36), z.literal(37), z.literal(38), z.literal(39), z.literal(40), z.literal(41), z.literal(42), z.literal(43), z.literal(44), z.literal(45), z.literal(46), z.literal(47), z.literal(48), z.literal(50), z.literal(51), z.literal(52), z.literal(53), z.literal(55), z.literal(60), z.literal(61), z.literal(62), z.literal(63), z.literal(64), z.literal(65), z.literal(66), z.literal(67), z.literal(68), z.literal(69), z.literal(72)]); export const surveyType = z.union([z.literal(1), z.literal(2), z.literal(3)]); export const surveyInputType = z.union([z.literal(1), z.literal(2)]); export const surveyLink = z.object({id: z.number().int().optional(), linkedId: z.number().int().optional(), linkedTable: table.optional()}); export const survey = z.object({id: z.number().int().optional(), accountId: z.number().int().optional(), departmentId: z.number().int().optional(), name: z.string().max(100).nullable().optional(), message: z.string().max(10000).nullable().optional(), linkedId: z.number().int().optional(), linkedTable: table.optional(), type: surveyType.optional(), inputType: surveyInputType.optional(), options: z.string().max(10000).nullable().optional(), sipServerId: z.number().int().optional(), createdDate: z.string().optional(), links: z.array(surveyLink).nullable().optional()}); export const chatWidgetModel = z.object({id: z.number().int().optional(), accountId: z.number().int().optional(), departmentId: z.number().int().optional(), name: z.string().min(1).max(32), domain: z.string().min(1).max(120), workingHourType: z.number().int().optional(), offlineMsg: z.string().max(1000).nullable().optional(), onlineMsg: z.string().max(1000).nullable().optional(), chatTitle: z.string().max(60).nullable().optional(), chatSubtitle: z.string().max(120).nullable().optional(), chatTitleOffline: z.string().max(60).nullable().optional(), chatSubtitleOffline: z.string().max(120).nullable().optional(), botLogo: z.string().max(1000).nullable().optional(), language: z.string().max(3).nullable().optional(), status: z.boolean().optional(), contactInfo: z.string().nullable().optional(), messages: z.string().nullable().optional(), showEmojiButton: z.boolean().optional(), allowVisitorCloseChat: z.boolean().optional(), showAttachmentButton: z.boolean().optional(), showChatHistory: z.boolean().optional(), otpEnabled: z.boolean().optional(), otpEmailRawHtml: z.boolean().optional(), mailId: z.number().int().optional(), removeBranding: z.boolean().optional(), apiKey: z.string().nullable().optional(), identityVerification: z.boolean().optional(), privateKey: z.string().nullable().optional(), primaryColor: z.string().nullable().optional(), contactInfoEnabled: z.boolean().optional(), androidKey: z.string().nullable().optional(), iosKey: z.string().nullable().optional(), integrations: z.string().nullable().optional(), chatHomePage: chatHomePage.optional(), desk: chatDesk.optional(), survey: survey.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 }; /** * Saves a chat widget. * * POST /v1/chatwidgets * * Also accepts auth: API Key */ export const createApiV1Chatwidgets = async (args: CreateApiV1ChatwidgetsArgs, options: CreateApiV1ChatwidgetsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/chatwidgets${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(chatWidgetModel, await res.json()) };