UNPKG

@oneclick.dev/sdk

Version:

The OneClick Studio SDK

41 lines (40 loc) 1.36 kB
class NotifyClient { constructor(config) { const { endpoint, key, project } = config; if (!endpoint || !key || !project) { throw new Error('Missing required configuration: endpoint, key, or project.'); } this.endpoint = endpoint; this.key = key; this.project = project; } /** * Sends a notification to a user. * * @param title The title of the notification * @param message The message of the notification * @param userId The ID of the user to receive the notification (optional) */ async send({ title, message, userId }) { if (!title || !message) { throw new Error('Missing required fields: title or message.'); } const response = await fetch(`${this.endpoint}/api/v1/projects/${this.project}/notifications`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.key}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ title, message, userId }) }); if (!response.ok) { const text = await response.text(); throw new Error(`Notification failed: ${response.status} ${text}`); } } } export default NotifyClient;