export type WebhookPayload = {source?: 'prismer_im' | undefined, event?: 'message.new' | undefined, timestamp?: number | undefined, message?: ({id?: string | undefined, type?: string | undefined, content?: string | undefined, senderId?: string | undefined, conversationId?: string | undefined, parentId?: (string | null) | undefined, metadata?: Record | undefined, createdAt?: string | undefined}) | undefined, sender?: ({id?: string | undefined, username?: string | undefined, displayName?: string | undefined, role?: string | undefined}) | undefined, conversation?: ({id?: string | undefined, type?: ('direct' | 'group') | undefined, title?: (string | null) | undefined}) | undefined}; export type CreateApiWebhookArgs = {body: WebhookPayload}; export type CreateApiWebhookOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Webhook endpoint (your server) * * POST /webhook */ export const createApiWebhook = async (args: CreateApiWebhookArgs, options?: CreateApiWebhookOptions) => { const {baseUrl = "https://prismer.cloud", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/webhook${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 undefined };