UNPKG

xerexjs

Version:

A simple notification system for NextJS/ReactJS with real-time capabilities

65 lines (54 loc) 1.79 kB
export class Xerex { private apiKey: string; private apiUrl: string; constructor(apiKey: string) { this.apiKey = apiKey; this.apiUrl = "https://xerex.100xbuild.com/api/v2"; } async addMembers(userIds: string[]) { try { const response = await fetch(`${this.apiUrl}/add-members`, { method: "POST", headers: { "Content-Type": "application/json", }, credentials: "include", body: JSON.stringify({ members: userIds }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || "Failed to add members"); } return { success: true, message: data.message }; } catch (error) { console.error("Error adding members:", error); return { success: false, message: (error as Error).message }; } } async sendNotification(userIds: string[], content: string, buttonText?: string, buttonUrl?: string) { try { const body: any = { recipients: userIds, content: content, }; if (buttonText) body.buttonText = buttonText; if (buttonUrl) body.buttonUrl = buttonUrl; const response = await fetch(`${this.apiUrl}/send-notification`, { method: "POST", headers: { "Content-Type": "application/json", }, credentials: "include", body: JSON.stringify(body), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || "Failed to send notification"); } return { success: true, message: data.message }; } catch (error) { console.error("Error sending notification:", error); return { success: false, message: (error as Error).message }; } } }