import {z} from 'zod' export type StripeAccountInput = {label: string, stripe_id: string, stripe_email?: string | undefined}; export type ContactInput = {external_id: string, name?: string | undefined, email?: string | undefined, phonenumber?: string | undefined, stripe_accounts?: Array | undefined, custom_attributes?: {[key: string]: unknown} | undefined}; export type CreateApiChatbotsChatbotIdContactsArgs = {chatbotId: string, body: {users: Array}}; export type CreateApiChatbotsChatbotIdContactsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const stripeAccount = z.object({label: z.string(), stripe_id: z.string(), stripe_email: z.string().optional()}); export const contact = z.object({id: z.string().optional(), external_id: z.string().optional(), name: z.string().optional(), email: z.string().optional(), phonenumber: z.string().optional(), stripe_accounts: z.array(stripeAccount).optional(), custom_attributes: z.record(z.string(), z.unknown()).optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional()}); export const createApiChatbotsChatbotIdContactsResponse = z.object({message: z.string().optional(), data: z.array(contact).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 }; /** * Create contacts for a chatbot * * POST /chatbots/{chatbotId}/contacts */ export const createApiChatbotsChatbotIdContacts = async (args: CreateApiChatbotsChatbotIdContactsArgs, options?: CreateApiChatbotsChatbotIdContactsOptions) => { const {baseUrl = "https://www.chatbase.co/api/v1", 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(/\/+$/, '')}/chatbots/${encodeURIComponent(String(args.chatbotId))}/contacts${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(createApiChatbotsChatbotIdContactsResponse, await res.json()) };