/** * Client for Nomadro notify FastAPI (`/api/notify/v1`) — Novu-domain secondary-dev. */ import type { NotifyActivity, NotifyIntegration, NotifyMessage, NotifyPreferences, NotifySubscriber, NotifyTopic, NotifyWorkflow, } from "./types"; import { requestJson } from "~/lib/client-request"; import { getGuestId } from '@social/lib/api'; const BASE = "/api/notify/v1"; async function j(path: string, init?: RequestInit): Promise { const headers = new Headers(init?.headers); headers.set('X-Social-Guest-Id', getGuestId()); return requestJson(`${BASE}${path}`, { ...init, headers }); } export const notifyApi = { health: () => j<{ ok: true; service: string }>("/health"), subscribers: () => j<{ ok: true; subscribers: NotifySubscriber[] }>("/subscribers"), upsertSubscriber: (body: Partial & { subscriber_id: string }) => j<{ ok: true; subscriber: NotifySubscriber }>("/subscribers", { method: "POST", body: JSON.stringify(body), }), deleteSubscriber: (id: string) => j<{ ok: true }>(`/subscribers/${encodeURIComponent(id)}`, { method: "DELETE" }), topics: () => j<{ ok: true; topics: NotifyTopic[] }>("/topics"), createTopic: (key: string, name: string) => j<{ ok: true; topic: NotifyTopic }>("/topics", { method: "POST", body: JSON.stringify({ key, name }), }), addTopicMember: (topicKey: string, subscriber_id: string) => j<{ ok: true; topic: NotifyTopic }>( `/topics/${encodeURIComponent(topicKey)}/subscribers`, { method: "POST", body: JSON.stringify({ subscriber_id }) }, ), workflows: () => j<{ ok: true; workflows: NotifyWorkflow[] }>("/workflows"), workflow: (id: string) => j<{ ok: true; workflow: NotifyWorkflow }>(`/workflows/${encodeURIComponent(id)}`), createWorkflow: (body: { workflow_id: string; name: string; description?: string; tags?: string[]; steps?: unknown[]; }) => j<{ ok: true; workflow: NotifyWorkflow }>("/workflows", { method: "POST", body: JSON.stringify(body), }), patchWorkflow: (id: string, patch: Record) => j<{ ok: true; workflow: NotifyWorkflow }>(`/workflows/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify(patch), }), trigger: (body: { workflow_id: string; subscriber_id?: string; topic_key?: string; payload?: Record; }) => j<{ ok: true; transaction_id: string; notifications: unknown[]; }>("/events/trigger", { method: "POST", body: JSON.stringify(body) }), inbox: ( subscriber_id = "guest", opts?: { include_archived?: boolean; category?: string; signal?: AbortSignal }, ) => { const q = new URLSearchParams({ subscriber_id }); if (opts?.include_archived) q.set("include_archived", "true"); if (opts?.category) q.set("category", opts.category); return j<{ ok: true; subscriber_id: string; unread: number; unread_by_category: Record; messages: NotifyMessage[]; }>(`/inbox?${q}`, { signal: opts?.signal }); }, unread: (subscriber_id = "guest") => j<{ ok: true; unread: number; unread_by_category: Record; }>(`/inbox/unread?subscriber_id=${encodeURIComponent(subscriber_id)}`), markAllRead: (subscriber_id = "guest", category?: string) => j<{ ok: true; updated: number; unread: number }>("/inbox/read-all", { method: "POST", body: JSON.stringify({ subscriber_id, category: category || null }), }), patchMessage: ( id: string, patch: { read?: boolean; archived?: boolean; deleted?: boolean }, ) => j<{ ok: true; message: NotifyMessage }>(`/inbox/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify(patch), }), deleteMessage: (id: string) => j<{ ok: true }>(`/inbox/${encodeURIComponent(id)}`, { method: "DELETE" }), preferences: (subscriber_id: string, signal?: AbortSignal) => j<{ ok: true; preferences: NotifyPreferences }>( `/subscribers/${encodeURIComponent(subscriber_id)}/preferences`, { signal }, ), setPreferences: ( subscriber_id: string, channels: Record, ) => j<{ ok: true; preferences: NotifyPreferences }>( `/subscribers/${encodeURIComponent(subscriber_id)}/preferences`, { method: "PUT", body: JSON.stringify({ channels }) }, ), activity: (limit = 50) => j<{ ok: true; activity: NotifyActivity[] }>(`/activity?limit=${limit}`), integrations: () => j<{ ok: true; integrations: NotifyIntegration[] }>("/integrations"), };