import {z} from 'zod' export type WidgetCreate = {name: string, agent_id?: (string | null) | undefined, team_id?: (string | null) | undefined, agent_revision_id?: (string | null) | undefined, theme?: {[key: string]: unknown} | undefined, copy?: {[key: string]: unknown} | undefined, behavior?: {[key: string]: unknown} | undefined, limits?: {[key: string]: unknown} | undefined}; export type CreateApiV1WebchatWidgetsArgs = {body: WidgetCreate}; export type CreateApiV1WebchatWidgetsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const domainResponse = z.object({id: z.string(), hostname: z.string(), include_subdomains: z.boolean(), verification_state: z.string(), verification_token: z.string(), verified_at: z.string().nullable(), last_checked_at: z.string().nullable(), failure_reason: z.string().nullable()}); export const widgetResponse = z.object({id: z.string(), organization_id: z.string(), name: z.string(), is_enabled: z.boolean(), agent_id: z.string().nullable(), team_id: z.string().nullable(), agent_revision_id: z.string().nullable(), public_key: z.string(), kill_epoch: z.number().int(), theme: z.record(z.string(), z.unknown()), copy: z.record(z.string(), z.unknown()), behavior: z.record(z.string(), z.unknown()), limits: z.record(z.string(), z.unknown()), created_at: z.string(), updated_at: z.string(), domains: z.array(domainResponse).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 a webchat widget * * POST /v1/webchat/widgets */ export const createApiV1WebchatWidgets = async (args: CreateApiV1WebchatWidgetsArgs, options: CreateApiV1WebchatWidgetsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webchat/widgets${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(widgetResponse, await res.json()) };