import {z} from 'zod' export type InboxEmbedCreateRequest = {inbox_embed: {name?: (string | null) | undefined, scope_type: 'project' | 'customer' | 'phone_number', scope_id?: (string | null) | undefined, assigned_user_id?: (string | null) | undefined, allowed_origins?: Array | undefined, default_mode?: ('system' | 'light' | 'dark') | undefined, language?: ('en' | 'es') | undefined, expires_at?: (string | null) | undefined}}; export type CreateApiInboxEmbedsArgs = {body: InboxEmbedCreateRequest}; export type CreateApiInboxEmbedsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const inboxEmbedCreated = z.object({id: z.string(), name: z.string().nullable().optional(), scope_type: z.enum(["project", "customer", "phone_number"]), scope_id: z.string().nullable().optional(), scope_name: z.string().nullable().optional(), assigned_user_id: z.string().nullable().optional(), assigned_user_name: z.string().nullable().optional(), status: z.enum(["active", "revoked"]), allowed_origins: z.array(z.string()), default_mode: z.enum(["system", "light", "dark"]), language: z.enum(["en", "es"]), expires_at: z.string().nullable().optional(), last_used_at: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), token: z.string(), embed_url: z.string()}); export const inboxEmbedCreatedResponse = z.object({data: inboxEmbedCreated}); 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 inbox embed * * POST /inbox_embeds */ export const createApiInboxEmbeds = async (args: CreateApiInboxEmbedsArgs, options?: CreateApiInboxEmbedsOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", 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("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/inbox_embeds${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(inboxEmbedCreatedResponse, await res.json()) };